thirdparty/google_appengine/lib/django/tests/regressiontests/bug639/tests.py
changeset 2866 a04b1e4126c4
parent 2864 2e0b0af889be
child 2868 9f7f269383f7
equal deleted inserted replaced
2864:2e0b0af889be 2866:a04b1e4126c4
     1 """
       
     2 Tests for file field behavior, and specifically #639, in which Model.save() gets
       
     3 called *again* for each FileField. This test will fail if calling an
       
     4 auto-manipulator's save() method causes Model.save() to be called more than once.
       
     5 """
       
     6 
       
     7 import os
       
     8 import unittest
       
     9 from regressiontests.bug639.models import Photo
       
    10 from django.http import QueryDict
       
    11 from django.utils.datastructures import MultiValueDict
       
    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 called.
       
    18         """
       
    19         # Grab an image for testing
       
    20         img = open(os.path.join(os.path.dirname(__file__), "test.jpg"), "rb").read()
       
    21         
       
    22         # Fake a request query dict with the file
       
    23         qd = QueryDict("title=Testing&image=", mutable=True)
       
    24         qd["image_file"] = {
       
    25             "filename" : "test.jpg",
       
    26             "content-type" : "image/jpeg",
       
    27             "content" : img
       
    28         }
       
    29         
       
    30         manip = Photo.AddManipulator()
       
    31         manip.do_html2python(qd)
       
    32         p = manip.save(qd)
       
    33         
       
    34         # Check the savecount stored on the object (see the model)
       
    35         self.assertEqual(p._savecount, 1)
       
    36         
       
    37     def tearDown(self):
       
    38         """
       
    39         Make sure to delete the "uploaded" file to avoid clogging /tmp.
       
    40         """
       
    41         p = Photo.objects.get()
       
    42         os.unlink(p.get_image_filename())