Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow num_regression to accept array-like inputs #45

Merged
merged 5 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2.2.0 (UNRELEASED)
------------------

* `#45 <https://github.com/ESSS/pytest-regressions/pull/45>`__: ``num_regression.check`` now accepts any object that can be coerced to a 1d ``numpy`` array with numeric ``dtype`` (e.g. list, tuple, etc).

2.1.1 (2020-12-7)
------------------

Expand Down
13 changes: 10 additions & 3 deletions src/pytest_regressions/num_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def check(
):
"""
Checks the given dict against a previously recorded version, or generate a new file.
The dict must map from user-defined keys to 1d numpy arrays.
The dict must map from user-defined keys to 1d numpy arrays or array-like values.

Example::

Expand All @@ -31,7 +31,8 @@ def check(
'P': Pa_to_bar(P)[positions],
})

:param dict data_dict: dict mapping keys to numpy arrays.
:param dict data_dict: dict mapping keys to numpy arrays, or objects that can be
coerced to 1d numpy arrays with a numeric dtype (e.g. list, tuple, etc).

:param str basename: basename of the file to test/record. If not given the name
of the test is used.
Expand Down Expand Up @@ -73,11 +74,17 @@ def check(

__tracebackhide__ = True

for k, obj in data_dict.items():
if not isinstance(obj, np.ndarray):
arr = np.atleast_1d(np.asarray(obj))
if np.issubdtype(arr.dtype, np.number):
data_dict[k] = arr

data_shapes = []
for obj in data_dict.values():
assert type(obj) in [
np.ndarray
], "Only numpy arrays are valid for numeric_data_regression fixture.\n"
], "Only objects that can be coerced to numpy arrays are valid for numeric_data_regression fixture.\n"
shape = obj.shape

assert len(shape) == 1, (
Expand Down
36 changes: 36 additions & 0 deletions tests/test_num_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,39 @@ def test_arrays_of_same_size(num_regression):
"world": np.zeros((1,), dtype=np.int),
}
num_regression.check(same_size_int_arrays)


def test_simple_numbers(num_regression, data_regression):
data1 = 1.1
data2 = 2
num_regression.check({"data1": data1, "data2": data2})
data_regression.check({"data1": data1, "data2": data2})
data1 += 0.00000001
num_regression.check({"data1": data1, "data2": data2}) # passes, within tol
with pytest.raises(
AssertionError,
match="FILES DIFFER.*",
):
data_regression.check({"data1": data1, "data2": data2}) # fails, must be exact


def test_simple_list_of_numbers(num_regression):
data1 = [1.1, 1.1, 1.1]
data2 = [2, 2, 2]
num_regression.check({"data1": data1, "data2": data2})


def test_simple_tuple_of_numbers(num_regression):
data1 = (1.1, 1.1, 1.1)
data2 = (2, 2, 2)
num_regression.check({"data1": data1, "data2": data2})


def test_simple_list_of_mostly_numbers(num_regression):
data1 = [1.1, "not a number", 1.1]
data2 = [2, 2, 2]
with pytest.raises(
AssertionError,
match="Only objects that can be coerced to numpy arrays are valid for numeric_data_regression fixture.",
):
num_regression.check({"data1": data1, "data2": data2})
2 changes: 2 additions & 0 deletions tests/test_num_regression/test_simple_floats.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
,data1,data2
0,1.1000000000000001,2.2000000000000002
4 changes: 4 additions & 0 deletions tests/test_num_regression/test_simple_list_of_numbers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
,data1,data2
0,1.1000000000000001,2
1,1.1000000000000001,2
2,1.1000000000000001,2
2 changes: 2 additions & 0 deletions tests/test_num_regression/test_simple_numbers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
,data1,data2
0,1.1000000000000001,2
2 changes: 2 additions & 0 deletions tests/test_num_regression/test_simple_numbers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data1: 1.1
data2: 2
4 changes: 4 additions & 0 deletions tests/test_num_regression/test_simple_tuple_of_numbers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
,data1,data2
0,1.1000000000000001,2
1,1.1000000000000001,2
2,1.1000000000000001,2