diff options
-rw-r--r-- | pyblackbird_cc/resources/forms.py | 1 | ||||
-rw-r--r-- | pyblackbird_cc/resources/tests/test_forms.py | 42 | ||||
-rw-r--r-- | pyblackbird_cc/resources/tests/test_models.py | 4 |
3 files changed, 39 insertions, 8 deletions
diff --git a/pyblackbird_cc/resources/forms.py b/pyblackbird_cc/resources/forms.py index 05d8a75..b8b638c 100644 --- a/pyblackbird_cc/resources/forms.py +++ b/pyblackbird_cc/resources/forms.py @@ -102,6 +102,7 @@ class ResourceCreateForm(forms.Form): ) pdf_files.widget.attrs.update({"class": "file_upload", "accept": ".pdf"}) thumbnail_files.widget.attrs.update({"class": "file_upload", "accept": ".png,.jpg"}) + feature_slot = forms.IntegerField(min_value=1, max_value=3, required=False) def clean_thumbnail_files(self): thumbnail_files = self.files.getlist("thumbnail_files") diff --git a/pyblackbird_cc/resources/tests/test_forms.py b/pyblackbird_cc/resources/tests/test_forms.py index 20fa5da..e19784f 100644 --- a/pyblackbird_cc/resources/tests/test_forms.py +++ b/pyblackbird_cc/resources/tests/test_forms.py @@ -1,7 +1,11 @@ +import pytest +from django.core.exceptions import ValidationError from django.core.files.uploadedfile import SimpleUploadedFile +from django.db import IntegrityError from django.test import TestCase from django.utils.datastructures import MultiValueDict +from pyblackbird_cc.resources.factories import ResourceModelFactory from pyblackbird_cc.resources.forms import ResourceCreateForm from pyblackbird_cc.resources.models import ResourceCategory from pyblackbird_cc.resources.models import ResourceType @@ -21,16 +25,23 @@ class ResourceCreateFormTest(TestCase): "main_resource_category": self.resource_category.id, } self.pdf_files = [ - SimpleUploadedFile(f"file{i}.pdf", b"file_content", content_type="application/pdf") + # use the correct PDF file header - this should pass validation + SimpleUploadedFile( + f"file{i}.pdf", + b"\x25\x50\x44\x46\x2d", + content_type="application/pdf", + ) for i in range(11) ] self.thumbnail_files = [ + # use the correct JPG file header - this should pass validation SimpleUploadedFile( f"thumbnail{i}.jpg", - b"thumbnail_content", + b"\xff\xd8\xff\xdb", content_type="image/jpeg", ) - for i in range(6) + # 5 is the max number currently set in ALLOWED_THUMBNAILS + for i in range(5) ] # This needs to be a MultiValueDict for the test to mimic production code # see clean_pdf_files - self.files.getlist() @@ -39,7 +50,26 @@ class ResourceCreateFormTest(TestCase): ) def test_form_valid(self): - # Create a list of dummy thumbnail files - form = ResourceCreateForm(data=self.form_data, files=self.form_files) - is_valid = form.is_valid() + assert form.is_valid() + + @pytest.mark.django_db() + def test_featured_slots_must_be_unique(self): + r1 = ResourceModelFactory(feature_slot=1) + with pytest.raises(IntegrityError): + ResourceModelFactory(feature_slot=1) + + @pytest.mark.django_db() + def test_featured_slots_allowable(self): + form_data = { + "name": "Test Resource", + "description": "Test Description", + "resource_type": self.resource_type.id, + "age_range": "5-7", + "curriculum": "English", + "feature_slot": 4, + "main_resource_category": self.resource_category.id, + } + form = ResourceCreateForm(data=form_data, files=self.form_files) + breakpoint() + assert not form.is_valid() diff --git a/pyblackbird_cc/resources/tests/test_models.py b/pyblackbird_cc/resources/tests/test_models.py index 34f2905..ec420ae 100644 --- a/pyblackbird_cc/resources/tests/test_models.py +++ b/pyblackbird_cc/resources/tests/test_models.py @@ -2,10 +2,11 @@ import unittest from unittest.mock import patch import pytest -from django.test import TestCase from django.db import IntegrityError +from django.test import TestCase from pyblackbird_cc.resources.factories import PDFPageSnapshotModelFactory +from pyblackbird_cc.resources.factories import ResourceModelFactory from pyblackbird_cc.resources.models import PDFPageSnapshot from pyblackbird_cc.resources.models import PDFResource from pyblackbird_cc.resources.models import Resource @@ -13,7 +14,6 @@ from pyblackbird_cc.resources.models import ResourceCategory from pyblackbird_cc.resources.models import ResourceType from pyblackbird_cc.resources.views import ResourceInfo from pyblackbird_cc.resources.views import _extract_metadata_from_resource -from pyblackbird_cc.resources.factories import ResourceModelFactory # TODO - convert this test into the test of a function # that takes a Resource object and returns all the PDF snapshots |