Skip to content

Commit 4f40792

Browse files
committed
refactor TestBackendIndexing to combine sync and async checks in one function
1 parent 432bbd5 commit 4f40792

File tree

1 file changed

+33
-49
lines changed

1 file changed

+33
-49
lines changed

xarray/tests/test_variable.py

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2897,34 +2897,31 @@ def setUp(self):
28972897
self.d = np.random.random((10, 3)).astype(np.float64)
28982898
self.cat = PandasExtensionArray(pd.Categorical(["a", "b"] * 5))
28992899

2900-
def check_orthogonal_indexing(self, v):
2901-
result = v.isel(x=[8, 3], y=[2, 1])
2900+
async def check_orthogonal_indexing(self, v):
29022901
expected = self.d[[8, 3]][:, [2, 1]]
2902+
2903+
result = v.isel(x=[8, 3], y=[2, 1])
29032904
assert np.allclose(result, expected)
29042905

2905-
async def check_orthogonal_async_indexing(self, v):
29062906
result = await v.isel(x=[8, 3], y=[2, 1]).load_async()
2907-
expected = self.d[[8, 3]][:, [2, 1]]
29082907
assert np.allclose(result, expected)
29092908

2910-
def check_vectorized_indexing(self, v):
2909+
async def check_vectorized_indexing(self, v):
29112910
ind_x = Variable("z", [0, 2])
29122911
ind_y = Variable("z", [2, 1])
2913-
result = v.isel(x=ind_x, y=ind_y)
29142912
expected = self.d[ind_x, ind_y]
2913+
2914+
result = v.isel(x=ind_x, y=ind_y).load()
29152915
assert np.allclose(result, expected)
29162916

2917-
async def check_vectorized_async_indexing(self, v):
2918-
ind_x = Variable("z", [0, 2])
2919-
ind_y = Variable("z", [2, 1])
29202917
result = await v.isel(x=ind_x, y=ind_y).load_async()
2921-
expected = self.d[ind_x, ind_y]
29222918
assert np.allclose(result, expected)
29232919

2924-
def test_NumpyIndexingAdapter(self):
2920+
@pytest.mark.asyncio
2921+
async def test_NumpyIndexingAdapter(self):
29252922
v = Variable(dims=("x", "y"), data=NumpyIndexingAdapter(self.d))
2926-
self.check_orthogonal_indexing(v)
2927-
self.check_vectorized_indexing(v)
2923+
await self.check_orthogonal_indexing(v)
2924+
await self.check_vectorized_indexing(v)
29282925
# could not doubly wrapping
29292926
with pytest.raises(TypeError, match=r"NumpyIndexingAdapter only wraps "):
29302927
v = Variable(
@@ -2939,71 +2936,58 @@ def test_extension_array_duck_indexed(self):
29392936
lazy = Variable(dims=("x"), data=LazilyIndexedArray(self.cat))
29402937
assert (lazy[[0, 1, 5]] == ["a", "b", "b"]).all()
29412938

2942-
def test_LazilyIndexedArray(self):
2943-
v = Variable(dims=("x", "y"), data=LazilyIndexedArray(self.d))
2944-
self.check_orthogonal_indexing(v)
2945-
self.check_vectorized_indexing(v)
2946-
# doubly wrapping
2947-
v = Variable(
2948-
dims=("x", "y"),
2949-
data=LazilyIndexedArray(LazilyIndexedArray(self.d)),
2950-
)
2951-
self.check_orthogonal_indexing(v)
2952-
# hierarchical wrapping
2953-
v = Variable(
2954-
dims=("x", "y"), data=LazilyIndexedArray(NumpyIndexingAdapter(self.d))
2955-
)
2956-
self.check_orthogonal_indexing(v)
2957-
29582939
@pytest.mark.asyncio
2959-
async def test_lazy_async_indexing(self) -> None:
2940+
async def test_LazilyIndexedArray(self):
29602941
v = Variable(dims=("x", "y"), data=LazilyIndexedArray(self.d))
2961-
await self.check_orthogonal_async_indexing(v)
2962-
await self.check_vectorized_async_indexing(v)
2942+
await self.check_orthogonal_indexing(v)
2943+
await self.check_vectorized_indexing(v)
29632944
# doubly wrapping
29642945
v = Variable(
29652946
dims=("x", "y"),
29662947
data=LazilyIndexedArray(LazilyIndexedArray(self.d)),
29672948
)
2968-
await self.check_orthogonal_async_indexing(v)
2949+
await self.check_orthogonal_indexing(v)
29692950
# hierarchical wrapping
29702951
v = Variable(
29712952
dims=("x", "y"), data=LazilyIndexedArray(NumpyIndexingAdapter(self.d))
29722953
)
2973-
await self.check_orthogonal_async_indexing(v)
2954+
await self.check_orthogonal_indexing(v)
29742955

2975-
def test_CopyOnWriteArray(self):
2956+
@pytest.mark.asyncio
2957+
async def test_CopyOnWriteArray(self):
29762958
v = Variable(dims=("x", "y"), data=CopyOnWriteArray(self.d))
2977-
self.check_orthogonal_indexing(v)
2978-
self.check_vectorized_indexing(v)
2959+
await self.check_orthogonal_indexing(v)
2960+
await self.check_vectorized_indexing(v)
29792961
# doubly wrapping
29802962
v = Variable(dims=("x", "y"), data=CopyOnWriteArray(LazilyIndexedArray(self.d)))
2981-
self.check_orthogonal_indexing(v)
2982-
self.check_vectorized_indexing(v)
2963+
await self.check_orthogonal_indexing(v)
2964+
await self.check_vectorized_indexing(v)
29832965

2984-
def test_MemoryCachedArray(self):
2966+
@pytest.mark.asyncio
2967+
async def test_MemoryCachedArray(self):
29852968
v = Variable(dims=("x", "y"), data=MemoryCachedArray(self.d))
2986-
self.check_orthogonal_indexing(v)
2987-
self.check_vectorized_indexing(v)
2969+
await self.check_orthogonal_indexing(v)
2970+
await self.check_vectorized_indexing(v)
29882971
# doubly wrapping
29892972
v = Variable(dims=("x", "y"), data=CopyOnWriteArray(MemoryCachedArray(self.d)))
2990-
self.check_orthogonal_indexing(v)
2991-
self.check_vectorized_indexing(v)
2973+
await self.check_orthogonal_indexing(v)
2974+
await self.check_vectorized_indexing(v)
29922975

29932976
@requires_dask
2994-
def test_DaskIndexingAdapter(self):
2977+
@pytest.mark.asyncio
2978+
async def test_DaskIndexingAdapter(self):
29952979
import dask.array as da
29962980

29972981
dask_array = da.asarray(self.d)
29982982
v = Variable(dims=("x", "y"), data=DaskIndexingAdapter(dask_array))
2999-
self.check_orthogonal_indexing(v)
3000-
self.check_vectorized_indexing(v)
2983+
await self.check_orthogonal_indexing(v)
2984+
await self.check_vectorized_indexing(v)
30012985
# doubly wrapping
30022986
v = Variable(
30032987
dims=("x", "y"), data=CopyOnWriteArray(DaskIndexingAdapter(dask_array))
30042988
)
3005-
self.check_orthogonal_indexing(v)
3006-
self.check_vectorized_indexing(v)
2989+
await self.check_orthogonal_indexing(v)
2990+
await self.check_vectorized_indexing(v)
30072991

30082992

30092993
def test_clip(var):

0 commit comments

Comments
 (0)