Exemplo n.º 1
0
 def pil_fixture(self):
     with open(test_image_path, 'rb') as f:
         blob = f.read()
     image = Image(blob, None)
     size = (204, 204)
     format = 'JPEG'
     dpi = (72, 72)
     return image, size, format, dpi
Exemplo n.º 2
0
 def test__image_ext_content_type_known_type(self):
     """Image._image_ext_content_type() correct for known content type"""
     # exercise ---------------------
     content_type = Image._image_ext_content_type('.jpeg')
     # verify -----------------------
     expected = 'image/jpeg'
     actual = content_type
     msg = ("expected content type '%s', got '%s'" % (expected, actual))
     self.assertEqual(expected, actual, msg)
Exemplo n.º 3
0
    def it_can_construct_from_a_path(self, from_blob_, image_):
        with open(test_image_path, "rb") as f:
            blob = f.read()
        from_blob_.return_value = image_

        image = Image.from_file(test_image_path)

        Image.from_blob.assert_called_once_with(blob, "python-icon.jpeg")
        assert image is image_
Exemplo n.º 4
0
 def test_construction_from_file(self):
     """Image(path) constructor produces correct attribute values"""
     # exercise ---------------------
     partname = PackURI('/ppt/media/image1.jpeg')
     image = Image.new(partname, test_image_path)
     # verify -----------------------
     assert image.ext == 'jpeg'
     assert image.content_type == 'image/jpeg'
     assert len(image._blob) == 3277
     assert image._desc == 'python-icon.jpeg'
Exemplo n.º 5
0
 def test_construction_from_file(self):
     """Image(path) constructor produces correct attribute values"""
     # exercise ---------------------
     partname = PackURI('/ppt/media/image1.jpeg')
     image = Image.new(partname, test_image_path)
     # verify -----------------------
     assert_that(image.ext, is_(equal_to('jpeg')))
     assert_that(image.content_type, is_(equal_to('image/jpeg')))
     assert_that(len(image._blob), is_(equal_to(3277)))
     assert_that(image._desc, is_(equal_to('python-icon.jpeg')))
Exemplo n.º 6
0
 def test_construction_from_file(self):
     """Image(path) constructor produces correct attribute values"""
     # exercise ---------------------
     partname = PackURI('/ppt/media/image1.jpeg')
     image = Image.new(partname, test_image_path)
     # verify -----------------------
     assert image.ext == 'jpeg'
     assert image.content_type == 'image/jpeg'
     assert len(image._blob) == 3277
     assert image._desc == 'python-icon.jpeg'
Exemplo n.º 7
0
 def test__image_ext_content_type_known_type(self):
     """
     Image._image_ext_content_type() correct for known content type
     """
     # exercise ---------------------
     content_type = Image._image_ext_content_type('jPeG')
     # verify -----------------------
     expected = 'image/jpeg'
     actual = content_type
     msg = ("expected content type '%s', got '%s'" % (expected, actual))
     self.assertEqual(expected, actual, msg)
Exemplo n.º 8
0
    def get_or_add_image_part(self, image_file):
        """Return |ImagePart| object containing the image in `image_file`.

        `image_file` can be either a path to an image file or a file-like object
        containing an image. If an image part containing this same image already exists,
        that instance is returned, otherwise a new image part is created.
        """
        image = Image.from_file(image_file)
        image_part = self._find_by_sha1(image.sha1)
        return ImagePart.new(self._package,
                             image) if image_part is None else image_part
Exemplo n.º 9
0
 def test_construction_from_stream(self):
     """Image(stream) construction produces correct attribute values"""
     # exercise ---------------------
     partname = PackURI('/ppt/media/image1.jpeg')
     with open(test_image_path, 'rb') as f:
         stream = StringIO(f.read())
     image = Image.new(partname, stream)
     # verify -----------------------
     assert_that(image.ext, is_(equal_to('jpg')))
     assert_that(image.content_type, is_(equal_to('image/jpeg')))
     assert_that(len(image._blob), is_(equal_to(3277)))
     assert_that(image._desc, is_(equal_to('image.jpg')))
Exemplo n.º 10
0
 def test_construction_from_stream(self):
     """Image(stream) construction produces correct attribute values"""
     # exercise ---------------------
     partname = PackURI('/ppt/media/image1.jpeg')
     with open(test_image_path, 'rb') as f:
         stream = StringIO(f.read())
     image = Image.new(partname, stream)
     # verify -----------------------
     assert image.ext == 'jpg'
     assert image.content_type == 'image/jpeg'
     assert len(image._blob) == 3277
     assert image._desc == 'image.jpg'
Exemplo n.º 11
0
 def test__scale_calculates_correct_dimensions(self):
     """Image._scale() calculates correct dimensions"""
     # setup ------------------------
     test_cases = (((None, None), (Px(204), Px(204))), ((1000, None),
                                                        (1000, 1000)),
                   ((None, 3000), (3000, 3000)), ((3337, 9999), (3337,
                                                                 9999)))
     partname = PackURI('/ppt/media/image1.png')
     image = Image.new(partname, test_image_path)
     # verify -----------------------
     for params, expected in test_cases:
         width, height = params
         assert image._scale(width, height) == expected
