|
16 | 16 | from zarr.codecs import ShardingCodec |
17 | 17 | from zarr.codecs.numcodecs import Quantize |
18 | 18 | from zarr.core.array import AsyncArray |
| 19 | +from zarr.core.group import ConsolidatedMetadata |
19 | 20 | from zarr.core.metadata import ArrayV2Metadata, ArrayV3Metadata |
20 | 21 | from zarr.core.metadata.upgrades import ( |
21 | 22 | RESAVE_HINT, |
@@ -1007,6 +1008,48 @@ def test_consolidated_upgraded_member_write_after_chunk_grid_change_raises( |
1007 | 1008 | assert {p: p.read_bytes() for p in path.rglob("*") if p.is_file()} == documents |
1008 | 1009 |
|
1009 | 1010 |
|
| 1011 | +@pytest.mark.filterwarnings("ignore:Consolidated metadata is currently not part:UserWarning") |
| 1012 | +@pytest.mark.parametrize("zarr_format", [2, 3]) |
| 1013 | +def test_consolidated_upgraded_member_attributes_kept_by_group_write( |
| 1014 | + tmp_path: Path, zarr_format: Literal[2, 3] |
| 1015 | +) -> None: |
| 1016 | + """Setting attributes of a member read from consolidated metadata stores the member's |
| 1017 | + upgraded document with them. A later write of the group, whose consolidated metadata |
| 1018 | + shares the member's metadata, then stores that upgrade: the new attributes, not the |
| 1019 | + legacy document as it was stored before.""" |
| 1020 | + path = tmp_path / "group.zarr" |
| 1021 | + _consolidated_legacy_member(path, zarr_format) |
| 1022 | + with pytest.warns(ZarrUserWarning, match="is read as"): |
| 1023 | + group = zarr.open_group(path, mode="r+", use_consolidated=True) |
| 1024 | + |
| 1025 | + group["a"].attrs["x"] = 1 |
| 1026 | + group.attrs["y"] = 2 |
| 1027 | + |
| 1028 | + with warnings.catch_warnings(record=True) as record: |
| 1029 | + warnings.simplefilter("always") |
| 1030 | + attributes = dict(zarr.open_group(path, mode="r", use_consolidated=True)["a"].attrs) |
| 1031 | + assert attributes == {"x": 1} |
| 1032 | + assert _stored_chunks(_consolidated_member(path, zarr_format, "a")) == [3] |
| 1033 | + assert not [w for w in record if "is read as" in str(w.message)] |
| 1034 | + |
| 1035 | + |
| 1036 | +@pytest.mark.parametrize("zarr_format", [2, 3]) |
| 1037 | +def test_upgraded_metadata_keeps_the_document_it_read(zarr_format: Literal[2, 3]) -> None: |
| 1038 | + """Metadata read from a document that had to be upgraded keeps that document as it |
| 1039 | + was read, whatever becomes of the caller's dict (or the objects in it, which the |
| 1040 | + metadata's attributes may hold): consolidated metadata stores it as read.""" |
| 1041 | + doc = _v2_doc([0], [0]) if zarr_format == 2 else _v3_doc([0], [0]) |
| 1042 | + doc["attributes"] = {"k": [1]} |
| 1043 | + read = json.loads(json.dumps(doc)) |
| 1044 | + metadata_cls = ArrayV2Metadata if zarr_format == 2 else ArrayV3Metadata |
| 1045 | + metadata = metadata_cls.from_dict(doc) |
| 1046 | + |
| 1047 | + cast("list[int]", metadata.attributes["k"]).append(2) |
| 1048 | + doc["shape"] = [4] |
| 1049 | + |
| 1050 | + assert ConsolidatedMetadata(metadata={"a": metadata}).to_dict()["metadata"] == {"a": read} |
| 1051 | + |
| 1052 | + |
1010 | 1053 | @pytest.mark.parametrize("zarr_format", [2, 3]) |
1011 | 1054 | def test_empty_write_stores_no_metadata(tmp_path: Path, zarr_format: Literal[2, 3]) -> None: |
1012 | 1055 | """A write of an empty selection stores no chunks, so it stores no metadata either.""" |
|
0 commit comments