Fixed some unclosed objects in tests

This commit is contained in:
Andriy Sokolovskiy 2015-07-02 00:37:10 +03:00 committed by Tim Graham
parent 69483e022a
commit b40c551fdf
5 changed files with 25 additions and 18 deletions

View File

@ -1,5 +1,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import contextlib
import hashlib import hashlib
import json import json
import os import os
@ -100,7 +101,10 @@ def file_upload_echo_content(request):
""" """
Simple view to echo back the content of uploaded files for tests. 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)) return HttpResponse(json.dumps(r))

View File

@ -60,10 +60,10 @@ class GeoSitemapTest(TestCase):
elif kml_type == 'kmz': elif kml_type == 'kmz':
# Have to decompress KMZ before parsing. # Have to decompress KMZ before parsing.
buf = BytesIO(self.client.get(kml_url).content) buf = BytesIO(self.client.get(kml_url).content)
zf = zipfile.ZipFile(buf) with zipfile.ZipFile(buf) as zf:
self.assertEqual(1, len(zf.filelist)) self.assertEqual(1, len(zf.filelist))
self.assertEqual('doc.kml', zf.filelist[0].filename) self.assertEqual('doc.kml', zf.filelist[0].filename)
kml_doc = minidom.parseString(zf.read('doc.kml')) kml_doc = minidom.parseString(zf.read('doc.kml'))
# Ensuring the correct number of placemarks are in the KML doc. # Ensuring the correct number of placemarks are in the KML doc.
if 'city' in kml_url: if 'city' in kml_url:

View File

@ -633,7 +633,8 @@ class GZipMiddlewareTest(SimpleTestCase):
@staticmethod @staticmethod
def decompress(gzipped_string): 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): def test_compress_response(self):
""" """

View File

@ -4,6 +4,7 @@ Tests for django.core.servers.
""" """
from __future__ import unicode_literals from __future__ import unicode_literals
import contextlib
import os import os
import socket import socket
@ -114,16 +115,16 @@ class LiveServerViews(LiveServerBase):
Ensure that the LiveServerTestCase serves views. Ensure that the LiveServerTestCase serves views.
Refs #2879. Refs #2879.
""" """
f = self.urlopen('/example_view/') with contextlib.closing(self.urlopen('/example_view/')) as f:
self.assertEqual(f.read(), b'example view') self.assertEqual(f.read(), b'example view')
def test_static_files(self): def test_static_files(self):
""" """
Ensure that the LiveServerTestCase serves static files. Ensure that the LiveServerTestCase serves static files.
Refs #2879. Refs #2879.
""" """
f = self.urlopen('/static/example_static_file.txt') with contextlib.closing(self.urlopen('/static/example_static_file.txt')) as f:
self.assertEqual(f.read().rstrip(b'\r\n'), b'example static file') self.assertEqual(f.read().rstrip(b'\r\n'), b'example static file')
def test_no_collectstatic_emulation(self): def test_no_collectstatic_emulation(self):
""" """
@ -143,12 +144,12 @@ class LiveServerViews(LiveServerBase):
Ensure that the LiveServerTestCase serves media files. Ensure that the LiveServerTestCase serves media files.
Refs #2879. Refs #2879.
""" """
f = self.urlopen('/media/example_media_file.txt') with contextlib.closing(self.urlopen('/media/example_media_file.txt')) as f:
self.assertEqual(f.read().rstrip(b'\r\n'), b'example media file') self.assertEqual(f.read().rstrip(b'\r\n'), b'example media file')
def test_environ(self): def test_environ(self):
f = self.urlopen('/environ_view/?%s' % urlencode({'q': 'тест'})) 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()) self.assertIn(b"QUERY_STRING: 'q=%D1%82%D0%B5%D1%81%D1%82'", f.read())
class LiveServerDatabase(LiveServerBase): class LiveServerDatabase(LiveServerBase):
@ -159,8 +160,8 @@ class LiveServerDatabase(LiveServerBase):
live server thread. live server thread.
Refs #2879. Refs #2879.
""" """
f = self.urlopen('/model_view/') with contextlib.closing(self.urlopen('/model_view/')) as f:
self.assertEqual(f.read().splitlines(), [b'jane', b'robert']) self.assertEqual(f.read().splitlines(), [b'jane', b'robert'])
def test_database_writes(self): def test_database_writes(self):
""" """

View File

@ -4,6 +4,7 @@ django.contrib.staticfiles.testing.StaticLiveServerTestCase instead of
django.test.LiveServerTestCase. django.test.LiveServerTestCase.
""" """
import contextlib
import os import os
from django.contrib.staticfiles.testing import StaticLiveServerTestCase from django.contrib.staticfiles.testing import StaticLiveServerTestCase
@ -94,5 +95,5 @@ class StaticLiveServerView(LiveServerBase):
Test that StaticLiveServerTestCase use of staticfiles' serve() allows it Test that StaticLiveServerTestCase use of staticfiles' serve() allows it
to discover app's static assets without having to collectstatic first. to discover app's static assets without having to collectstatic first.
""" """
f = self.urlopen('/static/test/file.txt') with contextlib.closing(self.urlopen('/static/test/file.txt')) as f:
self.assertEqual(f.read().rstrip(b'\r\n'), b'In static directory.') self.assertEqual(f.read().rstrip(b'\r\n'), b'In static directory.')