def test_invalid_path(self, image_fetcher: ImageFetcher) -> None: resolution = ResolutionName.HIGH with pytest.raises(InvalidPathError) as error: image_fetcher.resize_image(self.path, self.resized_path, resolution) assert error.value.message == f"Invalid image path: '{self.path}'"
def test_invalid_path(self, image_fetcher: ImageFetcher) -> None: resolution = ResolutionName.HIGH path = "path" with pytest.raises(InvalidPathError) as error: image_fetcher.get_resolution_path(path, resolution) assert error.value.message == f"Invalid image path: '{path}'"
def test_invalid_resolution(self, image_fetcher: ImageFetcher) -> None: resolution = "test" with TemporaryImageFile(self.path): with pytest.raises(InvalidResolutionError) as error: image_fetcher.resize_image(self.path, self.resized_path, resolution) assert error.value.message == f"Invalid resolution: '{resolution}'"
def test_resize_image_call(self, image_fetcher: ImageFetcher) -> None: resolution = ResolutionName.HIGH with TemporaryImageFile(self.path): image_fetcher.fetch_image(self.path, resolution) image_fetcher.resize_image.assert_called_once_with( raw_path=self.path, resized_path=self.resized_path, resolution=resolution, )
def test_get_resolution_path_call(self, image_fetcher: ImageFetcher) -> None: resolution = ResolutionName.HIGH with TemporaryImageFile(self.resized_path) as resized_image: ImageFetcher.resize_image.return_value = resized_image with TemporaryImageFile(self.path): image_fetcher.fetch_image(self.path, resolution) image_fetcher.get_resolution_path.assert_called_once_with( path=self.path, resolution=resolution)
def test_non_raw_path(self, image_fetcher: ImageFetcher) -> None: resolution = ResolutionName.HIGH path = "path/image.jpg" output_path = f"path/resized_images/image.{resolution.lower()}.jpg" assert image_fetcher.get_resolution_path(path, resolution) == output_path
def test_fetch_existing_resize(self, image_fetcher: ImageFetcher) -> None: with TemporaryImageFile(self.resized_path) as resized_image: with TemporaryImageFile(self.path): image = image_fetcher.fetch_image(self.path, ResolutionName.HIGH) assert image.size == resized_image.size assert image.filename == resized_image.filename assert not ImageFetcher.resize_image.called
def test_fetch_raw_resolution(self, image_fetcher: ImageFetcher) -> None: ImageFetcher.get_resolution_path.return_value = self.path with TemporaryImageFile(self.path) as raw_image: image = image_fetcher.fetch_image(self.path) assert image.size == raw_image.size assert image.filename == raw_image.filename assert not ImageFetcher.resize_image.called
def test_fetch_non_existing_resize(self, image_fetcher: ImageFetcher) -> None: ImageFetcher.get_resolution_path.return_value = "./test.png" with TemporaryImageFile(self.resized_path) as resized_image: ImageFetcher.resize_image.return_value = resized_image with TemporaryImageFile(self.path): image = image_fetcher.fetch_image(self.path, ResolutionName.HIGH) assert image == resized_image
def _image_fetcher(self) -> ImageFetcher: image_fetcher = ImageFetcher() ImageFetcher.get_internal_path = Mock() ImageFetcher.get_internal_path.return_value = self.path ImageFetcher.get_resolution_path = Mock() ImageFetcher.get_resolution_path.return_value = self.resized_path ImageFetcher.resize_image = Mock() return image_fetcher
def test_resize(self, image_fetcher: ImageFetcher) -> None: resolution = ResolutionName.LOW max_dimension = getattr(ResolutionMaxDimension, resolution) raw_dimensions = (1000, 1000) with TemporaryImageFile(self.path, raw_dimensions): image = image_fetcher.resize_image(self.path, self.resized_path, resolution) assert isinstance(image, Image.Image) assert image.size == (max_dimension, max_dimension) assert os.path.exists(self.resized_path)
def test_resolution_greater_than_raw(self, image_fetcher: ImageFetcher) -> None: resolution = ResolutionName.LOW reduced_dimensions = (1, 1) with TemporaryImageFile(self.path, reduced_dimensions): image = image_fetcher.resize_image(self.path, self.resized_path, resolution) assert isinstance(image, Image.Image) assert image.size == reduced_dimensions assert not os.path.exists(self.resized_path)
def _image_fetcher(self) -> ImageFetcher: image_fetcher = ImageFetcher() ImageFetcher.fetch_image = Mock() image = Image.new('RGB', (1, 1)) image.save(f"/{self.filename}") ImageFetcher.fetch_image.return_value = Image.open(f"/{self.filename}") yield image_fetcher os.remove(f"/{self.filename}")
def get_image(image_path: str) -> Response: resolution = "RAW" path_split = image_path.rstrip("/").split("/") if "." not in path_split[-1]: resolution = path_split[-1].upper() path_split = path_split[:-1] try: image = ImageFetcher.fetch_image(path="/".join(path_split), resolution=resolution) except InvalidPathError: raise APIError(404, "IMAGE_NOT_FOUND") except InvalidResolutionError: raise APIError(400, "BAD_RESOLUTION", {"resolution": resolution}) return send_file(image.filename, mimetype=Image.MIME[image.format])
def test_raw_path(self, image_fetcher: ImageFetcher) -> None: path = "path/image.jpg" assert image_fetcher.get_resolution_path(path, ResolutionName.RAW) == path
def test_strip_dir_navigation(self, app: SamsiteFlask, image_fetcher: ImageFetcher) -> None: assert (image_fetcher.get_internal_path(f"../../../{self.path}") == f"{app.STATIC_DIR}/{self.path}")
def test_strip_trailing_slash(self, app: SamsiteFlask, image_fetcher: ImageFetcher) -> None: assert image_fetcher.get_internal_path( f"{self.path}/") == f"{app.STATIC_DIR}/{self.path}"
def test_append_store_path(self, app: SamsiteFlask, image_fetcher: ImageFetcher) -> None: assert image_fetcher.get_internal_path( self.path) == f"{app.STATIC_DIR}/{self.path}"
def test_invalid_path(self, image_fetcher: ImageFetcher) -> None: with pytest.raises(InvalidPathError) as error: image_fetcher.fetch_image(self.path) assert error.value.message == f"Invalid image path: '{self.path}'"
def _image_fetcher() -> ImageFetcher: image_fetcher = ImageFetcher() return image_fetcher