import os from pathlib import Path import pytest from hircine.thumbnailer import Thumbnailer, ThumbnailParameters from PIL import Image mock_params = ThumbnailParameters(bounds=(1440, 2880), options={}) def test_thumbnailer_object(): thumb = Thumbnailer("objects/", params={}) assert thumb.object("abcdef", "foo") == os.path.join("objects/", "ab/cdef_foo.webp") @pytest.mark.parametrize( "extension, can_process", [ (".png", True), (".jpeg", True), (".jpg", True), (".gif", True), (".bmp", True), (".json", False), (".txt", False), ], ids=["png", "jpeg", "jpg", "gif", "bmp", "json", "txt"], ) def test_thumbnailer_can_process(extension, can_process): assert Thumbnailer.can_process(extension) == can_process def test_thumbnailer_process(data): thumb = Thumbnailer(data("objects/"), params={"mock": mock_params}) with open(data("example_rgb.png"), "rb") as f: size = Image.open(f, mode="r").size reported_size = thumb.process(f, "abcdef") assert reported_size == size output = thumb.object("abcdef", "mock") assert os.path.exists(output) def test_thumbnailer_converts_non_rgb(data): thumb = Thumbnailer(data("objects/"), params={"mock": mock_params}) with open(data("example_palette.png"), "rb") as f: size = Image.open(f, mode="r").size reported_size = thumb.process(f, "abcdef") assert reported_size == size output = thumb.object("abcdef", "mock") assert os.path.exists(output) output_image = Image.open(output) assert output_image.mode == "RGB" def test_thumbnailer_process_ignores_existing(data): thumb = Thumbnailer(data("objects/"), params={"mock": mock_params}) output = Path(thumb.object("abcdef", "mock")) os.makedirs(os.path.dirname(output)) output.touch() with open(data("example_palette.png"), "rb") as f: thumb.process(f, "abcdef") assert output.stat().st_size == 0