PageRenderTime 27ms CodeModel.GetById 23ms RepoModel.GetById 1ms 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
Possible License(s): BSD-3-Clause
  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. import os
  7. import shutil
  8. from django.core.files.uploadedfile import SimpleUploadedFile
  9. from django.utils import unittest
  10. from regressiontests.bug639.models import Photo, PhotoForm, temp_storage_dir
  11. class Bug639Test(unittest.TestCase):
  12. def testBug639(self):
  13. """
  14. Simulate a file upload and check how many times Model.save() gets
  15. called.
  16. """
  17. # Grab an image for testing.
  18. filename = os.path.join(os.path.dirname(__file__), "test.jpg")
  19. img = open(filename, "rb").read()
  20. # Fake a POST QueryDict and FILES MultiValueDict.
  21. data = {'title': 'Testing'}
  22. files = {"image": SimpleUploadedFile('test.jpg', img, 'image/jpeg')}
  23. form = PhotoForm(data=data, files=files)
  24. p = form.save()
  25. # Check the savecount stored on the object (see the model).
  26. self.assertEqual(p._savecount, 1)
  27. def tearDown(self):
  28. """
  29. Make sure to delete the "uploaded" file to avoid clogging /tmp.
  30. """
  31. p = Photo.objects.get()
  32. p.image.delete(save=False)
  33. shutil.rmtree(temp_storage_dir)