From 3fd196cf98ac755868eb5fc587a6805f5129690a Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Wed, 15 Nov 2023 17:46:13 +0100
Subject: [PATCH] add factories for all forums models

---
 scipost_django/forums/factories.py            | 133 ++++++++++++++++++
 scipost_django/forums/tests/__init__.py       |   0
 scipost_django/forums/tests/test_factories.py |  40 ++++++
 3 files changed, 173 insertions(+)
 create mode 100644 scipost_django/forums/factories.py
 create mode 100644 scipost_django/forums/tests/__init__.py
 create mode 100644 scipost_django/forums/tests/test_factories.py

diff --git a/scipost_django/forums/factories.py b/scipost_django/forums/factories.py
new file mode 100644
index 000000000..dcfa3edbe
--- /dev/null
+++ b/scipost_django/forums/factories.py
@@ -0,0 +1,133 @@
+from django.utils.text import slugify
+import factory
+
+from common.faker import LazyAwareDate
+from scipost.factories import UserFactory
+
+from .models import Forum, Meeting, Motion, Post
+
+from common.faker import fake
+
+
+class ForumFactory(factory.django.DjangoModelFactory):
+    class Meta:
+        model = Forum
+
+    name = factory.Faker("sentence", nb_words=3)
+    slug = factory.lazy_attribute(lambda self: slugify(self.name))
+    description = factory.Faker("sentence")
+    publicly_visible = True
+
+
+class MeetingFactory(factory.django.DjangoModelFactory):
+    class Meta:
+        model = Meeting
+
+    forum = factory.SubFactory(ForumFactory)
+    date_from = LazyAwareDate("date_this_year")
+    date_until = factory.LazyAttribute(
+        lambda self: fake.aware.date_between(start_date=self.date_from, end_date="+1y")
+    )
+    preamble = factory.Faker("paragraph")
+    minutes = factory.Faker("paragraph")
+
+
+class BasePostFactory(factory.django.DjangoModelFactory):
+    class Meta:
+        model = Post
+        abstract = True
+
+    posted_by = factory.SubFactory(UserFactory)
+    posted_on = LazyAwareDate("date_this_year")
+    needs_vetting = False
+    vetted_by = factory.SubFactory(UserFactory)
+    subject = factory.Faker("sentence")
+    text = factory.Faker("paragraph")
+
+
+class PostFactory(BasePostFactory):
+    class Meta:
+        model = Post
+
+    # class Params:
+    anchor = factory.SubFactory("forums.factories.ForumFactory")
+    parent = factory.SelfAttribute("anchor")
+
+    # parent_object_id = factory.SelfAttribute("anchored_to.id")
+    # parent_content_type = factory.LazyAttribute(
+    #     lambda self: django.contrib.contenttypes.models.ContentType.objects.get_for_model(
+    #         self.anchored_to
+    #     )
+    # )
+    # anchor_object_id = factory.SelfAttribute("anchored_to.id")
+    # anchor_content_type = factory.LazyAttribute(
+    #     lambda self: django.contrib.contenttypes.models.ContentType.objects.get_for_model(
+    #         self.anchored_to
+    #     )
+    # )
+
+
+class ReplyPostFactory(PostFactory):
+    class Meta:
+        model = Post
+
+    # class Params:
+    parent = factory.SubFactory("forums.factories.PostFactory")
+
+    # parent_object_id = factory.SelfAttribute("reply_to.id")
+    # parent_content_type = factory.LazyAttribute(
+    #     lambda self: django.contrib.contenttypes.models.ContentType.objects.get_for_model(
+    #         self.reply_to
+    #     )
+    # )
+
+
+class MotionFactory(PostFactory):
+    class Meta:
+        model = Motion
+
+    post = factory.SubFactory(PostFactory)
+    voting_deadline = LazyAwareDate("date_this_year")
+    accepted = False
+
+    @factory.post_generation
+    def eligible_for_voting(self, create, extracted, **kwargs):
+        if not create:
+            return
+        if extracted:
+            for user in extracted:
+                self.eligible_for_voting.add(user)
+        else:
+            self.eligible_for_voting.add(*UserFactory.create_batch(5))
+
+    @factory.post_generation
+    def in_agreement(self, create, extracted, **kwargs):
+        if not create:
+            return
+        if extracted:
+            for user in extracted:
+                self.in_agreement.add(user)
+
+    @factory.post_generation
+    def in_disagreement(self, create, extracted, **kwargs):
+        if not create:
+            return
+        if extracted:
+            for user in extracted:
+                self.in_disagreement.add(user)
+
+    @factory.post_generation
+    def in_abstain(self, create, extracted, **kwargs):
+        if not create:
+            return
+        if extracted:
+            for user in extracted:
+                self.in_abstain.add(user)
+
+    @factory.post_generation
+    def in_doubt(self, create, extracted, **kwargs):
+        if not create:
+            return
+        if extracted:
+            for user in extracted:
+                self.in_doubt.add(user)
diff --git a/scipost_django/forums/tests/__init__.py b/scipost_django/forums/tests/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/scipost_django/forums/tests/test_factories.py b/scipost_django/forums/tests/test_factories.py
new file mode 100644
index 000000000..b1799facf
--- /dev/null
+++ b/scipost_django/forums/tests/test_factories.py
@@ -0,0 +1,40 @@
+from django.test import TestCase
+from ..factories import (
+    ForumFactory,
+    MeetingFactory,
+    MotionFactory,
+    PostFactory,
+    ReplyPostFactory,
+)
+
+
+class TestForumFactory(TestCase):
+    def test_can_create_forums(self):
+        forum = ForumFactory()
+        self.assertIsNotNone(forum)
+
+
+class TestPostFactory(TestCase):
+    def test_can_create_posts(self):
+        post = PostFactory()
+        self.assertIsNotNone(post)
+
+
+class TestReplyPostFactory(TestCase):
+    def test_can_create_reply_posts(self):
+        parent_post = PostFactory()
+        post = ReplyPostFactory(parent=parent_post)
+        self.assertIsNotNone(post)
+        self.assertEqual(post.parent, parent_post)
+
+
+class TestMotionFactory(TestCase):
+    def test_can_create_motions(self):
+        motion = MotionFactory()
+        self.assertIsNotNone(motion)
+
+
+class TestMeetingFactory(TestCase):
+    def test_can_create_meetings(self):
+        meeting = MeetingFactory()
+        self.assertIsNotNone(meeting)
-- 
GitLab