PageRenderTime 125ms CodeModel.GetById 43ms app.highlight 4ms RepoModel.GetById 37ms app.codeStats 0ms

/tests/regressiontests/bug639/tests.py

https://code.google.com/p/mango-py/
Python | 42 lines | 23 code | 0 blank | 19 comment | 1 complexity | ecb466c7a7752319657b8b4649690590 MD5 | raw file
 1"""
 2Tests for file field behavior, and specifically #639, in which Model.save()
 3gets called *again* for each FileField. This test will fail if calling a
 4ModelForm's save() method causes Model.save() to be called more than once.
 5"""
 6
 7import os
 8import shutil
 9
10from django.core.files.uploadedfile import SimpleUploadedFile
11from django.utils import unittest
12
13from regressiontests.bug639.models import Photo, PhotoForm, temp_storage_dir
14
15class Bug639Test(unittest.TestCase):
16
17    def testBug639(self):
18        """
19        Simulate a file upload and check how many times Model.save() gets
20        called.
21        """
22        # Grab an image for testing.
23        filename = os.path.join(os.path.dirname(__file__), "test.jpg")
24        img = open(filename, "rb").read()
25
26        # Fake a POST QueryDict and FILES MultiValueDict.
27        data = {'title': 'Testing'}
28        files = {"image": SimpleUploadedFile('test.jpg', img, 'image/jpeg')}
29
30        form = PhotoForm(data=data, files=files)
31        p = form.save()
32
33        # Check the savecount stored on the object (see the model).
34        self.assertEqual(p._savecount, 1)
35
36    def tearDown(self):
37        """
38        Make sure to delete the "uploaded" file to avoid clogging /tmp.
39        """
40        p = Photo.objects.get()
41        p.image.delete(save=False)
42        shutil.rmtree(temp_storage_dir)