Skip to content

Reduce redundancy between namedarray and variable tests #8405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions xarray/tests/test_namedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,35 @@ def __array_namespace__(self) -> ModuleType:
return np


class NamedArraySubclassobjects:
@pytest.fixture
def target(self):
"""Fixture that needs to be re-declared"""
assert 0

@pytest.fixture
def data(self):
return 0.5 * np.arange(10).reshape(2, 5)

def test_properties(self, target, data):
assert target.dims == ("x", "y")
assert np.array_equal(target.data, data)
assert target.dtype == float
assert target.shape == (2, 5)
assert target.attrs == {"key": "value"}
assert target.ndim == 2
assert target.sizes == {"x": 2, "y": 5}
assert target.size == 10
assert target.nbytes == 80
assert len(target) == 2


class TestNamedArray(NamedArraySubclassobjects):
@pytest.fixture
def target(self, data):
return NamedArray(["x", "y"], data, {"key": "value"})


@pytest.fixture
def random_inputs() -> np.ndarray[Any, np.dtype[np.float32]]:
return np.arange(3 * 4 * 5, dtype=np.float32).reshape((3, 4, 5))
Expand Down Expand Up @@ -137,20 +166,6 @@ def test_from_array_with_explicitly_indexed(
assert isinstance(output2.data, CustomArrayIndexable)


def test_properties() -> None:
data = 0.5 * np.arange(10).reshape(2, 5)
named_array: NamedArray[Any, Any]
named_array = NamedArray(["x", "y"], data, {"key": "value"})
assert named_array.dims == ("x", "y")
assert np.array_equal(np.asarray(named_array.data), data)
assert named_array.attrs == {"key": "value"}
assert named_array.ndim == 2
assert named_array.sizes == {"x": 2, "y": 5}
assert named_array.size == 10
assert named_array.nbytes == 80
assert len(named_array) == 2


def test_attrs() -> None:
named_array: NamedArray[Any, Any]
named_array = NamedArray(["x", "y"], np.arange(10).reshape(2, 5))
Expand Down
20 changes: 6 additions & 14 deletions xarray/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
requires_sparse,
source_ndarray,
)
from xarray.tests.test_namedarray import NamedArraySubclassobjects

dask_array_type = array_type("dask")

Expand All @@ -63,24 +64,15 @@ def var():
return Variable(dims=list("xyz"), data=np.random.rand(3, 4, 5))


class VariableSubclassobjects(ABC):
class VariableSubclassobjects(NamedArraySubclassobjects, ABC):
@abstractmethod
def cls(self, *args, **kwargs) -> Variable:
raise NotImplementedError

def test_properties(self):
data = 0.5 * np.arange(10)
v = self.cls(["time"], data, {"foo": "bar"})
assert v.dims == ("time",)
assert_array_equal(v.values, data)
assert v.dtype == float
assert v.shape == (10,)
assert v.size == 10
assert v.sizes == {"time": 10}
assert v.nbytes == 80
assert v.ndim == 1
assert len(v) == 10
assert v.attrs == {"foo": "bar"}
@pytest.fixture
def target(self, data):
data = 0.5 * np.arange(10).reshape(2, 5)
return Variable(["x", "y"], data, {"key": "value"})

def test_attrs(self):
v = self.cls(["time"], 0.5 * np.arange(10))
Expand Down