PageRenderTime 58ms CodeModel.GetById 44ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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