Skip to content

Commit 6a546d6

Browse files
d-v-bTomAugspurger
andauthored
chore/add missing tests (#3353)
* exclude notimplementederrors from test coverage * add tests for untested features of api.asynchronous * add tests for untested features of api.asynchronous * remove whitespace removal * fix import of arraylike * be more explicit about the data type * Update tests/test_api/test_asynchronous.py Co-authored-by: Tom Augspurger <[email protected]> --------- Co-authored-by: Tom Augspurger <[email protected]>
1 parent 4b26501 commit 6a546d6

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

src/zarr/abc/codec.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def validate(
138138
"""
139139

140140
async def _decode_single(self, chunk_data: CodecOutput, chunk_spec: ArraySpec) -> CodecInput:
141-
raise NotImplementedError
141+
raise NotImplementedError # pragma: no cover
142142

143143
async def decode(
144144
self,
@@ -161,7 +161,7 @@ async def decode(
161161
async def _encode_single(
162162
self, chunk_data: CodecInput, chunk_spec: ArraySpec
163163
) -> CodecOutput | None:
164-
raise NotImplementedError
164+
raise NotImplementedError # pragma: no cover
165165

166166
async def encode(
167167
self,
@@ -242,7 +242,7 @@ async def _encode_partial_single(
242242
selection: SelectorTuple,
243243
chunk_spec: ArraySpec,
244244
) -> None:
245-
raise NotImplementedError
245+
raise NotImplementedError # pragma: no cover
246246

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

0 commit comments

Comments
 (0)