Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Oct 8, 2024
1 parent 80a2821 commit 02ee889
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/zarr/core/metadata/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from zarr.core.common import ZARR_JSON, parse_named_configuration, parse_shapelike
from zarr.core.config import config
from zarr.core.metadata.common import ArrayMetadata, parse_attributes
from zarr.errors import MetadataValidationError, NodeTypeValidationError
from zarr.errors import NodeTypeValidationError, ZarrVersionValidationError
from zarr.registry import get_codec_class

DEFAULT_DTYPE = "float64"
Expand All @@ -37,13 +37,13 @@
def parse_zarr_format(data: object) -> Literal[3]:
if data == 3:
return 3
raise MetadataValidationError(f"Invalid value. Expected 3. Got {data}.")
raise ZarrVersionValidationError(3, data)


def parse_node_type_array(data: object) -> Literal["array"]:
if data == "array":
return "array"
raise NodeTypeValidationError(f"Invalid value. Expected 'array'. Got {data}.")
raise NodeTypeValidationError("array", data)


def parse_codecs(data: object) -> tuple[Codec, ...]:
Expand Down
8 changes: 8 additions & 0 deletions src/zarr/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class MetadataValidationError(_BaseZarrError):
"""An exception raised when the Zarr metadata is invalid in some way"""


class ZarrVersionValidationError(MetadataValidationError):
"""Raised when the zarr_version in the Zarr metadata is invalid."""

_msg = "Invalid value for 'zarr_format'. Expected '{}'. Got '{}'."


class NodeTypeValidationError(MetadataValidationError):
"""
Specialized exception when the node_type of the metadata document is incorrect..
Expand All @@ -37,6 +43,8 @@ class NodeTypeValidationError(MetadataValidationError):
for example an 'array' node when we expected a 'group'.
"""

_msg = "Invalid value for 'node_type'. Expected '{}'. Got '{}'."


__all__ = [
"ContainsArrayAndGroupError",
Expand Down
19 changes: 18 additions & 1 deletion tests/v3/test_metadata/test_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from zarr.core.buffer import default_buffer_prototype
from zarr.core.chunk_key_encodings import DefaultChunkKeyEncoding, V2ChunkKeyEncoding
from zarr.core.metadata.v3 import ArrayV3Metadata, DataType
from zarr.errors import NodeTypeValidationError

if TYPE_CHECKING:
from collections.abc import Sequence
Expand All @@ -23,6 +24,7 @@
from zarr.core.metadata.v3 import (
parse_dimension_names,
parse_fill_value,
parse_node_type_array,
parse_zarr_format,
)

Expand Down Expand Up @@ -52,14 +54,29 @@

@pytest.mark.parametrize("data", [None, 1, 2, 4, 5, "3"])
def test_parse_zarr_format_invalid(data: Any) -> None:
with pytest.raises(ValueError, match=f"Invalid value. Expected 3. Got {data}"):
with pytest.raises(
ValueError, match=f"Invalid value for 'zarr_format'. Expected '3'. Got '{data}'"
):
parse_zarr_format(data)


def test_parse_zarr_format_valid() -> None:
assert parse_zarr_format(3) == 3


@pytest.mark.parametrize("data", [None, "invalid"])
def test_parse_node_type_array_invalid(data: Any) -> None:
with pytest.raises(
NodeTypeValidationError,
match=f"Invalid value for 'node_type'. Expected 'array'. Got '{data}'.",
):
assert parse_node_type_array(data)


def test_parse_node_type_array_valid() -> None:
assert parse_node_type_array("array") == "array"


@pytest.mark.parametrize("data", [(), [1, 2, "a"], {"foo": 10}])
def parse_dimension_names_invalid(data: Any) -> None:
with pytest.raises(TypeError, match="Expected either None or iterable of str,"):
Expand Down

0 comments on commit 02ee889

Please sign in to comment.