184 lines
5.2 KiB
Python
184 lines
5.2 KiB
Python
import tempfile
|
|
from pathlib import Path
|
|
from typing import List
|
|
|
|
import pytest
|
|
from fonts.ttf import SourceSansPro
|
|
from PIL import Image, ImageChops, ImageFont
|
|
|
|
from ucast import tests
|
|
from ucast.service import cover, typ
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"height,width,text,expect",
|
|
[
|
|
(40, 300, "Hello", ["Hello"]),
|
|
(40, 300, "Hello World, this is me", ["Hello World,…"]),
|
|
(90, 300, "Hello World, this is me", ["Hello World, this", "is me"]),
|
|
(
|
|
90,
|
|
300,
|
|
"Rindfleischettikettierungsüberwachungsaufgabenübertragungsgesetz",
|
|
["Rindfleischettik…"],
|
|
),
|
|
(
|
|
1000,
|
|
300,
|
|
"Ha! du wärst Obrigkeit von Gott? Gott spendet Segen aus; du raubst! Du nicht von Gott, Tyrann!",
|
|
[
|
|
"Ha! du wärst",
|
|
"Obrigkeit von",
|
|
"Gott? Gott",
|
|
"spendet Segen",
|
|
"aus; du raubst!",
|
|
"Du nicht von Gott,",
|
|
"Tyrann!",
|
|
],
|
|
),
|
|
],
|
|
)
|
|
def test_split_text(height: int, width: int, text: str, expect: List[str]):
|
|
font = ImageFont.truetype(SourceSansPro, 40)
|
|
lines = cover._split_text(height, width, text, font, 8)
|
|
assert lines == expect
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"file_name,color",
|
|
[
|
|
("t1.webp", (63, 63, 62)),
|
|
("t2.webp", (22, 20, 20)),
|
|
("t3.webp", (54, 24, 28)),
|
|
],
|
|
)
|
|
def test_get_dominant_color(file_name: str, color: typ.Color):
|
|
img = Image.open(tests.DIR_TESTFILES / "thumbnail" / file_name)
|
|
c = cover._get_dominant_color(img)
|
|
assert c == color
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"bg_color,text_color",
|
|
[
|
|
((100, 0, 0), (255, 255, 255)),
|
|
((200, 200, 0), (0, 0, 0)),
|
|
],
|
|
)
|
|
def test_get_text_color(bg_color: typ.Color, text_color: typ.Color):
|
|
c = cover._get_text_color(bg_color)
|
|
assert c == text_color
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"n_image,title,channel,style",
|
|
[
|
|
(1, "ThetaDev @ Embedded World 2019", "ThetaDev", cover.COVER_STYLE_GRADIENT),
|
|
(1, "ThetaDev @ Embedded World 2019", "ThetaDev", cover.COVER_STYLE_BLUR),
|
|
(
|
|
2,
|
|
"Sintel - Open Movie by Blender Foundation",
|
|
"Blender",
|
|
cover.COVER_STYLE_GRADIENT,
|
|
),
|
|
(
|
|
2,
|
|
"Sintel - Open Movie by Blender Foundation",
|
|
"Blender",
|
|
cover.COVER_STYLE_BLUR,
|
|
),
|
|
(
|
|
3,
|
|
"Systemabsturz Teaser zur DiVOC bb3",
|
|
"media.ccc.de",
|
|
cover.COVER_STYLE_GRADIENT,
|
|
),
|
|
(
|
|
3,
|
|
"Systemabsturz Teaser zur DiVOC bb3",
|
|
"media.ccc.de",
|
|
cover.COVER_STYLE_BLUR,
|
|
),
|
|
],
|
|
)
|
|
def test_create_cover_image(
|
|
n_image: int, title: str, channel: str, style: cover.CoverStyle
|
|
):
|
|
tn_file = tests.DIR_TESTFILES / "thumbnail" / f"t{n_image}.webp"
|
|
av_file = tests.DIR_TESTFILES / "avatar" / f"a{n_image}.jpg"
|
|
expected_cv_file = tests.DIR_TESTFILES / "cover" / f"c{n_image}_{style}.png"
|
|
|
|
tn_image = Image.open(tn_file)
|
|
av_image = Image.open(av_file)
|
|
expected_cv_image = Image.open(expected_cv_file)
|
|
|
|
cv_image = cover._create_cover_image(tn_image, av_image, title, channel, style)
|
|
|
|
assert cv_image.width == cover.COVER_WIDTH
|
|
assert cv_image.height == cover.COVER_WIDTH
|
|
|
|
diff = ImageChops.difference(cv_image, expected_cv_image)
|
|
assert diff.getbbox() is None
|
|
|
|
|
|
def test_create_cover_image_noavatar():
|
|
tn_file = tests.DIR_TESTFILES / "thumbnail" / "t1.webp"
|
|
expected_cv_file = tests.DIR_TESTFILES / "cover" / "c1_noavatar.png"
|
|
|
|
tn_image = Image.open(tn_file)
|
|
expected_cv_image = Image.open(expected_cv_file)
|
|
|
|
cv_image = cover._create_cover_image(
|
|
tn_image,
|
|
None,
|
|
"ThetaDev @ Embedded World 2019",
|
|
"ThetaDev",
|
|
cover.COVER_STYLE_GRADIENT,
|
|
)
|
|
|
|
assert cv_image.width == cover.COVER_WIDTH
|
|
assert cv_image.height == cover.COVER_WIDTH
|
|
|
|
diff = ImageChops.difference(cv_image, expected_cv_image)
|
|
assert diff.getbbox() is None
|
|
|
|
|
|
def test_create_blank_cover_image():
|
|
av_file = tests.DIR_TESTFILES / "avatar" / "a1.jpg"
|
|
expected_cv_file = tests.DIR_TESTFILES / "cover" / "blank.png"
|
|
|
|
av_image = Image.open(av_file)
|
|
expected_cv_image = Image.open(expected_cv_file)
|
|
|
|
cv_image = cover._create_blank_cover_image(av_image, "missingno", "ThetaDev")
|
|
|
|
assert cv_image.width == cover.COVER_WIDTH
|
|
assert cv_image.height == cover.COVER_WIDTH
|
|
|
|
diff = ImageChops.difference(cv_image, expected_cv_image)
|
|
assert diff.getbbox() is None
|
|
|
|
|
|
def test_create_cover_file():
|
|
tn_file = tests.DIR_TESTFILES / "thumbnail" / "t1.webp"
|
|
av_file = tests.DIR_TESTFILES / "avatar" / "a1.jpg"
|
|
expected_cv_file = tests.DIR_TESTFILES / "cover" / "c1_gradient.png"
|
|
|
|
tmpdir_o = tempfile.TemporaryDirectory()
|
|
tmpdir = Path(tmpdir_o.name)
|
|
cv_file = tmpdir / "cover.png"
|
|
|
|
cover.create_cover_file(
|
|
tn_file,
|
|
av_file,
|
|
"ThetaDev @ Embedded World 2019",
|
|
"ThetaDev",
|
|
"gradient",
|
|
cv_file,
|
|
)
|
|
|
|
cv_image = Image.open(cv_file)
|
|
expected_cv_image = Image.open(expected_cv_file)
|
|
|
|
diff = ImageChops.difference(cv_image, expected_cv_image)
|
|
assert diff.getbbox() is None
|