| 1 |
import sys |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
import pytest |
| 5 |
|
| 6 |
from pytest_regressions.testing import check_regression_fixture_workflow |
| 7 |
|
| 8 |
|
| 9 |
@pytest.fixture |
| 10 |
def no_regen(ndarrays_regression, request): |
| 11 |
if ndarrays_regression._force_regen or request.config.getoption("force_regen"): |
| 12 |
pytest.fail("--force-regen should not be used on this test.") |
| 13 |
|
| 14 |
|
| 15 |
def test_usage_workflow(pytester, monkeypatch): |
| 16 |
monkeypatch.setattr( |
| 17 |
sys, "testing_get_data", lambda: {"data": 1.1 * np.ones(50)}, raising=False |
| 18 |
) |
| 19 |
source = """ |
| 20 |
import sys |
| 21 |
def test_1(ndarrays_regression): |
| 22 |
contents = sys.testing_get_data() |
| 23 |
ndarrays_regression.check(contents) |
| 24 |
""" |
| 25 |
|
| 26 |
def get_npz_contents(): |
| 27 |
filename = pytester.path / "test_file" / "test_1.npz" |
| 28 |
return dict(np.load(str(filename))) |
| 29 |
|
| 30 |
def compare_arrays(obtained, expected): |
| 31 |
assert (obtained["data"] == expected["data"]).all() |
| 32 |
|
| 33 |
check_regression_fixture_workflow( |
| 34 |
pytester, |
| 35 |
source=source, |
| 36 |
data_getter=get_npz_contents, |
| 37 |
data_modifier=lambda: monkeypatch.setattr( |
| 38 |
sys, "testing_get_data", lambda: {"data": 1.2 * np.ones(50)}, raising=False |
| 39 |
), |
| 40 |
expected_data_1={"data": 1.1 * np.ones(50)}, |
| 41 |
expected_data_2={"data": 1.2 * np.ones(50)}, |
| 42 |
compare_fn=compare_arrays, |
| 43 |
) |
| 44 |
|
| 45 |
|
| 46 |
def test_common_case(ndarrays_regression, no_regen): |
| 47 |
# Most common case: Data is valid, is present and should pass |
| 48 |
data1 = np.full(5000, 1.1, dtype=float) |
| 49 |
data2 = np.arange(5000, dtype=int) |
| 50 |
ndarrays_regression.check({"data1": data1, "data2": data2}) |
| 51 |
|
| 52 |
# Assertion error case 1: Data has one invalid place |
| 53 |
data1 = np.full(5000, 1.1, dtype=float) |
| 54 |
data2 = np.arange(5000, dtype=int) |
| 55 |
data1[500] += 0.1 |
| 56 |
with pytest.raises(AssertionError) as excinfo: |
| 57 |
ndarrays_regression.check({"data1": data1, "data2": data2}) |
| 58 |
obtained_error_msg = str(excinfo.value) |
| 59 |
expected = "\n".join( |
| 60 |
[ |
| 61 |
"Values are not sufficiently close.", |
| 62 |
"To update values, use --force-regen option.", |
| 63 |
] |
| 64 |
) |
| 65 |
assert expected in obtained_error_msg |
| 66 |
expected = "\n".join( |
| 67 |
[ |
| 68 |
"data1:", |
| 69 |
" Shape: (5000,)", |
| 70 |
" Number of differences: 1 / 5000 (0.0%)", |
| 71 |
" Individual errors:", |
| 72 |
" Index Obtained Expected Difference", |
| 73 |
" 500 1.2000000000000002 1.1 0.10000000000000009", |
| 74 |
] |
| 75 |
) |
| 76 |
assert expected in obtained_error_msg |
| 77 |
|
| 78 |
# Assertion error case 2: More than one invalid data |
| 79 |
data1 = np.full(5000, 1.1, dtype=float) |
| 80 |
data2 = np.arange(5000, dtype=int) |
| 81 |
data1[500] += 0.1 |
| 82 |
data1[600] += 0.2 |
| 83 |
data2[0] += 5 |
| 84 |
data2[700:900] += 5 |
| 85 |
with pytest.raises(AssertionError) as excinfo: |
| 86 |
ndarrays_regression.check({"data1": data1, "data2": data2}) |
| 87 |
obtained_error_msg = str(excinfo.value) |
| 88 |
expected = "\n".join( |
| 89 |
[ |
| 90 |
"Values are not sufficiently close.", |
| 91 |
"To update values, use --force-regen option.", |
| 92 |
] |
| 93 |
) |
| 94 |
assert expected in obtained_error_msg |
| 95 |
expected = "\n".join( |
| 96 |
[ |
| 97 |
"data1:", |
| 98 |
" Shape: (5000,)", |
| 99 |
" Number of differences: 2 / 5000 (0.0%)", |
| 100 |
" Statistics are computed for differing elements only.", |
| 101 |
" Stats for abs(obtained - expected):", |
| 102 |
" Max: 0.19999999999999996", |
| 103 |
" Mean: 0.15000000000000002", |
| 104 |
" Median: 0.15000000000000002", |
| 105 |
" Stats for abs(obtained - expected) / abs(expected):", |
| 106 |
" Max: 0.18181818181818177", |
| 107 |
" Mean: 0.13636363636363638", |
| 108 |
" Median: 0.13636363636363638", |
| 109 |
" Individual errors:", |
| 110 |
" Index Obtained Expected Difference", |
| 111 |
" 500 1.2000000000000002 1.1 0.10000000000000009", |
| 112 |
" 600 1.3 1.1 0.19999999999999996", |
| 113 |
] |
| 114 |
) |
| 115 |
assert expected in obtained_error_msg |
| 116 |
expected = "\n".join( |
| 117 |
[ |
| 118 |
"data2:", |
| 119 |
" Shape: (5000,)", |
| 120 |
" Number of differences: 201 / 5000 (4.0%)", |
| 121 |
" Statistics are computed for differing elements only.", |
| 122 |
" Stats for abs(obtained - expected):", |
| 123 |
" Max: 5", |
| 124 |
" Mean: 5.0", |
| 125 |
" Median: 5.0", |
| 126 |
" Stats for abs(obtained - expected) / abs(expected):", |
| 127 |
" Number of (differing) non-zero expected results: 200 / 201 (99.5%)", |
| 128 |
" Relative errors are computed for the non-zero expected results.", |
| 129 |
" Max: 0.007142857142857143", |
| 130 |
" Mean: 0.006286830640674575", |
| 131 |
" Median: 0.006253911138923655", |
| 132 |
" Individual errors:", |
| 133 |
" Only showing first 100 mismatches.", |
| 134 |
" Index Obtained Expected Difference", |
| 135 |
" 0 5 0 5", |
| 136 |
" 700 705 700 5", |
| 137 |
" 701 706 701 5", |
| 138 |
] |
| 139 |
) |
| 140 |
assert expected in obtained_error_msg |
| 141 |
|
| 142 |
|
| 143 |
def test_common_case_nd(ndarrays_regression, no_regen): |
| 144 |
# Most common case: Data is valid, is present and should pass |
| 145 |
data1 = np.full((50, 20), 1.1, dtype=float) |
| 146 |
data2 = np.arange(60, dtype=int).reshape((3, 4, 5)) |
| 147 |
ndarrays_regression.check({"data1": data1, "data2": data2}) |
| 148 |
|
| 149 |
# Assertion error case 1: Data has one invalid place |
| 150 |
data1 = np.full((50, 20), 1.1, dtype=float) |
| 151 |
data2 = np.arange(60, dtype=int).reshape((3, 4, 5)) |
| 152 |
data1[30, 2] += 0.1 |
| 153 |
with pytest.raises(AssertionError) as excinfo: |
| 154 |
ndarrays_regression.check({"data1": data1, "data2": data2}) |
| 155 |
obtained_error_msg = str(excinfo.value) |
| 156 |
expected = "\n".join( |
| 157 |
[ |
| 158 |
"Values are not sufficiently close.", |
| 159 |
"To update values, use --force-regen option.", |
| 160 |
] |
| 161 |
) |
| 162 |
assert expected in obtained_error_msg |
| 163 |
expected = "\n".join( |
| 164 |
[ |
| 165 |
"data1:", |
| 166 |
" Shape: (50, 20)", |
| 167 |
" Number of differences: 1 / 1000 (0.1%)", |
| 168 |
" Individual errors:", |
| 169 |
" Index Obtained Expected Difference", |
| 170 |
" (30, 2) 1.2000000000000002 1.1 0.10000000000000009", |
| 171 |
] |
| 172 |
) |
| 173 |
assert expected in obtained_error_msg |
| 174 |
|
| 175 |
# Assertion error case 2: More than one invalid data |
| 176 |
data1 = np.full((50, 20), 1.1, dtype=float) |
| 177 |
data2 = np.arange(60, dtype=int).reshape((3, 4, 5)) |
| 178 |
data1[20, 15] += 0.1 |
| 179 |
data1[0, 9] = 1.43248324e35 |
| 180 |
data2[:2, 0, [0, 2, 4]] += 71 |
| 181 |
with pytest.raises(AssertionError) as excinfo: |
| 182 |
ndarrays_regression.check({"data1": data1, "data2": data2}) |
| 183 |
obtained_error_msg = str(excinfo.value) |
| 184 |
expected = "\n".join( |
| 185 |
[ |
| 186 |
"Values are not sufficiently close.", |
| 187 |
"To update values, use --force-regen option.", |
| 188 |
] |
| 189 |
) |
| 190 |
assert expected in obtained_error_msg |
| 191 |
expected = "\n".join( |
| 192 |
[ |
| 193 |
"data1:", |
| 194 |
" Shape: (50, 20)", |
| 195 |
" Number of differences: 2 / 1000 (0.2%)", |
| 196 |
" Statistics are computed for differing elements only.", |
| 197 |
" Stats for abs(obtained - expected):", |
| 198 |
" Max: 1.43248324e+35", |
| 199 |
" Mean: 7.1624162e+34", |
| 200 |
" Median: 7.1624162e+34", |
| 201 |
" Stats for abs(obtained - expected) / abs(expected):", |
| 202 |
" Max: 1.3022574909090907e+35", |
| 203 |
" Mean: 6.511287454545454e+34", |
| 204 |
" Median: 6.511287454545454e+34", |
| 205 |
" Individual errors:", |
| 206 |
" Index Obtained Expected Difference", |
| 207 |
" (0, 9) 1.43248324e+35 1.1 1.43248324e+35", |
| 208 |
" (20, 15) 1.2000000000000002 1.1 0.10000000000000009", |
| 209 |
] |
| 210 |
) |
| 211 |
assert expected in obtained_error_msg |
| 212 |
expected = "\n".join( |
| 213 |
[ |
| 214 |
"data2:", |
| 215 |
" Shape: (3, 4, 5)", |
| 216 |
" Number of differences: 6 / 60 (10.0%)", |
| 217 |
" Statistics are computed for differing elements only.", |
| 218 |
" Stats for abs(obtained - expected):", |
| 219 |
" Max: 71", |
| 220 |
" Mean: 71.0", |
| 221 |
" Median: 71.0", |
| 222 |
" Stats for abs(obtained - expected) / abs(expected):", |
| 223 |
" Number of (differing) non-zero expected results: 5 / 6 (83.3%)", |
| 224 |
" Relative errors are computed for the non-zero expected results.", |
| 225 |
" Max: 35.5", |
| 226 |
" Mean: 12.597121212121213", |
| 227 |
" Median: 3.55", |
| 228 |
" Individual errors:", |
| 229 |
" Index Obtained Expected Difference", |
| 230 |
" (0, 0, 0) 71 0 71", |
| 231 |
" (0, 0, 2) 73 2 71", |
| 232 |
" (0, 0, 4) 75 4 71", |
| 233 |
" (1, 0, 0) 91 20 71", |
| 234 |
" (1, 0, 2) 93 22 71", |
| 235 |
" (1, 0, 4) 95 24 71", |
| 236 |
] |
| 237 |
) |
| 238 |
assert expected in obtained_error_msg |
| 239 |
|
| 240 |
|
| 241 |
def test_common_case_zero_expected(ndarrays_regression, no_regen): |
| 242 |
# Most common case: Data is valid, is present and should pass |
| 243 |
data = {"data1": np.array([0, 0, 2, 3, 0, 5, 0, 7])} |
| 244 |
ndarrays_regression.check(data) |
| 245 |
|
| 246 |
# Assertion error case: Only some zeros are not reproduced. |
| 247 |
data = {"data1": np.array([1, 5, 2, 3, 0, 5, 3, 7])} |
| 248 |
with pytest.raises(AssertionError) as excinfo: |
| 249 |
ndarrays_regression.check(data) |
| 250 |
obtained_error_msg = str(excinfo.value) |
| 251 |
expected = "\n".join( |
| 252 |
[ |
| 253 |
"Values are not sufficiently close.", |
| 254 |
"To update values, use --force-regen option.", |
| 255 |
] |
| 256 |
) |
| 257 |
assert expected in obtained_error_msg |
| 258 |
expected = "\n".join( |
| 259 |
[ |
| 260 |
"data1:", |
| 261 |
" Shape: (8,)", |
| 262 |
" Number of differences: 3 / 8 (37.5%)", |
| 263 |
" Statistics are computed for differing elements only.", |
| 264 |
" Stats for abs(obtained - expected):", |
| 265 |
" Max: 5", |
| 266 |
" Mean: 3.0", |
| 267 |
" Median: 3.0", |
| 268 |
" Relative errors are not reported because all expected values are zero.", |
| 269 |
" Individual errors:", |
| 270 |
" Index Obtained Expected Difference", |
| 271 |
" 0 1 0 1", |
| 272 |
" 1 5 0 5", |
| 273 |
" 6 3 0 3", |
| 274 |
] |
| 275 |
) |
| 276 |
assert expected in obtained_error_msg |
| 277 |
|
| 278 |
|
| 279 |
def test_different_data_types(ndarrays_regression, no_regen): |
| 280 |
# Generate data with integer array. |
| 281 |
data = {"data1": np.array([1] * 10)} |
| 282 |
ndarrays_regression.check(data) |
| 283 |
|
| 284 |
# Run check with incompatible type. |
| 285 |
data = {"data1": np.array([True] * 10)} |
| 286 |
with pytest.raises(AssertionError) as excinfo: |
| 287 |
ndarrays_regression.check(data) |
| 288 |
obtained_error_msg = str(excinfo.value) |
| 289 |
expected = "\n".join( |
| 290 |
[ |
| 291 |
"Data types are not the same.", |
| 292 |
" key: data1", |
| 293 |
" Obtained: bool", |
| 294 |
" Expected: int64", |
| 295 |
] |
| 296 |
) |
| 297 |
assert expected in obtained_error_msg |
| 298 |
|
| 299 |
|
| 300 |
class Foo: |
| 301 |
def __init__(self, bar): |
| 302 |
self.bar = bar |
| 303 |
|
| 304 |
|
| 305 |
def test_object_dtype(ndarrays_regression, no_regen): |
| 306 |
data1 = {"data1": np.array([Foo(i) for i in range(4)], dtype=object)} |
| 307 |
with pytest.raises(TypeError) as excinfo: |
| 308 |
ndarrays_regression.check(data1) |
| 309 |
obtained_error_msg = str(excinfo.value) |
| 310 |
expected = "\n".join( |
| 311 |
[ |
| 312 |
"Only numeric or unicode data is supported on ndarrays_regression fixture.", |
| 313 |
"Array 'data1' with type 'object' was given.", |
| 314 |
] |
| 315 |
) |
| 316 |
assert expected in obtained_error_msg |
| 317 |
|
| 318 |
|
| 319 |
def test_integer_values_smoke_test(ndarrays_regression, no_regen): |
| 320 |
data1 = np.ones(11, dtype=int) |
| 321 |
ndarrays_regression.check({"data1": data1}) |
| 322 |
|
| 323 |
|
| 324 |
def test_float_values_smoke_test(ndarrays_regression): |
| 325 |
data1 = np.array([1.2345678e50, 1.2345678e-50, 0.0]) |
| 326 |
ndarrays_regression.check({"data1": data1}) |
| 327 |
|
| 328 |
|
| 329 |
def test_bool_array(ndarrays_regression, no_regen): |
| 330 |
# Correct data |
| 331 |
data1 = np.array([False, False, False], dtype=bool) |
| 332 |
ndarrays_regression.check({"data1": data1}) |
| 333 |
|
| 334 |
# Data with errors |
| 335 |
data1 = np.array([True, True, False], dtype=bool) |
| 336 |
with pytest.raises(AssertionError) as excinfo: |
| 337 |
ndarrays_regression.check({"data1": data1}) |
| 338 |
obtained_error_msg = str(excinfo.value) |
| 339 |
expected = "\n".join( |
| 340 |
[ |
| 341 |
"Values are not sufficiently close.", |
| 342 |
"To update values, use --force-regen option.", |
| 343 |
] |
| 344 |
) |
| 345 |
assert expected in obtained_error_msg |
| 346 |
expected = "\n".join( |
| 347 |
[ |
| 348 |
"data1:", |
| 349 |
" Shape: (3,)", |
| 350 |
" Number of differences: 2 / 3 (66.7%)", |
| 351 |
" Individual errors:", |
| 352 |
" Index Obtained Expected Difference", |
| 353 |
" 0 True False ", |
| 354 |
" 1 True False ", |
| 355 |
] |
| 356 |
) |
| 357 |
assert expected in obtained_error_msg |
| 358 |
|
| 359 |
|
| 360 |
def test_complex_array(ndarrays_regression, no_regen): |
| 361 |
# Correct data |
| 362 |
data1 = np.array([3.0 + 2.5j, -0.5, -1.879j]) |
| 363 |
ndarrays_regression.check({"data1": data1}) |
| 364 |
|
| 365 |
# Data with errors |
| 366 |
data1 = np.array([3.0 + 2.5j, 0.5, -1.879]) |
| 367 |
with pytest.raises(AssertionError) as excinfo: |
| 368 |
ndarrays_regression.check({"data1": data1}) |
| 369 |
str(excinfo.value) |
| 370 |
expected = "\n".join( |
| 371 |
[ |
| 372 |
"data1:", |
| 373 |
" Shape: (3,)", |
| 374 |
" Number of differences: 2 / 3 (66.7%)", |
| 375 |
" Statistics are computed for differing elements only.", |
| 376 |
" Stats for abs(obtained - expected):", |
| 377 |
" Max: 2.6573072836990455", |
| 378 |
" Mean: 1.8286536418495227", |
| 379 |
" Median: 1.8286536418495227", |
| 380 |
" Stats for abs(obtained - expected) / abs(expected):", |
| 381 |
" Max: 2.0", |
| 382 |
" Mean: 1.7071067811865475", |
| 383 |
" Median: 1.7071067811865475", |
| 384 |
" Individual errors:", |
| 385 |
" Index Obtained Expected Difference", |
| 386 |
" 1 (0.5+0j) (-0.5+0j) (1+0j)", |
| 387 |
" 2 (-1.879+0j) (-0-1.879j) (-1.879+1.879j)", |
| 388 |
] |
| 389 |
) |
| 390 |
|
| 391 |
|
| 392 |
def test_arrays_of_same_size_1d(ndarrays_regression): |
| 393 |
data = { |
| 394 |
"hello": np.zeros((1,), dtype=int), |
| 395 |
"world": np.zeros((1,), dtype=int), |
| 396 |
} |
| 397 |
ndarrays_regression.check(data) |
| 398 |
|
| 399 |
|
| 400 |
def test_arrays_with_different_sizes_1d(ndarrays_regression, no_regen): |
| 401 |
data = {"data1": np.ones(11, dtype=np.float64)} |
| 402 |
ndarrays_regression.check(data) |
| 403 |
|
| 404 |
# Original NPY file contains 11 elements. |
| 405 |
data = {"data1": np.ones(10, dtype=np.float64)} |
| 406 |
with pytest.raises(AssertionError) as excinfo: |
| 407 |
ndarrays_regression.check(data) |
| 408 |
obtained_error_msg = str(excinfo.value) |
| 409 |
expected = "\n".join( |
| 410 |
[ |
| 411 |
"Shapes are not the same.", |
| 412 |
" Key: data1", |
| 413 |
" Obtained: (10,)", |
| 414 |
" Expected: (11,)", |
| 415 |
] |
| 416 |
) |
| 417 |
assert expected in obtained_error_msg |
| 418 |
|
| 419 |
|
| 420 |
def test_arrays_of_same_shape(ndarrays_regression): |
| 421 |
data = { |
| 422 |
"2d": np.zeros((3, 4), dtype=int), |
| 423 |
"3d": np.ones((7, 8, 9), dtype=float), |
| 424 |
"4d": np.full((2, 1, 1, 4), 3, dtype=int), |
| 425 |
} |
| 426 |
ndarrays_regression.check(data) |
| 427 |
|
| 428 |
|
| 429 |
def test_arrays_with_different_shapes(ndarrays_regression): |
| 430 |
# Prepare data with one shape. |
| 431 |
data = {"2d": np.zeros((3, 4), dtype=int)} |
| 432 |
ndarrays_regression.check(data) |
| 433 |
|
| 434 |
# Check with other shape. |
| 435 |
data = {"2d": np.zeros((3, 2), dtype=int)} |
| 436 |
with pytest.raises(AssertionError) as excinfo: |
| 437 |
ndarrays_regression.check(data) |
| 438 |
obtained_error_msg = str(excinfo.value) |
| 439 |
expected = "\n".join( |
| 440 |
[ |
| 441 |
"Shapes are not the same.", |
| 442 |
" Key: 2d", |
| 443 |
" Obtained: (3, 2)", |
| 444 |
" Expected: (3, 4)", |
| 445 |
] |
| 446 |
) |
| 447 |
assert expected in obtained_error_msg |
| 448 |
|
| 449 |
|
| 450 |
def test_scalars(ndarrays_regression): |
| 451 |
# Initial data with scalars. |
| 452 |
data1 = {"data1": 4.0, "data2": 42} |
| 453 |
ndarrays_regression.check(data1) |
| 454 |
|
| 455 |
# Run check with non-scalar data. |
| 456 |
data2 = {"data1": np.array([4.0]), "data2": np.array([42, 21])} |
| 457 |
with pytest.raises(AssertionError) as excinfo: |
| 458 |
ndarrays_regression.check(data2) |
| 459 |
obtained_error_msg = str(excinfo.value) |
| 460 |
expected = "\n".join( |
| 461 |
[ |
| 462 |
"Shapes are not the same.", |
| 463 |
" Key: data1", |
| 464 |
" Obtained: (1,)", |
| 465 |
" Expected: ()", |
| 466 |
] |
| 467 |
) |
| 468 |
assert expected in obtained_error_msg |
| 469 |
|
| 470 |
# Other test case. |
| 471 |
data3 = {"data1": 5.0, "data2": 21} |
| 472 |
with pytest.raises(AssertionError) as excinfo: |
| 473 |
ndarrays_regression.check(data3) |
| 474 |
obtained_error_msg = str(excinfo.value) |
| 475 |
expected = "\n".join( |
| 476 |
[ |
| 477 |
"data1:", |
| 478 |
" Shape: ()", |
| 479 |
" Number of differences: 1 / 1 (100.0%)", |
| 480 |
" Individual errors:", |
| 481 |
" Index Obtained Expected Difference", |
| 482 |
" () 5.0 4.0 1.0", |
| 483 |
] |
| 484 |
) |
| 485 |
assert expected in obtained_error_msg |
| 486 |
expected = "\n".join( |
| 487 |
[ |
| 488 |
"data2:", |
| 489 |
" Shape: ()", |
| 490 |
" Number of differences: 1 / 1 (100.0%)", |
| 491 |
" Individual errors:", |
| 492 |
" Index Obtained Expected Difference", |
| 493 |
" () 21 42 -21", |
| 494 |
] |
| 495 |
) |
| 496 |
assert expected in obtained_error_msg |
| 497 |
|
| 498 |
|
| 499 |
def test_string_array(ndarrays_regression): |
| 500 |
# Initial data. |
| 501 |
data1 = {"potato": ["delicious", "nutritive", "yummy"]} |
| 502 |
ndarrays_regression.check(data1) |
| 503 |
|
| 504 |
# Run check with wrong data. |
| 505 |
data1 = {"potato": ["delicious", "nutritive", "yikes"]} |
| 506 |
with pytest.raises(AssertionError) as excinfo: |
| 507 |
ndarrays_regression.check(data1) |
| 508 |
obtained_error_msg = str(excinfo.value) |
| 509 |
expected = "\n".join( |
| 510 |
[ |
| 511 |
"potato:", |
| 512 |
" Shape: (3,)", |
| 513 |
" Number of differences: 1 / 3 (33.3%)", |
| 514 |
" Individual errors:", |
| 515 |
" Index Obtained Expected Difference", |
| 516 |
" 2 yikes yummy ", |
| 517 |
] |
| 518 |
) |
| 519 |
assert expected in obtained_error_msg |
| 520 |
|
| 521 |
# Try data with incompatible dtype |
| 522 |
data1 = {"potato": ["disgusting", "nutritive", "yikes"]} |
| 523 |
with pytest.raises(AssertionError) as excinfo: |
| 524 |
ndarrays_regression.check(data1) |
| 525 |
obtained_error_msg = str(excinfo.value) |
| 526 |
expected = "\n".join( |
| 527 |
[ |
| 528 |
"potato:", |
| 529 |
" Shape: (3,)", |
| 530 |
" Number of differences: 2 / 3 (66.7%)", |
| 531 |
" Individual errors:", |
| 532 |
" Index Obtained Expected Difference", |
| 533 |
" 0 disgusting delicious ", |
| 534 |
" 2 yikes yummy ", |
| 535 |
] |
| 536 |
) |
| 537 |
assert expected in obtained_error_msg |
| 538 |
|
| 539 |
|
| 540 |
def test_non_dict(ndarrays_regression): |
| 541 |
data = np.ones(shape=(10, 10)) |
| 542 |
with pytest.raises(TypeError) as excinfo: |
| 543 |
ndarrays_regression.check(data) |
| 544 |
obtained_error_msg = str(excinfo.value) |
| 545 |
expected = "\n".join( |
| 546 |
[ |
| 547 |
"Only dictionaries with NumPy arrays or array-like objects are supported " |
| 548 |
"on ndarray_regression fixture.", |
| 549 |
"Object with type '{}' was given.".format( |
| 550 |
str(type(data)), |
| 551 |
), |
| 552 |
] |
| 553 |
) |
| 554 |
assert expected in obtained_error_msg |
| 555 |
|
| 556 |
|
| 557 |
def test_structured_array(ndarrays_regression): |
| 558 |
data = { |
| 559 |
"array": np.array( |
| 560 |
[("spam", 1, 3.0), ("egg", 0, 4.3)], |
| 561 |
dtype=[("item", "U5"), ("count", "i4"), ("price", "f8")], |
| 562 |
) |
| 563 |
} |
| 564 |
with pytest.raises(TypeError) as excinfo: |
| 565 |
ndarrays_regression.check(data) |
| 566 |
obtained_error_msg = str(excinfo.value) |
| 567 |
expected = "\n".join( |
| 568 |
[ |
| 569 |
"Only numeric or unicode data is supported on ndarrays_regression fixture.", |
| 570 |
"Array 'array' with type '{}' was given.".format(data["array"].dtype), |
| 571 |
] |
| 572 |
) |
| 573 |
assert expected in obtained_error_msg |
| 574 |
|
| 575 |
|
| 576 |
def test_new_obtained(ndarrays_regression): |
| 577 |
# Prepare data with one array. |
| 578 |
data = {"ar1": np.array([2.3, 9.4])} |
| 579 |
ndarrays_regression.check(data) |
| 580 |
|
| 581 |
# Run check with two arrays. |
| 582 |
data = {"ar1": np.array([2.3, 9.4]), "ar2": np.array([3, 4, 9])} |
| 583 |
with pytest.raises(AssertionError) as excinfo: |
| 584 |
ndarrays_regression.check(data) |
| 585 |
obtained_error_msg = str(excinfo.value) |
| 586 |
expected = "\n".join( |
| 587 |
[ |
| 588 |
"They keys in the obtained results differ from the expected results.", |
| 589 |
" Matching keys: ['ar1']", |
| 590 |
" New in obtained: ['ar2']", |
| 591 |
" Missing from obtained: []", |
| 592 |
] |
| 593 |
) |
| 594 |
assert expected in obtained_error_msg |
| 595 |
|
| 596 |
|
| 597 |
def test_missing_obtained(ndarrays_regression): |
| 598 |
# Prepare data with two arrays. |
| 599 |
data = {"ar1": np.array([2.3, 9.4]), "ar2": np.array([3, 4, 9])} |
| 600 |
ndarrays_regression.check(data) |
| 601 |
|
| 602 |
# Run check with just one array. |
| 603 |
data = {"ar1": np.array([2.3, 9.4])} |
| 604 |
with pytest.raises(AssertionError) as excinfo: |
| 605 |
ndarrays_regression.check(data) |
| 606 |
obtained_error_msg = str(excinfo.value) |
| 607 |
expected = "\n".join( |
| 608 |
[ |
| 609 |
"They keys in the obtained results differ from the expected results.", |
| 610 |
" Matching keys: ['ar1']", |
| 611 |
" New in obtained: []", |
| 612 |
" Missing from obtained: ['ar2']", |
| 613 |
] |
| 614 |
) |
| 615 |
assert expected in obtained_error_msg |
| 616 |
|
| 617 |
|
| 618 |
@pytest.mark.parametrize("prefix", [True, False]) |
| 619 |
def test_corrupt_npz(ndarrays_regression, tmp_path, prefix): |
| 620 |
data = {"data1": np.array([4, 5])} |
| 621 |
fn_npz = tmp_path / "corrupt.npz" |
| 622 |
# Write random bytes to a file |
| 623 |
with open(fn_npz, "wb") as f: |
| 624 |
if prefix: |
| 625 |
f.write(b"PK\x03\x04") |
| 626 |
np.random.randint(0, 256, 1000, dtype=np.ubyte).tofile(f) |
| 627 |
with pytest.raises(IOError) as excinfo: |
| 628 |
ndarrays_regression.check(data, fullpath=fn_npz) |
| 629 |
obtained_error_msg = str(excinfo.value) |
| 630 |
expected = f"NPZ file {fn_npz} could not be loaded. Corrupt file?" |
| 631 |
assert expected in obtained_error_msg |