-
Notifications
You must be signed in to change notification settings - Fork 45
/
test_fractal.py
executable file
·123 lines (102 loc) · 3.59 KB
/
test_fractal.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
#!/usr/bin/env py.test
# -*- coding: utf-8 -*-
# Created on Fri Apr 25 02:33:04 2014
# License is MIT, see COPYING.txt for more details.
# @author: Danilo de Jesus da Silva Bellini
import os, re, pytest, pylab
from fractal import generate_fractal, call_kw, cli_parse_args, img2output
from io import BytesIO
from pylab import imread, imsave, array
p = pytest.mark.parametrize
#
# Helper functions
#
def show_parameters(fname):
""" String with CLI args to show the fractal with the given ``fname`` """
re_complex = re.compile("(?:([+-]?\s*[0-9.]+))?\s*"
"(?:([+-]\s*[0-9.]+)\s*)?(.*)")
def part_generator():
for part in fname.rsplit(".", 1)[0].split("_"):
if "=" in part:
yield "--" + part
else:
yield " ".join(filter(lambda x: x, re_complex.match(part).groups()))
yield "--show"
return " ".join(part_generator())
def to_dict_params(fname):
""" Get full kwargs from file name """
return cli_parse_args(show_parameters(fname).split())
def almost_equal_diff(a, b, max_diff = 2e-2):
return abs(a - b) < max_diff
#
# Tests!
#
@p("fname", os.listdir("images"))
def test_file_image(fname):
ext = os.path.splitext(fname)[-1][len(os.path.extsep):]
kwargs = to_dict_params(fname)
# Creates the image in memory
mem = BytesIO()
fractal_data = call_kw(generate_fractal, kwargs)
imsave(mem, fractal_data, cmap=kwargs["cmap"], format=ext)
mem.seek(0) # Return stream position back for reading
# Comparison pixel-by-pixel
img_file = imread("images/" + fname)
img_mem = imread(mem, format=ext)
assert img_file.tolist() == img_mem.tolist()
def test_generate_fractal_wrong_name(): # Only possible from API
with pytest.raises(ValueError):
generate_fractal(model="julia_")
class TestImg2Output(object):
""" Tests interface with Matplotlib """
def test_file_as_memory_output(self):
img = array([[0, 1], [1, 0], [.5, .3]])
mem = BytesIO()
img2output(img, cmap="gray", output=mem)
mem.seek(0)
png_data = imread(mem)
for row_png, row_img in zip(png_data, img):
for pixel_png, pixel_img in zip(row_png, row_img):
assert almost_equal_diff(pixel_png[-1], 1.)
for pixel in pixel_png[:-1]:
assert almost_equal_diff(pixel, pixel_img)
def test_show(self, monkeypatch):
def imshow(img, cmap):
assert not imshow.called
assert cmap == "gray"
assert img.shape == (3, 2)
imshow.called = True
def show():
assert imshow.called
monkeypatch.setattr(pylab, "imshow", imshow)
monkeypatch.setattr(pylab, "show", show)
img = array([[0, 1], [1, 0], [.5, .3]])
imshow.called = False
assert img2output(img, cmap="gray", show=True) is None
def test_show_and_output(self, monkeypatch):
def imsave(output, img, cmap): # Should be called first
assert not imsave.called
assert not imshow.called
assert cmap == "something"
assert img.shape == (2, 4)
imsave.called = True
output.write(b"Data!")
def imshow(img, cmap):
assert imsave.called
assert not imshow.called
assert cmap == "something"
assert img.shape == (2, 4)
imshow.called = True
def show():
assert imsave.called
assert imshow.called
monkeypatch.setattr(pylab, "imsave", imsave)
monkeypatch.setattr(pylab, "imshow", imshow)
monkeypatch.setattr(pylab, "show", show)
img = array([[5, -1, 4, .0003], [.1, 0., 47.8, -8]])
imshow.called = False
imsave.called = False
mem = BytesIO()
assert img2output(img, cmap="something", output=mem, show=True) is None
mem.seek(0)
assert mem.read() == b"Data!"