|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import numpy as np |
| 4 | +import pytest |
4 | 5 |
|
5 | 6 | from tests.test_dtype.test_wrapper import BaseTestZDType |
| 7 | +from zarr.core.dtype import get_data_type_from_json |
6 | 8 | from zarr.core.dtype.npy.int import Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64 |
| 9 | +from zarr.errors import DataTypeValidationError |
7 | 10 |
|
8 | 11 |
|
9 | 12 | class TestInt8(BaseTestZDType): |
@@ -329,3 +332,30 @@ def test_string_integer_from_json_scalar() -> None: |
329 | 332 | # Test that it works for v2 format too |
330 | 333 | result = dtype_instance.from_json_scalar("123", zarr_format=2) |
331 | 334 | assert result == np.int32(123) |
| 335 | + |
| 336 | + |
| 337 | +@pytest.mark.parametrize("name", ["|u1", "<u1", ">u1"]) |
| 338 | +def test_uint8_v2_aliases(name: str) -> None: |
| 339 | + data = {"name": name, "object_codec_id": None} |
| 340 | + dtype = UInt8.from_json(data, zarr_format=2) |
| 341 | + assert dtype == UInt8() |
| 342 | + assert get_data_type_from_json(data, zarr_format=2) == dtype |
| 343 | + assert dtype.to_native_dtype() == np.dtype("uint8") |
| 344 | + serialized = dtype.to_json(zarr_format=2) |
| 345 | + assert serialized["name"] == "|u1" |
| 346 | + assert serialized["object_codec_id"] is None |
| 347 | + assert dtype.to_json(zarr_format=3) == "uint8" |
| 348 | + with pytest.raises(DataTypeValidationError): |
| 349 | + UInt8.from_json(name, zarr_format=3) |
| 350 | + |
| 351 | + |
| 352 | +@pytest.mark.parametrize("name", ["|u1", "<u1", ">u1"]) |
| 353 | +def test_uint8_v2_aliases_reject_object_codec(name: str) -> None: |
| 354 | + with pytest.raises(DataTypeValidationError): |
| 355 | + UInt8.from_json({"name": name, "object_codec_id": "vlen-utf8"}, zarr_format=2) |
| 356 | + |
| 357 | + |
| 358 | +@pytest.mark.parametrize("name", ["|i1", "<u2", ">u2", "uint8", "invalid"]) |
| 359 | +def test_uint8_v2_rejects_other_types(name: str) -> None: |
| 360 | + with pytest.raises(DataTypeValidationError): |
| 361 | + UInt8.from_json({"name": name, "object_codec_id": None}, zarr_format=2) |
0 commit comments