|
| 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