summaryrefslogtreecommitdiff
path: root/tests/files
diff options
context:
space:
mode:
authorIngo Klöcker <ingokloecker@users.noreply.github.com>2017-04-07 14:21:06 +0200
committerFlorian Apolloner <apollo13@users.noreply.github.com>2017-04-07 14:21:06 +0200
commitc4536c4a54282cd89bc815b58cc3c73280712df1 (patch)
tree3d425bbaaafe0cffd5d7ad19cf1f733fa805d975 /tests/files
parent695d4dd7908ca32e118716b474c23b43727579d2 (diff)
downloaddjango-c4536c4a54282cd89bc815b58cc3c73280712df1.tar.gz
Fixed #27777 -- Made File.open() work with the with statement (#8310)
Fixed #27777 -- Made File.open() work with the with statement
Diffstat (limited to 'tests/files')
-rw-r--r--tests/files/tests.py36
1 files changed, 35 insertions, 1 deletions
diff --git a/tests/files/tests.py b/tests/files/tests.py
index 9b3e019f8d..1a383090f9 100644
--- a/tests/files/tests.py
+++ b/tests/files/tests.py
@@ -10,7 +10,9 @@ from django.core.files import File
from django.core.files.base import ContentFile
from django.core.files.move import file_move_safe
from django.core.files.temp import NamedTemporaryFile
-from django.core.files.uploadedfile import SimpleUploadedFile, UploadedFile
+from django.core.files.uploadedfile import (
+ InMemoryUploadedFile, SimpleUploadedFile, UploadedFile,
+)
try:
from PIL import Image
@@ -38,6 +40,23 @@ class FileTests(unittest.TestCase):
self.assertTrue(f.closed)
self.assertTrue(orig_file.closed)
+ def test_open_resets_opened_file_to_start_and_returns_context_manager(self):
+ file = File(BytesIO(b'content'))
+ file.read()
+ with file.open() as f:
+ self.assertEqual(f.read(), b'content')
+
+ def test_open_reopens_closed_file_and_returns_context_manager(self):
+ temporary_file = tempfile.NamedTemporaryFile(delete=False)
+ file = File(temporary_file)
+ try:
+ file.close()
+ with file.open() as f:
+ self.assertFalse(f.closed)
+ finally:
+ # remove temporary file
+ os.unlink(file.name)
+
def test_namedtemporaryfile_closes(self):
"""
The symbol django.core.files.NamedTemporaryFile is assigned as
@@ -178,6 +197,21 @@ class ContentFileTestCase(unittest.TestCase):
self.assertIsInstance(ContentFile(b"content").read(), bytes)
self.assertIsInstance(ContentFile("español").read(), str)
+ def test_open_resets_file_to_start_and_returns_context_manager(self):
+ file = ContentFile(b'content')
+ with file.open() as f:
+ self.assertEqual(f.read(), b'content')
+ with file.open() as f:
+ self.assertEqual(f.read(), b'content')
+
+
+class InMemoryUploadedFileTests(unittest.TestCase):
+ def test_open_resets_file_to_start_and_returns_context_manager(self):
+ uf = InMemoryUploadedFile(StringIO('1'), '', 'test', 'text/plain', 1, 'utf8')
+ uf.read()
+ with uf.open() as f:
+ self.assertEqual(f.read(), '1')
+
class DimensionClosingBug(unittest.TestCase):
"""