From 157940c8893bfb7903c041a6b4d5b850bb0e48c0 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Fri, 17 Nov 2023 14:22:48 +0100
Subject: [PATCH] add factories for all organization models

---
 scipost_django/organizations/factories.py     | 86 +++++++++++++++++--
 .../organizations/tests/__init__.py           |  0
 .../organizations/tests/test_factories.py     | 49 +++++++++++
 3 files changed, 126 insertions(+), 9 deletions(-)
 create mode 100644 scipost_django/organizations/tests/__init__.py

diff --git a/scipost_django/organizations/factories.py b/scipost_django/organizations/factories.py
index 1641007ca..0849b3e98 100644
--- a/scipost_django/organizations/factories.py
+++ b/scipost_django/organizations/factories.py
@@ -1,8 +1,22 @@
 import factory
-from common import faker
-from common.faker import LazyRandEnum
-from organizations.constants import ORGANIZATION_STATUSES, ORGANIZATION_TYPES
-from organizations.models import Organization
+import faker
+
+from common.faker import LazyAwareDate, LazyRandEnum, fake
+from organizations.constants import (
+    ORGANIZATION_EVENTS,
+    ORGANIZATION_STATUSES,
+    ORGANIZATION_TYPES,
+    ROLE_KINDS,
+)
+from organizations.models import (
+    Contact,
+    ContactPerson,
+    ContactRole,
+    Organization,
+    OrganizationEvent,
+    OrganizationLogo,
+)
+from scipost.constants import TITLE_CHOICES
 
 
 class OrganizationFactory(factory.django.DjangoModelFactory):
@@ -26,11 +40,65 @@ class OrganizationFactory(factory.django.DjangoModelFactory):
     address = factory.Faker("address")
     logo = factory.django.ImageField()
     css_class = ""
-    grid_json = {}
+    ror_json = {}
     crossref_json = {}
     parent = None
     superseded_by = None
-    cf_associated_publication_ids = {}
-    cf_nr_associated_publications = 0
-    cf_balance_info = {}
-    cf_expenditure_for_publication = {}
+
+
+class OrganizationLogoFactory(factory.django.DjangoModelFactory):
+    class Meta:
+        model = OrganizationLogo
+
+    organization = factory.SubFactory(OrganizationFactory)
+    image = factory.django.ImageField()
+    mimetype = LazyRandEnum(OrganizationLogo.MIMETYPE_CHOICES)
+    width = factory.Faker("pyint")
+    height = factory.Faker("pyint")
+    order = factory.Sequence(lambda n: n)
+
+
+class OrganizationEventFactory(factory.django.DjangoModelFactory):
+    class Meta:
+        model = OrganizationEvent
+
+    organization = factory.SubFactory(OrganizationFactory)
+    event = LazyRandEnum(ORGANIZATION_EVENTS)
+    comments = factory.Faker("text")
+    noted_on = LazyAwareDate("date_this_year")
+    noted_by = factory.SubFactory("scipost.factories.UserFactory")
+
+
+class ContactFactory(factory.django.DjangoModelFactory):
+    class Meta:
+        model = Contact
+
+    user = factory.SubFactory("scipost.factories.UserFactory")
+    title = factory.SelfAttribute("user.contributor.profile.title")
+    activation_key = factory.SelfAttribute("user.contributor.activation_key")
+    key_expires = factory.SelfAttribute("user.contributor.key_expires")
+
+
+class ContactRoleFactory(factory.django.DjangoModelFactory):
+    class Meta:
+        model = ContactRole
+
+    contact = factory.SubFactory(ContactFactory)
+    organization = factory.SubFactory(OrganizationFactory)
+    kind = LazyRandEnum(ROLE_KINDS, repeat=2)
+    date_from = LazyAwareDate("date_this_year")
+    date_until = factory.LazyAttribute(
+        lambda self: fake.aware.date_between(start_date=self.date_from, end_date="+1y")
+    )
+
+
+class ContactPersonFactory(factory.django.DjangoModelFactory):
+    class Meta:
+        model = ContactPerson
+
+    organization = factory.SubFactory(OrganizationFactory)
+    title = LazyRandEnum(TITLE_CHOICES)
+    first_name = factory.Faker("first_name")
+    last_name = factory.Faker("last_name")
+    email = factory.Faker("email")
+    role = factory.Faker("job")
diff --git a/scipost_django/organizations/tests/__init__.py b/scipost_django/organizations/tests/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/scipost_django/organizations/tests/test_factories.py b/scipost_django/organizations/tests/test_factories.py
index e69de29bb..ecb19309d 100644
--- a/scipost_django/organizations/tests/test_factories.py
+++ b/scipost_django/organizations/tests/test_factories.py
@@ -0,0 +1,49 @@
+__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
+__license__ = "AGPL v3"
+
+from django.test import TestCase
+
+from organizations.factories import (
+    OrganizationLogoFactory,
+    OrganizationFactory,
+    OrganizationEventFactory,
+    ContactFactory,
+    ContactRoleFactory,
+    ContactPersonFactory,
+)
+
+
+class TestOrganizationLogoFactory(TestCase):
+    def test_can_create_organization_logos(self):
+        organization_logo = OrganizationLogoFactory()
+        self.assertIsNotNone(organization_logo)
+
+
+class TestOrganizationFactory(TestCase):
+    def test_can_create_organizations(self):
+        organization = OrganizationFactory()
+        self.assertIsNotNone(organization)
+
+
+class TestOrganizationEventFactory(TestCase):
+    def test_can_create_organization_events(self):
+        organization_event = OrganizationEventFactory()
+        self.assertIsNotNone(organization_event)
+
+
+class TestContactFactory(TestCase):
+    def test_can_create_contacts(self):
+        contact = ContactFactory()
+        self.assertIsNotNone(contact)
+
+
+class TestContactPersonFactory(TestCase):
+    def test_can_create_contact_persons(self):
+        contact_person = ContactPersonFactory()
+        self.assertIsNotNone(contact_person)
+
+
+class TestContactRoleFactory(TestCase):
+    def test_can_create_contact_roles(self):
+        contact_role = ContactRoleFactory()
+        self.assertIsNotNone(contact_role)
-- 
GitLab