2
Tests for file field behavior, and specifically #639, in which Model.save()
3
gets called *again* for each FileField. This test will fail if calling a
4
ModelForm's save() method causes Model.save() to be called more than once.
7
from __future__ import absolute_import
12
from django.core.files.uploadedfile import SimpleUploadedFile
13
from django.utils import unittest
14
from django.utils._os import upath
16
from .models import Photo, PhotoForm, temp_storage_dir
19
class Bug639Test(unittest.TestCase):
23
Simulate a file upload and check how many times Model.save() gets
26
# Grab an image for testing.
27
filename = os.path.join(os.path.dirname(upath(__file__)), "test.jpg")
28
with open(filename, "rb") as fp:
31
# Fake a POST QueryDict and FILES MultiValueDict.
32
data = {'title': 'Testing'}
33
files = {"image": SimpleUploadedFile('test.jpg', img, 'image/jpeg')}
35
form = PhotoForm(data=data, files=files)
38
# Check the savecount stored on the object (see the model).
39
self.assertEqual(p._savecount, 1)
43
Make sure to delete the "uploaded" file to avoid clogging /tmp.
45
p = Photo.objects.get()
46
p.image.delete(save=False)
47
shutil.rmtree(temp_storage_dir)