|
1
|
|
|
"""Tests for image generation.""" |
|
2
|
|
|
|
|
3
|
|
|
# pylint: disable=bad-continuation |
|
4
|
|
|
|
|
5
|
|
|
from pathlib import Path |
|
6
|
|
|
|
|
7
|
|
|
import pytest |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
from memegen.settings import get_config |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
SAMPLES = Path(__file__).parent.joinpath("examples") |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def unset(name): |
|
16
|
|
|
config = get_config('test') |
|
17
|
|
|
if getattr(config, name): |
|
18
|
|
|
return dict(condition=False, reason="") |
|
19
|
|
|
else: |
|
20
|
|
|
return dict(condition=True, reason="{} unset".format(name)) |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def save_image(client, url, name): |
|
24
|
|
|
response = client.get(url) |
|
25
|
|
|
data = response.get_data() |
|
26
|
|
|
|
|
27
|
|
|
with SAMPLES.joinpath(name).open('wb') as image: |
|
28
|
|
|
image.write(data) |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
@pytest.mark.skipif(**unset('REGENERATE_IMAGES')) |
|
32
|
|
|
def test_text_wrapping(client): |
|
33
|
|
|
"""Create various example images for manual verification.""" |
|
34
|
|
|
for name, url in [ |
|
35
|
|
|
("text-short.jpg", "/ch/hello/world.jpg"), |
|
36
|
|
|
("text-short-on-tall.jpg", "/drake/tabs/spaces.jpg"), |
|
37
|
|
|
("text-nominal.jpg", "/ch/a_normal_line_of_top_meme_text_followed_by/" |
|
38
|
|
|
"another_normal_line_of_bottom_meme_text.jpg"), |
|
39
|
|
|
("text-long.jpg", "/ch/" + ("long_" * 15) + "line/short_line.jpg"), |
|
40
|
|
|
("text-subscripts.jpg", "/ch/some_unicode_subscripts/h%E2%82%82o.jpg"), |
|
41
|
|
|
]: |
|
42
|
|
|
save_image(client, url + "?watermark=none", name) |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
@pytest.mark.skipif(**unset('REGENERATE_IMAGES')) |
|
46
|
|
|
def test_standard_font(client): |
|
47
|
|
|
"""Create a meme using the standard meme font. |
|
48
|
|
|
|
|
49
|
|
|
See: https://github.com/jacebrowning/memegen/issues/216 |
|
50
|
|
|
|
|
51
|
|
|
""" |
|
52
|
|
|
url = "/ch/we_like_using_the/custom_fonts.jpg?font=impact&watermark=none" |
|
53
|
|
|
save_image(client, url, "font-impact.jpg") |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
@pytest.mark.skipif(**unset('REGENERATE_IMAGES')) |
|
57
|
|
|
def test_japanese_font(client): |
|
58
|
|
|
"""Create a meme using a font that supports Japanese characters.""" |
|
59
|
|
|
url = "/ch/turning/日本語.jpg?font=notosanscjkjp-black&watermark=none" |
|
60
|
|
|
save_image(client, url, "font-notosans.jpg") |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
@pytest.mark.skipif(**unset('REGENERATE_IMAGES')) |
|
64
|
|
|
def test_custom_sizes(client): |
|
65
|
|
|
"""Create memes using custom sizes.""" |
|
66
|
|
|
for name, url in [ |
|
67
|
|
|
("size-width.jpg", "/fry/hello/world.jpg?width=200"), |
|
68
|
|
|
("size-height.jpg", "/fry/hello/world.jpg?height=300"), |
|
69
|
|
|
("size-both.jpg", "/fry/hello/world.jpg?width=200&height=300"), |
|
70
|
|
|
("size-large.jpg", "/fry/hello/world.jpg?height=1000"), |
|
71
|
|
|
("size-width-huge.jpg", "/fry/hello/world.jpg?width=6000"), |
|
72
|
|
|
("size-height-huge.jpg", "/fry/hello/world.jpg?height=8000"), |
|
73
|
|
|
("size-both-huge.jpg", "/fry/hello/world.jpg?height=8000&width=6000"), |
|
74
|
|
|
]: |
|
75
|
|
|
save_image(client, url + "&watermark=none", name) |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
@pytest.mark.skipif(**unset('REGENERATE_IMAGES')) |
|
79
|
|
|
def test_custom_watermark(client): |
|
80
|
|
|
"""Create meme with a watermark.""" |
|
81
|
|
|
for name, partial in [ |
|
82
|
|
|
("watermark.jpg", "/fry/hello/world.jpg?"), |
|
83
|
|
|
("watermark-pad-h.jpg", "/fry/hello/world.jpg?width=300&height=200&"), |
|
84
|
|
|
("watermark-pad-v.jpg", "/fry/hello/world.jpg?width=200&height=300&"), |
|
85
|
|
|
]: |
|
86
|
|
|
save_image(client, partial + "watermark=memegen.test", name) |
|
87
|
|
|
|