|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from zarr.core.chunk_key_encodings import DefaultChunkKeyEncoding, V2ChunkKeyEncoding |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.parametrize("separator", ["/", "."]) |
| 9 | +@pytest.mark.parametrize( |
| 10 | + "coords", |
| 11 | + [(), (0,), (1, 2), (10, 0, 3)], |
| 12 | +) |
| 13 | +def test_default_encoding_round_trips(separator: str, coords: tuple[int, ...]) -> None: |
| 14 | + """Encoding coordinates and decoding the result returns the coordinates.""" |
| 15 | + encoding = DefaultChunkKeyEncoding(separator=separator) # type: ignore[arg-type] |
| 16 | + |
| 17 | + key = encoding.encode_chunk_key(coords) |
| 18 | + assert encoding.decode_chunk_key(key) == coords |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize("separator", ["/", "."]) |
| 22 | +@pytest.mark.parametrize("coords", [(0,), (1, 2), (10, 0, 3)]) |
| 23 | +def test_v2_encoding_round_trips(separator: str, coords: tuple[int, ...]) -> None: |
| 24 | + """The v2 encoding round-trips coordinates for either separator.""" |
| 25 | + encoding = V2ChunkKeyEncoding(separator=separator) # type: ignore[arg-type] |
| 26 | + |
| 27 | + key = encoding.encode_chunk_key(coords) |
| 28 | + assert encoding.decode_chunk_key(key) == coords |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("separator", ["/", "."]) |
| 32 | +def test_v2_zero_dimensional_key_is_ambiguous(separator: str) -> None: |
| 33 | + """A 0-d v2 array stores its sole chunk under `"0"`, the same key a 1-d |
| 34 | + array uses for chunk 0, so decoding cannot recover the empty tuple on its |
| 35 | + own -- the array's dimensionality is what disambiguates it.""" |
| 36 | + encoding = V2ChunkKeyEncoding(separator=separator) # type: ignore[arg-type] |
| 37 | + |
| 38 | + assert encoding.encode_chunk_key(()) == "0" |
| 39 | + assert encoding.decode_chunk_key("0") == (0,) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "chunk_key", |
| 44 | + [ |
| 45 | + "0/1", # no "c" prefix at all |
| 46 | + "c0/1", # "c" not followed by the separator |
| 47 | + "x/0/1", # wrong prefix character |
| 48 | + "", |
| 49 | + ], |
| 50 | +) |
| 51 | +def test_default_encoding_rejects_key_without_prefix(chunk_key: str) -> None: |
| 52 | + """A key that does not carry the `c<separator>` prefix is not a chunk key |
| 53 | + for this encoding, and must be rejected rather than silently decoded.""" |
| 54 | + encoding = DefaultChunkKeyEncoding(separator="/") |
| 55 | + |
| 56 | + with pytest.raises(ValueError, match="Invalid chunk key"): |
| 57 | + encoding.decode_chunk_key(chunk_key) |
| 58 | + |
| 59 | + |
| 60 | +def test_default_encoding_rejects_key_using_the_other_separator() -> None: |
| 61 | + """A key encoded with `.` is not valid for a `/`-separated encoding.""" |
| 62 | + encoding = DefaultChunkKeyEncoding(separator="/") |
| 63 | + |
| 64 | + with pytest.raises(ValueError, match="Invalid chunk key"): |
| 65 | + encoding.decode_chunk_key("c.0.1") |
0 commit comments