From 491273d9a868ea48209765824f37194b85399e71 Mon Sep 17 00:00:00 2001 From: Tom Nicholas Date: Thu, 22 Aug 2024 15:17:06 -0600 Subject: [PATCH] Parametrize over different reduction operations (#5) * parametrize over different reduction operations * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- xarray_array_testing/reduction.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/xarray_array_testing/reduction.py b/xarray_array_testing/reduction.py index 8e58949..54af662 100644 --- a/xarray_array_testing/reduction.py +++ b/xarray_array_testing/reduction.py @@ -1,6 +1,7 @@ from contextlib import nullcontext import hypothesis.strategies as st +import pytest import xarray.testing.strategies as xrst from hypothesis import given @@ -12,22 +13,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): + def test_variable_mean(self, op, 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): - 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)