From 63edeb8cba65bda5d5475565965443f2f7d3ac58 Mon Sep 17 00:00:00 2001 From: George Katsikas <giorgakis.katsikas@gmail.com> Date: Mon, 20 Nov 2023 13:44:43 +0100 Subject: [PATCH] add factories for all production models --- scipost_django/production/factories.py | 71 ++++++++++++++----- .../production/tests/test_factories.py | 42 +++++++++++ .../production/tests/test_models.py | 2 - 3 files changed, 97 insertions(+), 18 deletions(-) create mode 100644 scipost_django/production/tests/test_factories.py diff --git a/scipost_django/production/factories.py b/scipost_django/production/factories.py index 378d25a60..44ff401ca 100644 --- a/scipost_django/production/factories.py +++ b/scipost_django/production/factories.py @@ -5,29 +5,25 @@ import random from django.db.models.signals import post_save import factory +from common.faker import LazyAwareDate, LazyRandEnum, fake from production.constants import ( PRODUCTION_EVENTS, PRODUCTION_STREAM_STATUS, PROOFS_REPO_STATUSES, + PROOFS_STATUSES, ) -from production.models import ( - ProductionEvent, - ProductionStream, - ProductionUser, - ProofsRepository, -) +from finances.factories import ProductionStreamWorkLogFactory from scipost.factories import UserFactory from submissions.factories.submission import SubmissionFactory -from common.faker import LazyAwareDate, LazyRandEnum, fake - -import datetime +from .models import * class ProductionUserFactory(factory.django.DjangoModelFactory): class Meta: model = ProductionUser + django_get_or_create = ("user",) user = factory.SubFactory(UserFactory) name = factory.LazyAttribute( @@ -43,18 +39,31 @@ class ProductionStreamFactory(factory.django.DjangoModelFactory): submission = factory.SubFactory(SubmissionFactory) opened = LazyAwareDate("date_this_decade") closed = factory.LazyAttribute( - # Random date between opened and 1 year later - lambda self: self.opened - + datetime.timedelta( - seconds=random.randint(0, 60 * 60 * 24 * 365), - ) + lambda self: fake.aware.date_between(start_date=self.opened, end_date="+1y") ) status = LazyRandEnum(PRODUCTION_STREAM_STATUS) officer = factory.SubFactory(ProductionUserFactory) supervisor = factory.SubFactory(ProductionUserFactory) invitations_officer = factory.SubFactory(ProductionUserFactory) on_hold = False - # work_logs = factory.SubFactory(WorkLogFactory) + + @factory.post_generation + def work_logs(self, create, extracted, **kwargs): + if not create: + return + + if extracted: + for work_log in extracted: + self.work_logs.add(work_log) + + else: + self.work_logs.add( + *ProductionStreamWorkLogFactory.create_batch( + random.randint(1, 4), + stream=self, + user=random.choice([self.officer.user, self.supervisor.user]), + ) + ) class ProductionEventFactory(factory.django.DjangoModelFactory): @@ -64,7 +73,12 @@ class ProductionEventFactory(factory.django.DjangoModelFactory): stream = factory.SubFactory(ProductionStreamFactory) event = factory.Faker("random_element", elements=PRODUCTION_EVENTS) comments = factory.Faker("paragraph") - noted_on = factory.Faker("past_date", start_date="-1y") + noted_on = factory.LazyAttribute( + lambda self: fake.aware.date_between( + start_date=self.stream.opened, + end_date=self.stream.closed, + ) + ) noted_by = factory.LazyAttribute( lambda self: random.choice([self.stream.officer, self.stream.supervisor]) ) @@ -74,12 +88,37 @@ class ProductionEventFactory(factory.django.DjangoModelFactory): duration = fake.duration() +class ProductionEventAttachmentFactory(factory.django.DjangoModelFactory): + class Meta: + model = ProductionEventAttachment + + production_event = factory.SubFactory(ProductionEventFactory) + attachment = factory.django.FileField(filename="author_comments.pdf") + + class ProofsRepositoryFactory(factory.django.DjangoModelFactory): class Meta: model = ProofsRepository + django_get_or_create = ("stream",) stream = factory.SubFactory(ProductionStreamFactory) status = LazyRandEnum(PROOFS_REPO_STATUSES) name = factory.LazyAttribute( lambda self: ProofsRepository._get_repo_name(self.stream) ) + + +class ProofsFactory(factory.django.DjangoModelFactory): + class Meta: + model = Proofs + + attachment = factory.django.FileField(filename="proofs.pdf") + stream = factory.SubFactory(ProductionStreamFactory) + uploaded_by = factory.LazyAttribute(lambda self: self.stream.officer) + created = factory.LazyAttribute( + lambda self: fake.aware.date_between( + start_date=self.stream.opened, + end_date=self.stream.closed, + ) + ) + status = LazyRandEnum(PROOFS_STATUSES) diff --git a/scipost_django/production/tests/test_factories.py b/scipost_django/production/tests/test_factories.py new file mode 100644 index 000000000..76efa54f5 --- /dev/null +++ b/scipost_django/production/tests/test_factories.py @@ -0,0 +1,42 @@ +__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)" +__license__ = "AGPL v3" + +from django.test import TestCase + +from production.factories import ( + ProductionEventFactory, + ProductionStreamFactory, + ProductionUserFactory, + ProofsFactory, + ProofsRepositoryFactory, +) + + +class TestProductionUserFactory(TestCase): + def test_can_create_production_users(self): + production_user = ProductionUserFactory() + self.assertIsNotNone(production_user) + + +class TestProductionStreamFactory(TestCase): + def test_can_create_production_streams(self): + production_stream = ProductionStreamFactory() + self.assertIsNotNone(production_stream) + + +class TestProductionEventFactory(TestCase): + def test_can_create_production_events(self): + production_event = ProductionEventFactory() + self.assertIsNotNone(production_event) + + +class TestProofsRepositoryFactory(TestCase): + def test_can_create_proofs_repositoriess(self): + proofs_repository = ProofsRepositoryFactory() + self.assertIsNotNone(proofs_repository) + + +class TestProofsFactory(TestCase): + def test_can_create_proofss(self): + proofs = ProofsFactory() + self.assertIsNotNone(proofs) diff --git a/scipost_django/production/tests/test_models.py b/scipost_django/production/tests/test_models.py index 60d92d293..84e047d09 100644 --- a/scipost_django/production/tests/test_models.py +++ b/scipost_django/production/tests/test_models.py @@ -147,13 +147,11 @@ class TestProofRepository(TestCase): EditorialDecisionFactory( submission=submission, for_journal=scipost_phys_proc, - taken_on=make_aware(datetime.datetime(1994, 2, 20)), decision=EIC_REC_PUBLISH, ) proofs_repo = ProofsRepositoryFactory( stream__submission=submission, - stream__opened=make_aware(datetime.datetime(1994, 2, 23)), ) self.assertEqual( -- GitLab