Exemplo n.º 12
0
 def test__scale_calculates_correct_dimensions(self):
     """Image._scale() calculates correct dimensions"""
     # setup ------------------------
     test_cases = (
         ((None, None), (Px(204), Px(204))),
         ((1000, None), (1000, 1000)),
         ((None, 3000), (3000, 3000)),
         ((3337, 9999), (3337, 9999)))
     partname = PackURI('/ppt/media/image1.png')
     image = Image.new(partname, test_image_path)
     # verify -----------------------
     for params, expected in test_cases:
         width, height = params
         assert_that(image._scale(width, height), is_(equal_to(expected)))
Exemplo n.º 13
0
 def test_construction_from_file_raises_on_bad_path(self):
     """Image(path) constructor raises on bad path"""
     partname = PackURI('/ppt/media/image1.jpeg')
     with self.assertRaises(IOError):
         Image.new(partname, 'foobar27.png')
Exemplo n.º 14
0
    def it_can_construct_from_a_blob(self, _init_):
        image = Image.from_blob(b"blob", "foo.png")

        _init_.assert_called_once_with(image, b"blob", "foo.png")
        assert isinstance(image, Image)
Exemplo n.º 15
0
 def test__image_ext_content_type_raises_on_non_img_ext(self):
     with self.assertRaises(TypeError):
         Image._image_ext_content_type('.xml')
Exemplo n.º 16
0
 def filename_fixture(self, request):
     filename = request.param
     image = Image(None, filename)
     return image, filename
Exemplo n.º 17
0
 def ext_fixture(self, request, _format_):
     format, expected_value = request.param
     image = Image(None, None)
     _format_.return_value = format
     return image, expected_value
Exemplo n.º 18
0
 def test_construction_from_file_raises_on_bad_path(self):
     """Image(path) constructor raises on bad path"""
     partname = PackURI('/ppt/media/image1.jpeg')
     with self.assertRaises(IOError):
         Image.new(partname, 'foobar27.png')
Exemplo n.º 19
0
 def test__ext_from_image_stream_raises_on_incompatible_format(self):
     with self.assertRaises(ValueError):
         with open(test_eps_path) as stream:
             Image._ext_from_image_stream(stream)
Exemplo n.º 20
0
 def it_can_construct_from_a_blob(self, from_blob_fixture):
     blob, filename = from_blob_fixture
     image = Image.from_blob(blob, filename)
     Image.__init__.assert_called_once_with(blob, filename)
     assert isinstance(image, Image)
Exemplo n.º 21
0
 def test__image_ext_content_type_raises_on_bad_ext(self):
     with self.assertRaises(ValueError):
         Image._image_ext_content_type('xj7')
Exemplo n.º 22
0
 def it_can_construct_from_a_path(self, from_path_fixture):
     image_file, blob, filename, image_ = from_path_fixture
     image = Image.from_file(image_file)
     Image.from_blob.assert_called_once_with(blob, filename)
     assert image is image_
Exemplo n.º 23
0
 def test__image_ext_content_type_raises_on_bad_ext(self):
     with self.assertRaises(ValueError):
         Image._image_ext_content_type('xj7')
Exemplo n.º 24
0
 def test__ext_from_image_stream_raises_on_incompatible_format(self):
     with self.assertRaises(ValueError):
         with open(test_bmp_path) as stream:
             Image._ext_from_image_stream(stream)
Exemplo n.º 25
0
 def test__size_returns_image_native_pixel_dimensions(self):
     """Image._size is width, height tuple of image pixel dimensions"""
     partname = PackURI('/ppt/media/image1.png')
     image = Image.new(partname, test_image_path)
     assert_that(image._size, is_(equal_to((204, 204))))
Exemplo n.º 26
0
 def test__size_returns_image_native_pixel_dimensions(self):
     """Image._size is width, height tuple of image pixel dimensions"""
     partname = PackURI('/ppt/media/image1.png')
     image = Image.new(partname, test_image_path)
     assert image._size == (204, 204)
Exemplo n.º 27
0
 def blob_fixture(self):
     blob = b'foobar'
     image = Image(blob, None)
     return image, blob
Exemplo n.º 28
0
 def it_can_construct_from_a_stream(self, from_stream_fixture):
     image_file, blob, image_ = from_stream_fixture
     image = Image.from_file(image_file)
     Image.from_blob.assert_called_once_with(blob, None)
     assert image is image_
Exemplo n.º 29
0
 def dpi_fixture(self, request, _pil_props_):
     raw_dpi, expected_dpi = request.param
     image = Image(None, None)
     _pil_props_.return_value = (None, None, raw_dpi)
     return image, expected_dpi
Exemplo n.º 30
0
 def it_can_construct_from_a_stream(self, from_stream_fixture):
     image_file, blob, image_ = from_stream_fixture
     image = Image.from_file(image_file)
     Image.from_blob.assert_called_once_with(blob, None)
     assert image is image_
Exemplo n.º 31
0
 def it_can_construct_from_a_blob(self, from_blob_fixture):
     blob, filename = from_blob_fixture
     image = Image.from_blob(blob, filename)
     Image.__init__.assert_called_once_with(blob, filename)
     assert isinstance(image, Image)
Exemplo n.º 32
0
 def it_can_construct_from_a_path(self, from_path_fixture):
     image_file, blob, filename, image_ = from_path_fixture
     image = Image.from_file(image_file)
     Image.from_blob.assert_called_once_with(blob, filename)
     assert image is image_
Exemplo n.º 33
0
 def it_knows_its_sha1_hash(self):
     image = Image(b'foobar', None)
     assert image.sha1 == '8843d7f92416211de9ebb963ff4ce28125932878'