|
7 | 7 | import numpy as np |
8 | 8 | import pytest |
9 | 9 |
|
| 10 | +from zarr import consolidate_metadata, create_group |
10 | 11 | from zarr.codecs.bytes import BytesCodec |
11 | 12 | from zarr.core.buffer import default_buffer_prototype |
12 | 13 | from zarr.core.chunk_key_encodings import DefaultChunkKeyEncoding, V2ChunkKeyEncoding |
|
21 | 22 | parse_dimension_names, |
22 | 23 | parse_zarr_format, |
23 | 24 | ) |
24 | | -from zarr.errors import MetadataValidationError, NodeTypeValidationError, UnknownCodecError |
| 25 | +from zarr.errors import ( |
| 26 | + MetadataValidationError, |
| 27 | + NodeTypeValidationError, |
| 28 | + UnknownCodecError, |
| 29 | + ZarrUserWarning, |
| 30 | +) |
25 | 31 |
|
26 | 32 | if TYPE_CHECKING: |
27 | 33 | from collections.abc import Sequence |
@@ -338,3 +344,52 @@ def test_parse_codecs_unknown_codec_raises(monkeypatch: pytest.MonkeyPatch) -> N |
338 | 344 | codecs = [{"name": "unknown"}] |
339 | 345 | with pytest.raises(UnknownCodecError): |
340 | 346 | parse_codecs(codecs) |
| 347 | + |
| 348 | + |
| 349 | +@pytest.mark.parametrize("use_consolidated", [True, False]) |
| 350 | +@pytest.mark.parametrize("attributes", [None, {"foo": "bar"}]) |
| 351 | +def test_group_to_dict(use_consolidated: bool, attributes: None | dict[str, Any]) -> None: |
| 352 | + """ |
| 353 | + Test that the output of GroupMetadata.to_dict() is what we expect |
| 354 | + """ |
| 355 | + store: dict[str, object] = {} |
| 356 | + if attributes is None: |
| 357 | + expect_attributes = {} |
| 358 | + else: |
| 359 | + expect_attributes = attributes |
| 360 | + |
| 361 | + group = create_group(store, attributes=attributes, zarr_format=3) |
| 362 | + group.create_group("foo") |
| 363 | + if use_consolidated: |
| 364 | + with pytest.warns( |
| 365 | + ZarrUserWarning, |
| 366 | + match="Consolidated metadata is currently not part in the Zarr format 3 specification.", |
| 367 | + ): |
| 368 | + group = consolidate_metadata(store) |
| 369 | + meta = group.metadata |
| 370 | + expect = { |
| 371 | + "node_type": "group", |
| 372 | + "zarr_format": 3, |
| 373 | + "consolidated_metadata": { |
| 374 | + "kind": "inline", |
| 375 | + "must_understand": False, |
| 376 | + "metadata": { |
| 377 | + "foo": { |
| 378 | + "attributes": {}, |
| 379 | + "zarr_format": 3, |
| 380 | + "node_type": "group", |
| 381 | + "consolidated_metadata": { |
| 382 | + "kind": "inline", |
| 383 | + "metadata": {}, |
| 384 | + "must_understand": False, |
| 385 | + }, |
| 386 | + } |
| 387 | + }, |
| 388 | + }, |
| 389 | + "attributes": expect_attributes, |
| 390 | + } |
| 391 | + else: |
| 392 | + meta = group.metadata |
| 393 | + expect = {"node_type": "group", "zarr_format": 3, "attributes": expect_attributes} |
| 394 | + |
| 395 | + assert meta.to_dict() == expect |
0 commit comments