mirror of https://github.com/django/django.git
Removed unnecessary bytes prefix for ContentFile
This commit is contained in:
parent
361d6738f8
commit
b5240d25c1
|
@ -20,7 +20,7 @@ class FileStorageTests(TestCase):
|
||||||
shutil.rmtree(temp_storage_location)
|
shutil.rmtree(temp_storage_location)
|
||||||
|
|
||||||
def test_files(self):
|
def test_files(self):
|
||||||
temp_storage.save('tests/default.txt', ContentFile(b'default content'))
|
temp_storage.save('tests/default.txt', ContentFile('default content'))
|
||||||
# Attempting to access a FileField from the class raises a descriptive
|
# Attempting to access a FileField from the class raises a descriptive
|
||||||
# error
|
# error
|
||||||
self.assertRaises(AttributeError, lambda: Storage.normal)
|
self.assertRaises(AttributeError, lambda: Storage.normal)
|
||||||
|
@ -31,7 +31,7 @@ class FileStorageTests(TestCase):
|
||||||
self.assertRaises(ValueError, lambda: obj1.normal.size)
|
self.assertRaises(ValueError, lambda: obj1.normal.size)
|
||||||
|
|
||||||
# Saving a file enables full functionality.
|
# Saving a file enables full functionality.
|
||||||
obj1.normal.save("django_test.txt", ContentFile(b"content"))
|
obj1.normal.save("django_test.txt", ContentFile("content"))
|
||||||
self.assertEqual(obj1.normal.name, "tests/django_test.txt")
|
self.assertEqual(obj1.normal.name, "tests/django_test.txt")
|
||||||
self.assertEqual(obj1.normal.size, 7)
|
self.assertEqual(obj1.normal.size, 7)
|
||||||
self.assertEqual(obj1.normal.read(), b"content")
|
self.assertEqual(obj1.normal.read(), b"content")
|
||||||
|
@ -59,7 +59,7 @@ class FileStorageTests(TestCase):
|
||||||
|
|
||||||
# Save another file with the same name.
|
# Save another file with the same name.
|
||||||
obj2 = Storage()
|
obj2 = Storage()
|
||||||
obj2.normal.save("django_test.txt", ContentFile(b"more content"))
|
obj2.normal.save("django_test.txt", ContentFile("more content"))
|
||||||
self.assertEqual(obj2.normal.name, "tests/django_test_1.txt")
|
self.assertEqual(obj2.normal.name, "tests/django_test_1.txt")
|
||||||
self.assertEqual(obj2.normal.size, 12)
|
self.assertEqual(obj2.normal.size, 12)
|
||||||
|
|
||||||
|
@ -70,13 +70,13 @@ class FileStorageTests(TestCase):
|
||||||
|
|
||||||
# Deleting an object does not delete the file it uses.
|
# Deleting an object does not delete the file it uses.
|
||||||
obj2.delete()
|
obj2.delete()
|
||||||
obj2.normal.save("django_test.txt", ContentFile(b"more content"))
|
obj2.normal.save("django_test.txt", ContentFile("more content"))
|
||||||
self.assertEqual(obj2.normal.name, "tests/django_test_2.txt")
|
self.assertEqual(obj2.normal.name, "tests/django_test_2.txt")
|
||||||
|
|
||||||
# Multiple files with the same name get _N appended to them.
|
# Multiple files with the same name get _N appended to them.
|
||||||
objs = [Storage() for i in range(3)]
|
objs = [Storage() for i in range(3)]
|
||||||
for o in objs:
|
for o in objs:
|
||||||
o.normal.save("multiple_files.txt", ContentFile(b"Same Content"))
|
o.normal.save("multiple_files.txt", ContentFile("Same Content"))
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
[o.normal.name for o in objs],
|
[o.normal.name for o in objs],
|
||||||
["tests/multiple_files.txt", "tests/multiple_files_1.txt", "tests/multiple_files_2.txt"]
|
["tests/multiple_files.txt", "tests/multiple_files_1.txt", "tests/multiple_files_2.txt"]
|
||||||
|
@ -100,7 +100,7 @@ class FileStorageTests(TestCase):
|
||||||
# Verify the fix for #5655, making sure the directory is only
|
# Verify the fix for #5655, making sure the directory is only
|
||||||
# determined once.
|
# determined once.
|
||||||
obj4 = Storage()
|
obj4 = Storage()
|
||||||
obj4.random.save("random_file", ContentFile(b"random content"))
|
obj4.random.save("random_file", ContentFile("random content"))
|
||||||
self.assertTrue(obj4.random.name.endswith("/random_file"))
|
self.assertTrue(obj4.random.name.endswith("/random_file"))
|
||||||
|
|
||||||
def test_max_length(self):
|
def test_max_length(self):
|
||||||
|
|
|
@ -128,7 +128,7 @@ class FileStorageTests(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
self.assertFalse(self.storage.exists('test.file'))
|
self.assertFalse(self.storage.exists('test.file'))
|
||||||
|
|
||||||
f = ContentFile(b'custom contents')
|
f = ContentFile('custom contents')
|
||||||
f_name = self.storage.save('test.file', f)
|
f_name = self.storage.save('test.file', f)
|
||||||
atime = self.storage.accessed_time(f_name)
|
atime = self.storage.accessed_time(f_name)
|
||||||
|
|
||||||
|
@ -144,7 +144,7 @@ class FileStorageTests(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
self.assertFalse(self.storage.exists('test.file'))
|
self.assertFalse(self.storage.exists('test.file'))
|
||||||
|
|
||||||
f = ContentFile(b'custom contents')
|
f = ContentFile('custom contents')
|
||||||
f_name = self.storage.save('test.file', f)
|
f_name = self.storage.save('test.file', f)
|
||||||
ctime = self.storage.created_time(f_name)
|
ctime = self.storage.created_time(f_name)
|
||||||
|
|
||||||
|
@ -161,7 +161,7 @@ class FileStorageTests(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
self.assertFalse(self.storage.exists('test.file'))
|
self.assertFalse(self.storage.exists('test.file'))
|
||||||
|
|
||||||
f = ContentFile(b'custom contents')
|
f = ContentFile('custom contents')
|
||||||
f_name = self.storage.save('test.file', f)
|
f_name = self.storage.save('test.file', f)
|
||||||
mtime = self.storage.modified_time(f_name)
|
mtime = self.storage.modified_time(f_name)
|
||||||
|
|
||||||
|
@ -178,7 +178,7 @@ class FileStorageTests(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
self.assertFalse(self.storage.exists('test.file'))
|
self.assertFalse(self.storage.exists('test.file'))
|
||||||
|
|
||||||
f = ContentFile(b'custom contents')
|
f = ContentFile('custom contents')
|
||||||
f.name = 'test.file'
|
f.name = 'test.file'
|
||||||
|
|
||||||
storage_f_name = self.storage.save(None, f)
|
storage_f_name = self.storage.save(None, f)
|
||||||
|
@ -195,7 +195,7 @@ class FileStorageTests(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
self.assertFalse(self.storage.exists('path/to'))
|
self.assertFalse(self.storage.exists('path/to'))
|
||||||
self.storage.save('path/to/test.file',
|
self.storage.save('path/to/test.file',
|
||||||
ContentFile(b'file saved with path'))
|
ContentFile('file saved with path'))
|
||||||
|
|
||||||
self.assertTrue(self.storage.exists('path/to'))
|
self.assertTrue(self.storage.exists('path/to'))
|
||||||
with self.storage.open('path/to/test.file') as f:
|
with self.storage.open('path/to/test.file') as f:
|
||||||
|
@ -212,7 +212,7 @@ class FileStorageTests(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
self.assertFalse(self.storage.exists('test.file'))
|
self.assertFalse(self.storage.exists('test.file'))
|
||||||
|
|
||||||
f = ContentFile(b'custom contents')
|
f = ContentFile('custom contents')
|
||||||
f_name = self.storage.save('test.file', f)
|
f_name = self.storage.save('test.file', f)
|
||||||
|
|
||||||
self.assertEqual(self.storage.path(f_name),
|
self.assertEqual(self.storage.path(f_name),
|
||||||
|
@ -247,8 +247,8 @@ class FileStorageTests(unittest.TestCase):
|
||||||
self.assertFalse(self.storage.exists('storage_test_2'))
|
self.assertFalse(self.storage.exists('storage_test_2'))
|
||||||
self.assertFalse(self.storage.exists('storage_dir_1'))
|
self.assertFalse(self.storage.exists('storage_dir_1'))
|
||||||
|
|
||||||
f = self.storage.save('storage_test_1', ContentFile(b'custom content'))
|
f = self.storage.save('storage_test_1', ContentFile('custom content'))
|
||||||
f = self.storage.save('storage_test_2', ContentFile(b'custom content'))
|
f = self.storage.save('storage_test_2', ContentFile('custom content'))
|
||||||
os.mkdir(os.path.join(self.temp_dir, 'storage_dir_1'))
|
os.mkdir(os.path.join(self.temp_dir, 'storage_dir_1'))
|
||||||
|
|
||||||
dirs, files = self.storage.listdir('')
|
dirs, files = self.storage.listdir('')
|
||||||
|
@ -305,18 +305,18 @@ class FileStorageTests(unittest.TestCase):
|
||||||
os.makedirs = fake_makedirs
|
os.makedirs = fake_makedirs
|
||||||
|
|
||||||
self.storage.save('normal/test.file',
|
self.storage.save('normal/test.file',
|
||||||
ContentFile(b'saved normally'))
|
ContentFile('saved normally'))
|
||||||
with self.storage.open('normal/test.file') as f:
|
with self.storage.open('normal/test.file') as f:
|
||||||
self.assertEqual(f.read(), b'saved normally')
|
self.assertEqual(f.read(), b'saved normally')
|
||||||
|
|
||||||
self.storage.save('raced/test.file',
|
self.storage.save('raced/test.file',
|
||||||
ContentFile(b'saved with race'))
|
ContentFile('saved with race'))
|
||||||
with self.storage.open('raced/test.file') as f:
|
with self.storage.open('raced/test.file') as f:
|
||||||
self.assertEqual(f.read(), b'saved with race')
|
self.assertEqual(f.read(), b'saved with race')
|
||||||
|
|
||||||
# Check that OSErrors aside from EEXIST are still raised.
|
# Check that OSErrors aside from EEXIST are still raised.
|
||||||
self.assertRaises(OSError,
|
self.assertRaises(OSError,
|
||||||
self.storage.save, 'error/test.file', ContentFile(b'not saved'))
|
self.storage.save, 'error/test.file', ContentFile('not saved'))
|
||||||
finally:
|
finally:
|
||||||
os.makedirs = real_makedirs
|
os.makedirs = real_makedirs
|
||||||
|
|
||||||
|
@ -342,16 +342,16 @@ class FileStorageTests(unittest.TestCase):
|
||||||
try:
|
try:
|
||||||
os.remove = fake_remove
|
os.remove = fake_remove
|
||||||
|
|
||||||
self.storage.save('normal.file', ContentFile(b'delete normally'))
|
self.storage.save('normal.file', ContentFile('delete normally'))
|
||||||
self.storage.delete('normal.file')
|
self.storage.delete('normal.file')
|
||||||
self.assertFalse(self.storage.exists('normal.file'))
|
self.assertFalse(self.storage.exists('normal.file'))
|
||||||
|
|
||||||
self.storage.save('raced.file', ContentFile(b'delete with race'))
|
self.storage.save('raced.file', ContentFile('delete with race'))
|
||||||
self.storage.delete('raced.file')
|
self.storage.delete('raced.file')
|
||||||
self.assertFalse(self.storage.exists('normal.file'))
|
self.assertFalse(self.storage.exists('normal.file'))
|
||||||
|
|
||||||
# Check that OSErrors aside from ENOENT are still raised.
|
# Check that OSErrors aside from ENOENT are still raised.
|
||||||
self.storage.save('error.file', ContentFile(b'delete with error'))
|
self.storage.save('error.file', ContentFile('delete with error'))
|
||||||
self.assertRaises(OSError, self.storage.delete, 'error.file')
|
self.assertRaises(OSError, self.storage.delete, 'error.file')
|
||||||
finally:
|
finally:
|
||||||
os.remove = real_remove
|
os.remove = real_remove
|
||||||
|
@ -375,9 +375,9 @@ class CustomStorageTests(FileStorageTests):
|
||||||
storage_class = CustomStorage
|
storage_class = CustomStorage
|
||||||
|
|
||||||
def test_custom_get_available_name(self):
|
def test_custom_get_available_name(self):
|
||||||
first = self.storage.save('custom_storage', ContentFile(b'custom contents'))
|
first = self.storage.save('custom_storage', ContentFile('custom contents'))
|
||||||
self.assertEqual(first, 'custom_storage')
|
self.assertEqual(first, 'custom_storage')
|
||||||
second = self.storage.save('custom_storage', ContentFile(b'more contents'))
|
second = self.storage.save('custom_storage', ContentFile('more contents'))
|
||||||
self.assertEqual(second, 'custom_storage.2')
|
self.assertEqual(second, 'custom_storage.2')
|
||||||
self.storage.delete(first)
|
self.storage.delete(first)
|
||||||
self.storage.delete(second)
|
self.storage.delete(second)
|
||||||
|
@ -434,7 +434,7 @@ class FileStoragePermissions(unittest.TestCase):
|
||||||
shutil.rmtree(self.storage_dir)
|
shutil.rmtree(self.storage_dir)
|
||||||
|
|
||||||
def test_file_upload_permissions(self):
|
def test_file_upload_permissions(self):
|
||||||
name = self.storage.save("the_file", ContentFile(b"data"))
|
name = self.storage.save("the_file", ContentFile("data"))
|
||||||
actual_mode = os.stat(self.storage.path(name))[0] & 0o777
|
actual_mode = os.stat(self.storage.path(name))[0] & 0o777
|
||||||
self.assertEqual(actual_mode, 0o666)
|
self.assertEqual(actual_mode, 0o666)
|
||||||
|
|
||||||
|
@ -454,8 +454,8 @@ class FileStoragePathParsing(unittest.TestCase):
|
||||||
sure we still mangle the file name instead of the directory name.
|
sure we still mangle the file name instead of the directory name.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.storage.save('dotted.path/test', ContentFile(b"1"))
|
self.storage.save('dotted.path/test', ContentFile("1"))
|
||||||
self.storage.save('dotted.path/test', ContentFile(b"2"))
|
self.storage.save('dotted.path/test', ContentFile("2"))
|
||||||
|
|
||||||
self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
|
self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
|
||||||
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test')))
|
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test')))
|
||||||
|
@ -466,8 +466,8 @@ class FileStoragePathParsing(unittest.TestCase):
|
||||||
File names with a dot as their first character don't have an extension,
|
File names with a dot as their first character don't have an extension,
|
||||||
and the underscore should get added to the end.
|
and the underscore should get added to the end.
|
||||||
"""
|
"""
|
||||||
self.storage.save('dotted.path/.test', ContentFile(b"1"))
|
self.storage.save('dotted.path/.test', ContentFile("1"))
|
||||||
self.storage.save('dotted.path/.test', ContentFile(b"2"))
|
self.storage.save('dotted.path/.test', ContentFile("2"))
|
||||||
|
|
||||||
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test')))
|
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test')))
|
||||||
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test_1')))
|
self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/.test_1')))
|
||||||
|
|
Loading…
Reference in New Issue