18
18
from zarr .core import Array
19
19
from zarr .hierarchy import Group
20
20
from zarr .attrs import Attributes
21
+ from zarr .errors import ReadOnlyError
21
22
22
23
23
24
# noinspection PyStatementEffect
@@ -187,6 +188,45 @@ def test_create_dataset(self):
187
188
eq ('/bar' , d3 .name )
188
189
assert_is (store , d3 .store )
189
190
191
+ def test_create_errors (self ):
192
+ store = self .create_store ()
193
+ init_group (store )
194
+
195
+ # array obstructs group, array
196
+ g = Group (store = store )
197
+ g .create_dataset ('foo' , shape = 100 , chunks = 10 )
198
+ with assert_raises (KeyError ):
199
+ g .create_group ('foo/bar' )
200
+ with assert_raises (KeyError ):
201
+ g .require_group ('foo/bar' )
202
+ with assert_raises (KeyError ):
203
+ g .create_dataset ('foo/bar' , shape = 100 , chunks = 10 )
204
+
205
+ # array obstructs group, array
206
+ g .create_dataset ('a/b' , shape = 100 , chunks = 10 )
207
+ with assert_raises (KeyError ):
208
+ g .create_group ('a/b' )
209
+ with assert_raises (KeyError ):
210
+ g .require_group ('a/b' )
211
+ with assert_raises (KeyError ):
212
+ g .create_dataset ('a/b' , shape = 100 , chunks = 10 )
213
+
214
+ # group obstructs array
215
+ g .create_group ('c/d' )
216
+ with assert_raises (KeyError ):
217
+ g .create_dataset ('c' , shape = 100 , chunks = 10 )
218
+ with assert_raises (KeyError ):
219
+ g .create_dataset ('c/d' , shape = 100 , chunks = 10 )
220
+
221
+ # read-only
222
+ g = Group (store = store , readonly = True )
223
+ with assert_raises (ReadOnlyError ):
224
+ g .create_group ('zzz' )
225
+ with assert_raises (ReadOnlyError ):
226
+ g .require_group ('zzz' )
227
+ with assert_raises (ReadOnlyError ):
228
+ g .create_dataset ('zzz' , shape = 100 , chunks = 10 )
229
+
190
230
def test_getitem_contains_iterators (self ):
191
231
# setup
192
232
store = self .create_store ()
@@ -243,18 +283,67 @@ def test_getitem_contains_iterators(self):
243
283
eq (1 , len (g1 ['a' ]))
244
284
eq (1 , len (g1 ['a/b' ]))
245
285
246
- # test keys()
247
- eq (['a' , 'foo' ], sorted (g1 .keys ()))
248
- eq (['bar' , 'baz' ], sorted (g1 ['foo' ].keys ()))
286
+ # test __iter__, keys()
287
+ # currently assumes sorted by key
288
+
289
+ eq (['a' , 'foo' ], list (g1 ))
290
+ eq (['a' , 'foo' ], list (g1 .keys ()))
291
+ eq (['bar' , 'baz' ], list (g1 ['foo' ]))
292
+ eq (['bar' , 'baz' ], list (g1 ['foo' ].keys ()))
293
+ eq ([], sorted (g1 ['foo/bar' ]))
249
294
eq ([], sorted (g1 ['foo/bar' ].keys ()))
250
295
296
+ # test items(), values()
297
+ # currently assumes sorted by key
298
+
299
+ items = list (g1 .items ())
300
+ values = list (g1 .values ())
301
+ eq ('a' , items [0 ][0 ])
302
+ eq (g1 ['a' ], items [0 ][1 ])
303
+ eq (g1 ['a' ], values [0 ])
304
+ eq ('foo' , items [1 ][0 ])
305
+ eq (g1 ['foo' ], items [1 ][1 ])
306
+ eq (g1 ['foo' ], values [1 ])
307
+
308
+ items = list (g1 ['foo' ].items ())
309
+ values = list (g1 ['foo' ].values ())
310
+ eq ('bar' , items [0 ][0 ])
311
+ eq (g1 ['foo' ]['bar' ], items [0 ][1 ])
312
+ eq (g1 ['foo' ]['bar' ], values [0 ])
313
+ eq ('baz' , items [1 ][0 ])
314
+ eq (g1 ['foo' ]['baz' ], items [1 ][1 ])
315
+ eq (g1 ['foo' ]['baz' ], values [1 ])
316
+
317
+ # test array_keys(), arrays(), group_keys(), groups()
318
+ # currently assumes sorted by key
319
+
320
+ eq (['a' , 'foo' ], list (g1 .group_keys ()))
321
+ groups = list (g1 .groups ())
322
+ arrays = list (g1 .arrays ())
323
+ eq ('a' , groups [0 ][0 ])
324
+ eq (g1 ['a' ], groups [0 ][1 ])
325
+ eq ('foo' , groups [1 ][0 ])
326
+ eq (g1 ['foo' ], groups [1 ][1 ])
327
+ eq ([], list (g1 .array_keys ()))
328
+ eq ([], arrays )
329
+
330
+ eq (['bar' ], list (g1 ['foo' ].group_keys ()))
331
+ eq (['baz' ], list (g1 ['foo' ].array_keys ()))
332
+ groups = list (g1 ['foo' ].groups ())
333
+ arrays = list (g1 ['foo' ].arrays ())
334
+ eq ('bar' , groups [0 ][0 ])
335
+ eq (g1 ['foo' ]['bar' ], groups [0 ][1 ])
336
+ eq ('baz' , arrays [0 ][0 ])
337
+ eq (g1 ['foo' ]['baz' ], arrays [0 ][1 ])
338
+
251
339
def test_empty_getitem_contains_iterators (self ):
252
340
# setup
253
341
store = self .create_store ()
254
342
init_group (store )
255
343
g = Group (store = store )
256
344
257
345
# test
346
+ eq ([], list (g ))
258
347
eq ([], list (g .keys ()))
259
348
eq (0 , len (g ))
260
349
assert 'foo' not in g
@@ -263,9 +352,32 @@ def test_group_repr(self):
263
352
store = self .create_store ()
264
353
init_group (store )
265
354
g = Group (store = store )
266
- expect = 'zarr.hierarchy.Group(/, 0)\n store: builtins.dict'
355
+ store_class = '%s.%s' % (dict .__module__ , dict .__name__ )
356
+ expect = 'zarr.hierarchy.Group(/, 0)\n store: %s' % store_class
267
357
actual = repr (g )
268
358
eq (expect , actual )
359
+ g .create_group ('foo' )
360
+ g .create_group ('bar' )
361
+ g .create_group ('y' * 80 )
362
+ g .create_dataset ('baz' , shape = 100 , chunks = 10 )
363
+ g .create_dataset ('quux' , shape = 100 , chunks = 10 )
364
+ g .create_dataset ('z' * 80 , shape = 100 , chunks = 10 )
365
+ expect = \
366
+ 'zarr.hierarchy.Group(/, 6)\n ' \
367
+ ' arrays: 3; baz, quux, ' \
368
+ 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz...\n ' \
369
+ ' groups: 3; bar, foo, ' \
370
+ 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy...\n ' \
371
+ ' store: %s' % store_class
372
+ actual = repr (g )
373
+ eq (expect , actual )
374
+
375
+ def test_setitem (self ):
376
+ store = self .create_store ()
377
+ init_group (store )
378
+ g = Group (store = store )
379
+ with assert_raises (NotImplementedError ):
380
+ g ['foo' ] = 'bar'
269
381
270
382
271
383
class TestGroupDictStore (TestGroup ):
0 commit comments