19
19
20
20
from zarr ._storage .store import (
21
21
v3_api_available ,
22
- _prefix_to_array_key ,
23
- _prefix_to_attrs_key ,
24
- _prefix_to_group_key
25
22
)
26
23
from zarr .core import Array
27
24
from zarr .errors import ArrayNotFoundError , ContainsGroupError
64
61
class TestArray (unittest .TestCase ):
65
62
66
63
version = 2
64
+ root = ''
65
+ KVStoreClass = KVStore
67
66
68
67
def test_array_init (self ):
69
68
70
69
# normal initialization
71
- store = KVStore (dict ())
70
+ store = self . KVStoreClass (dict ())
72
71
init_array (store , shape = 100 , chunks = 10 , dtype = "<f8" )
73
- a = Array (store )
72
+ a = Array (store , zarr_version = self . version )
74
73
assert isinstance (a , Array )
75
74
assert (100 ,) == a .shape
76
75
assert (10 ,) == a .chunks
77
76
assert '' == a .path
78
77
assert a .name is None
79
78
assert a .basename is None
80
79
assert store is a .store
81
- assert "8fecb7a17ea1493d9c1430d04437b4f5b0b34985" == a .hexdigest ()
80
+ if self .version == 2 :
81
+ assert "8fecb7a17ea1493d9c1430d04437b4f5b0b34985" == a .hexdigest ()
82
+ else :
83
+ assert "968dccbbfc0139f703ead2fd1d503ad6e44db307" == a .hexdigest ()
82
84
store .close ()
83
85
84
86
# initialize at path
85
- store = KVStore (dict ())
87
+ store = self . KVStoreClass (dict ())
86
88
init_array (store , shape = 100 , chunks = 10 , path = 'foo/bar' , dtype = '<f8' )
87
- a = Array (store , path = 'foo/bar' )
89
+ a = Array (store , path = 'foo/bar' , zarr_version = self . version )
88
90
assert isinstance (a , Array )
89
91
assert (100 ,) == a .shape
90
92
assert (10 ,) == a .chunks
91
93
assert 'foo/bar' == a .path
92
94
assert '/foo/bar' == a .name
93
95
assert 'bar' == a .basename
94
96
assert store is a .store
95
- assert "8fecb7a17ea1493d9c1430d04437b4f5b0b34985" == a .hexdigest ()
96
-
97
+ if self .version == 2 :
98
+ assert "8fecb7a17ea1493d9c1430d04437b4f5b0b34985" == a .hexdigest ()
99
+ else :
100
+ assert "968dccbbfc0139f703ead2fd1d503ad6e44db307" == a .hexdigest ()
97
101
# store not initialized
98
- store = KVStore (dict ())
102
+ store = self . KVStoreClass (dict ())
99
103
with pytest .raises (ValueError ):
100
- Array (store )
104
+ Array (store , zarr_version = self . version )
101
105
102
106
# group is in the way
103
- store = KVStore (dict ())
107
+ store = self . KVStoreClass (dict ())
104
108
init_group (store , path = 'baz' )
105
109
with pytest .raises (ValueError ):
106
- Array (store , path = 'baz' )
110
+ Array (store , path = 'baz' , zarr_version = self . version )
107
111
108
112
def create_array (self , read_only = False , ** kwargs ):
109
- store = KVStore (dict ())
113
+ store = self . KVStoreClass (dict ())
110
114
kwargs .setdefault ('compressor' , Zlib (level = 1 ))
111
115
cache_metadata = kwargs .pop ('cache_metadata' , True )
112
116
cache_attrs = kwargs .pop ('cache_attrs' , True )
113
117
write_empty_chunks = kwargs .pop ('write_empty_chunks' , True )
114
118
init_array (store , ** kwargs )
115
119
return Array (store , read_only = read_only , cache_metadata = cache_metadata ,
116
- cache_attrs = cache_attrs , write_empty_chunks = write_empty_chunks )
120
+ cache_attrs = cache_attrs , write_empty_chunks = write_empty_chunks ,
121
+ zarr_version = self .version )
117
122
118
123
def test_store_has_text_keys (self ):
119
124
# Initialize array
@@ -162,15 +167,28 @@ def test_nbytes_stored(self):
162
167
163
168
# dict as store
164
169
z = self .create_array (shape = 1000 , chunks = 100 )
165
- expect_nbytes_stored = sum (buffer_size (v ) for v in z .store .values ())
170
+ if self .version == 3 :
171
+ expect_nbytes_stored = sum (
172
+ buffer_size (v ) for k , v in z .store .items () if k != 'zarr.json'
173
+ )
174
+ else :
175
+ expect_nbytes_stored = sum (buffer_size (v ) for v in z .store .values ())
166
176
assert expect_nbytes_stored == z .nbytes_stored
167
177
z [:] = 42
168
- expect_nbytes_stored = sum (buffer_size (v ) for v in z .store .values ())
178
+ if self .version == 3 :
179
+ expect_nbytes_stored = sum (
180
+ buffer_size (v ) for k , v in z .store .items () if k != 'zarr.json'
181
+ )
182
+ else :
183
+ expect_nbytes_stored = sum (buffer_size (v ) for v in z .store .values ())
169
184
assert expect_nbytes_stored == z .nbytes_stored
170
185
171
186
# mess with store
172
187
try :
173
- z .store [z ._key_prefix + 'foo' ] = list (range (10 ))
188
+ if self .version == 2 :
189
+ z .store [z ._key_prefix + 'foo' ] = list (range (10 ))
190
+ else :
191
+ z .store ['meta/root/foo' ] = list (range (10 ))
174
192
assert - 1 == z .nbytes_stored
175
193
except TypeError :
176
194
pass
@@ -1003,7 +1021,7 @@ def test_nchunks_initialized(self):
1003
1021
1004
1022
assert 0 == z .nchunks_initialized
1005
1023
# manually put something into the store to confuse matters
1006
- z .store ['foo' ] = b'bar'
1024
+ z .store [self . root + 'foo' ] = b'bar'
1007
1025
assert 0 == z .nchunks_initialized
1008
1026
z [:] = 42
1009
1027
assert 10 == z .nchunks_initialized
@@ -2703,36 +2721,25 @@ def test_read_from_all_blocks(self):
2703
2721
# StoreV3 test classes inheriting from the above below this point
2704
2722
####
2705
2723
2706
- # Start with TestArrayWithPathV3 not TestArrayV3 since path must be supplied
2707
-
2708
2724
@pytest .mark .skipif (not v3_api_available , reason = "V3 is disabled" )
2709
- class TestArrayV3 (unittest . TestCase ):
2725
+ class TestArrayV3 (TestArray ):
2710
2726
2711
2727
version = 3
2728
+ root = meta_root
2729
+ KVStoreClass = KVStoreV3
2712
2730
2713
- def test_array_init (self ):
2714
-
2715
- # normal initialization
2716
- store = KVStoreV3 (dict ())
2717
- with pytest .raises (ValueError ):
2718
- # cannot init_array for v3 without a path
2719
- init_array (store , shape = 100 , chunks = 10 , dtype = "<f8" )
2720
-
2721
- init_array (store , path = 'x' , shape = 100 , chunks = 10 , dtype = "<f8" )
2722
- with pytest .raises (ValueError ):
2723
- # cannot initialize a v3 array without a path
2724
- Array (store )
2725
-
2726
- def test_prefix_exceptions (self ):
2727
- store = KVStoreV3 (dict ())
2728
- with pytest .raises (ValueError ):
2729
- _prefix_to_array_key (store , '' )
2730
-
2731
- with pytest .raises (ValueError ):
2732
- _prefix_to_group_key (store , '' )
2731
+ def expected (self ):
2732
+ # tests for array without path will not be run for v3 stores
2733
+ assert self .version == 3
2734
+ return [
2735
+ "73ab8ace56719a5c9308c3754f5e2d57bc73dc20" ,
2736
+ "5fb3d02b8f01244721582929b3cad578aec5cea5" ,
2737
+ "26b098bedb640846e18dc2fbc1c27684bb02b532" ,
2738
+ "799a458c287d431d747bec0728987ca4fe764549" ,
2739
+ "c780221df84eb91cb62f633f12d3f1eaa9cee6bd"
2740
+ ]
2733
2741
2734
- with pytest .raises (ValueError ):
2735
- _prefix_to_attrs_key (store , '' )
2742
+ # TODO: fix test_nbytes_stored
2736
2743
2737
2744
2738
2745
@pytest .mark .skipif (not v3_api_available , reason = "V3 is disabled" )
@@ -2754,10 +2761,19 @@ def create_array(array_path='arr1', read_only=False, **kwargs):
2754
2761
2755
2762
def test_array_init (self ):
2756
2763
2757
- # should not be able to initialize without a path in V3
2758
2764
store = KVStoreV3 (dict ())
2759
- with pytest .raises (ValueError ):
2760
- init_array (store , shape = 100 , chunks = 10 , dtype = "<f8" )
2765
+ # can initialize an array without a path
2766
+ init_array (store , shape = 100 , chunks = 10 , dtype = "<f8" )
2767
+ b = Array (store )
2768
+ assert not b .is_view
2769
+ assert isinstance (b , Array )
2770
+ assert (100 ,) == b .shape
2771
+ assert (10 ,) == b .chunks
2772
+ assert '' == b .path
2773
+ assert b .name is None
2774
+ assert b .basename is None
2775
+ assert store is b .store
2776
+ assert "968dccbbfc0139f703ead2fd1d503ad6e44db307" == b .hexdigest ()
2761
2777
2762
2778
# initialize at path
2763
2779
store = KVStoreV3 (dict ())
@@ -2797,11 +2813,6 @@ def test_array_init(self):
2797
2813
assert group_key not in store
2798
2814
assert (meta_root + path + '.array.json' ) in store
2799
2815
2800
- def test_array_no_path (self ):
2801
- # passing path=None to init_array will raise an exception
2802
- with pytest .raises (ValueError ):
2803
- self .create_array (shape = 1000 , chunks = 100 , array_path = None )
2804
-
2805
2816
def expected (self ):
2806
2817
return [
2807
2818
"73ab8ace56719a5c9308c3754f5e2d57bc73dc20" ,
0 commit comments