|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import json |
4 | 3 | import re |
5 | 4 | from typing import TYPE_CHECKING, Literal |
6 | 5 |
|
7 | 6 | from zarr.codecs.bytes import BytesCodec |
8 | | -from zarr.core.buffer import default_buffer_prototype |
9 | 7 | from zarr.core.chunk_key_encodings import DefaultChunkKeyEncoding, V2ChunkKeyEncoding |
10 | 8 | from zarr.core.metadata.v3 import ArrayV3Metadata |
11 | 9 |
|
|
19 | 17 | import numpy as np |
20 | 18 | import pytest |
21 | 19 |
|
22 | | -from zarr.core.metadata.v3 import parse_dimension_names, parse_fill_value, parse_zarr_format |
| 20 | +from zarr.core.metadata.v3 import ( |
| 21 | + parse_dimension_names, |
| 22 | + parse_dtype, |
| 23 | + parse_fill_value, |
| 24 | + parse_zarr_format, |
| 25 | +) |
23 | 26 |
|
24 | 27 | bool_dtypes = ("bool",) |
25 | 28 |
|
@@ -234,22 +237,43 @@ def test_metadata_to_dict( |
234 | 237 | assert observed == expected |
235 | 238 |
|
236 | 239 |
|
237 | | -@pytest.mark.parametrize("fill_value", [-1, 0, 1, 2932897]) |
238 | | -@pytest.mark.parametrize("precision", ["ns", "D"]) |
239 | | -async def test_datetime_metadata(fill_value: int, precision: str) -> None: |
| 240 | +# @pytest.mark.parametrize("fill_value", [-1, 0, 1, 2932897]) |
| 241 | +# @pytest.mark.parametrize("precision", ["ns", "D"]) |
| 242 | +# async def test_datetime_metadata(fill_value: int, precision: str) -> None: |
| 243 | +# metadata_dict = { |
| 244 | +# "zarr_format": 3, |
| 245 | +# "node_type": "array", |
| 246 | +# "shape": (1,), |
| 247 | +# "chunk_grid": {"name": "regular", "configuration": {"chunk_shape": (1,)}}, |
| 248 | +# "data_type": f"<M8[{precision}]", |
| 249 | +# "chunk_key_encoding": {"name": "default", "separator": "."}, |
| 250 | +# "codecs": (), |
| 251 | +# "fill_value": np.datetime64(fill_value, precision), |
| 252 | +# } |
| 253 | +# metadata = ArrayV3Metadata.from_dict(metadata_dict) |
| 254 | +# # ensure there isn't a TypeError here. |
| 255 | +# d = metadata.to_buffer_dict(default_buffer_prototype()) |
| 256 | + |
| 257 | +# result = json.loads(d["zarr.json"].to_bytes()) |
| 258 | +# assert result["fill_value"] == fill_value |
| 259 | + |
| 260 | + |
| 261 | +async def test_invalid_dtype_raises() -> None: |
240 | 262 | metadata_dict = { |
241 | 263 | "zarr_format": 3, |
242 | 264 | "node_type": "array", |
243 | 265 | "shape": (1,), |
244 | 266 | "chunk_grid": {"name": "regular", "configuration": {"chunk_shape": (1,)}}, |
245 | | - "data_type": f"<M8[{precision}]", |
| 267 | + "data_type": "<M8[ns]", |
246 | 268 | "chunk_key_encoding": {"name": "default", "separator": "."}, |
247 | 269 | "codecs": (), |
248 | | - "fill_value": np.datetime64(fill_value, precision), |
| 270 | + "fill_value": np.datetime64(0, "ns"), |
249 | 271 | } |
250 | | - metadata = ArrayV3Metadata.from_dict(metadata_dict) |
251 | | - # ensure there isn't a TypeError here. |
252 | | - d = metadata.to_buffer_dict(default_buffer_prototype()) |
| 272 | + with pytest.raises(ValueError, match=r"Invalid V3 data_type"): |
| 273 | + ArrayV3Metadata.from_dict(metadata_dict) |
| 274 | + |
253 | 275 |
|
254 | | - result = json.loads(d["zarr.json"].to_bytes()) |
255 | | - assert result["fill_value"] == fill_value |
| 276 | +@pytest.mark.parametrize("data", ["datetime64[s]", "foo", object()]) |
| 277 | +def test_parse_invalid_dtype_raises(data): |
| 278 | + with pytest.raises(ValueError, match=r"Invalid V3 data_type"): |
| 279 | + parse_dtype(data) |
0 commit comments