From 7019d4a15d40852f019ac63cdb63740d9b4638d2 Mon Sep 17 00:00:00 2001 From: George Katsikas <giorgakis.katsikas@gmail.com> Date: Mon, 9 Oct 2023 13:42:31 +0200 Subject: [PATCH] provide new factories for making tests --- .../commentaries/tests/test_forms.py | 5 ++ scipost_django/finances/factories.py | 27 ++++++ scipost_django/journals/factories.py | 34 ++++++-- scipost_django/proceedings/factories.py | 44 ++++++++++ scipost_django/production/factories.py | 85 +++++++++++++++++++ .../submissions/factories/__init__.py | 2 + .../submissions/factories/preprintserver.py | 21 +++++ .../submissions/models/factories.py | 21 +++++ 8 files changed, 232 insertions(+), 7 deletions(-) create mode 100644 scipost_django/finances/factories.py create mode 100644 scipost_django/proceedings/factories.py create mode 100644 scipost_django/production/factories.py create mode 100644 scipost_django/submissions/factories/preprintserver.py create mode 100644 scipost_django/submissions/models/factories.py diff --git a/scipost_django/commentaries/tests/test_forms.py b/scipost_django/commentaries/tests/test_forms.py index 4a6e195df..a667d2822 100644 --- a/scipost_django/commentaries/tests/test_forms.py +++ b/scipost_django/commentaries/tests/test_forms.py @@ -9,6 +9,9 @@ from django.test import TestCase from common.helpers import model_form_data from scipost.factories import UserFactory, ContributorFactory + +from submissions.factories import PreprintServerFactory + from ..factories import ( CommentaryFactory, UnvettedCommentaryFactory, @@ -33,6 +36,8 @@ class TestArxivQueryForm(TestCase): def setUp(self): add_groups_and_permissions() ContributorFactory.create_batch(5) + # Create the arXiv preprint server + PreprintServerFactory.arxiv() def test_new_arxiv_identifier_is_valid(self): new_identifier_data = {"identifier": "1612.07611v1"} diff --git a/scipost_django/finances/factories.py b/scipost_django/finances/factories.py new file mode 100644 index 000000000..13210f224 --- /dev/null +++ b/scipost_django/finances/factories.py @@ -0,0 +1,27 @@ +__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)" +__license__ = "AGPL v3" + +import random +import factory + +from finances.models import WorkLog +from production.constants import PRODUCTION_ALL_WORK_LOG_TYPES +from scipost.factories import UserFactory + +from common.faker import LazyRandEnum, fake + + +class WorkLogFactory(factory.django.DjangoModelFactory): + class Meta: + model = WorkLog + + user = factory.SubFactory(UserFactory) + comments = factory.Faker("paragraph") + log_type = LazyRandEnum(PRODUCTION_ALL_WORK_LOG_TYPES) + duration = factory.LazyAttribute(lambda _: fake.duration()) + work_date = factory.Faker("date_this_year") + created = factory.Faker("past_date", start_date="-1y") + + content_type = None + object_id = None + content = None diff --git a/scipost_django/journals/factories.py b/scipost_django/journals/factories.py index 19d5a5e25..02689f39c 100644 --- a/scipost_django/journals/factories.py +++ b/scipost_django/journals/factories.py @@ -2,22 +2,26 @@ __copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)" __license__ = "AGPL v3" -import factory import datetime -import pytz import random from string import ascii_lowercase +import factory +import pytz from common.helpers import ( random_digits, random_external_doi, random_external_journal_abbrev, ) -from journals.constants import JOURNAL_STRUCTURE, PUBLICATION_PUBLISHED - -from .models import Journal, Volume, Issue, Publication, Reference - from faker import Faker +from journals.constants import ( + ISSUES_AND_VOLUMES, + ISSUES_ONLY, + JOURNAL_STRUCTURE, + PUBLICATION_PUBLISHED, +) + +from .models import Issue, Journal, Publication, Reference, Volume class ReferenceFactory(factory.django.DjangoModelFactory): @@ -53,6 +57,22 @@ class JournalFactory(factory.django.DjangoModelFactory): model = Journal django_get_or_create = ("name",) + @classmethod + def SciPostPhysics(cls): + return cls( + name="SciPost Physics", + doi_label="SciPostPhys", + structure=ISSUES_AND_VOLUMES, + ) + + @classmethod + def SciPostPhysicsProc(cls): + return cls( + name="SciPost Physics Proceedings", + doi_label="SciPostPhysProc", + structure=ISSUES_ONLY, + ) + class VolumeFactory(factory.django.DjangoModelFactory): in_journal = factory.SubFactory(JournalFactory) @@ -71,7 +91,7 @@ class VolumeFactory(factory.django.DjangoModelFactory): class IssueFactory(factory.django.DjangoModelFactory): - in_volume = factory.Iterator(Volume.objects.all()) + in_volume = factory.SubFactory(VolumeFactory) number = factory.LazyAttribute(lambda o: o.in_volume.issues.count() + 1) doi_label = factory.LazyAttribute( lambda o: "%s.%i" % (o.in_volume.doi_label, o.number) diff --git a/scipost_django/proceedings/factories.py b/scipost_django/proceedings/factories.py new file mode 100644 index 000000000..361f7bfb3 --- /dev/null +++ b/scipost_django/proceedings/factories.py @@ -0,0 +1,44 @@ +__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)" +__license__ = "AGPL v3" + + +import random + +import factory +from journals.factories import IssueFactory +from proceedings.models import Proceedings +import datetime + + +class ProceedingsFactory(factory.django.DjangoModelFactory): + class Meta: + model = Proceedings + + issue = factory.SubFactory(IssueFactory) + event_name = factory.Faker("company") + event_suffix = factory.Faker("company_suffix") + event_description = factory.Faker("paragraph") + event_start_date = factory.Faker("date_this_decade") + event_end_date = factory.LazyAttribute( + lambda self: self.event_start_date + + datetime.timedelta( + seconds=random.randint(0, 60 * 60 * 24 * 365), + ) + ) + + submissions_open = factory.Faker("date_this_decade") + submissions_close = factory.LazyAttribute( + lambda self: self.submissions_open + + datetime.timedelta( + seconds=random.randint(0, 60 * 60 * 24 * 365), + ) + ) + submissions_deadline = factory.LazyAttribute( + lambda self: self.submissions_close + + datetime.timedelta( + seconds=random.randint(0, 60 * 60 * 24 * 365), + ) + ) + + preface_title = factory.Faker("sentence") + preface_text = factory.Faker("paragraph") diff --git a/scipost_django/production/factories.py b/scipost_django/production/factories.py new file mode 100644 index 000000000..378d25a60 --- /dev/null +++ b/scipost_django/production/factories.py @@ -0,0 +1,85 @@ +__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)" +__license__ = "AGPL v3" + +import random +from django.db.models.signals import post_save + +import factory + +from production.constants import ( + PRODUCTION_EVENTS, + PRODUCTION_STREAM_STATUS, + PROOFS_REPO_STATUSES, +) +from production.models import ( + ProductionEvent, + ProductionStream, + ProductionUser, + ProofsRepository, +) +from scipost.factories import UserFactory +from submissions.factories.submission import SubmissionFactory + +from common.faker import LazyAwareDate, LazyRandEnum, fake + +import datetime + + +class ProductionUserFactory(factory.django.DjangoModelFactory): + class Meta: + model = ProductionUser + + user = factory.SubFactory(UserFactory) + name = factory.LazyAttribute( + lambda self: self.user.first_name + " " + self.user.last_name + ) + + +@factory.django.mute_signals(post_save) +class ProductionStreamFactory(factory.django.DjangoModelFactory): + class Meta: + model = ProductionStream + + 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), + ) + ) + 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) + + +class ProductionEventFactory(factory.django.DjangoModelFactory): + class Meta: + model = ProductionEvent + + 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_by = factory.LazyAttribute( + lambda self: random.choice([self.stream.officer, self.stream.supervisor]) + ) + noted_to = factory.LazyAttribute( + lambda self: random.choice([self.stream.officer, self.stream.supervisor]) + ) + duration = fake.duration() + + +class ProofsRepositoryFactory(factory.django.DjangoModelFactory): + class Meta: + model = ProofsRepository + + stream = factory.SubFactory(ProductionStreamFactory) + status = LazyRandEnum(PROOFS_REPO_STATUSES) + name = factory.LazyAttribute( + lambda self: ProofsRepository._get_repo_name(self.stream) + ) diff --git a/scipost_django/submissions/factories/__init__.py b/scipost_django/submissions/factories/__init__.py index 1daedb943..f236ef4ba 100644 --- a/scipost_django/submissions/factories/__init__.py +++ b/scipost_django/submissions/factories/__init__.py @@ -28,3 +28,5 @@ from .report import ( ) from .recommendation import EICRecommendationFactory + +from .preprintserver import PreprintServerFactory diff --git a/scipost_django/submissions/factories/preprintserver.py b/scipost_django/submissions/factories/preprintserver.py new file mode 100644 index 000000000..654dcc580 --- /dev/null +++ b/scipost_django/submissions/factories/preprintserver.py @@ -0,0 +1,21 @@ +__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)" +__license__ = "AGPL v3" + + +import factory + +from faker import Faker + +from submissions.models.preprint_server import PreprintServer + + +class PreprintServerFactory(factory.django.DjangoModelFactory): + name = factory.Faker("sentence") + url = factory.Faker("url") + + class Meta: + model = PreprintServer + + @classmethod + def arxiv(cls): + return cls(name="arXiv", url="https://arxiv.org/") diff --git a/scipost_django/submissions/models/factories.py b/scipost_django/submissions/models/factories.py new file mode 100644 index 000000000..070465198 --- /dev/null +++ b/scipost_django/submissions/models/factories.py @@ -0,0 +1,21 @@ +__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)" +__license__ = "AGPL v3" + +import factory +from common.faker import LazyRandEnum +from journals.factories import JournalFactory +from submissions.factories.submission import SubmissionFactory +from submissions.models.decision import EditorialDecision + + +class EditorialDecisionFactory(factory.django.DjangoModelFactory): + class Meta: + model = EditorialDecision + django_get_or_create = ("submission", "for_journal") + + submission = factory.SubFactory(SubmissionFactory) + for_journal = factory.SubFactory(JournalFactory) + decision = LazyRandEnum(EditorialDecision.EDITORIAL_DECISION_STATUSES) + taken_on = factory.Faker("date_this_decade") + remarks_for_authors = factory.Faker("paragraph") + status = LazyRandEnum(EditorialDecision.EDITORIAL_DECISION_STATUSES) -- GitLab