Exemplo n.º 1
0
    def test_closing_of_filenames(self):
        """
        get_image_dimensions() called with a filename should closed the file.
        """
        # We need to inject a modified open() builtin into the images module
        # that checks if the file was closed properly if the function is
        # called with a filename instead of an file object.
        # get_image_dimensions will call our catching_open instead of the
        # regular builtin one.

        class FileWrapper(object):
            _closed = []
            def __init__(self, f):
                self.f = f
            def __getattr__(self, name):
                return getattr(self.f, name)
            def close(self):
                self._closed.append(True)
                self.f.close()

        def catching_open(*args):
            return FileWrapper(open(*args))

        from djangocg.core.files import images
        images.open = catching_open
        try:
            get_image_dimensions(os.path.join(os.path.dirname(__file__), "test1.png"))
        finally:
            del images.open
        self.assertTrue(FileWrapper._closed)
Exemplo n.º 2
0
 def test_not_closing_of_files(self):
     """
     Open files passed into get_image_dimensions() should stay opened.
     """
     empty_io = BytesIO()
     try:
         get_image_dimensions(empty_io)
     finally:
         self.assertTrue(not empty_io.closed)
Exemplo n.º 3
0
    def test_multiple_calls(self):
        """
        Multiple calls of get_image_dimensions() should return the same size.
        """
        from djangocg.core.files.images import ImageFile

        img_path = os.path.join(os.path.dirname(__file__), "test.png")
        image = ImageFile(open(img_path, 'rb'))
        image_pil = Image.open(img_path)
        size_1, size_2 = get_image_dimensions(image), get_image_dimensions(image)
        self.assertEqual(image_pil.size, size_1)
        self.assertEqual(size_1, size_2)