I'm writing unit tests using Django 1.7 with Python 3.4. The form below validates fine when the file_data
element is commented out. With the file_data
included it doesn't validate and the test fails.
from django.core.files.uploadedfile import SimpleUploadedFile
...
data = {
'phone_number': '123456',
'job': Job.objects.latest('id').pk,
}
file_data = {
'portrait': SimpleUploadedFile(
content=b'',
content_type='image/jpeg',
name='test.jpg',
)
}
form = PersonForm(data, file_data)
self.assertTrue(form.is_valid())
Similar code works fine for FileField
upload tests elsewhere in my site. Running the code in a shell I get the following in form.errors
: 'Upload a valid image. The file you uploaded was either not an image or a corrupted image.'
I therefore think the problem lies either in the content
or content_type
fields. I've tried using an image-as-a-string as in this answer to no avail. [Edit: the image-as-a-string approach was in fact the answer but I must've implemented it badly. The accepted answer is verified as working.] I couldn't find any clues in the SimpleUloadedFile source code.
The form works fine in reality but I want to make sure it's covered by a working test for future maintenance. Ideally I'd like to avoid having to have an actual test.jpg
file existing because images are in my .gitignore
file and I don't want to have to start hacking what is currently a very smooth automated deployment.
What input should I give SimpleUploadedFile
so that it validates correctly?
Is there any difference of running
Image
lib between your test suit and your working code? Can theImage
lib process files such as GIF properly? You may need to check thePIL/pillow
installation as well.works for me.
The form internally relies on
forms.ImageField
to test image in itsto_python()
, in the way like:Any exception from the
verify()
line would result in the error'Upload a valid image...'
. You could check the image bytes by assigning it toimage_content
:works for me either.
Also, you could generate image bytes online: