22import itertools
33
44import asdf
5+ import asdf_zarr
6+ import asdf_zarr .storage
57import numpy
68import pytest
79import zarr
8- from zarr .storage import DirectoryStore , NestedDirectoryStore
10+ from zarr .storage import DirectoryStore , KVStore , MemoryStore , NestedDirectoryStore , TempStore
911
1012
11- def create_zarray (shape = None , chunks = None , dtype = "f8" , store = None ):
13+ def create_zarray (shape = None , chunks = None , dtype = "f8" , store = None , chunk_store = None ):
1214 if shape is None :
1315 shape = (6 , 9 )
1416 if chunks is None :
1517 chunks = [max (1 , d // 3 ) for d in shape ]
16- arr = zarr .creation .create ((6 , 9 ), store = store , chunks = chunks , dtype = dtype , compressor = None )
18+ arr = zarr .creation .create (
19+ (6 , 9 ), store = store , chunk_store = chunk_store , chunks = chunks , dtype = dtype , compressor = None
20+ )
1721 for chunk_index in itertools .product (* [range (c ) for c in arr .cdata_shape ]):
1822 inds = []
1923 for i , c in zip (chunk_index , arr .chunks ):
@@ -25,14 +29,37 @@ def create_zarray(shape=None, chunks=None, dtype="f8", store=None):
2529@pytest .mark .parametrize ("copy_arrays" , [True , False ])
2630@pytest .mark .parametrize ("lazy_load" , [True , False ])
2731@pytest .mark .parametrize ("compression" , ["input" , "zlib" ])
28- @pytest .mark .parametrize ("store_type" , [DirectoryStore , NestedDirectoryStore ])
29- def test_write_to (tmp_path , copy_arrays , lazy_load , compression , store_type ):
30- store1 = store_type (tmp_path / "zarr_array_1" )
31- store2 = store_type (tmp_path / "zarr_array_2" )
32+ @pytest .mark .parametrize ("store_type" , [DirectoryStore , KVStore , MemoryStore , NestedDirectoryStore , TempStore ])
33+ @pytest .mark .parametrize ("to_internal" , [True , False ])
34+ @pytest .mark .parametrize ("meta_store" , [True , False ])
35+ def test_write_to (tmp_path , copy_arrays , lazy_load , compression , store_type , to_internal , meta_store ):
36+ if store_type in (DirectoryStore , NestedDirectoryStore ):
37+ store1 = store_type (tmp_path / "zarr_array_1" )
38+ store2 = store_type (tmp_path / "zarr_array_2" )
39+ elif store_type is KVStore :
40+ store1 = store_type ({})
41+ store2 = store_type ({})
42+ else :
43+ store1 = store_type ()
44+ store2 = store_type ()
45+
46+ # should meta be in a different store?
47+ if meta_store :
48+ chunk_store1 = store1
49+ store1 = KVStore ({})
50+ chunk_store2 = store2
51+ store2 = KVStore ({})
52+ else :
53+ chunk_store1 = None
54+ chunk_store2 = None
55+
56+ arr1 = create_zarray (store = store1 , chunk_store = chunk_store1 )
57+ arr2 = create_zarray (store = store2 , chunk_store = chunk_store2 )
3258
33- arr1 = create_zarray (store = store1 )
34- arr2 = create_zarray (store = store2 )
3559 arr2 [:] = arr2 [:] * - 2
60+ if to_internal :
61+ arr1 = asdf_zarr .storage .to_internal (arr1 )
62+ arr2 = asdf_zarr .storage .to_internal (arr2 )
3663 tree = {"arr1" : arr1 , "arr2" : arr2 }
3764
3865 fn = tmp_path / "test.asdf"
@@ -42,8 +69,10 @@ def test_write_to(tmp_path, copy_arrays, lazy_load, compression, store_type):
4269 with asdf .open (fn , mode = "r" , copy_arrays = copy_arrays , lazy_load = lazy_load ) as af :
4370 for n , a in (("arr1" , arr1 ), ("arr2" , arr2 )):
4471 assert isinstance (af [n ], zarr .core .Array )
45- # for these tests, data should not be converted to a different storage format
46- assert isinstance (af [n ].chunk_store , store_type )
72+ if to_internal or store_type in (KVStore , MemoryStore , TempStore ):
73+ assert isinstance (af [n ].chunk_store , asdf_zarr .storage .InternalStore )
74+ else :
75+ assert isinstance (af [n ].chunk_store , store_type )
4776 assert numpy .allclose (af [n ], a )
4877
4978
0 commit comments