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