1- import pathlib
21import warnings
32from typing import Literal
43
2726from zarr .storage .memory import MemoryStore
2827
2928
30- def test_create_array (memory_store : Store ) -> None :
31- store = memory_store
32-
29+ @pytest .mark .parametrize ("store" , ["local" , "memory" , "remote" , "zip" ], indirect = True )
30+ def test_create_array (store : Store ) -> None :
3331 # create array
3432 z = create (shape = 100 , store = store )
3533 assert isinstance (z , Array )
@@ -48,23 +46,23 @@ def test_create_array(memory_store: Store) -> None:
4846 assert z .chunks == (40 ,)
4947
5048
49+ @pytest .mark .parametrize ("store" , ["local" , "memory" , "remote" , "zip" ], indirect = True )
5150@pytest .mark .parametrize ("path" , ["foo" , "/" , "/foo" , "///foo/bar" ])
5251@pytest .mark .parametrize ("node_type" , ["array" , "group" ])
5352def test_open_normalized_path (
54- memory_store : MemoryStore , path : str , node_type : Literal ["array" , "group" ]
53+ store : Store , path : str , node_type : Literal ["array" , "group" ]
5554) -> None :
5655 node : Group | Array
5756 if node_type == "group" :
58- node = group (store = memory_store , path = path )
57+ node = group (store = store , path = path )
5958 elif node_type == "array" :
60- node = create (store = memory_store , path = path , shape = (2 ,))
59+ node = create (store = store , path = path , shape = (2 ,))
6160
6261 assert node .path == normalize_path (path )
6362
6463
65- async def test_open_array (memory_store : MemoryStore ) -> None :
66- store = memory_store
67-
64+ @pytest .mark .parametrize ("store" , ["local" , "memory" , "remote" , "zip" ], indirect = True )
65+ async def test_open_array (store : Store ) -> None :
6866 # open array, create if doesn't exist
6967 z = open (store = store , shape = 100 )
7068 assert isinstance (z , Array )
@@ -90,9 +88,8 @@ async def test_open_array(memory_store: MemoryStore) -> None:
9088 open (store = "doesnotexist" , mode = "r" )
9189
9290
93- async def test_open_group (memory_store : MemoryStore ) -> None :
94- store = memory_store
95-
91+ @pytest .mark .parametrize ("store" , ["local" , "memory" , "remote" , "zip" ], indirect = True )
92+ async def test_open_group (store : Store ) -> None :
9693 # open group, create if doesn't exist
9794 g = open_group (store = store )
9895 g .create_group ("foo" )
@@ -105,26 +102,44 @@ async def test_open_group(memory_store: MemoryStore) -> None:
105102 # assert "foo" not in g
106103
107104 # open group, read-only
108- store_cls = type (store )
109- ro_store = await store_cls .open (store_dict = store ._store_dict , mode = "r" )
105+ ro_store = store .with_mode ("r" )
110106 g = open_group (store = ro_store )
111107 assert isinstance (g , Group )
112108 # assert g.read_only
113109
114110
111+ @pytest .mark .parametrize (
112+ "store" ,
113+ ["local" , "memory" , "remote" , pytest .param ("zip" , marks = pytest .mark .xfail )],
114+ indirect = True ,
115+ )
116+ async def test_open_array_or_group (zarr_format : ZarrFormat , store : Store ) -> None :
117+ # create a group and an array
118+ grp_attrs = {"foo" : "bar" }
119+ grp_w = group (store = store , path = "group" , zarr_format = zarr_format , attributes = grp_attrs )
120+ arr_w = grp_w .create_array (name = "foo" , shape = (1 ,))
121+
122+ grp_r = open (store = store , path = "group" , mode = "r" , zarr_format = zarr_format )
123+ assert isinstance (grp_r , Group )
124+ assert grp_r .attrs == grp_attrs
125+
126+ arr_r = open (store = store , path = "group/foo" , mode = "r" , zarr_format = zarr_format )
127+ assert isinstance (arr_r , Array )
128+ assert arr_r .shape == arr_w .shape
129+
130+
131+ @pytest .mark .parametrize ("store" , ["local" , "memory" , "remote" , "zip" ], indirect = True )
115132@pytest .mark .parametrize ("zarr_format" , [None , 2 , 3 ])
116- async def test_open_group_unspecified_version (
117- tmpdir : pathlib .Path , zarr_format : ZarrFormat
118- ) -> None :
133+ async def test_open_group_unspecified_version (store : Store , zarr_format : ZarrFormat ) -> None :
119134 """Regression test for https://github.com/zarr-developers/zarr-python/issues/2175"""
120135
121136 # create a group with specified zarr format (could be 2, 3, or None)
122137 _ = await zarr .api .asynchronous .open_group (
123- store = str ( tmpdir ) , mode = "w" , zarr_format = zarr_format , attributes = {"foo" : "bar" }
138+ store = store , mode = "w" , zarr_format = zarr_format , attributes = {"foo" : "bar" }
124139 )
125140
126141 # now open that group without specifying the format
127- g2 = await zarr .api .asynchronous .open_group (store = str ( tmpdir ) , mode = "r" )
142+ g2 = await zarr .api .asynchronous .open_group (store , mode = "r" )
128143
129144 assert g2 .attrs == {"foo" : "bar" }
130145
@@ -144,66 +159,71 @@ def test_save_errors() -> None:
144159 save ("data/group.zarr" )
145160
146161
147- def test_open_with_mode_r (tmp_path : pathlib .Path ) -> None :
162+ @pytest .mark .parametrize ("store" , ["local" , "memory" , "remote" , "zip" ], indirect = True )
163+ def test_open_with_mode_r (store : Store ) -> None :
148164 # 'r' means read only (must exist)
149165 with pytest .raises (FileNotFoundError ):
150- zarr .open (store = tmp_path , mode = "r" )
151- z1 = zarr .ones (store = tmp_path , shape = (3 , 3 ))
166+ zarr .open (store = store , mode = "r" )
167+ z1 = zarr .ones (store = store , shape = (3 , 3 ))
152168 assert z1 .fill_value == 1
153- z2 = zarr .open (store = tmp_path , mode = "r" )
169+ z2 = zarr .open (store = store , mode = "r" )
154170 assert isinstance (z2 , Array )
155171 assert z2 .fill_value == 1
156172 assert (z2 [:] == 1 ).all ()
157173 with pytest .raises (ValueError ):
158174 z2 [:] = 3
159175
160176
161- def test_open_with_mode_r_plus (tmp_path : pathlib .Path ) -> None :
177+ @pytest .mark .parametrize ("store" , ["local" , "memory" , "remote" , "zip" ], indirect = True )
178+ def test_open_with_mode_r_plus (store : Store ) -> None :
162179 # 'r+' means read/write (must exist)
163180 with pytest .raises (FileNotFoundError ):
164- zarr .open (store = tmp_path , mode = "r+" )
165- zarr .ones (store = tmp_path , shape = (3 , 3 ))
166- z2 = zarr .open (store = tmp_path , mode = "r+" )
181+ zarr .open (store = store , mode = "r+" )
182+ zarr .ones (store = store , shape = (3 , 3 ))
183+ z2 = zarr .open (store = store , mode = "r+" )
167184 assert isinstance (z2 , Array )
168185 assert (z2 [:] == 1 ).all ()
169186 z2 [:] = 3
170187
171188
172- async def test_open_with_mode_a (tmp_path : pathlib .Path ) -> None :
189+ @pytest .mark .parametrize ("store" , ["local" , "memory" , "remote" , "zip" ], indirect = True )
190+ async def test_open_with_mode_a (store : Store ) -> None :
173191 # Open without shape argument should default to group
174- g = zarr .open (store = tmp_path , mode = "a" )
192+ g = zarr .open (store = store , mode = "a" )
175193 assert isinstance (g , Group )
176194 await g .store_path .delete ()
177195
178196 # 'a' means read/write (create if doesn't exist)
179- arr = zarr .open (store = tmp_path , mode = "a" , shape = (3 , 3 ))
197+ arr = zarr .open (store = store , mode = "a" , shape = (3 , 3 ))
180198 assert isinstance (arr , Array )
181199 arr [...] = 1
182- z2 = zarr .open (store = tmp_path , mode = "a" )
200+ z2 = zarr .open (store = store , mode = "a" )
183201 assert isinstance (z2 , Array )
184202 assert (z2 [:] == 1 ).all ()
185203 z2 [:] = 3
186204
187205
188- def test_open_with_mode_w (tmp_path : pathlib .Path ) -> None :
206+ @pytest .mark .parametrize ("store" , ["local" , "memory" , "remote" , "zip" ], indirect = True )
207+ def test_open_with_mode_w (store : Store ) -> None :
189208 # 'w' means create (overwrite if exists);
190- arr = zarr .open (store = tmp_path , mode = "w" , shape = (3 , 3 ))
209+ arr = zarr .open (store = store , mode = "w" , shape = (3 , 3 ))
191210 assert isinstance (arr , Array )
192211
193212 arr [...] = 3
194- z2 = zarr .open (store = tmp_path , mode = "w" , shape = (3 , 3 ))
213+ z2 = zarr .open (store = store , mode = "w" , shape = (3 , 3 ))
195214 assert isinstance (z2 , Array )
196215 assert not (z2 [:] == 3 ).all ()
197216 z2 [:] = 3
198217
199218
200- def test_open_with_mode_w_minus (tmp_path : pathlib .Path ) -> None :
219+ @pytest .mark .parametrize ("store" , ["local" , "memory" , "remote" , "zip" ], indirect = True )
220+ def test_open_with_mode_w_minus (store : Store ) -> None :
201221 # 'w-' means create (fail if exists)
202- arr = zarr .open (store = tmp_path , mode = "w-" , shape = (3 , 3 ))
222+ arr = zarr .open (store = store , mode = "w-" , shape = (3 , 3 ))
203223 assert isinstance (arr , Array )
204224 arr [...] = 1
205225 with pytest .raises (FileExistsError ):
206- zarr .open (store = tmp_path , mode = "w-" )
226+ zarr .open (store = store , mode = "w-" )
207227
208228
209229@pytest .mark .parametrize ("order" , ["C" , "F" , None ])
@@ -238,8 +258,8 @@ def test_array_order(order: MemoryOrder | None, zarr_format: ZarrFormat) -> None
238258# assert "LazyLoader: " in repr(loader)
239259
240260
241- def test_load_array ( memory_store : Store ) -> None :
242- store = memory_store
261+ @ pytest . mark . parametrize ( "store" , [ "local" , "memory" , "remote" , "zip" ], indirect = True )
262+ def test_load_array ( store : Store ) -> None :
243263 foo = np .arange (100 )
244264 bar = np .arange (100 , 0 , - 1 )
245265 save (store , foo = foo , bar = bar )
@@ -967,19 +987,19 @@ def test_open_group_positional_args_deprecated() -> None:
967987 open_group (store , "w" )
968988
969989
970- def test_open_falls_back_to_open_group () -> None :
990+ @pytest .mark .parametrize ("store" , ["local" , "memory" , "remote" , "zip" ], indirect = True )
991+ def test_open_falls_back_to_open_group (store : Store ) -> None :
971992 # https://github.com/zarr-developers/zarr-python/issues/2309
972- store = MemoryStore (mode = "w" )
973993 zarr .open_group (store , attributes = {"key" : "value" })
974994
975995 group = zarr .open (store )
976996 assert isinstance (group , Group )
977997 assert group .attrs == {"key" : "value" }
978998
979999
980- async def test_open_falls_back_to_open_group_async () -> None :
1000+ @pytest .mark .parametrize ("store" , ["local" , "memory" , "remote" , "zip" ], indirect = True )
1001+ async def test_open_falls_back_to_open_group_async (store : Store ) -> None :
9811002 # https://github.com/zarr-developers/zarr-python/issues/2309
982- store = MemoryStore (mode = "w" )
9831003 await zarr .api .asynchronous .open_group (store , attributes = {"key" : "value" })
9841004
9851005 group = await zarr .api .asynchronous .open (store = store )
0 commit comments