2007-02-27 01:17:11 +08:00
|
|
|
"""
|
2008-08-11 23:24:46 +08:00
|
|
|
Tests for file field behavior, and specifically #639, in which Model.save()
|
|
|
|
gets called *again* for each FileField. This test will fail if calling a
|
|
|
|
ModelForm's save() method causes Model.save() to be called more than once.
|
2007-02-27 01:17:11 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
2009-04-06 04:59:20 +08:00
|
|
|
import shutil
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2008-08-11 23:24:46 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
2012-12-08 18:13:52 +08:00
|
|
|
from django.utils._os import upath
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2011-10-14 02:51:33 +08:00
|
|
|
from .models import Photo, PhotoForm, temp_storage_dir
|
|
|
|
|
2007-02-27 01:17:11 +08:00
|
|
|
|
|
|
|
class Bug639Test(unittest.TestCase):
|
2008-08-11 23:24:46 +08:00
|
|
|
|
2007-02-27 01:17:11 +08:00
|
|
|
def testBug639(self):
|
|
|
|
"""
|
2008-08-11 23:24:46 +08:00
|
|
|
Simulate a file upload and check how many times Model.save() gets
|
|
|
|
called.
|
2007-02-27 01:17:11 +08:00
|
|
|
"""
|
2008-08-11 23:24:46 +08:00
|
|
|
# Grab an image for testing.
|
2012-12-08 18:13:52 +08:00
|
|
|
filename = os.path.join(os.path.dirname(upath(__file__)), "test.jpg")
|
2012-05-05 20:01:38 +08:00
|
|
|
with open(filename, "rb") as fp:
|
|
|
|
img = fp.read()
|
2008-08-11 23:24:46 +08:00
|
|
|
|
|
|
|
# Fake a POST QueryDict and FILES MultiValueDict.
|
|
|
|
data = {'title': 'Testing'}
|
|
|
|
files = {"image": SimpleUploadedFile('test.jpg', img, 'image/jpeg')}
|
|
|
|
|
|
|
|
form = PhotoForm(data=data, files=files)
|
|
|
|
p = form.save()
|
|
|
|
|
|
|
|
# Check the savecount stored on the object (see the model).
|
2007-03-01 08:02:04 +08:00
|
|
|
self.assertEqual(p._savecount, 1)
|
2008-08-11 23:24:46 +08:00
|
|
|
|
2007-03-01 08:02:04 +08:00
|
|
|
def tearDown(self):
|
|
|
|
"""
|
|
|
|
Make sure to delete the "uploaded" file to avoid clogging /tmp.
|
|
|
|
"""
|
|
|
|
p = Photo.objects.get()
|
2008-08-09 04:59:02 +08:00
|
|
|
p.image.delete(save=False)
|
2009-04-06 04:59:20 +08:00
|
|
|
shutil.rmtree(temp_storage_dir)
|