-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
executable file
·269 lines (185 loc) · 7.46 KB
/
test.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env pytest
import hashlib
import os
from pathlib import Path
import shutil
import vignette
from pytest import fixture, skip
ALL_THUMBNAIL = vignette.THUMBNAILER_BACKENDS
IMAGE_THUMBNAIL = [vignette.QtBackend(), vignette.PilBackend(), vignette.MagickBackend()]
IMAGE_THUMBNAIL = [b for b in IMAGE_THUMBNAIL if b.is_available()]
ALL_METADATA = vignette.METADATA_BACKENDS
AVAIL_METADATA = [b for b in ALL_METADATA if b.is_available()]
SAMPLES_PATH = Path(__file__).parent / "samples"
@fixture()
def workdir(tmp_path):
os.environ["XDG_CACHE_HOME"] = str(tmp_path)
yield tmp_path
@fixture()
def image_src(workdir):
shutil.copyfile(SAMPLES_PATH / "checkerboard.png", workdir / "test.png")
src = str(workdir / "test.png")
yield src
def get_backend_name(backend):
return type(backend).__name__
@fixture(params=ALL_METADATA, ids=get_backend_name)
def metadata_backend(request):
if not request.param.is_available():
skip(f"{request.param} is not available")
vignette.METADATA_BACKENDS = [request.param]
vignette.THUMBNAILER_BACKENDS = [request.param]
yield
vignette.METADATA_BACKENDS = ALL_METADATA
vignette.THUMBNAILER_BACKENDS = ALL_THUMBNAIL
def require_pil():
try:
import PIL.Image
except ImportError:
skip("the test requires PIL for checking correctness")
def get_metadata(path):
import PIL.Image
img = PIL.Image.open(path)
# PIL might not load all metadata without load()
# in particular, images saved with the magickbackend
img.load()
return img.info
def test_at_least_one_backend():
assert any(backend.is_available() for backend in ALL_METADATA)
def test_hash(image_src, workdir, metadata_backend):
uri = f"file://{image_src}"
hash = hashlib.md5(uri.encode("utf-8")).hexdigest()
dest = workdir / "thumbnails" / "large" / f"{hash}.png"
assert str(dest) == vignette.build_thumbnail_path(image_src, "large")
def test_metadata(image_src, metadata_backend):
require_pil()
dest = vignette.create_thumbnail(image_src, "large")
assert dest
info = get_metadata(dest)
assert info["Thumb::URI"] == f"file://{image_src}"
assert str(int(os.path.getmtime(image_src))) == info["Thumb::MTime"]
assert "512" == info["Thumb::Image::Width"]
assert "512" == info["Thumb::Image::Height"]
assert str(os.path.getsize(image_src)) == info["Thumb::Size"]
def test_dont_expand(workdir, metadata_backend):
require_pil()
import PIL.Image
src_path = SAMPLES_PATH / "rose.jpg"
shutil.copy(src_path, workdir)
thumb_path = vignette.get_thumbnail(str(src_path))
thumb_img = PIL.Image.open(thumb_path)
assert thumb_img.size == (70, 46)
def test_shrink(image_src, metadata_backend):
require_pil()
import PIL.Image
thumb_path = vignette.get_thumbnail(str(image_src))
thumb_img = PIL.Image.open(thumb_path)
assert thumb_img.size == (256, 256)
thumb_path = vignette.get_thumbnail(str(image_src), size="normal")
thumb_img = PIL.Image.open(thumb_path)
assert thumb_img.size == (128, 128)
thumb_path = vignette.get_thumbnail(str(image_src), size="x-large")
thumb_img = PIL.Image.open(thumb_path)
assert thumb_img.size == (512, 512)
thumb_path = vignette.get_thumbnail(str(image_src), size="xx-large")
thumb_img = PIL.Image.open(thumb_path)
assert thumb_img.size == (512, 512)
def test_basic(image_src, metadata_backend):
dest = vignette.build_thumbnail_path(image_src, "large")
assert dest
assert vignette.try_get_thumbnail(image_src, "large") is None
assert not os.path.exists(dest)
assert dest == vignette.get_thumbnail(image_src, "large")
assert os.path.isfile(dest)
assert dest == vignette.try_get_thumbnail(image_src, "large")
def test_reuse_thumbnail(image_src, metadata_backend):
dest = vignette.get_thumbnail(image_src, "large")
assert dest
st = os.stat(dest)
assert dest == vignette.get_thumbnail(image_src, "large")
assert st == os.stat(dest)
assert dest == vignette.create_thumbnail(image_src, "large")
assert st != os.stat(dest)
def test_direct_thumbnail(image_src, metadata_backend):
dest = vignette.get_thumbnail(image_src, "large")
assert dest
assert os.path.isfile(dest)
assert dest == vignette.try_get_thumbnail(dest, "large")
def test_mtime_validity(image_src, metadata_backend):
dest = vignette.get_thumbnail(image_src, "large")
assert dest
os.utime(image_src, (0, 0))
assert vignette.try_get_thumbnail(image_src, "large") is None
assert os.path.isfile(dest)
assert dest == vignette.get_thumbnail(image_src, "large")
assert dest == vignette.try_get_thumbnail(image_src, "large")
def test_multisize(image_src, metadata_backend):
dest = vignette.get_thumbnail(image_src, "large")
assert dest
assert dest == vignette.try_get_thumbnail(image_src, "large")
assert vignette.try_get_thumbnail(image_src, "normal") is None
assert dest == vignette.try_get_thumbnail(image_src)
os.remove(dest)
dest = vignette.get_thumbnail(image_src, "normal")
assert dest
assert dest == vignette.try_get_thumbnail(image_src, "normal")
assert vignette.try_get_thumbnail(image_src, "large") is None
assert dest == vignette.try_get_thumbnail(image_src)
def test_fail(workdir, metadata_backend):
src = workdir / "empty"
src.touch()
src = str(src)
assert vignette.get_thumbnail(src, "large") is None
assert not (workdir / "thumbnails" / "fail").exists()
assert not vignette.is_thumbnail_failed(src, "foo")
assert vignette.get_thumbnail(src, "large", use_fail_appname="foo") is None
assert (workdir / "thumbnails" / "fail" / "foo").exists()
assert vignette.is_thumbnail_failed(src, "foo")
assert not vignette.is_thumbnail_failed(src, "bar")
def test_fail_mtime_validity(workdir, metadata_backend):
src = workdir / "empty"
src.touch()
src = str(src)
assert vignette.get_thumbnail(src, "large", use_fail_appname="foo") is None
assert vignette.is_thumbnail_failed(src, "foo")
os.utime(src, (0, 0))
assert not vignette.is_thumbnail_failed(src, "foo")
def test_put_thumbnail(image_src, metadata_backend):
uri = "https://example.com"
tmp = vignette.create_temp("large")
shutil.copyfile(image_src, tmp)
vignette.put_thumbnail(uri, "large", tmp, mtime=42)
assert vignette.try_get_thumbnail(uri, "large", mtime=42)
assert vignette.try_get_thumbnail(uri, "large", mtime=1) is None
def test_put_fail(image_src, metadata_backend):
# force a "failure"
vignette.put_fail(image_src, "foo")
assert vignette.is_thumbnail_failed(image_src, "foo")
assert not vignette.is_thumbnail_failed(image_src, "bar")
# check the failure is found instead of creating a thumbnail
assert vignette.get_thumbnail(image_src, use_fail_appname="foo") is None
# create the thumbnail
dest = vignette.get_thumbnail(image_src, use_fail_appname="bar")
assert dest
# check the failure is now ignored
assert dest == vignette.get_thumbnail(image_src, use_fail_appname="foo")
def test_orientation(workdir, metadata_backend):
require_pil()
import PIL.Image
correct_src = SAMPLES_PATH / "rose.jpg"
oriented_src = SAMPLES_PATH / "rose-oriented.jpg"
shutil.copy(correct_src, workdir)
shutil.copy(oriented_src, workdir)
correct_dst = vignette.get_thumbnail(str(correct_src))
correct_img = PIL.Image.open(correct_dst)
oriented_dst = vignette.get_thumbnail(str(oriented_src))
oriented_img = PIL.Image.open(oriented_dst)
assert correct_img.size == oriented_img.size
def test_moreinfo(image_src, metadata_backend):
require_pil()
moreinfo = {"foo": "bar", "Thumb::URI": "bad", "Thumb::MTime": "bad"}
dest = vignette.create_thumbnail(image_src, "large", moreinfo=moreinfo)
assert dest
info = get_metadata(dest)
assert info["foo"] == "bar"
assert info["Thumb::URI"] != "bad"
assert info["Thumb::MTime"] != "bad"