|
| 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 | +@st.composite |
| 11 | +def scalar_indexers(draw, sizes): |
| 12 | + # TODO: try to define this using builds and flatmap |
| 13 | + possible_indexers = { |
| 14 | + dim: st.integers(min_value=-size, max_value=size - 1) |
| 15 | + for dim, size in sizes.items() |
| 16 | + } |
| 17 | + indexers = xrst.unique_subset_of(possible_indexers) |
| 18 | + return {dim: draw(indexer) for dim, indexer in draw(indexers).items()} |
| 19 | + |
| 20 | + |
| 21 | +class IndexingTests(DuckArrayTestMixin): |
| 22 | + @staticmethod |
| 23 | + def expected_errors(op, **parameters): |
| 24 | + return nullcontext() |
| 25 | + |
| 26 | + @given(st.data()) |
| 27 | + def test_variable_scalar_isel(self, data): |
| 28 | + variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn)) |
| 29 | + indexers = data.draw(scalar_indexers(sizes=variable.sizes)) |
| 30 | + |
| 31 | + with self.expected_errors("scalar_isel", variable=variable): |
| 32 | + actual = variable.isel(indexers).data |
| 33 | + |
| 34 | + raw_indexers = { |
| 35 | + dim: indexers.get(dim, slice(None)) for dim in variable.dims |
| 36 | + } |
| 37 | + expected = variable.data[*raw_indexers.values()] |
| 38 | + |
| 39 | + assert isinstance(actual, self.array_type), f"wrong type: {type(actual)}" |
| 40 | + self.assert_equal(actual, expected) |
0 commit comments