From 5ef82b9ddc51150b0e4945ff269e0c89f0d27f52 Mon Sep 17 00:00:00 2001 From: TomNicholas Date: Thu, 22 Aug 2024 17:11:33 -0400 Subject: [PATCH 1/2] parametrize over different reduction operations --- xarray_array_testing/reduction.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/xarray_array_testing/reduction.py b/xarray_array_testing/reduction.py index 8e58949..3b5c23d 100644 --- a/xarray_array_testing/reduction.py +++ b/xarray_array_testing/reduction.py @@ -1,5 +1,7 @@ from contextlib import nullcontext +import pytest + import hypothesis.strategies as st import xarray.testing.strategies as xrst from hypothesis import given @@ -12,22 +14,15 @@ class ReductionTests(DuckArrayTestMixin): def expected_errors(op, **parameters): return nullcontext() + @pytest.mark.parametrize("op", ["mean", "sum", "prod", "std", "var"]) @given(st.data()) - def test_variable_mean(self, data): - variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn)) - - with self.expected_errors("mean", variable=variable): - actual = variable.mean().data - expected = self.xp.mean(variable.data) - - self.assert_equal(actual, expected) - - @given(st.data()) - def test_variable_prod(self, data): + def test_variable_mean(self, op, data): variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn)) - with self.expected_errors("prod", variable=variable): - actual = variable.prod().data - expected = self.xp.prod(variable.data) + with self.expected_errors(op, variable=variable): + # compute using xr.Variable.() + actual = getattr(variable, op)().data + # compute using xp.(array) + expected = getattr(self.xp, op)(variable.data) self.assert_equal(actual, expected) From d80f850e39d84677a4897a88043eddb657844a08 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 22 Aug 2024 21:12:17 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray_array_testing/reduction.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/xarray_array_testing/reduction.py b/xarray_array_testing/reduction.py index 3b5c23d..54af662 100644 --- a/xarray_array_testing/reduction.py +++ b/xarray_array_testing/reduction.py @@ -1,8 +1,7 @@ from contextlib import nullcontext -import pytest - import hypothesis.strategies as st +import pytest import xarray.testing.strategies as xrst from hypothesis import given