-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest_illustration.py
More file actions
69 lines (56 loc) · 2.48 KB
/
test_illustration.py
File metadata and controls
69 lines (56 loc) · 2.48 KB
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
from pathlib import Path
import pytest
from PIL.Image import open as pilopen
from zimscraperlib.image.illustration import get_zim_illustration
COMMONS_IMAGE_PATH = (Path(__file__) / "../../files/commons.png").resolve()
COMMONS_48_IMAGE_PATH = (Path(__file__) / "../../files/commons48.png").resolve()
NINJA_IMAGE_PATH = (Path(__file__) / "../../files/ninja.webp").resolve()
@pytest.mark.parametrize(
"user_illustration, expected_max_filesize",
[
pytest.param(COMMONS_IMAGE_PATH, 5000, id="big_commons"),
pytest.param(COMMONS_48_IMAGE_PATH, 4000, id="small_commons"),
pytest.param(NINJA_IMAGE_PATH, 5000, id="ninja"),
pytest.param(
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Commons-logo.svg/250px-Commons-logo.svg.png",
4000,
id="png_url",
),
pytest.param(
"https://upload.wikimedia.org/wikipedia/commons/4/4a/Commons-logo.svg",
4000,
id="svg_url",
),
],
)
def test_get_zim_illustration(
user_illustration: str | Path,
expected_max_filesize: int,
):
image = get_zim_illustration(user_illustration)
assert len(image.getvalue()) < expected_max_filesize
with pilopen(image) as image_details:
assert image_details.format == "PNG"
assert image_details.size == (48, 48)
def test_get_missing_user_zim_illustration():
with pytest.raises(Exception, match="missing.png could not be found"):
get_zim_illustration("./missing.png")
def test_get_missing_default_zim_illustration():
with pytest.raises(Exception, match="Illustration is missing"):
get_zim_illustration("")
def test_get_zim_illustration_custom_size():
image = get_zim_illustration(NINJA_IMAGE_PATH, 96, 120)
assert len(image.getvalue()) < 21000
with pilopen(image) as image_details:
assert image_details.format == "PNG"
assert image_details.size == (96, 120)
def test_get_zim_illustration_method():
image_cover = get_zim_illustration(NINJA_IMAGE_PATH, resize_method="cover")
image_contain = get_zim_illustration(NINJA_IMAGE_PATH, resize_method="contain")
# cover image is always bigger than contain image size more pixels are
# "used/non-transparent"
assert len(image_cover.getvalue()) > len(image_contain.getvalue())
for image in [image_cover, image_contain]:
with pilopen(image) as image_details:
assert image_details.format == "PNG"
assert image_details.size == (48, 48)