SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 63edeb8c authored by George Katsikas's avatar George Katsikas :goat:
Browse files

add factories for all production models

parent 17dc9c51
No related branches found
No related tags found
No related merge requests found
...@@ -5,29 +5,25 @@ import random ...@@ -5,29 +5,25 @@ import random
from django.db.models.signals import post_save from django.db.models.signals import post_save
import factory import factory
from common.faker import LazyAwareDate, LazyRandEnum, fake
from production.constants import ( from production.constants import (
PRODUCTION_EVENTS, PRODUCTION_EVENTS,
PRODUCTION_STREAM_STATUS, PRODUCTION_STREAM_STATUS,
PROOFS_REPO_STATUSES, PROOFS_REPO_STATUSES,
PROOFS_STATUSES,
) )
from production.models import ( from finances.factories import ProductionStreamWorkLogFactory
ProductionEvent,
ProductionStream,
ProductionUser,
ProofsRepository,
)
from scipost.factories import UserFactory from scipost.factories import UserFactory
from submissions.factories.submission import SubmissionFactory from submissions.factories.submission import SubmissionFactory
from common.faker import LazyAwareDate, LazyRandEnum, fake from .models import *
import datetime
class ProductionUserFactory(factory.django.DjangoModelFactory): class ProductionUserFactory(factory.django.DjangoModelFactory):
class Meta: class Meta:
model = ProductionUser model = ProductionUser
django_get_or_create = ("user",)
user = factory.SubFactory(UserFactory) user = factory.SubFactory(UserFactory)
name = factory.LazyAttribute( name = factory.LazyAttribute(
...@@ -43,18 +39,31 @@ class ProductionStreamFactory(factory.django.DjangoModelFactory): ...@@ -43,18 +39,31 @@ class ProductionStreamFactory(factory.django.DjangoModelFactory):
submission = factory.SubFactory(SubmissionFactory) submission = factory.SubFactory(SubmissionFactory)
opened = LazyAwareDate("date_this_decade") opened = LazyAwareDate("date_this_decade")
closed = factory.LazyAttribute( closed = factory.LazyAttribute(
# Random date between opened and 1 year later lambda self: fake.aware.date_between(start_date=self.opened, end_date="+1y")
lambda self: self.opened
+ datetime.timedelta(
seconds=random.randint(0, 60 * 60 * 24 * 365),
)
) )
status = LazyRandEnum(PRODUCTION_STREAM_STATUS) status = LazyRandEnum(PRODUCTION_STREAM_STATUS)
officer = factory.SubFactory(ProductionUserFactory) officer = factory.SubFactory(ProductionUserFactory)
supervisor = factory.SubFactory(ProductionUserFactory) supervisor = factory.SubFactory(ProductionUserFactory)
invitations_officer = factory.SubFactory(ProductionUserFactory) invitations_officer = factory.SubFactory(ProductionUserFactory)
on_hold = False 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): class ProductionEventFactory(factory.django.DjangoModelFactory):
...@@ -64,7 +73,12 @@ class ProductionEventFactory(factory.django.DjangoModelFactory): ...@@ -64,7 +73,12 @@ class ProductionEventFactory(factory.django.DjangoModelFactory):
stream = factory.SubFactory(ProductionStreamFactory) stream = factory.SubFactory(ProductionStreamFactory)
event = factory.Faker("random_element", elements=PRODUCTION_EVENTS) event = factory.Faker("random_element", elements=PRODUCTION_EVENTS)
comments = factory.Faker("paragraph") 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( noted_by = factory.LazyAttribute(
lambda self: random.choice([self.stream.officer, self.stream.supervisor]) lambda self: random.choice([self.stream.officer, self.stream.supervisor])
) )
...@@ -74,12 +88,37 @@ class ProductionEventFactory(factory.django.DjangoModelFactory): ...@@ -74,12 +88,37 @@ class ProductionEventFactory(factory.django.DjangoModelFactory):
duration = fake.duration() 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 ProofsRepositoryFactory(factory.django.DjangoModelFactory):
class Meta: class Meta:
model = ProofsRepository model = ProofsRepository
django_get_or_create = ("stream",)
stream = factory.SubFactory(ProductionStreamFactory) stream = factory.SubFactory(ProductionStreamFactory)
status = LazyRandEnum(PROOFS_REPO_STATUSES) status = LazyRandEnum(PROOFS_REPO_STATUSES)
name = factory.LazyAttribute( name = factory.LazyAttribute(
lambda self: ProofsRepository._get_repo_name(self.stream) 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)
__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)
...@@ -147,13 +147,11 @@ class TestProofRepository(TestCase): ...@@ -147,13 +147,11 @@ class TestProofRepository(TestCase):
EditorialDecisionFactory( EditorialDecisionFactory(
submission=submission, submission=submission,
for_journal=scipost_phys_proc, for_journal=scipost_phys_proc,
taken_on=make_aware(datetime.datetime(1994, 2, 20)),
decision=EIC_REC_PUBLISH, decision=EIC_REC_PUBLISH,
) )
proofs_repo = ProofsRepositoryFactory( proofs_repo = ProofsRepositoryFactory(
stream__submission=submission, stream__submission=submission,
stream__opened=make_aware(datetime.datetime(1994, 2, 23)),
) )
self.assertEqual( self.assertEqual(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment