SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit d0a6be98 authored by George Katsikas's avatar George Katsikas :goat:
Browse files

add factories for all ontology models

parent 00e96d42
No related branches found
No related tags found
No related merge requests found
...@@ -5,34 +5,115 @@ __license__ = "AGPL v3" ...@@ -5,34 +5,115 @@ __license__ = "AGPL v3"
import factory import factory
from django.utils.text import slugify 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 .models import Branch, AcademicField, Specialty
from common.faker import LazyObjectCount, LazyRandEnum, fake
class BranchFactory(factory.django.DjangoModelFactory): 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: class Meta:
model = Branch 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: class Meta:
model = AcademicField 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 SpecialtyFactory(factory.django.DjangoModelFactory):
class Meta:
model = Specialty
django_get_or_create = ["name", "slug"]
acad_field = factory.SubFactory(AcademicFieldFactory) acad_field = factory.SubFactory(AcademicFieldFactory)
name = factory.LazyAttribute(lambda b: "Specialty %d" % b.order) name = factory.LazyAttribute(
slug = factory.LazyAttribute(lambda b: slugify("specialty-%d" % b.order)) lambda _: f"{fake.word(part_of_speech='adjective').title()} {fake.word(part_of_speech='noun').title()}"
order = factory.Sequence(lambda n: Specialty.objects.count() + 1) )
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: 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))
__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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment