Commit b40c551f authored by Andriy Sokolovskiy's avatar Andriy Sokolovskiy Committed by Tim Graham
Browse files

Fixed some unclosed objects in tests

parent 69483e02
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
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))


+4 −4
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ 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)
                    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'))
+2 −1
Original line number Diff line number Diff line
@@ -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):
        """
+11 −10
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ Tests for django.core.servers.
"""
from __future__ import unicode_literals

import contextlib
import os
import socket

@@ -114,7 +115,7 @@ class LiveServerViews(LiveServerBase):
        Ensure that the LiveServerTestCase serves views.
        Refs #2879.
        """
        f = self.urlopen('/example_view/')
        with contextlib.closing(self.urlopen('/example_view/')) as f:
            self.assertEqual(f.read(), b'example view')

    def test_static_files(self):
@@ -122,7 +123,7 @@ class LiveServerViews(LiveServerBase):
        Ensure that the LiveServerTestCase serves static files.
        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')

    def test_no_collectstatic_emulation(self):
@@ -143,11 +144,11 @@ class LiveServerViews(LiveServerBase):
        Ensure that the LiveServerTestCase serves media files.
        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')

    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())


@@ -159,7 +160,7 @@ class LiveServerDatabase(LiveServerBase):
        live server thread.
        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'])

    def test_database_writes(self):
+3 −2
Original line number Diff line number Diff line
@@ -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')
        with contextlib.closing(self.urlopen('/static/test/file.txt')) as f:
            self.assertEqual(f.read().rstrip(b'\r\n'), b'In static directory.')