I’m having difficulty successfully creating a mock image for my TestCase. I’ve referenced these topics in guiding my efforts to do so, but still failing creating it. The error exactly being raised is:
- Upload a valid image. The file you uploaded was either not an image or a corrupted image.
An alternative I tried was using io.BytesIO.getvalue()
on the BytesIO object but got the same error when the content parameter was changed to content=f.getvalue()
I’m hitting a wall as to what else I can do to fix this.
from io import BytesIO
from django.test import TestCase
from django.contrib.auth.models import User
from ..forms import PhotoForm
from PIL import Image
from ..models import Photo
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile
@classmethod
def setUpTestData(cls):
f = BytesIO()
image = Image.new("RGB", (100, 100))
image.save(f, 'png')
f.seek(0)
test_image = SimpleUploadedFile(
"test_image.png",
content=open(f.read(), 'rb'),
)
user = User.objects.create_user("User")
import pdb; pdb.set_trace()
form = PhotoForm({'title': "Image Title"}, {'source': test_image})
instance = form.save(commit=False)
instance.photographer = user
instance.save()
cls.submitted_form = PhotoForm(
{'title': "Image Title"}, {'source': test_image}
)