From aad606a32f1e4cb7642b453a4a95d4448458ff02 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Sat, 20 May 2023 14:12:02 +0200 Subject: [PATCH 1/3] simplify the test for unary funcs --- xarray/tests/test_ufuncs.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/xarray/tests/test_ufuncs.py b/xarray/tests/test_ufuncs.py index 6cd73e9cfb7..6b4c3f38ee9 100644 --- a/xarray/tests/test_ufuncs.py +++ b/xarray/tests/test_ufuncs.py @@ -4,7 +4,7 @@ import pytest import xarray as xr -from xarray.tests import assert_array_equal, mock +from xarray.tests import assert_allclose, assert_array_equal, mock from xarray.tests import assert_identical as assert_identical_ @@ -16,16 +16,16 @@ def assert_identical(a, b): assert_array_equal(a, b) -def test_unary(): - args = [ - 0, - np.zeros(2), +@pytest.mark.parametrize( + "a", + [ xr.Variable(["x"], [0, 0]), xr.DataArray([0, 0], dims="x"), xr.Dataset({"y": ("x", [0, 0])}), - ] - for a in args: - assert_identical(a + 1, np.cos(a)) + ], +) +def test_unary(a): + assert_allclose(a + 1, np.cos(a)) def test_binary(): From a14aeb51f6787a93ce12958bc3e9634fb0e84cdb Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Sat, 20 May 2023 14:34:18 +0200 Subject: [PATCH 2/3] use `full_like` to construct the expected result --- xarray/tests/test_ufuncs.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_ufuncs.py b/xarray/tests/test_ufuncs.py index 6b4c3f38ee9..774b729b4fd 100644 --- a/xarray/tests/test_ufuncs.py +++ b/xarray/tests/test_ufuncs.py @@ -4,7 +4,7 @@ import pytest import xarray as xr -from xarray.tests import assert_allclose, assert_array_equal, mock +from xarray.tests import assert_array_equal, mock from xarray.tests import assert_identical as assert_identical_ @@ -25,7 +25,12 @@ def assert_identical(a, b): ], ) def test_unary(a): - assert_allclose(a + 1, np.cos(a)) + fill_value = np.cos(0) + + expected = xr.full_like(a, fill_value=fill_value, dtype=fill_value.dtype) + actual = np.cos(a) + + assert_identical(actual, expected) def test_binary(): From 882327e7549daae130c4aabee3a2562b9da57f77 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Sat, 27 May 2023 19:12:10 +0200 Subject: [PATCH 3/3] Revert "use `full_like` to construct the expected result" This reverts commit a14aeb51f6787a93ce12958bc3e9634fb0e84cdb. --- xarray/tests/test_ufuncs.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/xarray/tests/test_ufuncs.py b/xarray/tests/test_ufuncs.py index 774b729b4fd..6b4c3f38ee9 100644 --- a/xarray/tests/test_ufuncs.py +++ b/xarray/tests/test_ufuncs.py @@ -4,7 +4,7 @@ import pytest import xarray as xr -from xarray.tests import assert_array_equal, mock +from xarray.tests import assert_allclose, assert_array_equal, mock from xarray.tests import assert_identical as assert_identical_ @@ -25,12 +25,7 @@ def assert_identical(a, b): ], ) def test_unary(a): - fill_value = np.cos(0) - - expected = xr.full_like(a, fill_value=fill_value, dtype=fill_value.dtype) - actual = np.cos(a) - - assert_identical(actual, expected) + assert_allclose(a + 1, np.cos(a)) def test_binary():