aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-05-22 21:26:33 +0100
committerMatthew Lemon <y@yulqen.org>2024-05-22 21:26:33 +0100
commit295b84a90f36b437356dc8c22b69dd230e44a823 (patch)
tree85b9a0894059a01fb1daa61fc80d263e835bf2d9
parent6985c83f55f4b60f531c77d333aeb34b0105c956 (diff)
Fixed the failing form test
We now pass a MultiValueDict object to which allows self.files.getlist() to work properly inside clean_pdf_files()
-rw-r--r--pyblackbird_cc/resources/tests/test_forms.py61
1 files changed, 23 insertions, 38 deletions
diff --git a/pyblackbird_cc/resources/tests/test_forms.py b/pyblackbird_cc/resources/tests/test_forms.py
index 1ca823f..20fa5da 100644
--- a/pyblackbird_cc/resources/tests/test_forms.py
+++ b/pyblackbird_cc/resources/tests/test_forms.py
@@ -1,28 +1,30 @@
-import unittest
-
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
+from django.utils.datastructures import MultiValueDict
-from ..forms import ResourceCreateForm
-from ..models import ResourceCategory
-from ..models import ResourceType
+from pyblackbird_cc.resources.forms import ResourceCreateForm
+from pyblackbird_cc.resources.models import ResourceCategory
+from pyblackbird_cc.resources.models import ResourceType
-@unittest.skip("Skipping this test for now as it is broken")
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")
-
- def test_clean_pdf_files_validation_error(self):
- # Create a list of dummy PDF files
- pdf_files = [
+ self.form_data = {
+ "name": "Test Resource",
+ "description": "Test Description",
+ "resource_type": self.resource_type.id,
+ "age_range": "5-7",
+ "curriculum": "English",
+ "feature_slot": 1,
+ "main_resource_category": self.resource_category.id,
+ }
+ self.pdf_files = [
SimpleUploadedFile(f"file{i}.pdf", b"file_content", content_type="application/pdf")
for i in range(11)
]
-
- # Create a list of dummy thumbnail files
- thumbnail_files = [
+ self.thumbnail_files = [
SimpleUploadedFile(
f"thumbnail{i}.jpg",
b"thumbnail_content",
@@ -30,31 +32,14 @@ class ResourceCreateFormTest(TestCase):
)
for i in range(6)
]
+ # 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},
+ )
- # Create a form instance with the dummy PDF files and thumbnail files
- form_data = {
- "name": "Test Resource",
- "description": "Test Description",
- "resource_type": self.resource_type.id,
- "age_range": "5-7",
- "curriculum": "English",
- "main_resource_category": self.resource_category.id,
- }
- form_files = {
- "pdf_files": pdf_files,
- "thumbnail_files": thumbnail_files,
- }
- form = ResourceCreateForm(data=form_data, files=form_files)
+ def test_form_valid(self):
+ # Create a list of dummy thumbnail files
- # Validate the form
+ form = ResourceCreateForm(data=self.form_data, files=self.form_files)
is_valid = form.is_valid()
-
- # Check if the validation errors are raised for pdf_files and thumbnail_files
- self.assertFalse(is_valid)
- self.assertIn("pdf_files", form.errors)
- self.assertEqual(form.errors["pdf_files"], ["Please select up to 10 PDF files."])
- self.assertIn("thumbnail_files", form.errors)
- self.assertEqual(
- form.errors["thumbnail_files"],
- ["Please select up to 5 thumbnail files."],
- )