Skip to content

Commit d236e53

Browse files
committed
removed dupe tests
1 parent 5a08466 commit d236e53

File tree

4 files changed

+68
-172
lines changed

4 files changed

+68
-172
lines changed

src/zarr/api/asynchronous.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ def _default_zarr_version() -> ZarrFormat:
139139

140140

141141
async def consolidate_metadata(
142-
store: StoreLike, path: str | None = None, zarr_format: ZarrFormat = 3
142+
store: StoreLike,
143+
path: str | None = None,
144+
zarr_format: ZarrFormat | None = None,
143145
) -> AsyncGroup:
144146
"""
145147
Consolidate the metadata of all nodes in a hierarchy.

src/zarr/core/group.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ def from_dict(cls, data: dict[str, JSON]) -> ConsolidatedMetadata:
132132
node_type = v.get("node_type", None)
133133
if node_type == "group":
134134
metadata[k] = GroupMetadata.from_dict(v)
135-
elif node_type == "array":
135+
elif node_type == "array" and v.get("zarr_format") == 3:
136136
metadata[k] = ArrayV3Metadata.from_dict(v)
137+
elif node_type == "array":
138+
metadata[k] = ArrayV2Metadata.from_dict(v)
137139
else:
138140
# We either have V2 metadata, or invalid metadata
139141
if "shape" in v:

tests/v3/test_group.py

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -351,41 +351,66 @@ def test_group_child_iterators(store: Store, zarr_format: ZarrFormat, consolidat
351351

352352
if consolidate:
353353
group = zarr.consolidate_metadata(store)
354+
if zarr_format == 2:
355+
metadata = {
356+
"subarray": {
357+
"attributes": {},
358+
"dtype": "float64",
359+
"fill_value": np.float64(0.0),
360+
"shape": (1,),
361+
"chunks": (1,),
362+
"order": "C",
363+
"zarr_format": zarr_format,
364+
},
365+
"subgroup": {
366+
"attributes": {},
367+
"consolidated_metadata": {
368+
"metadata": {},
369+
"kind": "inline",
370+
"must_understand": False,
371+
},
372+
"node_type": "group",
373+
"zarr_format": zarr_format,
374+
},
375+
}
376+
else:
377+
metadata = {
378+
"subarray": {
379+
"attributes": {},
380+
"chunk_grid": {
381+
"configuration": {"chunk_shape": (1,)},
382+
"name": "regular",
383+
},
384+
"chunk_key_encoding": {
385+
"configuration": {"separator": "/"},
386+
"name": "default",
387+
},
388+
"codecs": ({"configuration": {"endian": "little"}, "name": "bytes"},),
389+
"data_type": "float64",
390+
"fill_value": np.float64(0.0),
391+
"node_type": "array",
392+
"shape": (1,),
393+
"zarr_format": zarr_format,
394+
},
395+
"subgroup": {
396+
"attributes": {},
397+
"consolidated_metadata": {
398+
"metadata": {},
399+
"kind": "inline",
400+
"must_understand": False,
401+
},
402+
"node_type": "group",
403+
"zarr_format": zarr_format,
404+
},
405+
}
406+
354407
object.__setattr__(
355408
expected_group_values[0].metadata,
356409
"consolidated_metadata",
357410
ConsolidatedMetadata.from_dict(
358411
{
359412
"kind": "inline",
360-
"metadata": {
361-
"subarray": {
362-
"attributes": {},
363-
"chunk_grid": {
364-
"configuration": {"chunk_shape": (1,)},
365-
"name": "regular",
366-
},
367-
"chunk_key_encoding": {
368-
"configuration": {"separator": "/"},
369-
"name": "default",
370-
},
371-
"codecs": ({"configuration": {"endian": "little"}, "name": "bytes"},),
372-
"data_type": "float64",
373-
"fill_value": np.float64(0.0),
374-
"node_type": "array",
375-
"shape": (1,),
376-
"zarr_format": 3,
377-
},
378-
"subgroup": {
379-
"attributes": {},
380-
"consolidated_metadata": {
381-
"metadata": {},
382-
"kind": "inline",
383-
"must_understand": False,
384-
},
385-
"node_type": "group",
386-
"zarr_format": 3,
387-
},
388-
},
413+
"metadata": metadata,
389414
"must_understand": False,
390415
}
391416
),
@@ -1055,7 +1080,13 @@ async def test_group_getitem_consolidated(self, store: Store) -> None:
10551080
attributes={},
10561081
zarr_format=3,
10571082
consolidated_metadata=ConsolidatedMetadata(
1058-
metadata={"g2": GroupMetadata(attributes={}, zarr_format=3)}
1083+
metadata={
1084+
"g2": GroupMetadata(
1085+
attributes={},
1086+
zarr_format=3,
1087+
consolidated_metadata=ConsolidatedMetadata(metadata={}),
1088+
)
1089+
}
10591090
),
10601091
),
10611092
}
@@ -1066,7 +1097,7 @@ async def test_group_getitem_consolidated(self, store: Store) -> None:
10661097
assert rg1.metadata.consolidated_metadata == expected.metadata["g1"].consolidated_metadata
10671098

10681099
rg2 = await rg1.getitem("g2")
1069-
assert rg2.metadata.consolidated_metadata is None
1100+
assert rg2.metadata.consolidated_metadata == ConsolidatedMetadata(metadata={})
10701101

10711102
async def test_group_delitem_consolidated(self, store: Store) -> None:
10721103
if isinstance(store, ZipStore):

tests/v3/test_metadata/test_v3.py

Lines changed: 0 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -530,142 +530,3 @@ async def test_non_root_node(self, memory_store_with_hierarchy: Store) -> None:
530530
assert child.metadata.consolidated_metadata is not None
531531
assert "air" not in child.metadata.consolidated_metadata.metadata
532532
assert "grandchild" in child.metadata.consolidated_metadata.metadata
533-
534-
def test_consolidated_metadata_from_dict(self):
535-
data = {"must_understand": False}
536-
537-
# missing kind
538-
with pytest.raises(ValueError, match="kind='None'"):
539-
ConsolidatedMetadata.from_dict(data)
540-
541-
# invalid kind
542-
data["kind"] = "invalid"
543-
with pytest.raises(ValueError, match="kind='invalid'"):
544-
ConsolidatedMetadata.from_dict(data)
545-
546-
# missing metadata
547-
data["kind"] = "inline"
548-
549-
with pytest.raises(TypeError, match="Unexpected type for 'metadata'"):
550-
ConsolidatedMetadata.from_dict(data)
551-
552-
data["kind"] = "inline"
553-
# empty is fine
554-
data["metadata"] = {}
555-
ConsolidatedMetadata.from_dict(data)
556-
557-
# invalid metadata
558-
data["metadata"]["a"] = {"node_type": "array", "zarr_format": 3}
559-
560-
with pytest.raises(TypeError):
561-
ConsolidatedMetadata.from_dict(data)
562-
563-
def test_flatten(self):
564-
array_metadata = {
565-
"attributes": {},
566-
"chunk_key_encoding": {
567-
"configuration": {"separator": "/"},
568-
"name": "default",
569-
},
570-
"codecs": ({"configuration": {"endian": "little"}, "name": "bytes"},),
571-
"data_type": np.dtype("float64"),
572-
"fill_value": np.float64(0.0),
573-
"node_type": "array",
574-
# "shape": (1, 2, 3),
575-
"zarr_format": 3,
576-
}
577-
578-
metadata = ConsolidatedMetadata(
579-
kind="inline",
580-
must_understand=False,
581-
metadata={
582-
"air": ArrayV3Metadata.from_dict(
583-
{
584-
**{
585-
"shape": (1, 2, 3),
586-
"chunk_grid": {
587-
"configuration": {"chunk_shape": (1, 2, 3)},
588-
"name": "regular",
589-
},
590-
},
591-
**array_metadata,
592-
}
593-
),
594-
"lat": ArrayV3Metadata.from_dict(
595-
{
596-
**{
597-
"shape": (1,),
598-
"chunk_grid": {
599-
"configuration": {"chunk_shape": (1,)},
600-
"name": "regular",
601-
},
602-
},
603-
**array_metadata,
604-
}
605-
),
606-
"child": GroupMetadata(
607-
attributes={"key": "child"},
608-
consolidated_metadata=ConsolidatedMetadata(
609-
metadata={
610-
"array": ArrayV3Metadata.from_dict(
611-
{
612-
**array_metadata,
613-
**{
614-
"attributes": {"key": "child"},
615-
"shape": (4, 4),
616-
"chunk_grid": {
617-
"configuration": {"chunk_shape": (4, 4)},
618-
"name": "regular",
619-
},
620-
},
621-
}
622-
),
623-
"grandchild": GroupMetadata(
624-
attributes={"key": "grandchild"},
625-
consolidated_metadata=ConsolidatedMetadata(
626-
metadata={
627-
"array": ArrayV3Metadata.from_dict(
628-
{
629-
**array_metadata,
630-
**{
631-
"attributes": {"key": "grandchild"},
632-
"shape": (4, 4),
633-
"chunk_grid": {
634-
"configuration": {"chunk_shape": (4, 4)},
635-
"name": "regular",
636-
},
637-
},
638-
}
639-
)
640-
}
641-
),
642-
),
643-
},
644-
),
645-
),
646-
},
647-
)
648-
result = metadata.flattened_metadata
649-
expected = {
650-
"air": metadata.metadata["air"],
651-
"lat": metadata.metadata["lat"],
652-
"child": GroupMetadata(attributes={"key": "child"}),
653-
"child/array": metadata.metadata["child"].consolidated_metadata.metadata["array"],
654-
"child/grandchild": GroupMetadata(attributes={"key": "grandchild"}),
655-
"child/grandchild/array": metadata.metadata["child"]
656-
.consolidated_metadata.metadata["grandchild"]
657-
.consolidated_metadata.metadata["array"],
658-
}
659-
assert result == expected
660-
661-
def test_invalid_metadata_raises(self):
662-
payload = {
663-
"kind": "inline",
664-
"must_understand": False,
665-
"metadata": {
666-
"foo": [1, 2, 3] # invalid
667-
},
668-
}
669-
670-
with pytest.raises(TypeError, match="key='foo', type='list'"):
671-
ConsolidatedMetadata.from_dict(payload)

0 commit comments

Comments
 (0)