From d1d654ebac2d51e3841675faeb56480e440f622f Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Tue, 5 Mar 2024 18:08:09 +0100 Subject: Initial commit --- tests/thumbnailer/data/example_palette.png | Bin 0 -> 703 bytes tests/thumbnailer/data/example_rgb.png | Bin 0 -> 14362 bytes tests/thumbnailer/test_thumbnailer.py | 74 +++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 tests/thumbnailer/data/example_palette.png create mode 100644 tests/thumbnailer/data/example_rgb.png create mode 100644 tests/thumbnailer/test_thumbnailer.py (limited to 'tests/thumbnailer') diff --git a/tests/thumbnailer/data/example_palette.png b/tests/thumbnailer/data/example_palette.png new file mode 100644 index 0000000..6bf25e1 Binary files /dev/null and b/tests/thumbnailer/data/example_palette.png differ diff --git a/tests/thumbnailer/data/example_rgb.png b/tests/thumbnailer/data/example_rgb.png new file mode 100644 index 0000000..a245642 Binary files /dev/null and b/tests/thumbnailer/data/example_rgb.png differ diff --git a/tests/thumbnailer/test_thumbnailer.py b/tests/thumbnailer/test_thumbnailer.py new file mode 100644 index 0000000..62bf127 --- /dev/null +++ b/tests/thumbnailer/test_thumbnailer.py @@ -0,0 +1,74 @@ +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 -- cgit v1.2.3-2-gb3c3