File tree Expand file tree Collapse file tree 6 files changed +110
-3
lines changed Expand file tree Collapse file tree 6 files changed +110
-3
lines changed Original file line number Diff line number Diff line change @@ -2,11 +2,11 @@ name: xarray-array-testing-tests
22channels :
33 - conda-forge
44dependencies :
5- - python=3.12
65 - ipython
76 - pre-commit
87 - pytest
98 - pytest-reportlog
9+ - pytest-cov
1010 - hypothesis
1111 - xarray
1212 - numpy
Original file line number Diff line number Diff line change 11[project ]
22name = " xarray-array-testing"
3- requires-python = " >= 3.12 "
3+ requires-python = " >= 3.11 "
44license = {text = " Apache-2.0" }
55dependencies = [
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 " ]
5555extend-safe-fixes = [
5656 " TID252" , # absolute imports
5757]
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments