diff options
author | Matthew Lemon <y@yulqen.org> | 2024-10-15 21:01:31 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-10-15 21:01:31 +0100 |
commit | eeaddb27560d723ca7d61359744ceb2709fccd2d (patch) | |
tree | 04ddbc49ae7b73d5f5a9e1716d7227aecd3b9f85 /alphabetlearning/resources/tests/test_forms.py | |
parent | 7a3044c859043837e6c7c95bb4894d04e9b2cbc2 (diff) |
Renamed from pyblackbird_cc to alphabetlearning - everywhere
Diffstat (limited to 'alphabetlearning/resources/tests/test_forms.py')
-rw-r--r-- | alphabetlearning/resources/tests/test_forms.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/alphabetlearning/resources/tests/test_forms.py b/alphabetlearning/resources/tests/test_forms.py new file mode 100644 index 0000000..aebc110 --- /dev/null +++ b/alphabetlearning/resources/tests/test_forms.py @@ -0,0 +1,74 @@ +import pytest +from django.core.files.uploadedfile import SimpleUploadedFile +from django.db import IntegrityError +from django.test import TestCase +from django.utils.datastructures import MultiValueDict + +from alphabetlearning.resources.factories import ResourceModelFactory +from alphabetlearning.resources.forms import ResourceCreateForm +from alphabetlearning.resources.models import ResourceCategory +from alphabetlearning.resources.models import ResourceType + + +class ResourceCreateFormTest(TestCase): + def setUp(self): + self.resource_type = ResourceType.objects.create(name="Test Resource Type") + self.resource_category = ResourceCategory.objects.create(name="Test Resource Category") + self.form_data = { + "name": "Test Resource", + "description": "Test Description", + "card_description": "Test Card Description", + "resource_type": self.resource_type.id, + "age_range": "Reception (4-5yrs)", + "curriculum": "English", + "feature_slot": 1, + "main_resource_category": self.resource_category.id, + } + self.pdf_files = [ + # 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"\xff\xd8\xff\xdb", + content_type="image/jpeg", + ) + # 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() + self.form_files = MultiValueDict( + {"pdf_files": self.pdf_files, "thumbnail_files": self.thumbnail_files}, + ) + + def test_form_valid(self): + form = ResourceCreateForm(data=self.form_data, files=self.form_files) + 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) + assert not form.is_valid() |