Skip to content

Commit b1d6e86

Browse files
committed
test: add test helpers and simplify metadata tests
1 parent ede5752 commit b1d6e86

3 files changed

Lines changed: 298 additions & 370 deletions

File tree

tests/conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@
5858
from zarr.core.dtype.wrapper import ZDType
5959

6060

61+
@dataclass
62+
class Expect[TIn, TOut]:
63+
"""A test case with explicit input, expected output, and a human-readable id."""
64+
65+
input: TIn
66+
output: TOut
67+
id: str
68+
69+
70+
@dataclass
71+
class ExpectFail[TIn]:
72+
"""A test case that should raise an exception."""
73+
74+
input: TIn
75+
exception: type[Exception]
76+
id: str
77+
msg: str | None = None
78+
79+
6180
async def parse_store(
6281
store: Literal["local", "memory", "fsspec", "zip", "memory_get_latency"], path: str
6382
) -> LocalStore | MemoryStore | FsspecStore | ZipStore | LatencyStore:

tests/test_metadata/conftest.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Any
4+
5+
from zarr.codecs.bytes import BytesCodec
6+
7+
if TYPE_CHECKING:
8+
from zarr.core.metadata.v3 import ArrayMetadataJSON_V3
9+
10+
11+
def minimal_metadata_dict_v3(
12+
extra_fields: dict[str, Any] | None = None, **overrides: Any
13+
) -> ArrayMetadataJSON_V3:
14+
"""Build a minimal valid V3 array metadata JSON dict.
15+
16+
The output matches the shape of ``ArrayV3Metadata.to_dict()`` — all
17+
fields that ``to_dict`` always emits are included.
18+
19+
Parameters
20+
----------
21+
extra_fields : dict, optional
22+
Extra keys to inject into the dict (e.g. extension fields).
23+
**overrides
24+
Override any of the standard metadata fields.
25+
"""
26+
d: ArrayMetadataJSON_V3 = {
27+
"zarr_format": 3,
28+
"node_type": "array",
29+
"shape": (4, 4),
30+
"data_type": "uint8",
31+
"chunk_grid": {"name": "regular", "configuration": {"chunk_shape": (4, 4)}},
32+
"chunk_key_encoding": {"name": "default", "configuration": {"separator": "/"}},
33+
"fill_value": 0,
34+
"codecs": (BytesCodec().to_dict(),),
35+
"attributes": {},
36+
"storage_transformers": (),
37+
}
38+
d.update(overrides)
39+
if extra_fields is not None:
40+
d.update(extra_fields)
41+
return d

0 commit comments

Comments
 (0)