From 66902b51eb87cb938f2a63307fde7b70901fa5b5 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Tue, 14 Nov 2023 16:11:37 +0100
Subject: [PATCH] add factories for all ethics models

---
 scipost_django/ethics/factories.py            | 92 +++++++++++++++++++
 scipost_django/ethics/tests.py                |  7 --
 scipost_django/ethics/tests/__init__.py       |  0
 scipost_django/ethics/tests/test_factories.py | 43 +++++++++
 4 files changed, 135 insertions(+), 7 deletions(-)
 create mode 100644 scipost_django/ethics/factories.py
 delete mode 100644 scipost_django/ethics/tests.py
 create mode 100644 scipost_django/ethics/tests/__init__.py
 create mode 100644 scipost_django/ethics/tests/test_factories.py

diff --git a/scipost_django/ethics/factories.py b/scipost_django/ethics/factories.py
new file mode 100644
index 000000000..214fb3eb4
--- /dev/null
+++ b/scipost_django/ethics/factories.py
@@ -0,0 +1,92 @@
+__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
+__license__ = "AGPL v3"
+
+
+import datetime
+import random
+import django
+import factory
+
+from common.faker import LazyAwareDate, LazyRandEnum, fake
+from scipost.factories import ContributorFactory
+from submissions.models.submission import Submission
+
+from .models import CompetingInterest, RedFlag, SubmissionClearance
+
+
+class CompetingInterestFactory(factory.django.DjangoModelFactory):
+    class Meta:
+        model = CompetingInterest
+        django_get_or_create = (
+            "profile",
+            "related_profile",
+            "nature",
+            "date_from",
+            "date_until",
+        )
+
+    profile = factory.SubFactory("scipost.factories.ProfileFactory")
+    related_profile = factory.SubFactory("scipost.factories.ProfileFactory")
+    declared_by = factory.LazyAttribute(
+        lambda self: ContributorFactory.from_profile(
+            profile=random.choice([self.profile, self.related_profile])
+        )
+    )
+    nature = LazyRandEnum(CompetingInterest.NATURE_CHOICES)
+    date_from = factory.Faker("date_time_this_decade")
+    date_until = factory.LazyAttribute(
+        lambda self: fake.aware.date_between(start_date=self.date_from, end_date="+1y")
+    )
+
+    comments = factory.Faker("text")
+
+
+class SubmissionClearanceFactory(factory.django.DjangoModelFactory):
+    class Meta:
+        model = SubmissionClearance
+
+    profile = factory.SubFactory("scipost.factories.ProfileFactory")
+    submission = factory.SubFactory("submissions.factories.SubmissionFactory")
+    asserted_by = factory.SubFactory("scipost.factories.ContributorFactory")
+    asserted_on = LazyAwareDate("date_time_this_decade")
+    comments = factory.Faker("text")
+
+
+class RedFlagFactory(factory.django.DjangoModelFactory):
+    class Meta:
+        model = RedFlag
+        abstract = True
+        django_get_or_create = (
+            "concerning_object_id",
+            "concerning_object_type",
+            "raised_by",
+            "raised_on",
+        )
+
+    description = factory.Faker("text")
+    raised_by = factory.SubFactory("scipost.factories.ContributorFactory")
+    raised_on = LazyAwareDate("date_time_this_decade")
+
+
+class SubmissionRedFlagFactory(RedFlagFactory):
+    class Params:
+        submission = factory.SubFactory("submissions.factories.SubmissionFactory")
+
+    concerning_object_id = factory.SelfAttribute("submission.id")
+    concerning_object_type = factory.LazyAttribute(
+        lambda self: django.contrib.contenttypes.models.ContentType.objects.get_for_model(
+            self.submission
+        )
+    )
+
+
+class ProfileRedFlagFactory(RedFlagFactory):
+    class Params:
+        profile = factory.SubFactory("scipost.factories.ProfileFactory")
+
+    concerning_object_id = factory.SelfAttribute("profile.id")
+    concerning_object_type = factory.LazyAttribute(
+        lambda self: django.contrib.contenttypes.models.ContentType.objects.get_for_model(
+            self.profile
+        )
+    )
diff --git a/scipost_django/ethics/tests.py b/scipost_django/ethics/tests.py
deleted file mode 100644
index ddef03c4d..000000000
--- a/scipost_django/ethics/tests.py
+++ /dev/null
@@ -1,7 +0,0 @@
-__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
-__license__ = "AGPL v3"
-
-
-from django.test import TestCase
-
-# Create your tests here.
diff --git a/scipost_django/ethics/tests/__init__.py b/scipost_django/ethics/tests/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/scipost_django/ethics/tests/test_factories.py b/scipost_django/ethics/tests/test_factories.py
new file mode 100644
index 000000000..577c84991
--- /dev/null
+++ b/scipost_django/ethics/tests/test_factories.py
@@ -0,0 +1,43 @@
+__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
+__license__ = "AGPL v3"
+
+from django.test import TestCase
+from ..factories import (
+    CompetingInterestFactory,
+    ProfileRedFlagFactory,
+    SubmissionClearanceFactory,
+    SubmissionRedFlagFactory,
+)
+
+
+class TestCompetingInterestFactory(TestCase):
+    def test_can_create_competing_interests(self):
+        competing_interest = CompetingInterestFactory()
+
+        self.assertIsNotNone(competing_interest)
+
+    def test_competing_interest_declaration_by_either_party(self):
+        competing_interest = CompetingInterestFactory()
+
+        self.assertIn(
+            competing_interest.declared_by.profile,
+            [competing_interest.profile, competing_interest.related_profile],
+        )
+
+
+class TestSubmissionClearanceFactory(TestCase):
+    def test_can_create_submission_clearances(self):
+        submission_clearance = SubmissionClearanceFactory()
+        self.assertIsNotNone(submission_clearance)
+
+
+class TestSubmissionRedFlagFactory(TestCase):
+    def test_can_create_submission_red_flags(self):
+        submission_red_flag = SubmissionRedFlagFactory()
+        self.assertIsNotNone(submission_red_flag)
+
+
+class TestProfileRedFlagFactory(TestCase):
+    def test_can_create_profile_red_flags(self):
+        profile_red_flag = ProfileRedFlagFactory()
+        self.assertIsNotNone(profile_red_flag)
-- 
GitLab