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
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)
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_
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'
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')))
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)
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
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')))
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'
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
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)))
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')
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)
def test__image_ext_content_type_raises_on_non_img_ext(self): with self.assertRaises(TypeError): Image._image_ext_content_type('.xml')
def filename_fixture(self, request): filename = request.param image = Image(None, filename) return image, filename
def ext_fixture(self, request, _format_): format, expected_value = request.param image = Image(None, None) _format_.return_value = format return image, expected_value
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)
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)
def test__image_ext_content_type_raises_on_bad_ext(self): with self.assertRaises(ValueError): Image._image_ext_content_type('xj7')
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_
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)
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))))
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)
def blob_fixture(self): blob = b'foobar' image = Image(blob, None) return image, blob
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_
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
def it_knows_its_sha1_hash(self): image = Image(b'foobar', None) assert image.sha1 == '8843d7f92416211de9ebb963ff4ce28125932878'