django version: 1.11, python version: 3.6.3
I found this stackoverflow question:
Unit Testing a Django Form with a FileField
and I like how there isn't an actual image/external file used for the unittest; however I tried these approaches:
from django.test import TestCase
from io import BytesIO
from PIL import Image
from my_app.forms import MyForm
from django.core.files.uploadedfile import InMemoryUploadedFile
class MyModelTest(TestCase):
def test_valid_form_data(self):
im_io = BytesIO() # BytesIO has to be used, StrinIO isn't working
im = Image.new(mode='RGB', size=(200, 200))
im.save(im_io, 'JPEG')
form_data = {
'some_field': 'some_data'
}
image_data = {
InMemoryUploadedFile(im_io, None, 'random.jpg', 'image/jpeg', len(im_io.getvalue()), None)
}
form = MyForm(data=form_data, files=image_data)
self.assertTrue(form.is_valid())
however, this always results in the following error message:
Traceback (most recent call last):
File "/home/my_user/projects/my_app/products/tests/test_forms.py", line 44, in test_valid_form_data
self.assertTrue(form.is_valid())
File "/home/my_user/.virtualenvs/forum/lib/python3.6/site-packages/django/forms/forms.py", line 183, in is_valid
return self.is_bound and not self.errors
File "/home/my_user/.virtualenvs/forum/lib/python3.6/site-packages/django/forms/forms.py", line 175, in errors
self.full_clean()
File "/home/my_user/.virtualenvs/forum/lib/python3.6/site-packages/django/forms/forms.py", line 384, in full_clean
self._clean_fields()
File "/home/my_user/.virtualenvs/forum/lib/python3.6/site-packages/django/forms/forms.py", line 396, in _clean_fields
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
File "/home/my_user/.virtualenvs/forum/lib/python3.6/site-packages/django/forms/widgets.py", line 423, in value_from_datadict
upload = super(ClearableFileInput, self).value_from_datadict(data, files, name)
File "/home/my_user/.virtualenvs/forum/lib/python3.6/site-packages/django/forms/widgets.py", line 367, in value_from_datadict
return files.get(name)
AttributeError: 'set' object has no attribute 'get'
Why? I understand that .get()
is a dictionary method, but I fail to see where it created a set.