|
| 1 | +import random |
| 2 | +import shutil |
| 3 | +import os |
| 4 | + |
| 5 | +from django.test import TestCase |
| 6 | +from django.core.files.uploadedfile import SimpleUploadedFile |
| 7 | +from django.core.files.base import ContentFile |
| 8 | +from django.core.cache import cache |
| 9 | + |
| 10 | +from models import Storage, temp_storage, temp_storage_location |
| 11 | + |
| 12 | +def make_obj(): |
| 13 | + obj = Storage() |
| 14 | + obj.normal.save('django_test.txt', ContentFile('content')) |
| 15 | + return obj |
| 16 | + |
| 17 | +class CustomFileStorageTestCase(TestCase): |
| 18 | + def setUp(self): |
| 19 | + #recreate our temp dir if necessary |
| 20 | + if not os.path.exists(temp_storage_location): |
| 21 | + os.mkdir(temp_storage_location) |
| 22 | + # Write out a file to be used as default content |
| 23 | + temp_storage.save('tests/default.txt', ContentFile('default content')) |
| 24 | + |
| 25 | + def tearDown(self): |
| 26 | + #remove the temp dir after each test |
| 27 | + shutil.rmtree(temp_storage_location) |
| 28 | + |
| 29 | + def test_access_from_class(self): |
| 30 | + # Attempting to access a FileField from the class raises a |
| 31 | + # descriptive error |
| 32 | + self.assertRaises(AttributeError, |
| 33 | + getattr, |
| 34 | + Storage, 'normal') |
| 35 | + |
| 36 | + def test_object_without_file(self): |
| 37 | + # An object without a file has limited functionality. |
| 38 | + obj = Storage() |
| 39 | + self.assertEqual(repr(obj.normal), '<FieldFile: None>') |
| 40 | + self.assertRaises(ValueError, |
| 41 | + getattr, |
| 42 | + obj.normal, 'size') |
| 43 | + |
| 44 | + def test_basic_saved_file(self): |
| 45 | + # Saving a file enables full functionality. |
| 46 | + obj = Storage() |
| 47 | + obj.normal.save('django_test.txt', ContentFile('content')) |
| 48 | + self.assertEqual(repr(obj.normal), '<FieldFile: tests/django_test.txt>') |
| 49 | + self.assertEqual(obj.normal.size, 7) |
| 50 | + self.assertEqual(obj.normal.read(), 'content') |
| 51 | + |
| 52 | + |
| 53 | + def test_attribute_assignment(self): |
| 54 | + # File objects can be assigned to FileField attributes, but |
| 55 | + # shouldn't get committed until the model it's attached to is |
| 56 | + # saved. |
| 57 | + obj = Storage() |
| 58 | + obj.normal = SimpleUploadedFile('assignment.txt', 'content') |
| 59 | + dirs, files = temp_storage.listdir('tests') |
| 60 | + self.assertEqual(len(dirs), 0) |
| 61 | + self.assertEqual(files, ['default.txt']) |
| 62 | + obj.save() |
| 63 | + dirs, files = temp_storage.listdir('tests') |
| 64 | + files.sort() |
| 65 | + self.assertEqual(files, ['assignment.txt', 'default.txt']) |
| 66 | + |
| 67 | + def test_file_read(self): |
| 68 | + # Files can be read in a little at a time, if necessary. |
| 69 | + obj = make_obj() |
| 70 | + obj.normal.open() |
| 71 | + self.assertEqual(obj.normal.read(3), 'con') |
| 72 | + self.assertEqual(obj.normal.read(), 'tent') |
| 73 | + self.assertEqual('-'.join(obj.normal.chunks(chunk_size=2)), |
| 74 | + 'co-nt-en-t') |
| 75 | + |
| 76 | + def test_file_duplicate_name(self): |
| 77 | + # Save another file with the same name. |
| 78 | + obj = make_obj() |
| 79 | + obj2 = Storage() |
| 80 | + obj2.normal.save('django_test.txt', ContentFile('more content')) |
| 81 | + self.assertEqual(repr(obj2.normal), |
| 82 | + "<FieldFile: tests/django_test_1.txt>") |
| 83 | + self.assertEqual(obj2.normal.size, 12) |
| 84 | + |
| 85 | + def test_object_pickling(self): |
| 86 | + # Push the objects into the cache to make sure they pickle properly |
| 87 | + obj = make_obj() |
| 88 | + cache.set('obj', obj) |
| 89 | + self.assertEqual(repr(cache.get('obj').normal), |
| 90 | + "<FieldFile: tests/django_test.txt>") |
| 91 | + |
| 92 | + def test_delete(self): |
| 93 | + # Deleting an object deletes the file it uses, if there are no |
| 94 | + # other objects still using that file. |
| 95 | + obj = make_obj() |
| 96 | + obj.delete() |
| 97 | + obj.normal.save('django_test.txt', ContentFile('more content')) |
| 98 | + self.assertEqual(repr(obj.normal), |
| 99 | + "<FieldFile: tests/django_test.txt>") |
| 100 | + |
| 101 | + def test_duplicate_file_name_differentiation(self): |
| 102 | + # Multiple files with the same name get _N appended to them. |
| 103 | + objs = [Storage() for i in range(3)] |
| 104 | + for o in objs: |
| 105 | + o.normal.save('multiple_files.txt', ContentFile('Same Content')) |
| 106 | + self.assertEqual(repr([o.normal for o in objs]), |
| 107 | + "[<FieldFile: tests/multiple_files.txt>, <FieldFile: tests/multiple_files_1.txt>, <FieldFile: tests/multiple_files_2.txt>]") |
| 108 | + |
| 109 | + def test_default_values(self): |
| 110 | + # Default values allow an object to access a single file. |
| 111 | + obj = Storage.objects.create() |
| 112 | + self.assertEqual(repr(obj.default), "<FieldFile: tests/default.txt>") |
| 113 | + self.assertEqual(obj.default.read(), 'default content') |
| 114 | + |
| 115 | + # But it shouldn't be deleted, even if there are no more |
| 116 | + # objects using it. |
| 117 | + obj.delete() |
| 118 | + obj = Storage() |
| 119 | + self.assertEqual(obj.default.read(), 'default content') |
| 120 | + |
| 121 | + def test_directory_determined_once(self): |
| 122 | + # Verify the fix for #5655, making sure the directory is only |
| 123 | + # determined once. |
| 124 | + obj = Storage() |
| 125 | + obj.random.save('random_file', ContentFile('random content')) |
| 126 | + self.assertEqual(obj.random.read(), 'random content') |
0 commit comments