|
Revision 8304, 1.2 kB
(checked in by gwilson, 2 months ago)
|
Refs #7742 -- Got bug639 regression tests using newforms instead of oldforms.
|
- Property svn:eol-style set to
native
|
| Line | |
|---|
| 1 |
""" |
|---|
| 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. |
|---|
| 5 |
""" |
|---|
| 6 |
|
|---|
| 7 |
import os |
|---|
| 8 |
import unittest |
|---|
| 9 |
|
|---|
| 10 |
from django.core.files.uploadedfile import SimpleUploadedFile |
|---|
| 11 |
from regressiontests.bug639.models import Photo, PhotoForm |
|---|
| 12 |
|
|---|
| 13 |
class Bug639Test(unittest.TestCase): |
|---|
| 14 |
|
|---|
| 15 |
def testBug639(self): |
|---|
| 16 |
""" |
|---|
| 17 |
Simulate a file upload and check how many times Model.save() gets |
|---|
| 18 |
called. |
|---|
| 19 |
""" |
|---|
| 20 |
# Grab an image for testing. |
|---|
| 21 |
filename = os.path.join(os.path.dirname(__file__), "test.jpg") |
|---|
| 22 |
img = open(filename, "rb").read() |
|---|
| 23 |
|
|---|
| 24 |
# Fake a POST QueryDict and FILES MultiValueDict. |
|---|
| 25 |
data = {'title': 'Testing'} |
|---|
| 26 |
files = {"image": SimpleUploadedFile('test.jpg', img, 'image/jpeg')} |
|---|
| 27 |
|
|---|
| 28 |
form = PhotoForm(data=data, files=files) |
|---|
| 29 |
p = form.save() |
|---|
| 30 |
|
|---|
| 31 |
# Check the savecount stored on the object (see the model). |
|---|
| 32 |
self.assertEqual(p._savecount, 1) |
|---|
| 33 |
|
|---|
| 34 |
def tearDown(self): |
|---|
| 35 |
""" |
|---|
| 36 |
Make sure to delete the "uploaded" file to avoid clogging /tmp. |
|---|
| 37 |
""" |
|---|
| 38 |
p = Photo.objects.get() |
|---|
| 39 |
p.image.delete(save=False) |
|---|