Esempio n. 1
0
    def test_get_internal_path_call(self, image_fetcher: ImageFetcher) -> None:
        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)

        image_fetcher.get_internal_path.assert_called_once_with(path=self.path)
Esempio n. 2
0
    def test_invalid_resolution(self, image_fetcher: ImageFetcher) -> None:
        resolution = "test"

        with pytest.raises(InvalidResolutionError) as error:
            image_fetcher.fetch_image(self.path, resolution)

        assert error.value.message == f"Invalid resolution: '{resolution}'"
Esempio n. 3
0
    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,
        )
Esempio n. 4
0
    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)
Esempio n. 5
0
    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
Esempio n. 6
0
    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
Esempio n. 7
0
    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
Esempio n. 8
0
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])
Esempio n. 9
0
    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}'"