Skip to content

Commit 83799df

Browse files
committed
add tests for untested features of api.asynchronous
1 parent 85665e0 commit 83799df

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
from __future__ import annotations
2+
3+
import json
4+
from dataclasses import dataclass
5+
6+
import numpy as np
7+
import pytest
8+
9+
from zarr import create_array
10+
from zarr.api.asynchronous import ArrayLike, _get_shape_chunks, _like_args, open
11+
from zarr.core.buffer.core import default_buffer_prototype
12+
13+
14+
@dataclass
15+
class WithShape:
16+
shape: tuple[int, ...]
17+
18+
19+
@dataclass
20+
class WithChunks(WithShape):
21+
chunks: tuple[int, ...]
22+
23+
24+
@dataclass
25+
class WithChunkLen(WithShape):
26+
chunklen: int
27+
28+
29+
@pytest.mark.parametrize(
30+
("observed", "expected"),
31+
[
32+
({}, (None, None)),
33+
(WithShape(shape=(1, 2)), ((1, 2), None)),
34+
(WithChunks(shape=(1, 2), chunks=(1, 2)), ((1, 2), (1, 2))),
35+
(WithChunkLen(shape=(10, 10), chunklen=1), ((10, 10), (1, 10))),
36+
],
37+
)
38+
def test_get_shape_chunks(
39+
observed: object, expected: tuple[tuple[int, ...] | None, tuple[int, ...] | None]
40+
) -> None:
41+
"""
42+
Test the _get_shape_chunks function
43+
"""
44+
assert _get_shape_chunks(observed) == expected
45+
46+
47+
@pytest.mark.parametrize(
48+
("observed", "expected"),
49+
[
50+
(np.arange(10, dtype="int"), {"shape": (10,), "dtype": np.dtype("int64")}),
51+
(WithChunks(shape=(1, 2), chunks=(1, 2)), {"chunks": (1, 2), "shape": (1, 2)}),
52+
(
53+
create_array(
54+
{},
55+
chunks=(10,),
56+
shape=(100,),
57+
dtype="f8",
58+
compressors=None,
59+
filters=None,
60+
zarr_format=2,
61+
)._async_array,
62+
{
63+
"chunks": (10,),
64+
"shape": (100,),
65+
"dtype": np.dtype("f8"),
66+
"compressor": None,
67+
"filters": None,
68+
"order": "C",
69+
},
70+
),
71+
],
72+
)
73+
def test_like_args(observed: ArrayLike, expected: object) -> None:
74+
"""
75+
Test the like_args function
76+
"""
77+
assert _like_args(observed, {}) == expected
78+
79+
80+
async def test_open_no_array() -> None:
81+
"""
82+
Test that zarr.api.asynchronous.open attempts to open a group when is no array found, but shape was specified in kwargs.
83+
This behavior makes no sense but we should still test it.
84+
"""
85+
store = {
86+
"zarr.json": default_buffer_prototype().buffer.from_bytes(
87+
json.dumps({"zarr_format": 3, "node_type": "group"}).encode("utf-8")
88+
)
89+
}
90+
with pytest.raises(
91+
TypeError, match=r"open_group\(\) got an unexpected keyword argument 'shape'"
92+
):
93+
await open(store=store, shape=(1,))

0 commit comments

Comments
 (0)