diff options
author | Matthew Lemon <y@yulqen.org> | 2024-09-08 17:12:58 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-09-08 17:12:58 +0100 |
commit | 63461fb0b86a8f4a1422673626311d0979cd1077 (patch) | |
tree | d7ae8a2457c1bc630cb68b6e5ce1ec371bfdc4ad | |
parent | db531eaa027d2abcfcceabfef4b8aa9dc5ca1e5d (diff) |
resources tests passing
-rw-r--r-- | pyblackbird_cc/resources/factories.py | 50 | ||||
-rw-r--r-- | pyblackbird_cc/resources/tests/test_file_processing.py | 2 | ||||
-rw-r--r-- | pyblackbird_cc/resources/tests/test_forms.py | 3 | ||||
-rw-r--r-- | pyblackbird_cc/resources/tests/test_models.py | 14 | ||||
-rw-r--r-- | pyblackbird_cc/resources/tests/test_views.py | 14 |
5 files changed, 51 insertions, 32 deletions
diff --git a/pyblackbird_cc/resources/factories.py b/pyblackbird_cc/resources/factories.py index 51afd09..6d6311a 100644 --- a/pyblackbird_cc/resources/factories.py +++ b/pyblackbird_cc/resources/factories.py @@ -1,3 +1,5 @@ +import itertools + import factory from .models import PDFPageSnapshot @@ -32,31 +34,43 @@ class PDFPageSnapshotModelFactory(factory.django.DjangoModelFactory): pdf_file = factory.SubFactory("pyblackbird_cc.resources.factories.PDFResourceModelFactory") + class ResourceModelFactory(factory.django.DjangoModelFactory): class Meta: model = Resource name = factory.Sequence(lambda n: f"Default Resource {n}") - thumbnail_filenames = factory.Sequence(lambda n: [f"thumbnail_{n}.jpg"]) + price = factory.Faker('pydecimal', left_digits=4, right_digits=2, positive=True) + thumbnail_filenames = factory.List([factory.Faker('file_name', extension='jpg') for _ in range(3)]) resource_type = factory.SubFactory(ResourceTypeModelFactory) main_resource_category = factory.SubFactory(ResourceCategoryModelFactory) - additional_resource_category = factory.SubFactory(ResourceCategoryModelFactory) - description = factory.Sequence(lambda n: f"Default description {n}") - age_range = factory.Iterator(["5-7", "7-10", "10+"]) - curriculum = factory.Iterator(["English", "Scottish"]) - - pdfs = factory.RelatedFactoryList( - "pyblackbird_cc.resources.factories.PDFResourceModelFactory", - factory_related_name="resource", - size=3, - ) - - # TODO: Don't think we can include this this way - we are getting import issues - # pdf_snapshots = factory.RelatedFactoryList( - # "pyblackbird_cc.resources.factories.PDFPageSnapshotModelFactory", - # factory_related_name="pdfs", - # size=20, - # ) + subcategories = factory.RelatedFactoryList(ResourceCategoryModelFactory, size=2) + description = factory.Faker('paragraph') + card_description = factory.Faker('text', max_nb_chars=1000) + age_range = factory.Iterator(["5-7", "7-9", "9-11"]) + curriculum = factory.Iterator(["English", "Scottish", "No curriculum"]) + feature_slot = factory.Iterator(itertools.chain([1, 2, 3], itertools.repeat(None))) + created_at = factory.Faker('date_time_this_year') + updated_at = factory.Faker('date_time_this_month') + + @factory.post_generation + def pdfs(self, create, extracted, **kwargs): + if not create: + return + + if extracted: + for pdf in extracted: + self.pdf_resources.add(pdf) + else: + PDFResourceModelFactory.create_batch(3, resource=self) + + +class ResourceSubcategoryModelFactory(factory.django.DjangoModelFactory): + class Meta: + model = ResourceCategory + django_get_or_create = ("name",) + + name = factory.Sequence(lambda n: f"Default Resource Subcategory {n}") class PDFResourceModelFactory(factory.django.DjangoModelFactory): diff --git a/pyblackbird_cc/resources/tests/test_file_processing.py b/pyblackbird_cc/resources/tests/test_file_processing.py index 0a0f1a3..e4ac781 100644 --- a/pyblackbird_cc/resources/tests/test_file_processing.py +++ b/pyblackbird_cc/resources/tests/test_file_processing.py @@ -25,7 +25,7 @@ def test_detect_snapshotted_pdf_collection(): class PDFFileUploadTestCase(TestCase): def setUp(self): self.url = reverse("resources:create_resource") - self.test_file_path = "pyblackbird_cc/resources/tests/testdata/test_small_file.pdf" + self.test_file_path = "resources/tests/testdata/test_small_file.pdf" # Create a test user self.email = "testuser@example.com" diff --git a/pyblackbird_cc/resources/tests/test_forms.py b/pyblackbird_cc/resources/tests/test_forms.py index cea3540..9190f55 100644 --- a/pyblackbird_cc/resources/tests/test_forms.py +++ b/pyblackbird_cc/resources/tests/test_forms.py @@ -18,8 +18,9 @@ class ResourceCreateFormTest(TestCase): self.form_data = { "name": "Test Resource", "description": "Test Description", + "card_description": "Test Card Description", "resource_type": self.resource_type.id, - "age_range": "5-7", + "age_range": "Reception (4-5yrs)", "curriculum": "English", "feature_slot": 1, "main_resource_category": self.resource_category.id, diff --git a/pyblackbird_cc/resources/tests/test_models.py b/pyblackbird_cc/resources/tests/test_models.py index 2e45d39..7398c43 100644 --- a/pyblackbird_cc/resources/tests/test_models.py +++ b/pyblackbird_cc/resources/tests/test_models.py @@ -18,18 +18,14 @@ from pyblackbird_cc.resources.views import _extract_metadata_from_resource @pytest.mark.django_db() def test_resource_model(resources): - # Create a resource r1 = resources[0] - # Get the first pdf resource on the resource pdf_on_rw = r1.pdf_resources.first() - # Create a single pdf_page_shapshot and associate it with the pdf resource on the resource pdf_page_snapshot = PDFPageSnapshotModelFactory(pdf_file=pdf_on_rw) - assert Resource.objects.filter(name="Default Resource 1").exists() - assert "test_0.pdf" in r1.get_pdf_file_names() - assert "test_1.pdf" in r1.get_pdf_file_names() - assert "test_2.pdf" in r1.get_pdf_file_names() - assert "test_3.pdf" not in r1.get_pdf_file_names() - assert "pdf_page_snapshot_0.jpg" in r1.get_pdf_snapshot_file_names() + + assert Resource.objects.filter(id=r1.id).exists() + assert any("test_" in name for name in r1.get_pdf_file_names()) + assert len(r1.get_pdf_file_names()) == 3 + assert "pdf_page_snapshot_" in r1.get_pdf_snapshot_file_names()[0] @pytest.mark.django_db() diff --git a/pyblackbird_cc/resources/tests/test_views.py b/pyblackbird_cc/resources/tests/test_views.py index 0cf242f..e5025a5 100644 --- a/pyblackbird_cc/resources/tests/test_views.py +++ b/pyblackbird_cc/resources/tests/test_views.py @@ -16,6 +16,7 @@ from ..views import create_resource pytestmark = pytest.mark.django_db + def test_create_featured_resource_view(client): url = reverse("resources:create_featured") response = client.get(url) @@ -28,17 +29,24 @@ def test_create_resource_view(client): assert response.status_code == 302 +@pytest.mark.django_db def test_create_resource_has_form(client): + User = get_user_model() + user = User.objects.create_user(email='testuser@example.com', password='12345') + client.login(email='testuser@example.com', password='12345') + url = reverse("resources:create_resource") response = client.get(url) - assert type(response.context["form"]) == ResourceCreateForm + assert response.status_code == 200 + assert 'form' in response.context + assert isinstance(response.context['form'], ResourceCreateForm) class PDFFileUploadTestCase(TestCase): def setUp(self): self.url = reverse("resources:create_resource") - self.two_page_pdf = "pyblackbird_cc/resources/tests/testdata/two_page.pdf" - self.seven_page_pdf = "pyblackbird_cc/resources/tests/testdata/seven_page.pdf" + self.two_page_pdf = "resources/tests/testdata/two_page.pdf" + self.seven_page_pdf = "resources/tests/testdata/seven_page.pdf" # Create a test user self.email = "testuser@example.com" |