Debian Codegraph @

tests/test_num_regression.py

raw symbols SCIP
1 import sys
2
3 import numpy as np
4 import pandas as pd
5 import pytest
6
7 from pytest_regressions.testing import check_regression_fixture_workflow
8
9
10 @pytest.fixture
11 def no_regen(num_regression, request):
12 if num_regression._force_regen or request.config.getoption("force_regen"):
13 pytest.fail("--force-regen should not be used on this test.")
14
15
16 def test_usage_workflow(pytester, monkeypatch):
17 monkeypatch.setattr(
18 sys, "testing_get_data", lambda: {"data": 1.1 * np.ones(50)}, raising=False
19 )
20 source = """
21 import sys
22 def test_1(num_regression):
23 contents = sys.testing_get_data()
24 num_regression.check(contents)
25 """
26
27 def get_csv_contents():
28 filename = pytester.path / "test_file" / "test_1.csv"
29 frame = pd.read_csv(str(filename))
30 return {"data": frame["data"].values}
31
32 def compare_arrays(obtained, expected):
33 assert (obtained["data"] == expected["data"]).all()
34
35 check_regression_fixture_workflow(
36 pytester,
37 source=source,
38 data_getter=get_csv_contents,
39 data_modifier=lambda: monkeypatch.setattr(
40 sys, "testing_get_data", lambda: {"data": 1.2 * np.ones(50)}, raising=False
41 ),
42 expected_data_1={"data": 1.1 * np.ones(50)},
43 expected_data_2={"data": 1.2 * np.ones(50)},
44 compare_fn=compare_arrays,
45 )
46
47
48 def test_common_cases(num_regression, no_regen):
49 # Most common case: Data is valid, is present and should pass
50 data1 = 1.1 * np.ones(5000)
51 data2 = 2.2 * np.ones(5000)
52 num_regression.check({"data1": data1, "data2": data2})
53
54 # Assertion error case 1: Data has one invalid place
55 data1 = 1.1 * np.ones(5000)
56 data2 = 2.2 * np.ones(5000)
57 data1[500] += 0.1
58 with pytest.raises(AssertionError) as excinfo:
59 num_regression.check({"data1": data1, "data2": data2})
60 obtained_error_msg = str(excinfo.value)
61 expected = "\n".join(
62 [
63 "Values are not sufficiently close.",
64 "To update values, use --force-regen option.",
65 ]
66 )
67 assert expected in obtained_error_msg
68 expected = "\n".join(
69 [
70 "data1:",
71 " obtained_data1 expected_data1 diff",
72 "500 1.20000000000000018 1.10000000000000009 0.10000000000000009",
73 ]
74 )
75 # prints used to debug #3
76 print()
77 print(expected)
78 print("-" * 200)
79 print(obtained_error_msg)
80 assert expected in obtained_error_msg
81
82 # Assertion error case 2: More than one invalid data
83 data1 = 1.1 * np.ones(5000)
84 data2 = 2.2 * np.ones(5000)
85 data1[500] += 0.1
86 data1[600] += 0.2
87 data2[700] += 0.3
88 with pytest.raises(AssertionError) as excinfo:
89 num_regression.check({"data1": data1, "data2": data2})
90 obtained_error_msg = str(excinfo.value)
91 expected = "\n".join(
92 [
93 "Values are not sufficiently close.",
94 "To update values, use --force-regen option.",
95 ]
96 )
97 assert expected in obtained_error_msg
98 expected = "\n".join(
99 [
100 "data1:",
101 " obtained_data1 expected_data1 diff",
102 "500 1.20000000000000018 1.10000000000000009 0.10000000000000009",
103 "600 1.30000000000000004 1.10000000000000009 0.19999999999999996",
104 ]
105 )
106 assert expected in obtained_error_msg
107 expected = "\n".join(
108 [
109 "data2:",
110 " obtained_data2 expected_data2 diff",
111 "700 2.5 2.20000000000000018 0.29999999999999982",
112 ]
113 )
114 assert expected in obtained_error_msg
115
116 # Assertion error case 3: More than one invalid data
117 data1 = 1.1 * np.ones(5000)
118 data2 = 2.2 * np.ones(5000)
119 data1[500] += 0.01
120 data2[500] += 0.01
121 with pytest.raises(AssertionError) as excinfo:
122 num_regression.check(
123 {"data1": data1, "data2": data2},
124 tolerances={
125 "data1": dict(atol=1e-1, rtol=1e-17),
126 "data2": dict(atol=1e-17, rtol=1e-17),
127 },
128 )
129 obtained_error_msg = str(excinfo.value)
130 assert " data1:" not in obtained_error_msg
131 assert (
132 "\n".join(
133 [
134 "Values are not sufficiently close.",
135 "To update values, use --force-regen option.",
136 ]
137 )
138 in obtained_error_msg
139 )
140 assert (
141 "\n".join(
142 [
143 "data2:",
144 " obtained_data2 expected_data2 diff",
145 "500 2.20999999999999996 2.20000000000000018 0.00999999999999979",
146 ]
147 )
148 in obtained_error_msg
149 )
150
151
152 def test_different_data_types(num_regression, no_regen):
153 data1 = np.ones(10)
154 # Smoke test: Should not raise any exception
155 num_regression.check({"data1": data1})
156
157 data2 = np.array(["a"] * 10)
158 with pytest.raises(
159 AssertionError,
160 match="Data type for data data1 of obtained and expected are not the same.",
161 ):
162 num_regression.check({"data1": data2})
163
164
165 def test_n_dimensions(num_regression, no_regen):
166 data1 = np.ones(shape=(10, 10), dtype=int)
167 with pytest.raises(
168 AssertionError,
169 match="Only 1D arrays are supported on num_data_regression fixture.",
170 ):
171 num_regression.check({"data1": data1})
172
173
174 def test_arrays_with_different_sizes(num_regression, no_regen):
175 data1 = np.ones(10, dtype=np.float64)
176 with pytest.raises(
177 AssertionError, match="Obtained and expected data shape are not the same."
178 ):
179 num_regression.check({"data1": data1})
180
181
182 def test_integer_values_smoke_test(num_regression, no_regen):
183 data1 = np.ones(11, dtype=int)
184 num_regression.check({"data1": data1})
185
186
187 def test_number_formats(num_regression, no_regen):
188 data1 = np.array([1.2345678e50, 1.2345678e-50, 0.0])
189 num_regression.check({"data1": data1})
190
191
192 def test_fill_different_shape_with_nan(num_regression, no_regen):
193 data1 = np.ones(5, dtype=np.float64)
194 data2 = np.ones(2, dtype=np.float32)
195 data3 = np.ones(6, dtype=np.float16)
196 num_regression.check({"data1": data1, "data2": data2, "data3": data3})
197
198
199 def test_fill_different_shape_with_nan_false(num_regression, no_regen):
200 data1 = np.ones(5, dtype=np.float64)
201 data2 = np.ones(2, dtype=np.float32)
202 data3 = np.ones(6, dtype=np.float16)
203 with pytest.raises(
204 AssertionError,
205 match="Data dict with different array lengths will not be accepted.",
206 ):
207 num_regression.check(
208 {"data1": data1, "data2": data2, "data3": data3},
209 fill_different_shape_with_nan=False,
210 )
211
212
213 def test_fill_different_shape_with_nan_for_non_float_array(num_regression, no_regen):
214 data1 = np.ones(5, dtype=np.int32)
215 data2 = np.ones(2, dtype=np.float64)
216 data3 = np.ones(6, dtype=np.float64)
217 with pytest.raises(
218 TypeError,
219 match="Checking multiple arrays with different shapes are not supported for non-float arrays",
220 ):
221 num_regression.check({"data1": data1, "data2": data2, "data3": data3})
222
223
224 def test_bool_array(num_regression, no_regen):
225 data1 = np.array([True, True, True], dtype=bool)
226 with pytest.raises(AssertionError) as excinfo:
227 num_regression.check({"data1": data1})
228 obtained_error_msg = str(excinfo.value)
229 expected = "\n".join(
230 [
231 "Values are not sufficiently close.",
232 "To update values, use --force-regen option.",
233 ]
234 )
235 assert expected in obtained_error_msg
236 expected = "\n".join(
237 [
238 "data1:",
239 " obtained_data1 expected_data1 diff",
240 "0 True False True",
241 "1 True False True",
242 "2 True False True",
243 ]
244 )
245 assert expected in obtained_error_msg
246
247
248 def test_arrays_of_same_size(num_regression):
249 same_size_int_arrays = {
250 "hello": np.zeros((1,), dtype=int),
251 "world": np.zeros((1,), dtype=int),
252 }
253 num_regression.check(same_size_int_arrays)
254
255
256 def test_simple_numbers(num_regression, data_regression):
257 data1 = 1.1
258 data2 = 2
259 num_regression.check({"data1": data1, "data2": data2})
260 data_regression.check({"data1": data1, "data2": data2})
261 data1 += 0.00000001
262 num_regression.check({"data1": data1, "data2": data2}) # passes, within tol
263 with pytest.raises(
264 AssertionError,
265 match="FILES DIFFER.*",
266 ):
267 data_regression.check({"data1": data1, "data2": data2}) # fails, must be exact
268
269
270 def test_simple_list_of_numbers(num_regression):
271 data1 = [1.1, 1.1, 1.1]
272 data2 = [2, 2, 2]
273 num_regression.check({"data1": data1, "data2": data2})
274
275
276 def test_simple_tuple_of_numbers(num_regression):
277 data1 = (1.1, 1.1, 1.1)
278 data2 = (2, 2, 2)
279 num_regression.check({"data1": data1, "data2": data2})
280
281
282 def test_simple_list_of_mostly_numbers(num_regression):
283 data1 = [1.1, "not a number", 1.1]
284 data2 = [2, 2, 2]
285 with pytest.raises(
286 AssertionError,
287 match="Only objects that can be coerced to numpy arrays are valid for numeric_data_regression fixture.",
288 ):
289 num_regression.check({"data1": data1, "data2": data2})
290
291
292 def test_array_dtype_stored_correctly(num_regression):
293 """
294 Related to bug #84, where data type was being stored incorrectly due to np.nan values in array.
295 Problem would only occur if the compared data has any difference, when evaluated as string.
296
297 ..see: https://github.com/ESSS/pytest-regressions/issues/84
298 """
299 data1 = np.array([1.100001, np.nan, 1.1])
300 num_regression.check({"data1": data1})