From 2dfc33a3cc74bf8254137ae0b0c053cbe05c888a Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Wed, 20 Mar 2024 12:15:45 +0100
Subject: [PATCH] add factories for all theses models

---
 scipost_django/theses/factories.py            | 30 ++++---------------
 scipost_django/theses/tests/test_factories.py | 11 +++++++
 2 files changed, 17 insertions(+), 24 deletions(-)
 create mode 100644 scipost_django/theses/tests/test_factories.py

diff --git a/scipost_django/theses/factories.py b/scipost_django/theses/factories.py
index 14c8856a6..10fc23b40 100644
--- a/scipost_django/theses/factories.py
+++ b/scipost_django/theses/factories.py
@@ -9,10 +9,12 @@ from ontology.models import AcademicField, Specialty
 from scipost.constants import SCIPOST_APPROACHES
 from scipost.models import Contributor
 
-from .models import ThesisLink
+from .models import *
 from .forms import VetThesisLinkForm
 from .constants import THESIS_TYPES
 
+from common.faker import LazyRandEnum, fake
+
 
 class BaseThesisLinkFactory(factory.django.DjangoModelFactory):
     class Meta:
@@ -23,34 +25,17 @@ class BaseThesisLinkFactory(factory.django.DjangoModelFactory):
     vetted_by = factory.SubFactory("scipost.factories.ContributorFactory")
     vetted = True
 
-    type = factory.Iterator(THESIS_TYPES, getter=lambda c: c[0])
+    type = LazyRandEnum(THESIS_TYPES)
     acad_field = factory.SubFactory("ontology.factories.AcademicFieldFactory")
-    approaches = factory.Iterator(
-        SCIPOST_APPROACHES,
-        getter=lambda c: [
-            c[0],
-        ],
-    )
+    approaches = LazyRandEnum(SCIPOST_APPROACHES, repeat=2)
     title = factory.Faker("sentence")
     pub_link = factory.Faker("uri")
     author = factory.Faker("name")
     supervisor = factory.Faker("name")
     institution = factory.Faker("company")
-    defense_date = factory.Faker("date_this_decade")
+    defense_date = fake.aware.date_this_decade()
     abstract = factory.Faker("paragraph")
 
-    @classmethod
-    def create(cls, **kwargs):
-        if AcademicField.objects.count() < 5:
-            from ontology.factories import AcademicFieldactory
-
-            AcademicFieldFactory.create_batch(5)
-        if Specialty.objects.count() < 5:
-            from ontology.factories import SpecialtyFactory
-
-            SpecialtyFactory.create_batch(5)
-        return super().create(**kwargs)
-
     @factory.post_generation
     def add_specialties(self, create, extracted, **kwargs):
         if create:
@@ -66,9 +51,6 @@ class BaseThesisLinkFactory(factory.django.DjangoModelFactory):
             # A list of groups were passed in, use them
             for contributor in extracted:
                 self.author_as_cont.add(contributor)
-        elif factory.Faker("boolean"):
-            contributor = Contributor.objects.order_by("?").first()
-            self.author_as_cont.add(contributor)
 
 
 class ThesisLinkFactory(BaseThesisLinkFactory):
diff --git a/scipost_django/theses/tests/test_factories.py b/scipost_django/theses/tests/test_factories.py
new file mode 100644
index 000000000..6dc5dba50
--- /dev/null
+++ b/scipost_django/theses/tests/test_factories.py
@@ -0,0 +1,11 @@
+__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
+__license__ = "AGPL v3"
+
+from django.test import TestCase
+from ..factories import *
+
+
+class TestThesisLinkFactory(TestCase):
+    def test_can_create_thesis_links(self):
+        thesis_link = ThesisLinkFactory()
+        self.assertIsNotNone(thesis_link)
-- 
GitLab