diff --git a/scipost_django/preprints/factories.py b/scipost_django/preprints/factories.py index 4eb0ebac3382d9f0a4d3a99c11b27b34a87b4ccb..bc137b07464a71d238e5ed326f980dc7cbac3460 100644 --- a/scipost_django/preprints/factories.py +++ b/scipost_django/preprints/factories.py @@ -10,16 +10,17 @@ from .models import Preprint class PreprintFactory(factory.django.DjangoModelFactory): - """ - Generate random Preprint instances. - """ - - identifier_w_vn_nr = factory.Sequence( - lambda n: random_arxiv_identifier_with_version_number() - ) - url = factory.lazy_attribute( - lambda o: ("https://arxiv.org/abs/%s" % o.identifier_w_vn_nr) - ) - class Meta: model = Preprint + + class Params: + arXiv = factory.Trait( + identifier_w_vn_nr=random_arxiv_identifier_with_version_number(), + url=factory.LazyAttribute( + lambda self: f"https://arxiv.org/abs/{self.identifier_w_vn_nr}" + ), + ) + scipost = factory.Trait( + identifier_w_vn_nr=factory.Faker("numerify", text="scipost_######_#####v#"), + _file=factory.django.FileField(filename="submission.pdf"), + ) diff --git a/scipost_django/preprints/tests/test_factories.py b/scipost_django/preprints/tests/test_factories.py new file mode 100644 index 0000000000000000000000000000000000000000..f083122bbb88439b583d84fb310ccd424119a2ee --- /dev/null +++ b/scipost_django/preprints/tests/test_factories.py @@ -0,0 +1,21 @@ +__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)" +__license__ = "AGPL v3" + +from django.test import TestCase +from ..factories import PreprintFactory + + +class TestPreprintFactory(TestCase): + def test_can_create_preprints(self): + preprint = PreprintFactory() + self.assertIsNotNone(preprint) + + def test_can_create_arxiv_preprints(self): + preprint = PreprintFactory(arXiv=True) + self.assertIsNotNone(preprint) + self.assertTrue(preprint.url.startswith("https://arxiv.org/abs/")) + + def test_can_create_scipost_preprints(self): + preprint = PreprintFactory(scipost=True) + self.assertIsNotNone(preprint) + self.assertTrue(preprint.identifier_w_vn_nr.startswith("scipost_"))