mirror of https://github.com/django/django.git
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
|
||||||
serializers.serialize(format, get_objects(), indent=indent,
|
stream = open(output, 'w') if output else None
|
||||||
use_natural_foreign_keys=use_natural_foreign_keys,
|
try:
|
||||||
use_natural_primary_keys=use_natural_primary_keys,
|
serializers.serialize(format, get_objects(), indent=indent,
|
||||||
stream=open(output, 'w') if output else self.stdout)
|
use_natural_foreign_keys=use_natural_foreign_keys,
|
||||||
|
use_natural_primary_keys=use_natural_primary_keys,
|
||||||
|
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,30 +51,30 @@ 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
|
||||||
file1.write(b'a' * (2 ** 21))
|
with file(suffix=".file1", dir=tdir) as file1, file(suffix=".file2", dir=tdir) as file2:
|
||||||
file1.seek(0)
|
file1.write(b'a' * (2 ** 21))
|
||||||
|
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)
|
|
||||||
|
|
||||||
post_data = {
|
post_data = {
|
||||||
'name': 'Ringo',
|
'name': 'Ringo',
|
||||||
'file_field1': file1,
|
'file_field1': file1,
|
||||||
'file_field2': file2,
|
'file_field2': file2,
|
||||||
}
|
}
|
||||||
|
|
||||||
for key in list(post_data):
|
for key in list(post_data):
|
||||||
try:
|
try:
|
||||||
post_data[key + '_hash'] = hashlib.sha1(post_data[key].read()).hexdigest()
|
post_data[key + '_hash'] = hashlib.sha1(post_data[key].read()).hexdigest()
|
||||||
post_data[key].seek(0)
|
post_data[key].seek(0)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
post_data[key + '_hash'] = hashlib.sha1(force_bytes(post_data[key])).hexdigest()
|
post_data[key + '_hash'] = hashlib.sha1(force_bytes(post_data[key])).hexdigest()
|
||||||
|
|
||||||
response = self.client.post('/verify/', post_data)
|
response = self.client.post('/verify/', post_data)
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
def _test_base64_upload(self, content):
|
def _test_base64_upload(self, content):
|
||||||
payload = client.FakePayload("\r\n".join([
|
payload = client.FakePayload("\r\n".join([
|
||||||
|
@ -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,22 +209,22 @@ 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
|
||||||
no_content_type.write(b'something')
|
with file(suffix=".ctype_extra", dir=tdir) as no_content_type, file(suffix=".ctype_extra", dir=tdir) as simple_file:
|
||||||
no_content_type.seek(0)
|
no_content_type.write(b'something')
|
||||||
|
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'
|
|
||||||
|
|
||||||
response = self.client.post('/echo_content_type_extra/', {
|
response = self.client.post('/echo_content_type_extra/', {
|
||||||
'no_content_type': no_content_type,
|
'no_content_type': no_content_type,
|
||||||
'simple_file': simple_file,
|
'simple_file': simple_file,
|
||||||
})
|
})
|
||||||
received = json.loads(response.content.decode('utf-8'))
|
received = json.loads(response.content.decode('utf-8'))
|
||||||
self.assertEqual(received['no_content_type'], {})
|
self.assertEqual(received['no_content_type'], {})
|
||||||
self.assertEqual(received['simple_file'], {'test-key': 'test_value'})
|
self.assertEqual(received['simple_file'], {'test-key': 'test_value'})
|
||||||
|
|
||||||
def test_truncated_multipart_handled_gracefully(self):
|
def test_truncated_multipart_handled_gracefully(self):
|
||||||
"""
|
"""
|
||||||
|
@ -266,65 +268,66 @@ class FileUploadTests(TestCase):
|
||||||
self.assertEqual(got, {})
|
self.assertEqual(got, {})
|
||||||
|
|
||||||
def test_custom_upload_handler(self):
|
def test_custom_upload_handler(self):
|
||||||
# A small file (under the 5M quota)
|
file = tempfile.NamedTemporaryFile
|
||||||
smallfile = tempfile.NamedTemporaryFile()
|
with file() as smallfile, file() as bigfile:
|
||||||
smallfile.write(b'a' * (2 ** 21))
|
# A small file (under the 5M quota)
|
||||||
smallfile.seek(0)
|
smallfile = tempfile.NamedTemporaryFile()
|
||||||
|
smallfile.write(b'a' * (2 ** 21))
|
||||||
|
smallfile.seek(0)
|
||||||
|
|
||||||
# A big file (over the quota)
|
# A big file (over the quota)
|
||||||
bigfile = tempfile.NamedTemporaryFile()
|
bigfile = tempfile.NamedTemporaryFile()
|
||||||
bigfile.write(b'a' * (10 * 2 ** 20))
|
bigfile.write(b'a' * (10 * 2 ** 20))
|
||||||
bigfile.seek(0)
|
bigfile.seek(0)
|
||||||
|
|
||||||
# Small file posting should work.
|
# Small file posting should work.
|
||||||
response = self.client.post('/quota/', {'f': smallfile})
|
response = self.client.post('/quota/', {'f': smallfile})
|
||||||
got = json.loads(response.content.decode('utf-8'))
|
got = json.loads(response.content.decode('utf-8'))
|
||||||
self.assertTrue('f' in got)
|
self.assertTrue('f' in got)
|
||||||
|
|
||||||
# Large files don't go through.
|
# Large files don't go through.
|
||||||
response = self.client.post("/quota/", {'f': bigfile})
|
response = self.client.post("/quota/", {'f': bigfile})
|
||||||
got = json.loads(response.content.decode('utf-8'))
|
got = json.loads(response.content.decode('utf-8'))
|
||||||
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
|
||||||
file1.write(b'a' * (2 ** 23))
|
with file() as file1, file() as file2, file() as file2a:
|
||||||
file1.seek(0)
|
file1.write(b'a' * (2 ** 23))
|
||||||
|
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)
|
|
||||||
|
|
||||||
response = self.client.post('/getlist_count/', {
|
response = self.client.post('/getlist_count/', {
|
||||||
'file1': file1,
|
'file1': file1,
|
||||||
'field1': 'test',
|
'field1': 'test',
|
||||||
'field2': 'test3',
|
'field2': 'test3',
|
||||||
'field3': 'test5',
|
'field3': 'test5',
|
||||||
'field4': 'test6',
|
'field4': 'test6',
|
||||||
'field5': 'test7',
|
'field5': 'test7',
|
||||||
'file2': (file2, file2a)
|
'file2': (file2, file2a)
|
||||||
})
|
})
|
||||||
got = json.loads(response.content.decode('utf-8'))
|
got = json.loads(response.content.decode('utf-8'))
|
||||||
|
|
||||||
self.assertEqual(got.get('file1'), 1)
|
self.assertEqual(got.get('file1'), 1)
|
||||||
self.assertEqual(got.get('file2'), 2)
|
self.assertEqual(got.get('file2'), 2)
|
||||||
|
|
||||||
def test_file_error_blocking(self):
|
def test_file_error_blocking(self):
|
||||||
"""
|
"""
|
||||||
|
@ -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