Fixed some unclosed objects in tests
This commit is contained in:
parent
69483e022a
commit
b40c551fdf
|
@ -1,5 +1,6 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
|
@ -100,7 +101,10 @@ def file_upload_echo_content(request):
|
|||
"""
|
||||
Simple view to echo back the content of uploaded files for tests.
|
||||
"""
|
||||
r = {k: f.read().decode('utf-8') for k, f in request.FILES.items()}
|
||||
def read_and_close(f):
|
||||
with contextlib.closing(f):
|
||||
return f.read().decode('utf-8')
|
||||
r = {k: read_and_close(f) for k, f in request.FILES.items()}
|
||||
return HttpResponse(json.dumps(r))
|
||||
|
||||
|
||||
|
|
|
@ -60,10 +60,10 @@ class GeoSitemapTest(TestCase):
|
|||
elif kml_type == 'kmz':
|
||||
# Have to decompress KMZ before parsing.
|
||||
buf = BytesIO(self.client.get(kml_url).content)
|
||||
zf = zipfile.ZipFile(buf)
|
||||
self.assertEqual(1, len(zf.filelist))
|
||||
self.assertEqual('doc.kml', zf.filelist[0].filename)
|
||||
kml_doc = minidom.parseString(zf.read('doc.kml'))
|
||||
with zipfile.ZipFile(buf) as zf:
|
||||
self.assertEqual(1, len(zf.filelist))
|
||||
self.assertEqual('doc.kml', zf.filelist[0].filename)
|
||||
kml_doc = minidom.parseString(zf.read('doc.kml'))
|
||||
|
||||
# Ensuring the correct number of placemarks are in the KML doc.
|
||||
if 'city' in kml_url:
|
||||
|
|
|
@ -633,7 +633,8 @@ class GZipMiddlewareTest(SimpleTestCase):
|
|||
|
||||
@staticmethod
|
||||
def decompress(gzipped_string):
|
||||
return gzip.GzipFile(mode='rb', fileobj=BytesIO(gzipped_string)).read()
|
||||
with gzip.GzipFile(mode='rb', fileobj=BytesIO(gzipped_string)) as f:
|
||||
return f.read()
|
||||
|
||||
def test_compress_response(self):
|
||||
"""
|
||||
|
|
|
@ -4,6 +4,7 @@ Tests for django.core.servers.
|
|||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import os
|
||||
import socket
|
||||
|
||||
|
@ -114,16 +115,16 @@ class LiveServerViews(LiveServerBase):
|
|||
Ensure that the LiveServerTestCase serves views.
|
||||
Refs #2879.
|
||||
"""
|
||||
f = self.urlopen('/example_view/')
|
||||
self.assertEqual(f.read(), b'example view')
|
||||
with contextlib.closing(self.urlopen('/example_view/')) as f:
|
||||
self.assertEqual(f.read(), b'example view')
|
||||
|
||||
def test_static_files(self):
|
||||
"""
|
||||
Ensure that the LiveServerTestCase serves static files.
|
||||
Refs #2879.
|
||||
"""
|
||||
f = self.urlopen('/static/example_static_file.txt')
|
||||
self.assertEqual(f.read().rstrip(b'\r\n'), b'example static file')
|
||||
with contextlib.closing(self.urlopen('/static/example_static_file.txt')) as f:
|
||||
self.assertEqual(f.read().rstrip(b'\r\n'), b'example static file')
|
||||
|
||||
def test_no_collectstatic_emulation(self):
|
||||
"""
|
||||
|
@ -143,12 +144,12 @@ class LiveServerViews(LiveServerBase):
|
|||
Ensure that the LiveServerTestCase serves media files.
|
||||
Refs #2879.
|
||||
"""
|
||||
f = self.urlopen('/media/example_media_file.txt')
|
||||
self.assertEqual(f.read().rstrip(b'\r\n'), b'example media file')
|
||||
with contextlib.closing(self.urlopen('/media/example_media_file.txt')) as f:
|
||||
self.assertEqual(f.read().rstrip(b'\r\n'), b'example media file')
|
||||
|
||||
def test_environ(self):
|
||||
f = self.urlopen('/environ_view/?%s' % urlencode({'q': 'тест'}))
|
||||
self.assertIn(b"QUERY_STRING: 'q=%D1%82%D0%B5%D1%81%D1%82'", f.read())
|
||||
with contextlib.closing(self.urlopen('/environ_view/?%s' % urlencode({'q': 'тест'}))) as f:
|
||||
self.assertIn(b"QUERY_STRING: 'q=%D1%82%D0%B5%D1%81%D1%82'", f.read())
|
||||
|
||||
|
||||
class LiveServerDatabase(LiveServerBase):
|
||||
|
@ -159,8 +160,8 @@ class LiveServerDatabase(LiveServerBase):
|
|||
live server thread.
|
||||
Refs #2879.
|
||||
"""
|
||||
f = self.urlopen('/model_view/')
|
||||
self.assertEqual(f.read().splitlines(), [b'jane', b'robert'])
|
||||
with contextlib.closing(self.urlopen('/model_view/')) as f:
|
||||
self.assertEqual(f.read().splitlines(), [b'jane', b'robert'])
|
||||
|
||||
def test_database_writes(self):
|
||||
"""
|
||||
|
|
|
@ -4,6 +4,7 @@ django.contrib.staticfiles.testing.StaticLiveServerTestCase instead of
|
|||
django.test.LiveServerTestCase.
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
import os
|
||||
|
||||
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
|
||||
|
@ -94,5 +95,5 @@ class StaticLiveServerView(LiveServerBase):
|
|||
Test that StaticLiveServerTestCase use of staticfiles' serve() allows it
|
||||
to discover app's static assets without having to collectstatic first.
|
||||
"""
|
||||
f = self.urlopen('/static/test/file.txt')
|
||||
self.assertEqual(f.read().rstrip(b'\r\n'), b'In static directory.')
|
||||
with contextlib.closing(self.urlopen('/static/test/file.txt')) as f:
|
||||
self.assertEqual(f.read().rstrip(b'\r\n'), b'In static directory.')
|
||||
|
|
Loading…
Reference in New Issue