|
| 1 | +"""Tests for metadata helpers shared by both Zarr formats.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import warnings |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import pytest |
| 10 | + |
| 11 | +from zarr.core.metadata.common import parse_stored_chunk_shape |
| 12 | +from zarr.errors import ZarrUserWarning |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + ("chunk_shape", "shape", "expected", "warns"), |
| 17 | + [ |
| 18 | + ((4, 5), (10, 10), (4, 5), False), |
| 19 | + ((1, 1), (0, 0), (1, 1), False), |
| 20 | + ((0,), (0,), (1,), True), |
| 21 | + ((False,), (0,), (1,), True), |
| 22 | + ((np.int64(0),), (0,), (1,), True), |
| 23 | + ((4, 0), (4, 0), (4, 1), True), |
| 24 | + ((0, 0), (0, 0), (1, 1), True), |
| 25 | + ((2, [5, 10, 5]), (6, 20), (2, [5, 10, 5]), False), |
| 26 | + ], |
| 27 | + ids=[ |
| 28 | + "valid", |
| 29 | + "valid-on-empty-axes", |
| 30 | + "legacy-zero", |
| 31 | + "legacy-json-false", |
| 32 | + "legacy-numpy-zero", |
| 33 | + "only-empty-axis-corrected", |
| 34 | + "every-empty-axis-corrected", |
| 35 | + "edge-list-passed-through", |
| 36 | + ], |
| 37 | +) |
| 38 | +def test_parse_stored_chunk_shape( |
| 39 | + chunk_shape: tuple[Any, ...], shape: tuple[int, ...], expected: tuple[Any, ...], warns: bool |
| 40 | +) -> None: |
| 41 | + """A valid chunk shape is returned as is; a chunk size of 0 on a zero-length |
| 42 | + axis is read as 1, with a warning naming the writer and how to re-save.""" |
| 43 | + with warnings.catch_warnings(record=True) as record: |
| 44 | + warnings.simplefilter("always") |
| 45 | + parsed = parse_stored_chunk_shape(chunk_shape, shape, legacy_writers="an old writer") |
| 46 | + assert parsed == expected |
| 47 | + messages = [str(w.message) for w in record if issubclass(w.category, ZarrUserWarning)] |
| 48 | + assert bool(messages) is warns |
| 49 | + for message in messages: |
| 50 | + assert "an old writer" in message |
| 51 | + assert "update_attributes({})" in message |
| 52 | + |
| 53 | + |
| 54 | +def test_parse_stored_chunk_shape_rejects_dimension_mismatch() -> None: |
| 55 | + """The chunk shape needs one entry per array axis.""" |
| 56 | + with pytest.raises(ValueError, match="same number of dimensions"): |
| 57 | + parse_stored_chunk_shape((4,), (10, 10), legacy_writers="an old writer") |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize(("chunk_shape", "shape"), [((0,), (5,)), ((4, 0), (4, 3))]) |
| 61 | +def test_parse_stored_chunk_shape_rejects_zero_on_nonempty_axis( |
| 62 | + chunk_shape: tuple[int, ...], shape: tuple[int, ...] |
| 63 | +) -> None: |
| 64 | + """A chunk size of 0 is only tolerated on a zero-length axis.""" |
| 65 | + with pytest.raises(ValueError, match="chunk edge length must be >= 1, got 0"): |
| 66 | + parse_stored_chunk_shape(chunk_shape, shape, legacy_writers="an old writer") |
| 67 | + |
| 68 | + |
| 69 | +def test_parse_stored_chunk_shape_rejects_negative() -> None: |
| 70 | + """A negative chunk size is rejected even on a zero-length axis.""" |
| 71 | + with pytest.raises(ValueError, match="chunk edge length must be >= 1, got -1"): |
| 72 | + parse_stored_chunk_shape((-1,), (0,), legacy_writers="an old writer") |
0 commit comments