~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/regressiontests/bug639/tests.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

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