PageRenderTime 104ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/modeltests/files/tests.py

https://code.google.com/p/mango-py/
Python | 105 lines | 96 code | 6 blank | 3 comment | 1 complexity | 2e25fcf51aa2a205f52e03d6ca9f1293 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import shutil
  2. import sys
  3. from django.core.cache import cache
  4. from django.core.files.base import ContentFile
  5. from django.core.files.uploadedfile import SimpleUploadedFile
  6. from django.test import TestCase
  7. from models import Storage, temp_storage, temp_storage_location
  8. if sys.version_info >= (2, 5):
  9. from tests_25 import FileObjTests
  10. class FileTests(TestCase):
  11. def tearDown(self):
  12. shutil.rmtree(temp_storage_location)
  13. def test_files(self):
  14. # Attempting to access a FileField from the class raises a descriptive
  15. # error
  16. self.assertRaises(AttributeError, lambda: Storage.normal)
  17. # An object without a file has limited functionality.
  18. obj1 = Storage()
  19. self.assertEqual(obj1.normal.name, "")
  20. self.assertRaises(ValueError, lambda: obj1.normal.size)
  21. # Saving a file enables full functionality.
  22. obj1.normal.save("django_test.txt", ContentFile("content"))
  23. self.assertEqual(obj1.normal.name, "tests/django_test.txt")
  24. self.assertEqual(obj1.normal.size, 7)
  25. self.assertEqual(obj1.normal.read(), "content")
  26. obj1.normal.close()
  27. # File objects can be assigned to FileField attributes, but shouldn't
  28. # get committed until the model it's attached to is saved.
  29. obj1.normal = SimpleUploadedFile("assignment.txt", "content")
  30. dirs, files = temp_storage.listdir("tests")
  31. self.assertEqual(dirs, [])
  32. self.assertEqual(sorted(files), ["default.txt", "django_test.txt"])
  33. obj1.save()
  34. dirs, files = temp_storage.listdir("tests")
  35. self.assertEqual(
  36. sorted(files), ["assignment.txt", "default.txt", "django_test.txt"]
  37. )
  38. # Files can be read in a little at a time, if necessary.
  39. obj1.normal.open()
  40. self.assertEqual(obj1.normal.read(3), "con")
  41. self.assertEqual(obj1.normal.read(), "tent")
  42. self.assertEqual(list(obj1.normal.chunks(chunk_size=2)), ["co", "nt", "en", "t"])
  43. obj1.normal.close()
  44. # Save another file with the same name.
  45. obj2 = Storage()
  46. obj2.normal.save("django_test.txt", ContentFile("more content"))
  47. self.assertEqual(obj2.normal.name, "tests/django_test_1.txt")
  48. self.assertEqual(obj2.normal.size, 12)
  49. # Push the objects into the cache to make sure they pickle properly
  50. cache.set("obj1", obj1)
  51. cache.set("obj2", obj2)
  52. self.assertEqual(cache.get("obj2").normal.name, "tests/django_test_1.txt")
  53. # Deleting an object does not delete the file it uses.
  54. obj2.delete()
  55. obj2.normal.save("django_test.txt", ContentFile("more content"))
  56. self.assertEqual(obj2.normal.name, "tests/django_test_2.txt")
  57. # Multiple files with the same name get _N appended to them.
  58. objs = [Storage() for i in range(3)]
  59. for o in objs:
  60. o.normal.save("multiple_files.txt", ContentFile("Same Content"))
  61. self.assertEqual(
  62. [o.normal.name for o in objs],
  63. ["tests/multiple_files.txt", "tests/multiple_files_1.txt", "tests/multiple_files_2.txt"]
  64. )
  65. for o in objs:
  66. o.delete()
  67. # Default values allow an object to access a single file.
  68. obj3 = Storage.objects.create()
  69. self.assertEqual(obj3.default.name, "tests/default.txt")
  70. self.assertEqual(obj3.default.read(), "default content")
  71. obj3.default.close()
  72. # But it shouldn't be deleted, even if there are no more objects using
  73. # it.
  74. obj3.delete()
  75. obj3 = Storage()
  76. self.assertEqual(obj3.default.read(), "default content")
  77. obj3.default.close()
  78. # Verify the fix for #5655, making sure the directory is only
  79. # determined once.
  80. obj4 = Storage()
  81. obj4.random.save("random_file", ContentFile("random content"))
  82. self.assertTrue(obj4.random.name.endswith("/random_file"))
  83. # Clean up the temporary files and dir.
  84. obj1.normal.delete()
  85. obj2.normal.delete()
  86. obj3.default.delete()
  87. obj4.random.delete()