Commit b7de5f5d authored by Loic Bistuer's avatar Loic Bistuer
Browse files

Fixed a few ResourceWarning in the test suite. Refs #22680.

parent 2779c299
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -155,10 +155,15 @@ class Command(BaseCommand):

        try:
            self.stdout.ending = None
            stream = open(output, 'w') if output else None
            try:
                serializers.serialize(format, get_objects(), indent=indent,
                        use_natural_foreign_keys=use_natural_foreign_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:
            if show_traceback:
                raise
+95 −91
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ MEDIA_ROOT = sys_tempfile.mkdtemp()
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):

    @classmethod
@@ -51,11 +51,11 @@ class FileUploadTests(TestCase):
    def test_large_upload(self):
        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.seek(0)

        file2 = tempfile.NamedTemporaryFile(suffix=".file2", dir=tdir)
            file2.write(b'a' * (10 * 2 ** 20))
            file2.seek(0)

@@ -196,7 +196,9 @@ class FileUploadTests(TestCase):
            'REQUEST_METHOD': 'POST',
            '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:
            got = result[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."""
        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.seek(0)

        simple_file = tempfile.NamedTemporaryFile(suffix=".ctype_extra", dir=tdir)
            simple_file.write(b'something')
            simple_file.seek(0)
            simple_file.content_type = 'text/plain; test-key=test_value'
@@ -266,6 +268,8 @@ class FileUploadTests(TestCase):
        self.assertEqual(got, {})

    def test_custom_upload_handler(self):
        file = tempfile.NamedTemporaryFile
        with file() as smallfile, file() as bigfile:
            # A small file (under the 5M quota)
            smallfile = tempfile.NamedTemporaryFile()
            smallfile.write(b'a' * (2 ** 21))
@@ -287,28 +291,27 @@ class FileUploadTests(TestCase):
            self.assertTrue('f' not in got)

    def test_broken_custom_upload_handler(self):
        f = tempfile.NamedTemporaryFile()
        f.write(b'a' * (2 ** 21))
        f.seek(0)
        with tempfile.NamedTemporaryFile() as file:
            file.write(b'a' * (2 ** 21))
            file.seek(0)

            # AttributeError: You cannot alter upload handlers after the upload has been processed.
            self.assertRaises(
                AttributeError,
                self.client.post,
                '/quota/broken/',
            {'f': f}
                {'f': file}
            )

    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.seek(0)

        file2 = tempfile.NamedTemporaryFile()
            file2.write(b'a' * (2 * 2 ** 18))
            file2.seek(0)

        file2a = tempfile.NamedTemporaryFile()
            file2a.write(b'a' * (5 * 2 ** 20))
            file2a.seek(0)

@@ -434,7 +437,8 @@ class DirectoryCreationTests(TestCase):
        open(UPLOAD_TO, 'wb').close()
        self.addCleanup(os.remove, UPLOAD_TO)
        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
        # is raised even without the patch (just not early enough)
        self.assertEqual(exc_info.exception.args[0],
+2 −0
Original line number Diff line number Diff line
@@ -58,6 +58,8 @@ class ImageFieldTestMixin(object):
        """
        Removes temp directory and all its contents.
        """
        self.file1.close()
        self.file2.close()
        shutil.rmtree(temp_storage_dir)

    def check_dimensions(self, instance, width, height,