Skip to content

Commit cc2e5e6

Browse files
committed
Bump up codecoverage
1 parent 118304a commit cc2e5e6

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

src/zarr/core/metadata/v2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ def parse_metadata(data: ArrayV2Metadata) -> ArrayV2Metadata:
318318
def parse_structured_fill_value(fill_value: Any, dtype: np.dtype[Any]) -> Any:
319319
"""Handle structured dtype/fill value pairs"""
320320
try:
321-
if isinstance(fill_value, (tuple, list)):
321+
if isinstance(fill_value, list):
322+
fill_value = tuple(fill_value)
323+
if isinstance(fill_value, tuple):
322324
fill_value = np.array([fill_value], dtype=dtype)[0]
323325
elif isinstance(fill_value, bytes):
324326
fill_value = np.frombuffer(fill_value, dtype=dtype)[0]

tests/test_v2.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from zarr import config
1616
from zarr.abc.store import Store
1717
from zarr.core.buffer.core import default_buffer_prototype
18+
from zarr.core.metadata.v2 import parse_structured_fill_value
1819
from zarr.core.sync import sync
1920
from zarr.storage import MemoryStore, StorePath
2021

@@ -315,6 +316,84 @@ def test_structured_dtype_roundtrip(fill_value, tmp_path) -> None:
315316
assert (a == za[:]).all()
316317

317318

319+
@pytest.mark.parametrize(
320+
"fill_value, dtype, expected_result",
321+
[
322+
(
323+
("Alice", 30),
324+
np.dtype([("name", "U10"), ("age", "i4")]),
325+
np.array([("Alice", 30)], dtype=[("name", "U10"), ("age", "i4")])[0],
326+
),
327+
(
328+
["Bob", 25],
329+
np.dtype([("name", "U10"), ("age", "i4")]),
330+
np.array([("Bob", 25)], dtype=[("name", "U10"), ("age", "i4")])[0],
331+
),
332+
(
333+
b"\x01\x00\x00\x00\x02\x00\x00\x00",
334+
np.dtype([("x", "i4"), ("y", "i4")]),
335+
np.array([(1, 2)], dtype=[("x", "i4"), ("y", "i4")])[0],
336+
),
337+
(
338+
"BQAAAA==",
339+
np.dtype([("val", "i4")]),
340+
np.array([(5,)], dtype=[("val", "i4")])[0],
341+
),
342+
(
343+
{'x':1, 'y':2},
344+
np.dtype([('location', 'O')]),
345+
np.array([({'x':1, 'y':2},)], dtype=[('location', 'O')])[0]
346+
),
347+
(
348+
{'x':1, 'y':2, 'z':3},
349+
np.dtype([('location', 'O')]),
350+
np.array([({'x':1, 'y':2, 'z':3},)], dtype=[('location', 'O')])[0]
351+
),
352+
],
353+
ids=[
354+
"tuple_input",
355+
"list_input",
356+
"bytes_input",
357+
"string_input",
358+
"dictionary_input",
359+
"dictionary_input_extra_fields"
360+
],
361+
)
362+
def test_parse_structured_fill_value_valid(
363+
fill_value: Any, dtype: np.dtype[Any], expected_result: Any
364+
) -> None:
365+
result = parse_structured_fill_value(fill_value, dtype)
366+
assert result.dtype == expected_result.dtype
367+
assert result == expected_result
368+
if isinstance(expected_result, np.void):
369+
for name in expected_result.dtype.names or []:
370+
assert result[name] == expected_result[name]
371+
372+
373+
@pytest.mark.parametrize(
374+
"fill_value, dtype",
375+
[
376+
(("Alice", 30), np.dtype([("name", "U10"), ("age", "i4"), ("city", "U20")])),
377+
(b"\x01\x00\x00\x00", np.dtype([("x", "i4"), ("y", "i4")])),
378+
("this_is_not_base64", np.dtype([("val", "i4")])),
379+
("hello", np.dtype([("age", "i4")])),
380+
({'x':1, 'y':2}, np.dtype([('location', 'i4')]))
381+
],
382+
ids=[
383+
"tuple_list_wrong_length",
384+
"bytes_wrong_length",
385+
"invalid_base64",
386+
"wrong_data_type",
387+
"wrong_dictionary"
388+
],
389+
)
390+
def test_parse_structured_fill_value_invalid(
391+
fill_value: Any, dtype: np.dtype[Any]
392+
) -> None:
393+
with pytest.raises(ValueError):
394+
parse_structured_fill_value(fill_value, dtype)
395+
396+
318397
@pytest.mark.parametrize("fill_value", [None, b"x"], ids=["no_fill", "fill"])
319398
def test_other_dtype_roundtrip(fill_value, tmp_path) -> None:
320399
a = np.array([b"a\0\0", b"bb", b"ccc"], dtype="V7")

0 commit comments

Comments
 (0)