Fixed a few ResourceWarning in the test suite. Refs #22680.
This commit is contained in:
parent
2779c299c8
commit
b7de5f5d3f
|
@ -155,10 +155,15 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.stdout.ending = None
|
self.stdout.ending = None
|
||||||
|
stream = open(output, 'w') if output else None
|
||||||
|
try:
|
||||||
serializers.serialize(format, get_objects(), indent=indent,
|
serializers.serialize(format, get_objects(), indent=indent,
|
||||||
use_natural_foreign_keys=use_natural_foreign_keys,
|
use_natural_foreign_keys=use_natural_foreign_keys,
|
||||||
use_natural_primary_keys=use_natural_primary_keys,
|
use_natural_primary_keys=use_natural_primary_keys,
|
||||||
stream=open(output, 'w') if output else self.stdout)
|
stream=stream or self.stdout)
|
||||||
|
finally:
|
||||||
|
if stream:
|
||||||
|
stream.close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if show_traceback:
|
if show_traceback:
|
||||||
raise
|
raise
|
||||||
|
|
|
@ -27,7 +27,7 @@ MEDIA_ROOT = sys_tempfile.mkdtemp()
|
||||||
UPLOAD_TO = os.path.join(MEDIA_ROOT, 'test_upload')
|
UPLOAD_TO = os.path.join(MEDIA_ROOT, 'test_upload')
|
||||||
|
|
||||||
|
|
||||||
@override_settings(MEDIA_ROOT=MEDIA_ROOT, ROOT_URLCONF='file_uploads.urls')
|
@override_settings(MEDIA_ROOT=MEDIA_ROOT, ROOT_URLCONF='file_uploads.urls', MIDDLEWARE_CLASSES=())
|
||||||
class FileUploadTests(TestCase):
|
class FileUploadTests(TestCase):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -51,11 +51,11 @@ class FileUploadTests(TestCase):
|
||||||
def test_large_upload(self):
|
def test_large_upload(self):
|
||||||
tdir = tempfile.gettempdir()
|
tdir = tempfile.gettempdir()
|
||||||
|
|
||||||
file1 = tempfile.NamedTemporaryFile(suffix=".file1", dir=tdir)
|
file = tempfile.NamedTemporaryFile
|
||||||
|
with file(suffix=".file1", dir=tdir) as file1, file(suffix=".file2", dir=tdir) as file2:
|
||||||
file1.write(b'a' * (2 ** 21))
|
file1.write(b'a' * (2 ** 21))
|
||||||
file1.seek(0)
|
file1.seek(0)
|
||||||
|
|
||||||
file2 = tempfile.NamedTemporaryFile(suffix=".file2", dir=tdir)
|
|
||||||
file2.write(b'a' * (10 * 2 ** 20))
|
file2.write(b'a' * (10 * 2 ** 20))
|
||||||
file2.seek(0)
|
file2.seek(0)
|
||||||
|
|
||||||
|
@ -196,7 +196,9 @@ class FileUploadTests(TestCase):
|
||||||
'REQUEST_METHOD': 'POST',
|
'REQUEST_METHOD': 'POST',
|
||||||
'wsgi.input': payload,
|
'wsgi.input': payload,
|
||||||
}
|
}
|
||||||
result = json.loads(self.client.request(**r).content.decode('utf-8'))
|
response = self.client.request(**r)
|
||||||
|
|
||||||
|
result = json.loads(response.content.decode('utf-8'))
|
||||||
for name, _, expected in cases:
|
for name, _, expected in cases:
|
||||||
got = result[name]
|
got = result[name]
|
||||||
self.assertEqual(expected, got, 'Mismatch for {0}'.format(name))
|
self.assertEqual(expected, got, 'Mismatch for {0}'.format(name))
|
||||||
|
@ -207,11 +209,11 @@ class FileUploadTests(TestCase):
|
||||||
"""Uploaded files may have content type parameters available."""
|
"""Uploaded files may have content type parameters available."""
|
||||||
tdir = tempfile.gettempdir()
|
tdir = tempfile.gettempdir()
|
||||||
|
|
||||||
no_content_type = tempfile.NamedTemporaryFile(suffix=".ctype_extra", dir=tdir)
|
file = tempfile.NamedTemporaryFile
|
||||||
|
with file(suffix=".ctype_extra", dir=tdir) as no_content_type, file(suffix=".ctype_extra", dir=tdir) as simple_file:
|
||||||
no_content_type.write(b'something')
|
no_content_type.write(b'something')
|
||||||
no_content_type.seek(0)
|
no_content_type.seek(0)
|
||||||
|
|
||||||
simple_file = tempfile.NamedTemporaryFile(suffix=".ctype_extra", dir=tdir)
|
|
||||||
simple_file.write(b'something')
|
simple_file.write(b'something')
|
||||||
simple_file.seek(0)
|
simple_file.seek(0)
|
||||||
simple_file.content_type = 'text/plain; test-key=test_value'
|
simple_file.content_type = 'text/plain; test-key=test_value'
|
||||||
|
@ -266,6 +268,8 @@ class FileUploadTests(TestCase):
|
||||||
self.assertEqual(got, {})
|
self.assertEqual(got, {})
|
||||||
|
|
||||||
def test_custom_upload_handler(self):
|
def test_custom_upload_handler(self):
|
||||||
|
file = tempfile.NamedTemporaryFile
|
||||||
|
with file() as smallfile, file() as bigfile:
|
||||||
# A small file (under the 5M quota)
|
# A small file (under the 5M quota)
|
||||||
smallfile = tempfile.NamedTemporaryFile()
|
smallfile = tempfile.NamedTemporaryFile()
|
||||||
smallfile.write(b'a' * (2 ** 21))
|
smallfile.write(b'a' * (2 ** 21))
|
||||||
|
@ -287,28 +291,27 @@ class FileUploadTests(TestCase):
|
||||||
self.assertTrue('f' not in got)
|
self.assertTrue('f' not in got)
|
||||||
|
|
||||||
def test_broken_custom_upload_handler(self):
|
def test_broken_custom_upload_handler(self):
|
||||||
f = tempfile.NamedTemporaryFile()
|
with tempfile.NamedTemporaryFile() as file:
|
||||||
f.write(b'a' * (2 ** 21))
|
file.write(b'a' * (2 ** 21))
|
||||||
f.seek(0)
|
file.seek(0)
|
||||||
|
|
||||||
# AttributeError: You cannot alter upload handlers after the upload has been processed.
|
# AttributeError: You cannot alter upload handlers after the upload has been processed.
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
AttributeError,
|
AttributeError,
|
||||||
self.client.post,
|
self.client.post,
|
||||||
'/quota/broken/',
|
'/quota/broken/',
|
||||||
{'f': f}
|
{'f': file}
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_fileupload_getlist(self):
|
def test_fileupload_getlist(self):
|
||||||
file1 = tempfile.NamedTemporaryFile()
|
file = tempfile.NamedTemporaryFile
|
||||||
|
with file() as file1, file() as file2, file() as file2a:
|
||||||
file1.write(b'a' * (2 ** 23))
|
file1.write(b'a' * (2 ** 23))
|
||||||
file1.seek(0)
|
file1.seek(0)
|
||||||
|
|
||||||
file2 = tempfile.NamedTemporaryFile()
|
|
||||||
file2.write(b'a' * (2 * 2 ** 18))
|
file2.write(b'a' * (2 * 2 ** 18))
|
||||||
file2.seek(0)
|
file2.seek(0)
|
||||||
|
|
||||||
file2a = tempfile.NamedTemporaryFile()
|
|
||||||
file2a.write(b'a' * (5 * 2 ** 20))
|
file2a.write(b'a' * (5 * 2 ** 20))
|
||||||
file2a.seek(0)
|
file2a.seek(0)
|
||||||
|
|
||||||
|
@ -434,7 +437,8 @@ class DirectoryCreationTests(TestCase):
|
||||||
open(UPLOAD_TO, 'wb').close()
|
open(UPLOAD_TO, 'wb').close()
|
||||||
self.addCleanup(os.remove, UPLOAD_TO)
|
self.addCleanup(os.remove, UPLOAD_TO)
|
||||||
with self.assertRaises(IOError) as exc_info:
|
with self.assertRaises(IOError) as exc_info:
|
||||||
self.obj.testfile.save('foo.txt', SimpleUploadedFile('foo.txt', b'x'))
|
with SimpleUploadedFile('foo.txt', b'x') as file:
|
||||||
|
self.obj.testfile.save('foo.txt', file)
|
||||||
# The test needs to be done on a specific string as IOError
|
# The test needs to be done on a specific string as IOError
|
||||||
# is raised even without the patch (just not early enough)
|
# is raised even without the patch (just not early enough)
|
||||||
self.assertEqual(exc_info.exception.args[0],
|
self.assertEqual(exc_info.exception.args[0],
|
||||||
|
|
|
@ -58,6 +58,8 @@ class ImageFieldTestMixin(object):
|
||||||
"""
|
"""
|
||||||
Removes temp directory and all its contents.
|
Removes temp directory and all its contents.
|
||||||
"""
|
"""
|
||||||
|
self.file1.close()
|
||||||
|
self.file2.close()
|
||||||
shutil.rmtree(temp_storage_dir)
|
shutil.rmtree(temp_storage_dir)
|
||||||
|
|
||||||
def check_dimensions(self, instance, width, height,
|
def check_dimensions(self, instance, width, height,
|
||||||
|
|
Loading…
Reference in New Issue