|
10 | 10 | import unittest |
11 | 11 | from io import BytesIO |
12 | 12 |
|
| 13 | +import pytest |
13 | 14 | from PIL import Image, ImageMath |
14 | 15 |
|
15 | 16 | logger = logging.getLogger(__name__) |
@@ -64,148 +65,118 @@ def convert_to_comparable(a, b): |
64 | 65 | return new_a, new_b |
65 | 66 |
|
66 | 67 |
|
67 | | -class PillowTestCase(unittest.TestCase): |
68 | | - def delete_tempfile(self, path): |
69 | | - try: |
70 | | - os.remove(path) |
71 | | - except OSError: |
72 | | - pass # report? |
73 | | - |
74 | | - def assert_deep_equal(self, a, b, msg=None): |
75 | | - try: |
76 | | - self.assertEqual( |
77 | | - len(a), |
78 | | - len(b), |
79 | | - msg or "got length {}, expected {}".format(len(a), len(b)), |
80 | | - ) |
81 | | - self.assertTrue( |
82 | | - all(x == y for x, y in zip(a, b)), |
83 | | - msg or "got {}, expected {}".format(a, b), |
84 | | - ) |
85 | | - except Exception: |
86 | | - self.assertEqual(a, b, msg) |
87 | | - |
88 | | - def assert_image(self, im, mode, size, msg=None): |
89 | | - if mode is not None: |
90 | | - self.assertEqual( |
91 | | - im.mode, |
92 | | - mode, |
93 | | - msg or "got mode {!r}, expected {!r}".format(im.mode, mode), |
94 | | - ) |
95 | | - |
96 | | - if size is not None: |
97 | | - self.assertEqual( |
98 | | - im.size, |
99 | | - size, |
100 | | - msg or "got size {!r}, expected {!r}".format(im.size, size), |
101 | | - ) |
102 | | - |
103 | | - def assert_image_equal(self, a, b, msg=None): |
104 | | - self.assertEqual( |
105 | | - a.mode, b.mode, msg or "got mode {!r}, expected {!r}".format(a.mode, b.mode) |
106 | | - ) |
107 | | - self.assertEqual( |
108 | | - a.size, b.size, msg or "got size {!r}, expected {!r}".format(a.size, b.size) |
| 68 | +def assert_deep_equal(a, b, msg=None): |
| 69 | + try: |
| 70 | + assert len(a) == len(b), msg or "got length {}, expected {}".format( |
| 71 | + len(a), len(b) |
109 | 72 | ) |
110 | | - if a.tobytes() != b.tobytes(): |
111 | | - if HAS_UPLOADER: |
112 | | - try: |
113 | | - url = test_image_results.upload(a, b) |
114 | | - logger.error("Url for test images: %s" % url) |
115 | | - except Exception: |
116 | | - pass |
117 | | - |
118 | | - self.fail(msg or "got different content") |
119 | | - |
120 | | - def assert_image_equal_tofile(self, a, filename, msg=None, mode=None): |
121 | | - with Image.open(filename) as img: |
122 | | - if mode: |
123 | | - img = img.convert(mode) |
124 | | - self.assert_image_equal(a, img, msg) |
125 | | - |
126 | | - def assert_image_similar(self, a, b, epsilon, msg=None): |
127 | | - self.assertEqual( |
128 | | - a.mode, b.mode, msg or "got mode {!r}, expected {!r}".format(a.mode, b.mode) |
| 73 | + except Exception: |
| 74 | + assert a == b, msg |
| 75 | + |
| 76 | + |
| 77 | +def assert_image(im, mode, size, msg=None): |
| 78 | + if mode is not None: |
| 79 | + assert im.mode == mode, msg or "got mode {!r}, expected {!r}".format( |
| 80 | + im.mode, mode |
129 | 81 | ) |
130 | | - self.assertEqual( |
131 | | - a.size, b.size, msg or "got size {!r}, expected {!r}".format(a.size, b.size) |
| 82 | + |
| 83 | + if size is not None: |
| 84 | + assert im.size == size, msg or "got size {!r}, expected {!r}".format( |
| 85 | + im.size, size |
132 | 86 | ) |
133 | 87 |
|
134 | | - a, b = convert_to_comparable(a, b) |
135 | 88 |
|
136 | | - diff = 0 |
137 | | - for ach, bch in zip(a.split(), b.split()): |
138 | | - chdiff = ImageMath.eval("abs(a - b)", a=ach, b=bch).convert("L") |
139 | | - diff += sum(i * num for i, num in enumerate(chdiff.histogram())) |
| 89 | +def assert_image_equal(a, b, msg=None): |
| 90 | + assert a.mode == b.mode, msg or "got mode {!r}, expected {!r}".format( |
| 91 | + a.mode, b.mode |
| 92 | + ) |
| 93 | + assert a.size == b.size, msg or "got size {!r}, expected {!r}".format( |
| 94 | + a.size, b.size |
| 95 | + ) |
| 96 | + if a.tobytes() != b.tobytes(): |
| 97 | + if HAS_UPLOADER: |
| 98 | + try: |
| 99 | + url = test_image_results.upload(a, b) |
| 100 | + logger.error("Url for test images: %s" % url) |
| 101 | + except Exception: |
| 102 | + pass |
| 103 | + |
| 104 | + assert False, msg or "got different content" |
| 105 | + |
| 106 | + |
| 107 | +def assert_image_equal_tofile(a, filename, msg=None, mode=None): |
| 108 | + with Image.open(filename) as img: |
| 109 | + if mode: |
| 110 | + img = img.convert(mode) |
| 111 | + assert_image_equal(a, img, msg) |
140 | 112 |
|
141 | | - ave_diff = diff / (a.size[0] * a.size[1]) |
142 | | - try: |
143 | | - self.assertGreaterEqual( |
144 | | - epsilon, |
145 | | - ave_diff, |
146 | | - (msg or "") |
147 | | - + " average pixel value difference %.4f > epsilon %.4f" |
148 | | - % (ave_diff, epsilon), |
149 | | - ) |
150 | | - except Exception as e: |
151 | | - if HAS_UPLOADER: |
152 | | - try: |
153 | | - url = test_image_results.upload(a, b) |
154 | | - logger.error("Url for test images: %s" % url) |
155 | | - except Exception: |
156 | | - pass |
157 | | - raise e |
158 | | - |
159 | | - def assert_image_similar_tofile(self, a, filename, epsilon, msg=None, mode=None): |
160 | | - with Image.open(filename) as img: |
161 | | - if mode: |
162 | | - img = img.convert(mode) |
163 | | - self.assert_image_similar(a, img, epsilon, msg) |
164 | | - |
165 | | - def assert_warning(self, warn_class, func, *args, **kwargs): |
166 | | - import warnings |
167 | | - |
168 | | - with warnings.catch_warnings(record=True) as w: |
169 | | - # Cause all warnings to always be triggered. |
170 | | - warnings.simplefilter("always") |
171 | | - |
172 | | - # Hopefully trigger a warning. |
173 | | - result = func(*args, **kwargs) |
174 | | - |
175 | | - # Verify some things. |
176 | | - if warn_class is None: |
177 | | - self.assertEqual( |
178 | | - len(w), 0, "Expected no warnings, got %s" % [v.category for v in w] |
179 | | - ) |
180 | | - else: |
181 | | - self.assertGreaterEqual(len(w), 1) |
182 | | - found = False |
183 | | - for v in w: |
184 | | - if issubclass(v.category, warn_class): |
185 | | - found = True |
186 | | - break |
187 | | - self.assertTrue(found) |
188 | | - return result |
189 | 113 |
|
190 | | - def assert_all_same(self, items, msg=None): |
191 | | - self.assertEqual(items.count(items[0]), len(items), msg) |
| 114 | +def assert_image_similar(a, b, epsilon, msg=None): |
| 115 | + assert a.mode == b.mode, msg or "got mode {!r}, expected {!r}".format( |
| 116 | + a.mode, b.mode |
| 117 | + ) |
| 118 | + assert a.size == b.size, msg or "got size {!r}, expected {!r}".format( |
| 119 | + a.size, b.size |
| 120 | + ) |
| 121 | + |
| 122 | + a, b = convert_to_comparable(a, b) |
| 123 | + |
| 124 | + diff = 0 |
| 125 | + for ach, bch in zip(a.split(), b.split()): |
| 126 | + chdiff = ImageMath.eval("abs(a - b)", a=ach, b=bch).convert("L") |
| 127 | + diff += sum(i * num for i, num in enumerate(chdiff.histogram())) |
| 128 | + |
| 129 | + ave_diff = diff / (a.size[0] * a.size[1]) |
| 130 | + try: |
| 131 | + assert epsilon >= ave_diff, ( |
| 132 | + msg or "" |
| 133 | + ) + " average pixel value difference %.4f > epsilon %.4f" % (ave_diff, epsilon) |
| 134 | + except Exception as e: |
| 135 | + if HAS_UPLOADER: |
| 136 | + try: |
| 137 | + url = test_image_results.upload(a, b) |
| 138 | + logger.error("Url for test images: %s" % url) |
| 139 | + except Exception: |
| 140 | + pass |
| 141 | + raise e |
| 142 | + |
| 143 | + |
| 144 | +def assert_image_similar_tofile(a, filename, epsilon, msg=None, mode=None): |
| 145 | + with Image.open(filename) as img: |
| 146 | + if mode: |
| 147 | + img = img.convert(mode) |
| 148 | + assert_image_similar(a, img, epsilon, msg) |
| 149 | + |
| 150 | + |
| 151 | +def assert_all_same(items, msg=None): |
| 152 | + assert items.count(items[0]) == len(items), msg |
192 | 153 |
|
193 | | - def assert_not_all_same(self, items, msg=None): |
194 | | - self.assertNotEqual(items.count(items[0]), len(items), msg) |
195 | 154 |
|
196 | | - def assert_tuple_approx_equal(self, actuals, targets, threshold, msg): |
197 | | - """Tests if actuals has values within threshold from targets""" |
| 155 | +def assert_not_all_same(items, msg=None): |
| 156 | + assert items.count(items[0]) != len(items), msg |
198 | 157 |
|
199 | | - value = True |
200 | | - for i, target in enumerate(targets): |
201 | | - value *= target - threshold <= actuals[i] <= target + threshold |
202 | 158 |
|
203 | | - self.assertTrue(value, msg + ": " + repr(actuals) + " != " + repr(targets)) |
| 159 | +def assert_tuple_approx_equal(actuals, targets, threshold, msg): |
| 160 | + """Tests if actuals has values within threshold from targets""" |
| 161 | + value = True |
| 162 | + for i, target in enumerate(targets): |
| 163 | + value *= target - threshold <= actuals[i] <= target + threshold |
204 | 164 |
|
205 | | - def skipKnownBadTest(self, msg=None): |
206 | | - # Skip if PILLOW_RUN_KNOWN_BAD is not true in the environment. |
207 | | - if not os.environ.get("PILLOW_RUN_KNOWN_BAD", False): |
208 | | - self.skipTest(msg or "Known Bad Test") |
| 165 | + assert value, msg + ": " + repr(actuals) + " != " + repr(targets) |
| 166 | + |
| 167 | + |
| 168 | +def skip_known_bad_test(msg=None): |
| 169 | + # Skip if PILLOW_RUN_KNOWN_BAD is not true in the environment. |
| 170 | + if not os.environ.get("PILLOW_RUN_KNOWN_BAD", False): |
| 171 | + pytest.skip(msg or "Known bad test") |
| 172 | + |
| 173 | + |
| 174 | +class PillowTestCase(unittest.TestCase): |
| 175 | + def delete_tempfile(self, path): |
| 176 | + try: |
| 177 | + os.remove(path) |
| 178 | + except OSError: |
| 179 | + pass # report? |
209 | 180 |
|
210 | 181 | def tempfile(self, template): |
211 | 182 | assert template[:5] in ("temp.", "temp_") |
|
0 commit comments