-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
346 lines (309 loc) · 9.68 KB
/
tests.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# builtin modules
import os
import os.path
from pathlib import Path
import shutil
import subprocess
import tempfile
import uuid
from xml.dom import minidom
# third-party modules
import numpy
from PIL import Image
import magic
import pytest
import imagesize
from slides35 import (
Slide,
do_slide,
SLIDES35_DEFAULT_SVG_TEMPLATE,
SLIDES35_DEFAULT_OUTPUT_DPI,
SLIDES35_SUPPORTED_CONVERTERS
)
DEFAULT_SLIDE_TEMPLATE = SLIDES35_DEFAULT_SVG_TEMPLATE
DEFAULT_NON_EXISTING_TEMPLATE = "missingTemplate.svg"
DEFAULT_PICTURE = str(Path("./templates/24x36mmImage.png"))
DEFAULT_NON_EXISTING_PICTURE = "missingPicture.png"
DEFAULT_COMMENT = "Tour simple"
EXECUTABLE_UNDER_TEST = "slides35.py"
def _normalize_xml(xml_string):
return minidom.parseString(xml_string).toxml()
def test_methods_chaining():
s = (
Slide()
.template(DEFAULT_SLIDE_TEMPLATE)
.id(1)
.comment(DEFAULT_COMMENT)
.picture(DEFAULT_PICTURE)
)
assert isinstance(s, Slide)
def test_xml_validity():
s = (
Slide()
.template(DEFAULT_SLIDE_TEMPLATE)
.id(1)
.comment(DEFAULT_COMMENT)
.picture(DEFAULT_PICTURE)
)
minidom.parseString(s.svg())
def test_template_setters():
assert Slide(DEFAULT_SLIDE_TEMPLATE) == Slide().template(DEFAULT_SLIDE_TEMPLATE)
def test_template_lookup_on_svg_output():
with pytest.raises(FileNotFoundError):
Slide(DEFAULT_NON_EXISTING_TEMPLATE)
assert isinstance(
Slide(DEFAULT_SLIDE_TEMPLATE).id(1).picture(DEFAULT_PICTURE).svg(), str
)
def test_picture_setter_getter():
assert (
Path(Slide().picture(DEFAULT_PICTURE).picture()).resolve()
== Path(DEFAULT_PICTURE).resolve()
)
with pytest.raises(FileNotFoundError):
Slide().picture(DEFAULT_NON_EXISTING_PICTURE)
@pytest.mark.parametrize("val", [True, False])
def test_verbose_setter_getter(val):
s = Slide().verbose(val)
assert s.verbose() == val
def test_verbose_setter_typing():
with pytest.raises(TypeError):
Slide().verbose("non boolean value")
@pytest.mark.parametrize("converter", SLIDES35_SUPPORTED_CONVERTERS)
def test_converter_setter_getter(converter):
s = Slide().converter(converter)
assert s.converter() == converter
def test_converter_setter_typing():
with pytest.raises(ValueError):
Slide().converter("someUnsupportedConverter")
with pytest.raises(TypeError):
Slide().converter([1, 2])
@pytest.mark.parametrize("converter", SLIDES35_SUPPORTED_CONVERTERS)
def test_converters_png_output(converter):
random_png_filename = str(uuid.uuid4()) + ".png"
result = subprocess.run(
[
"python",
EXECUTABLE_UNDER_TEST,
"--id",
"1",
"--picture",
DEFAULT_PICTURE,
"--output",
random_png_filename,
"--converter",
converter
]
)
assert result.returncode == 0
assert Path(random_png_filename).resolve().is_file()
assert os.path.getsize(random_png_filename) > 1000
assert magic.Magic(mime=True, uncompress=True).from_file(random_png_filename).endswith("png")
os.unlink(random_png_filename)
def test_command_stdout_svg_output():
command_output = subprocess.check_output(
[
"python",
EXECUTABLE_UNDER_TEST,
"--id",
"1",
"--picture",
DEFAULT_PICTURE,
"--stdout",
]
)
assert _normalize_xml(command_output) == _normalize_xml(
Slide(DEFAULT_SLIDE_TEMPLATE).id(1).picture(DEFAULT_PICTURE).svg()
)
output_png = Path(tempfile.gettempdir()) / Path("something.png")
result = subprocess.run(
[
"python",
EXECUTABLE_UNDER_TEST,
"--id",
"1",
"--picture",
DEFAULT_PICTURE,
"--output",
output_png,
"--stdout",
],
capture_output=True,
)
assert result.returncode != 0
assert "--stdout" in str(result.stdout)
def test_command_file_svg_output():
output_svg_path = "test1.svg"
subprocess.run(
[
"python",
EXECUTABLE_UNDER_TEST,
"--id",
"1",
"--picture",
DEFAULT_PICTURE,
"--output",
output_svg_path,
]
)
with open(output_svg_path) as f:
output_svg_string = f.read()
assert _normalize_xml(output_svg_string) == _normalize_xml(
Slide(DEFAULT_SLIDE_TEMPLATE).id(1).picture(DEFAULT_PICTURE).svg()
)
os.unlink(output_svg_path) # tear down
@pytest.mark.xfail("'GITHUB_JOB' in os.environ")
@pytest.mark.parametrize("dpi", [300, 400])
def test_command_file_png_output(dpi):
converter = "convert"
output_png_path = "test1.png"
output_must_not_exist_svg_path = "test1.svg"
assert (
dpi != SLIDES35_DEFAULT_OUTPUT_DPI
) # avoid false positive because of same value as default
subprocess.run(
[
"python",
EXECUTABLE_UNDER_TEST,
"--id",
"1",
"--picture",
DEFAULT_PICTURE,
"--output",
output_png_path,
"--dpi",
str(dpi),
]
)
assert False == Path(output_must_not_exist_svg_path).exists()
assert Path(output_png_path).exists()
svg_w, svg_h = imagesize.get(DEFAULT_SLIDE_TEMPLATE)
w, h = imagesize.get(output_png_path)
ratio = w / h
svg_ratio = w / h
assert ratio == svg_ratio
assert w >= svg_w
assert h >= svg_h
if converter == 'rsvg-convert':
if imagesize.getDPI(output_png_path)[0] != dpi:
os.unlink(output_png_path)
pytest.xfail("rsvg-convert does not store DPI metadata to PNG")
elif converter == 'convert' and 'GITHUB_JOB' in os.environ:
os.unlink(output_png_path)
pytest.xfail("convert fails setting DPI on recent versions")
else:
assert imagesize.getDPI(output_png_path)[0] == dpi
os.unlink(output_png_path)
def test_command_file_png_output_dir():
output_dir = Path(tempfile.gettempdir()) / Path("any") / Path("thing")
output_png_filename = "test2.png"
output_must_not_exist_svg_path = "test2.svg"
output_png_path = output_dir / Path(output_png_filename)
subprocess.run(
[
"python",
EXECUTABLE_UNDER_TEST,
"--id",
"1",
"--picture",
DEFAULT_PICTURE,
"--output",
output_png_filename,
"--output-dir",
output_dir,
]
)
assert False == Path(output_must_not_exist_svg_path).exists()
assert Path(output_png_path).exists()
svg_w, svg_h = imagesize.get(DEFAULT_SLIDE_TEMPLATE)
w, h = imagesize.get(output_png_path)
ratio = w / h
svg_ratio = w / h
assert ratio == svg_ratio
assert w >= svg_w
assert h >= svg_h
shutil.rmtree(output_dir.parent)
@pytest.mark.parametrize("dpi", [320, 420])
@pytest.mark.parametrize("converter", SLIDES35_SUPPORTED_CONVERTERS)
def test_command_file_png_output_default_density(dpi, converter):
output_png_path = "test1.png"
assert dpi != SLIDES35_DEFAULT_OUTPUT_DPI
subprocess.run(
[
"python",
EXECUTABLE_UNDER_TEST,
"--id",
"1",
"--picture",
DEFAULT_PICTURE,
"--output",
output_png_path,
"--dpi",
str(dpi),
"--converter",
converter
]
)
if converter == 'rsvg-convert':
if imagesize.getDPI(output_png_path)[0] != dpi:
os.unlink(output_png_path)
pytest.xfail("rsvg-convert does not store DPI metadata to PNG")
elif converter == 'convert' and 'GITHUB_JOB' in os.environ:
os.unlink(output_png_path)
pytest.xfail("convert fails setting DPI on recent versions")
else:
assert imagesize.getDPI(output_png_path)[0] == dpi
os.unlink(output_png_path)
subprocess.run(
[
"python",
EXECUTABLE_UNDER_TEST,
"--id",
"1",
"--picture",
DEFAULT_PICTURE,
"--output",
output_png_path,
"--converter",
converter
]
)
assert imagesize.getDPI(output_png_path)[0] == SLIDES35_DEFAULT_OUTPUT_DPI
os.unlink(output_png_path)
def test_command_pictures_dir_not_exists():
result = subprocess.run(
[
"python",
EXECUTABLE_UNDER_TEST,
"--id",
"1",
"--pictures-dir",
"non-existing-dir",
],
capture_output=True,
)
assert result.returncode != 0
assert "not exist" in str(result.stdout)
def test_command_pictures_impossible_with_stdout():
with tempfile.TemporaryDirectory() as tmpdirname:
for n in range(10):
a = numpy.random.rand(30, 30, 3) * 255
im_out = Image.fromarray(a.astype("uint8")).convert("RGB")
im_out.save(Path(tmpdirname) / Path("out%000d.jpg" % n))
result = subprocess.run(
[
"python",
EXECUTABLE_UNDER_TEST,
"--id",
"1",
"--pictures-dir",
tmpdirname,
"--stdout",
],
capture_output=True,
)
assert result.returncode == 1
assert "--pictures-dir cannot be used with --stdout." in str(result.stdout)
def test_do_slide_wrong_output_as_value():
with pytest.raises(ValueError) as e:
do_slide(DEFAULT_SLIDE_TEMPLATE, DEFAULT_PICTURE, 1, output_as="bad_extension")
assert "output_as parameter must be" in e.value.args[0]