parts/django/tests/regressiontests/bug639/tests.py
changeset 69 c6bca38c1cbf
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     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 shutil
       
     9 import unittest
       
    10 
       
    11 from django.core.files.uploadedfile import SimpleUploadedFile
       
    12 from regressiontests.bug639.models import Photo, PhotoForm, temp_storage_dir
       
    13 
       
    14 class Bug639Test(unittest.TestCase):
       
    15 
       
    16     def testBug639(self):
       
    17         """
       
    18         Simulate a file upload and check how many times Model.save() gets
       
    19         called.
       
    20         """
       
    21         # Grab an image for testing.
       
    22         filename = os.path.join(os.path.dirname(__file__), "test.jpg")
       
    23         img = open(filename, "rb").read()
       
    24 
       
    25         # Fake a POST QueryDict and FILES MultiValueDict.
       
    26         data = {'title': 'Testing'}
       
    27         files = {"image": SimpleUploadedFile('test.jpg', img, 'image/jpeg')}
       
    28 
       
    29         form = PhotoForm(data=data, files=files)
       
    30         p = form.save()
       
    31 
       
    32         # Check the savecount stored on the object (see the model).
       
    33         self.assertEqual(p._savecount, 1)
       
    34 
       
    35     def tearDown(self):
       
    36         """
       
    37         Make sure to delete the "uploaded" file to avoid clogging /tmp.
       
    38         """
       
    39         p = Photo.objects.get()
       
    40         p.image.delete(save=False)
       
    41         shutil.rmtree(temp_storage_dir)