Skip to content

Commit

Permalink
Parametrize over different reduction operations (#5)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
TomNicholas and pre-commit-ci[bot] authored Aug 22, 2024
1 parent 003ffaf commit 491273d
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions xarray_array_testing/reduction.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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.<OP>()
actual = getattr(variable, op)().data
# compute using xp.<OP>(array)
expected = getattr(self.xp, op)(variable.data)

self.assert_equal(actual, expected)

0 comments on commit 491273d

Please sign in to comment.