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