From d0a6be9854b4fdab94dec647ad6a8b826039b824 Mon Sep 17 00:00:00 2001 From: George Katsikas <giorgakis.katsikas@gmail.com> Date: Fri, 17 Nov 2023 14:07:06 +0100 Subject: [PATCH] add factories for all ontology models --- scipost_django/ontology/factories.py | 107 +++++++++++++++--- scipost_django/ontology/tests/__init__.py | 0 .../ontology/tests/test_factories.py | 56 +++++++++ 3 files changed, 150 insertions(+), 13 deletions(-) create mode 100644 scipost_django/ontology/tests/__init__.py create mode 100644 scipost_django/ontology/tests/test_factories.py diff --git a/scipost_django/ontology/factories.py b/scipost_django/ontology/factories.py index 02a1bd752..d40357d0a 100644 --- a/scipost_django/ontology/factories.py +++ b/scipost_django/ontology/factories.py @@ -5,34 +5,115 @@ __license__ = "AGPL v3" import factory from django.utils.text import slugify +import factory.fuzzy +from ontology.constants import TOPIC_RELATIONS_ASYM, TOPIC_RELATIONS_SYM +from ontology.models.relations import RelationAsym, RelationSym + +from ontology.models.tag import Tag +from ontology.models.topic import Topic from .models import Branch, AcademicField, Specialty +from common.faker import LazyObjectCount, LazyRandEnum, fake class BranchFactory(factory.django.DjangoModelFactory): - name = factory.LazyAttribute(lambda b: "Branch %d" % b.order) - slug = factory.LazyAttribute(lambda b: slugify("branch-%d" % b.order)) - order = factory.Sequence(lambda n: Branch.objects.count() + 1) - class Meta: model = Branch + django_get_or_create = ["name", "slug"] + name = factory.LazyAttribute(lambda _: fake.word(part_of_speech="noun").title()) + slug = factory.LazyAttribute(lambda self: slugify(self.name.lower())) + order = LazyObjectCount(Branch, offset=1) -class AcademicFieldFactory(factory.django.DjangoModelFactory): - branch = factory.SubFactory(BranchFactory) - name = factory.LazyAttribute(lambda b: "Field %d" % b.order) - slug = factory.LazyAttribute(lambda b: slugify("field-%d" % b.order)) - order = factory.Sequence(lambda n: AcademicField.objects.count() + 1) +class AcademicFieldFactory(factory.django.DjangoModelFactory): class Meta: model = AcademicField + django_get_or_create = ["name", "slug"] + + branch = factory.SubFactory(BranchFactory) + name = factory.LazyAttribute( + lambda self: f"{fake.word(part_of_speech='adjective').title()} {self.branch.name}" + ) + slug = factory.LazyAttribute(lambda self: slugify(self.name)) + order = LazyObjectCount(AcademicField, offset=1) class SpecialtyFactory(factory.django.DjangoModelFactory): + class Meta: + model = Specialty + django_get_or_create = ["name", "slug"] + acad_field = factory.SubFactory(AcademicFieldFactory) - name = factory.LazyAttribute(lambda b: "Specialty %d" % b.order) - slug = factory.LazyAttribute(lambda b: slugify("specialty-%d" % b.order)) - order = factory.Sequence(lambda n: Specialty.objects.count() + 1) + name = factory.LazyAttribute( + lambda _: f"{fake.word(part_of_speech='adjective').title()} {fake.word(part_of_speech='noun').title()}" + ) + slug = factory.LazyAttribute(lambda self: slugify(self.name.lower())) + order = LazyObjectCount(Specialty, offset=1) + + @factory.post_generation + def topics(self, create, extracted, **kwargs): + if not create: + return + + if extracted: + for topic in extracted: + self.topics.add(topic) + + self.topics.add(TopicFactory()) + +class TopicFactory(factory.django.DjangoModelFactory): class Meta: - model = Specialty + model = Topic + django_get_or_create = ["name", "slug"] + + name = factory.LazyAttribute(lambda _: fake.word(part_of_speech="noun").title()) + slug = factory.LazyAttribute(lambda self: slugify(self.name.lower())) + + @factory.post_generation + def tags(self, create, extracted, **kwargs): + if not create: + return + + if extracted: + for tag in extracted: + self.tags.add(tag) + + self.tags.add(TagFactory()) + + +class TagFactory(factory.django.DjangoModelFactory): + class Meta: + model = Tag + django_get_or_create = ["name"] + + name = factory.LazyAttribute(lambda _: fake.word(part_of_speech="noun").title()) + + +# Relations +class RelationAsymFactory(factory.django.DjangoModelFactory): + class Meta: + model = RelationAsym + + A = factory.SubFactory(TopicFactory) + B = factory.SubFactory(TopicFactory) + relation = LazyRandEnum(TOPIC_RELATIONS_ASYM) + + +class RelationSymFactory(factory.django.DjangoModelFactory): + class Meta: + model = RelationSym + + relation = LazyRandEnum(TOPIC_RELATIONS_SYM) + + @factory.post_generation + def topics(self, create, extracted, **kwargs): + if not create: + return + + if extracted: + for topic in extracted: + self.topics.add(topic) + + self.topics.add(*TopicFactory.create_batch(2)) diff --git a/scipost_django/ontology/tests/__init__.py b/scipost_django/ontology/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/scipost_django/ontology/tests/test_factories.py b/scipost_django/ontology/tests/test_factories.py new file mode 100644 index 000000000..3fd98de89 --- /dev/null +++ b/scipost_django/ontology/tests/test_factories.py @@ -0,0 +1,56 @@ +__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)" +__license__ = "AGPL v3" + + +from django.test import TestCase +from ontology.factories import ( + AcademicFieldFactory, + BranchFactory, + RelationAsymFactory, + RelationSymFactory, + SpecialtyFactory, + TagFactory, + TopicFactory, +) + + +class TestBranchFactory(TestCase): + def test_can_create_branches(self): + branch = BranchFactory() + self.assertIsNotNone(branch) + + +class TestAcademicFieldFactory(TestCase): + def test_can_create_academic_fields(self): + academic_field = AcademicFieldFactory() + self.assertIsNotNone(academic_field) + + +class TestSpecialtyFactory(TestCase): + def test_can_create_specialties(self): + specialty = SpecialtyFactory() + self.assertIsNotNone(specialty) + + +class TestTopicFactory(TestCase): + def test_can_create_topics(self): + topic = TopicFactory() + self.assertIsNotNone(topic) + + +class TestTagFactory(TestCase): + def test_can_create_tags(self): + tag = TagFactory() + self.assertIsNotNone(tag) + + +class TestRelationAsymFactory(TestCase): + def test_can_create_relation_asyms(self): + relation_asym = RelationAsymFactory() + self.assertIsNotNone(relation_asym) + + +class TestRelationSymFactory(TestCase): + def test_can_create_relation_syms(self): + relation_sym = RelationSymFactory() + self.assertIsNotNone(relation_sym) -- GitLab