2007-02-26 17:17:11 +00:00
|
|
|
"""
|
2008-08-11 15:24:46 +00: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-26 17:17:11 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
2009-04-05 20:59:20 +00:00
|
|
|
import shutil
|
2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2008-08-11 15:24:46 +00:00
|
|
|
|
2008-07-01 15:10:51 +00:00
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
2012-12-08 10:13:52 +00:00
|
|
|
from django.utils._os import upath
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2011-10-13 18:51:33 +00:00
|
|
|
from .models import Photo, PhotoForm, temp_storage_dir
|
|
|
|
|
2007-02-26 17:17:11 +00:00
|
|
|
|
|
|
|
class Bug639Test(unittest.TestCase):
|
2008-08-11 15:24:46 +00:00
|
|
|
|
2014-07-07 23:08:42 +00:00
|
|
|
def test_bug_639(self):
|
2007-02-26 17:17:11 +00:00
|
|
|
"""
|
2008-08-11 15:24:46 +00:00
|
|
|
Simulate a file upload and check how many times Model.save() gets
|
|
|
|
called.
|
2007-02-26 17:17:11 +00:00
|
|
|
"""
|
2008-08-11 15:24:46 +00:00
|
|
|
# Grab an image for testing.
|
2012-12-08 10:13:52 +00:00
|
|
|
filename = os.path.join(os.path.dirname(upath(__file__)), "test.jpg")
|
2012-05-05 12:01:38 +00:00
|
|
|
with open(filename, "rb") as fp:
|
|
|
|
img = fp.read()
|
2008-08-11 15:24:46 +00: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 00:02:04 +00:00
|
|
|
self.assertEqual(p._savecount, 1)
|
2008-08-11 15:24:46 +00:00
|
|
|
|
2007-03-01 00:02:04 +00:00
|
|
|
def tearDown(self):
|
|
|
|
"""
|
|
|
|
Make sure to delete the "uploaded" file to avoid clogging /tmp.
|
|
|
|
"""
|
|
|
|
p = Photo.objects.get()
|
2008-08-08 20:59:02 +00:00
|
|
|
p.image.delete(save=False)
|
2009-04-05 20:59:20 +00:00
|
|
|
shutil.rmtree(temp_storage_dir)
|