From c4536c4a54282cd89bc815b58cc3c73280712df1 Mon Sep 17 00:00:00 2001 From: Ingo Klöcker Date: Fri, 7 Apr 2017 14:21:06 +0200 Subject: Fixed #27777 -- Made File.open() work with the with statement (#8310) Fixed #27777 -- Made File.open() work with the with statement --- tests/files/tests.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'tests') 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): """ -- cgit v1.3