@@ -1994,3 +1994,110 @@ def test_iter_chunk_regions():
19941994 assert_array_equal (a [region ], np .ones_like (a [region ]))
19951995 a [region ] = 0
19961996 assert_array_equal (a [region ], np .zeros_like (a [region ]))
1997+
1998+
1999+ class TestAsync :
2000+ @pytest .mark .parametrize (
2001+ ("indexer" , "expected" ),
2002+ [
2003+ # int
2004+ ((0 ,), np .array ([1 , 2 ])),
2005+ ((1 ,), np .array ([3 , 4 ])),
2006+ ((0 , 1 ), np .array (2 )),
2007+ # slice
2008+ ((slice (None ),), np .array ([[1 , 2 ], [3 , 4 ]])),
2009+ ((slice (0 , 1 ),), np .array ([[1 , 2 ]])),
2010+ ((slice (1 , 2 ),), np .array ([[3 , 4 ]])),
2011+ ((slice (0 , 2 ),), np .array ([[1 , 2 ], [3 , 4 ]])),
2012+ ((slice (0 , 0 ),), np .empty (shape = (0 , 2 ), dtype = "i8" )),
2013+ # ellipsis
2014+ ((...,), np .array ([[1 , 2 ], [3 , 4 ]])),
2015+ ((0 , ...), np .array ([1 , 2 ])),
2016+ ((..., 0 ), np .array ([1 , 3 ])),
2017+ ((0 , 1 , ...), np .array (2 )),
2018+ # combined
2019+ ((0 , slice (None )), np .array ([1 , 2 ])),
2020+ ((slice (None ), 0 ), np .array ([1 , 3 ])),
2021+ ((slice (None ), slice (None )), np .array ([[1 , 2 ], [3 , 4 ]])),
2022+ # array of ints
2023+ (([0 ]), np .array ([[1 , 2 ]])),
2024+ (([1 ]), np .array ([[3 , 4 ]])),
2025+ (([0 ], [1 ]), np .array (2 )),
2026+ (([0 , 1 ], [0 ]), np .array ([[1 ], [3 ]])),
2027+ (([0 , 1 ], [0 , 1 ]), np .array ([[1 , 2 ], [3 , 4 ]])),
2028+ # boolean array
2029+ (np .array ([True , True ]), np .array ([[1 , 2 ], [3 , 4 ]])),
2030+ (np .array ([True , False ]), np .array ([[1 , 2 ]])),
2031+ (np .array ([False , True ]), np .array ([[3 , 4 ]])),
2032+ (np .array ([False , False ]), np .empty (shape = (0 , 2 ), dtype = "i8" )),
2033+ ],
2034+ )
2035+ @pytest .mark .asyncio
2036+ async def test_async_oindex (self , store , indexer , expected ):
2037+ z = zarr .create_array (store = store , shape = (2 , 2 ), chunks = (1 , 1 ), zarr_format = 3 , dtype = "i8" )
2038+ z [...] = np .array ([[1 , 2 ], [3 , 4 ]])
2039+ async_zarr = z ._async_array
2040+
2041+ result = await async_zarr .oindex .getitem (indexer )
2042+ assert_array_equal (result , expected )
2043+
2044+ @pytest .mark .asyncio
2045+ async def test_async_oindex_with_zarr_array (self , store ):
2046+ z1 = zarr .create_array (store = store , shape = (2 , 2 ), chunks = (1 , 1 ), zarr_format = 3 , dtype = "i8" )
2047+ z1 [...] = np .array ([[1 , 2 ], [3 , 4 ]])
2048+ async_zarr = z1 ._async_array
2049+
2050+ # create boolean zarr array to index with
2051+ z2 = zarr .create_array (
2052+ store = store , name = "z2" , shape = (2 ,), chunks = (1 ,), zarr_format = 3 , dtype = "?"
2053+ )
2054+ z2 [...] = np .array ([True , False ])
2055+
2056+ result = await async_zarr .oindex .getitem (z2 )
2057+ expected = np .array ([[1 , 2 ]])
2058+ assert_array_equal (result , expected )
2059+
2060+ @pytest .mark .parametrize (
2061+ ("indexer" , "expected" ),
2062+ [
2063+ (([0 ], [0 ]), np .array (1 )),
2064+ (([0 , 1 ], [0 , 1 ]), np .array ([1 , 4 ])),
2065+ (np .array ([[False , True ], [False , True ]]), np .array ([2 , 4 ])),
2066+ ],
2067+ )
2068+ @pytest .mark .asyncio
2069+ async def test_async_vindex (self , store , indexer , expected ):
2070+ z = zarr .create_array (store = store , shape = (2 , 2 ), chunks = (1 , 1 ), zarr_format = 3 , dtype = "i8" )
2071+ z [...] = np .array ([[1 , 2 ], [3 , 4 ]])
2072+ async_zarr = z ._async_array
2073+
2074+ result = await async_zarr .vindex .getitem (indexer )
2075+ assert_array_equal (result , expected )
2076+
2077+ @pytest .mark .asyncio
2078+ async def test_async_vindex_with_zarr_array (self , store ):
2079+ z1 = zarr .create_array (store = store , shape = (2 , 2 ), chunks = (1 , 1 ), zarr_format = 3 , dtype = "i8" )
2080+ z1 [...] = np .array ([[1 , 2 ], [3 , 4 ]])
2081+ async_zarr = z1 ._async_array
2082+
2083+ # create boolean zarr array to index with
2084+ z2 = zarr .create_array (
2085+ store = store , name = "z2" , shape = (2 , 2 ), chunks = (1 , 1 ), zarr_format = 3 , dtype = "?"
2086+ )
2087+ z2 [...] = np .array ([[False , True ], [False , True ]])
2088+
2089+ result = await async_zarr .vindex .getitem (z2 )
2090+ expected = np .array ([2 , 4 ])
2091+ assert_array_equal (result , expected )
2092+
2093+ @pytest .mark .asyncio
2094+ async def test_async_invalid_indexer (self , store ):
2095+ z = zarr .create_array (store = store , shape = (2 , 2 ), chunks = (1 , 1 ), zarr_format = 3 , dtype = "i8" )
2096+ z [...] = np .array ([[1 , 2 ], [3 , 4 ]])
2097+ async_zarr = z ._async_array
2098+
2099+ with pytest .raises (IndexError ):
2100+ await async_zarr .vindex .getitem ("invalid_indexer" )
2101+
2102+ with pytest .raises (IndexError ):
2103+ await async_zarr .oindex .getitem ("invalid_indexer" )
0 commit comments