|
15 | 15 | from zarr import config
|
16 | 16 | from zarr.abc.store import Store
|
17 | 17 | from zarr.core.buffer.core import default_buffer_prototype
|
| 18 | +from zarr.core.metadata.v2 import parse_structured_fill_value |
18 | 19 | from zarr.core.sync import sync
|
19 | 20 | from zarr.storage import MemoryStore, StorePath
|
20 | 21 |
|
@@ -315,6 +316,89 @@ def test_structured_dtype_roundtrip(fill_value, tmp_path) -> None:
|
315 | 316 | assert (a == za[:]).all()
|
316 | 317 |
|
317 | 318 |
|
| 319 | +@pytest.mark.parametrize( |
| 320 | + ( |
| 321 | + "fill_value", |
| 322 | + "dtype", |
| 323 | + "expected_result", |
| 324 | + ), |
| 325 | + [ |
| 326 | + ( |
| 327 | + ("Alice", 30), |
| 328 | + np.dtype([("name", "U10"), ("age", "i4")]), |
| 329 | + np.array([("Alice", 30)], dtype=[("name", "U10"), ("age", "i4")])[0], |
| 330 | + ), |
| 331 | + ( |
| 332 | + ["Bob", 25], |
| 333 | + np.dtype([("name", "U10"), ("age", "i4")]), |
| 334 | + np.array([("Bob", 25)], dtype=[("name", "U10"), ("age", "i4")])[0], |
| 335 | + ), |
| 336 | + ( |
| 337 | + b"\x01\x00\x00\x00\x02\x00\x00\x00", |
| 338 | + np.dtype([("x", "i4"), ("y", "i4")]), |
| 339 | + np.array([(1, 2)], dtype=[("x", "i4"), ("y", "i4")])[0], |
| 340 | + ), |
| 341 | + ( |
| 342 | + "BQAAAA==", |
| 343 | + np.dtype([("val", "i4")]), |
| 344 | + np.array([(5,)], dtype=[("val", "i4")])[0], |
| 345 | + ), |
| 346 | + ( |
| 347 | + {"x": 1, "y": 2}, |
| 348 | + np.dtype([("location", "O")]), |
| 349 | + np.array([({"x": 1, "y": 2},)], dtype=[("location", "O")])[0], |
| 350 | + ), |
| 351 | + ( |
| 352 | + {"x": 1, "y": 2, "z": 3}, |
| 353 | + np.dtype([("location", "O")]), |
| 354 | + np.array([({"x": 1, "y": 2, "z": 3},)], dtype=[("location", "O")])[0], |
| 355 | + ), |
| 356 | + ], |
| 357 | + ids=[ |
| 358 | + "tuple_input", |
| 359 | + "list_input", |
| 360 | + "bytes_input", |
| 361 | + "string_input", |
| 362 | + "dictionary_input", |
| 363 | + "dictionary_input_extra_fields", |
| 364 | + ], |
| 365 | +) |
| 366 | +def test_parse_structured_fill_value_valid( |
| 367 | + fill_value: Any, dtype: np.dtype[Any], expected_result: Any |
| 368 | +) -> None: |
| 369 | + result = parse_structured_fill_value(fill_value, dtype) |
| 370 | + assert result.dtype == expected_result.dtype |
| 371 | + assert result == expected_result |
| 372 | + if isinstance(expected_result, np.void): |
| 373 | + for name in expected_result.dtype.names or []: |
| 374 | + assert result[name] == expected_result[name] |
| 375 | + |
| 376 | + |
| 377 | +@pytest.mark.parametrize( |
| 378 | + ( |
| 379 | + "fill_value", |
| 380 | + "dtype", |
| 381 | + ), |
| 382 | + [ |
| 383 | + (("Alice", 30), np.dtype([("name", "U10"), ("age", "i4"), ("city", "U20")])), |
| 384 | + (b"\x01\x00\x00\x00", np.dtype([("x", "i4"), ("y", "i4")])), |
| 385 | + ("this_is_not_base64", np.dtype([("val", "i4")])), |
| 386 | + ("hello", np.dtype([("age", "i4")])), |
| 387 | + ({"x": 1, "y": 2}, np.dtype([("location", "i4")])), |
| 388 | + ], |
| 389 | + ids=[ |
| 390 | + "tuple_list_wrong_length", |
| 391 | + "bytes_wrong_length", |
| 392 | + "invalid_base64", |
| 393 | + "wrong_data_type", |
| 394 | + "wrong_dictionary", |
| 395 | + ], |
| 396 | +) |
| 397 | +def test_parse_structured_fill_value_invalid(fill_value: Any, dtype: np.dtype[Any]) -> None: |
| 398 | + with pytest.raises(ValueError): |
| 399 | + parse_structured_fill_value(fill_value, dtype) |
| 400 | + |
| 401 | + |
318 | 402 | @pytest.mark.parametrize("fill_value", [None, b"x"], ids=["no_fill", "fill"])
|
319 | 403 | def test_other_dtype_roundtrip(fill_value, tmp_path) -> None:
|
320 | 404 | a = np.array([b"a\0\0", b"bb", b"ccc"], dtype="V7")
|
|
0 commit comments