diff --git a/mlair/helpers/testing.py b/mlair/helpers/testing.py index 15c2d79c296883ac7be8022ff1a1c50ff98fa872..eb8982ae3625cfccedf894717eebf299faffb3ee 100644 --- a/mlair/helpers/testing.py +++ b/mlair/helpers/testing.py @@ -141,8 +141,16 @@ def check_nested_equality(obj1, obj2, precision=None): print(f"check np {obj1} and {obj2} with precision {precision}") assert np.testing.assert_array_almost_equal(obj1, obj2, decimal=precision) is None else: - print(f"check equal {obj1} and {obj2}") - assert obj1 == obj2 + if isinstance(obj1, (int, float)) and isinstance(obj2, (int, float)): + if precision is None: + print(f"check number equal {obj1} and {obj2}") + assert np.testing.assert_equal(obj1, obj2) is None + else: + print(f"check number equal {obj1} and {obj2} with precision {precision}") + assert np.testing.assert_almost_equal(obj1, obj2, decimal=precision) is None + else: + print(f"check equal {obj1} and {obj2}") + assert obj1 == obj2 except AssertionError: return False return True diff --git a/test/test_helpers/test_testing_helpers.py b/test/test_helpers/test_testing_helpers.py index c15a7ea98bf3ba00f7ba4bec8b2a1f0361c39ec0..8a4bdb92e41f14a8680ea797dcd74db74bd95c9c 100644 --- a/test/test_helpers/test_testing_helpers.py +++ b/test/test_helpers/test_testing_helpers.py @@ -58,14 +58,14 @@ class TestNestedEquality: assert check_nested_equality("3", 3) is False assert check_nested_equality("3", "3") is True assert check_nested_equality(None, None) is True - assert check_nested_equality(3.91, 3.9, 1) is True - assert check_nested_equality(3.91, 3.9, 2) is False + assert check_nested_equality(3.92, 3.9, 1) is True + assert check_nested_equality(3.92, 3.9, 2) is False def test_nested_equality_xarray(self): obj1 = xr.DataArray(np.random.randn(2, 3), dims=('x', 'y'), coords={'x': [10, 20], 'y': [0, 10, 20]}) obj2 = xr.ones_like(obj1) * obj1 assert check_nested_equality(obj1, obj2) is True - obj2 = 1.0001 * obj2 + obj2 = obj2 * 1.0001 assert check_nested_equality(obj1, obj2) is False assert check_nested_equality(obj1, obj2, 3) is True