2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-03-28 13:58:43 +08:00
|
|
|
|
2013-06-20 18:55:27 +08:00
|
|
|
import os
|
2012-05-31 15:59:58 +08:00
|
|
|
import gzip
|
2010-09-13 04:03:55 +08:00
|
|
|
import shutil
|
2011-03-28 13:58:43 +08:00
|
|
|
import tempfile
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2010-09-13 04:03:55 +08:00
|
|
|
|
|
|
|
from django.core.cache import cache
|
2011-03-28 13:58:43 +08:00
|
|
|
from django.core.files import File
|
2013-06-20 18:55:27 +08:00
|
|
|
from django.core.files.move import file_move_safe
|
2010-09-13 04:03:55 +08:00
|
|
|
from django.core.files.base import ContentFile
|
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
2013-09-07 02:23:50 +08:00
|
|
|
from django.core.files.temp import NamedTemporaryFile
|
2010-09-13 04:03:55 +08:00
|
|
|
from django.test import TestCase
|
2013-02-24 01:47:36 +08:00
|
|
|
from django.utils.six import StringIO
|
2010-09-13 04:03:55 +08:00
|
|
|
|
2011-10-14 02:04:12 +08:00
|
|
|
from .models import Storage, temp_storage, temp_storage_location
|
2010-09-13 04:03:55 +08:00
|
|
|
|
|
|
|
|
2012-05-31 15:59:58 +08:00
|
|
|
class FileStorageTests(TestCase):
|
2010-09-13 04:03:55 +08:00
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(temp_storage_location)
|
|
|
|
|
|
|
|
def test_files(self):
|
2012-08-29 16:00:29 +08:00
|
|
|
temp_storage.save('tests/default.txt', ContentFile('default content'))
|
2010-09-13 04:03:55 +08:00
|
|
|
# Attempting to access a FileField from the class raises a descriptive
|
|
|
|
# error
|
|
|
|
self.assertRaises(AttributeError, lambda: Storage.normal)
|
|
|
|
|
|
|
|
# An object without a file has limited functionality.
|
|
|
|
obj1 = Storage()
|
|
|
|
self.assertEqual(obj1.normal.name, "")
|
|
|
|
self.assertRaises(ValueError, lambda: obj1.normal.size)
|
|
|
|
|
|
|
|
# Saving a file enables full functionality.
|
2012-08-29 16:00:29 +08:00
|
|
|
obj1.normal.save("django_test.txt", ContentFile("content"))
|
2010-09-13 04:03:55 +08:00
|
|
|
self.assertEqual(obj1.normal.name, "tests/django_test.txt")
|
|
|
|
self.assertEqual(obj1.normal.size, 7)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(obj1.normal.read(), b"content")
|
2011-02-21 21:45:29 +08:00
|
|
|
obj1.normal.close()
|
2010-09-13 04:03:55 +08:00
|
|
|
|
|
|
|
# File objects can be assigned to FileField attributes, but shouldn't
|
|
|
|
# get committed until the model it's attached to is saved.
|
2012-06-08 00:08:47 +08:00
|
|
|
obj1.normal = SimpleUploadedFile("assignment.txt", b"content")
|
2010-09-13 04:03:55 +08:00
|
|
|
dirs, files = temp_storage.listdir("tests")
|
|
|
|
self.assertEqual(dirs, [])
|
|
|
|
self.assertEqual(sorted(files), ["default.txt", "django_test.txt"])
|
|
|
|
|
|
|
|
obj1.save()
|
|
|
|
dirs, files = temp_storage.listdir("tests")
|
|
|
|
self.assertEqual(
|
|
|
|
sorted(files), ["assignment.txt", "default.txt", "django_test.txt"]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Files can be read in a little at a time, if necessary.
|
|
|
|
obj1.normal.open()
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(obj1.normal.read(3), b"con")
|
|
|
|
self.assertEqual(obj1.normal.read(), b"tent")
|
|
|
|
self.assertEqual(list(obj1.normal.chunks(chunk_size=2)), [b"co", b"nt", b"en", b"t"])
|
2011-02-21 21:45:29 +08:00
|
|
|
obj1.normal.close()
|
2010-09-13 04:03:55 +08:00
|
|
|
|
|
|
|
# Save another file with the same name.
|
|
|
|
obj2 = Storage()
|
2012-08-29 16:00:29 +08:00
|
|
|
obj2.normal.save("django_test.txt", ContentFile("more content"))
|
2010-09-13 04:03:55 +08:00
|
|
|
self.assertEqual(obj2.normal.name, "tests/django_test_1.txt")
|
|
|
|
self.assertEqual(obj2.normal.size, 12)
|
2013-10-15 02:26:54 +08:00
|
|
|
obj2.normal.close()
|
2010-09-13 04:03:55 +08:00
|
|
|
|
|
|
|
# Push the objects into the cache to make sure they pickle properly
|
|
|
|
cache.set("obj1", obj1)
|
|
|
|
cache.set("obj2", obj2)
|
|
|
|
self.assertEqual(cache.get("obj2").normal.name, "tests/django_test_1.txt")
|
|
|
|
|
2011-01-26 15:46:19 +08:00
|
|
|
# Deleting an object does not delete the file it uses.
|
2010-09-13 04:03:55 +08:00
|
|
|
obj2.delete()
|
2012-08-29 16:00:29 +08:00
|
|
|
obj2.normal.save("django_test.txt", ContentFile("more content"))
|
2011-01-26 15:46:19 +08:00
|
|
|
self.assertEqual(obj2.normal.name, "tests/django_test_2.txt")
|
2013-10-15 02:26:54 +08:00
|
|
|
obj2.normal.close()
|
2010-09-13 04:03:55 +08:00
|
|
|
|
|
|
|
# Multiple files with the same name get _N appended to them.
|
|
|
|
objs = [Storage() for i in range(3)]
|
|
|
|
for o in objs:
|
2012-08-29 16:00:29 +08:00
|
|
|
o.normal.save("multiple_files.txt", ContentFile("Same Content"))
|
2010-09-13 04:03:55 +08:00
|
|
|
self.assertEqual(
|
|
|
|
[o.normal.name for o in objs],
|
|
|
|
["tests/multiple_files.txt", "tests/multiple_files_1.txt", "tests/multiple_files_2.txt"]
|
|
|
|
)
|
|
|
|
for o in objs:
|
|
|
|
o.delete()
|
|
|
|
|
|
|
|
# Default values allow an object to access a single file.
|
|
|
|
obj3 = Storage.objects.create()
|
|
|
|
self.assertEqual(obj3.default.name, "tests/default.txt")
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(obj3.default.read(), b"default content")
|
2011-02-21 21:45:29 +08:00
|
|
|
obj3.default.close()
|
2010-09-13 04:03:55 +08:00
|
|
|
|
|
|
|
# But it shouldn't be deleted, even if there are no more objects using
|
|
|
|
# it.
|
|
|
|
obj3.delete()
|
|
|
|
obj3 = Storage()
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(obj3.default.read(), b"default content")
|
2011-02-21 21:45:29 +08:00
|
|
|
obj3.default.close()
|
2010-09-13 04:03:55 +08:00
|
|
|
|
|
|
|
# Verify the fix for #5655, making sure the directory is only
|
|
|
|
# determined once.
|
|
|
|
obj4 = Storage()
|
2012-08-29 16:00:29 +08:00
|
|
|
obj4.random.save("random_file", ContentFile("random content"))
|
2010-09-13 04:03:55 +08:00
|
|
|
self.assertTrue(obj4.random.name.endswith("/random_file"))
|
2013-10-15 02:26:54 +08:00
|
|
|
obj4.random.close()
|
2010-09-13 04:03:55 +08:00
|
|
|
|
2013-10-11 20:07:25 +08:00
|
|
|
# upload_to can be empty, meaning it does not use subdirectory.
|
|
|
|
obj5 = Storage()
|
|
|
|
obj5.empty.save('django_test.txt', ContentFile('more content'))
|
|
|
|
self.assertEqual(obj5.empty.name, "./django_test.txt")
|
|
|
|
self.assertEqual(obj5.empty.read(), b"more content")
|
2013-10-15 02:26:54 +08:00
|
|
|
obj5.empty.close()
|
2013-10-11 20:07:25 +08:00
|
|
|
|
2013-02-23 20:42:04 +08:00
|
|
|
def test_file_object(self):
|
|
|
|
# Create sample file
|
|
|
|
temp_storage.save('tests/example.txt', ContentFile('some content'))
|
|
|
|
|
|
|
|
# Load it as python file object
|
2013-02-24 20:10:42 +08:00
|
|
|
with open(temp_storage.path('tests/example.txt')) as file_obj:
|
|
|
|
# Save it using storage and read its content
|
|
|
|
temp_storage.save('tests/file_obj', file_obj)
|
2013-02-23 20:42:04 +08:00
|
|
|
self.assertTrue(temp_storage.exists('tests/file_obj'))
|
2013-02-24 20:10:42 +08:00
|
|
|
with temp_storage.open('tests/file_obj') as f:
|
|
|
|
self.assertEqual(f.read(), b'some content')
|
2013-02-23 20:42:04 +08:00
|
|
|
|
|
|
|
def test_stringio(self):
|
|
|
|
# Test passing StringIO instance as content argument to save
|
2013-02-24 01:47:36 +08:00
|
|
|
output = StringIO()
|
2013-02-23 20:42:04 +08:00
|
|
|
output.write('content')
|
|
|
|
output.seek(0)
|
|
|
|
|
|
|
|
# Save it and read written file
|
|
|
|
temp_storage.save('tests/stringio', output)
|
|
|
|
self.assertTrue(temp_storage.exists('tests/stringio'))
|
2013-02-24 20:10:42 +08:00
|
|
|
with temp_storage.open('tests/stringio') as f:
|
|
|
|
self.assertEqual(f.read(), b'content')
|
2013-02-23 20:42:04 +08:00
|
|
|
|
|
|
|
|
2012-05-31 15:59:58 +08:00
|
|
|
class FileTests(unittest.TestCase):
|
2011-03-28 13:58:43 +08:00
|
|
|
def test_context_manager(self):
|
|
|
|
orig_file = tempfile.TemporaryFile()
|
|
|
|
base_file = File(orig_file)
|
|
|
|
with base_file as f:
|
|
|
|
self.assertIs(base_file, f)
|
|
|
|
self.assertFalse(f.closed)
|
|
|
|
self.assertTrue(f.closed)
|
|
|
|
self.assertTrue(orig_file.closed)
|
2012-05-31 15:59:58 +08:00
|
|
|
|
2013-09-07 02:23:50 +08:00
|
|
|
def test_namedtemporaryfile_closes(self):
|
|
|
|
"""
|
|
|
|
The symbol django.core.files.NamedTemporaryFile is assigned as
|
|
|
|
a different class on different operating systems. In
|
|
|
|
any case, the result should minimally mock some of the API of
|
|
|
|
tempfile.NamedTemporaryFile from the Python standard library.
|
|
|
|
"""
|
|
|
|
tempfile = NamedTemporaryFile()
|
|
|
|
self.assertTrue(hasattr(tempfile, "closed"))
|
|
|
|
self.assertFalse(tempfile.closed)
|
|
|
|
|
|
|
|
tempfile.close()
|
|
|
|
self.assertTrue(tempfile.closed)
|
|
|
|
|
2012-05-31 15:59:58 +08:00
|
|
|
def test_file_mode(self):
|
|
|
|
# Should not set mode to None if it is not present.
|
|
|
|
# See #14681, stdlib gzip module crashes if mode is set to None
|
2012-08-12 04:29:18 +08:00
|
|
|
file = SimpleUploadedFile("mode_test.txt", b"content")
|
2012-05-31 15:59:58 +08:00
|
|
|
self.assertFalse(hasattr(file, 'mode'))
|
|
|
|
g = gzip.GzipFile(fileobj=file)
|
2013-06-20 18:55:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
class FileMoveSafeTests(unittest.TestCase):
|
|
|
|
def test_file_move_overwrite(self):
|
|
|
|
handle_a, self.file_a = tempfile.mkstemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
|
|
|
|
handle_b, self.file_b = tempfile.mkstemp(dir=os.environ['DJANGO_TEST_TEMP_DIR'])
|
|
|
|
|
|
|
|
# file_move_safe should raise an IOError exception if destination file exists and allow_overwrite is False
|
|
|
|
self.assertRaises(IOError, lambda: file_move_safe(self.file_a, self.file_b, allow_overwrite=False))
|
|
|
|
|
|
|
|
# should allow it and continue on if allow_overwrite is True
|
|
|
|
self.assertIsNone(file_move_safe(self.file_a, self.file_b, allow_overwrite=True))
|
2013-09-20 05:02:49 +08:00
|
|
|
|
|
|
|
os.close(handle_a)
|
|
|
|
os.close(handle_b)
|