summaryrefslogtreecommitdiffstatshomepage
path: root/tests/thumbnailer/test_thumbnailer.py
blob: 3a3405ad5e3ae683cd18a67938c7e53de18a6732 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
from pathlib import Path

import pytest
from PIL import Image

from hircine.thumbnailer import Thumbnailer, ThumbnailParameters

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