Skip to content

Commit 003ffaf

Browse files
authored
simple test classes for creation and reduction (#2)
* first draft of the variable creation tests * reduction tests * missing test dependency * also allow python 3.11 * remove left-over class attribute * move the configuration hooks to a abstract base class * typing
1 parent a663bdc commit 003ffaf

File tree

6 files changed

+110
-3
lines changed

6 files changed

+110
-3
lines changed

ci/requirements/environment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ name: xarray-array-testing-tests
22
channels:
33
- conda-forge
44
dependencies:
5-
- python=3.12
65
- ipython
76
- pre-commit
87
- pytest
98
- pytest-reportlog
9+
- pytest-cov
1010
- hypothesis
1111
- xarray
1212
- numpy

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "xarray-array-testing"
3-
requires-python = ">= 3.12"
3+
requires-python = ">= 3.11"
44
license = {text = "Apache-2.0"}
55
dependencies = [
66
"hypothesis",
@@ -51,7 +51,7 @@ ignore = [
5151
"E501", # E501: line too long - let black worry about that
5252
"E731", # E731: do not assign a lambda expression, use a def
5353
]
54-
fixable = ["I"]
54+
fixable = ["I", "TID"]
5555
extend-safe-fixes = [
5656
"TID252", # absolute imports
5757
]

xarray_array_testing/base.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import abc
2+
from abc import ABC
3+
from types import ModuleType
4+
5+
import numpy.testing as npt
6+
from xarray.namedarray._typing import duckarray
7+
8+
9+
class DuckArrayTestMixin(ABC):
10+
@property
11+
@abc.abstractmethod
12+
def xp() -> ModuleType:
13+
pass
14+
15+
@property
16+
@abc.abstractmethod
17+
def array_type(self) -> type[duckarray]:
18+
pass
19+
20+
@staticmethod
21+
@abc.abstractmethod
22+
def array_strategy_fn(*, shape, dtype):
23+
raise NotImplementedError("has to be overridden")
24+
25+
@staticmethod
26+
def assert_equal(a, b):
27+
npt.assert_equal(a, b)

xarray_array_testing/creation.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import hypothesis.strategies as st
2+
import xarray.testing.strategies as xrst
3+
from hypothesis import given
4+
5+
from xarray_array_testing.base import DuckArrayTestMixin
6+
7+
8+
class CreationTests(DuckArrayTestMixin):
9+
@given(st.data())
10+
def test_create_variable(self, data):
11+
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))
12+
13+
assert isinstance(variable.data, self.array_type)

xarray_array_testing/reduction.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from contextlib import nullcontext
2+
3+
import hypothesis.strategies as st
4+
import xarray.testing.strategies as xrst
5+
from hypothesis import given
6+
7+
from xarray_array_testing.base import DuckArrayTestMixin
8+
9+
10+
class ReductionTests(DuckArrayTestMixin):
11+
@staticmethod
12+
def expected_errors(op, **parameters):
13+
return nullcontext()
14+
15+
@given(st.data())
16+
def test_variable_mean(self, data):
17+
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))
18+
19+
with self.expected_errors("mean", variable=variable):
20+
actual = variable.mean().data
21+
expected = self.xp.mean(variable.data)
22+
23+
self.assert_equal(actual, expected)
24+
25+
@given(st.data())
26+
def test_variable_prod(self, data):
27+
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))
28+
29+
with self.expected_errors("prod", variable=variable):
30+
actual = variable.prod().data
31+
expected = self.xp.prod(variable.data)
32+
33+
self.assert_equal(actual, expected)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from types import ModuleType
2+
3+
import hypothesis.strategies as st
4+
import numpy as np
5+
6+
from xarray_array_testing.base import DuckArrayTestMixin
7+
from xarray_array_testing.creation import CreationTests
8+
from xarray_array_testing.reduction import ReductionTests
9+
10+
11+
def create_numpy_array(*, shape, dtype):
12+
return st.builds(np.ones, shape=st.just(shape), dtype=st.just(dtype))
13+
14+
15+
class NumpyTestMixin(DuckArrayTestMixin):
16+
@property
17+
def xp(self) -> ModuleType:
18+
return np
19+
20+
@property
21+
def array_type(self) -> type[np.ndarray]:
22+
return np.ndarray
23+
24+
@staticmethod
25+
def array_strategy_fn(*, shape, dtype):
26+
return create_numpy_array(shape=shape, dtype=dtype)
27+
28+
29+
class TestCreationNumpy(CreationTests, NumpyTestMixin):
30+
pass
31+
32+
33+
class TestReductionNumpy(ReductionTests, NumpyTestMixin):
34+
pass

0 commit comments

Comments
 (0)