Skip to content

Commit 9980823

Browse files
committed
add to_json_tests
1 parent d7d4e02 commit 9980823

File tree

7 files changed

+107
-2
lines changed

7 files changed

+107
-2
lines changed

tests/test_codecs/test_blosc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import zarr
99
from zarr.abc.store import Store
1010
from zarr.codecs import BloscCodec
11-
from zarr.codecs.blosc import BLOSC_CNAME, BLOSC_SHUFFLE, BloscCname, BloscJSON_V2, BloscShuffle
11+
from zarr.codecs.blosc import BLOSC_CNAME, BLOSC_SHUFFLE, BloscCname, BloscJSON_V2, BloscJSON_V3, BloscShuffle
1212
from zarr.core.buffer import default_buffer_prototype
1313
from zarr.storage import StorePath
1414

tests/test_codecs/test_bytes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,27 @@
66
import zarr
77
from zarr.abc.store import Store
88
from zarr.codecs import BytesCodec
9+
from zarr.codecs.bytes import BytesJSON_V2, BytesJSON_V3
910
from zarr.storage import StorePath
1011

1112
from .test_codecs import _AsyncArrayProxy
1213

14+
@pytest.mark.parametrize("endian", ["big", "little"])
15+
def test_bytescodec_to_json(endian: Literal["big", "little"]) -> None:
16+
codec = BytesCodec(endian=endian)
17+
expected_v2: BytesJSON_V2 = {
18+
"id": "bytes",
19+
"endian": endian,
20+
}
21+
expected_v3: BytesJSON_V3 = {
22+
"name": "bytes",
23+
"configuration": {
24+
"endian": endian,
25+
},
26+
}
27+
assert codec.to_json(zarr_format=2) == expected_v2
28+
assert codec.to_json(zarr_format=3) == expected_v3
29+
1330

1431
@pytest.mark.filterwarnings("ignore:The endianness of the requested serializer")
1532
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])

tests/test_codecs/test_gzip.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,25 @@
44
import zarr
55
from zarr.abc.store import Store
66
from zarr.codecs import GzipCodec
7+
from zarr.codecs.gzip import GZipJSON_V2, GZipJSON_V3
78
from zarr.storage import StorePath
89

10+
@pytest.mark.parametrize("level", [1, 5, 9])
11+
def test_json(level: int) -> None:
12+
codec = GzipCodec(level=level)
13+
expected_v2: GZipJSON_V2 = {
14+
"id": "gzip",
15+
"level": level,
16+
}
17+
expected_v3: GZipJSON_V3 = {
18+
"name": "gzip",
19+
"configuration": {
20+
"level": level,
21+
},
22+
}
23+
assert codec.to_json(zarr_format=2) == expected_v2
24+
assert codec.to_json(zarr_format=3) == expected_v3
25+
926

1027
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
1128
def test_gzip(store: Store) -> None:

tests/test_codecs/test_sharding.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from codecs import Codec
12
import pickle
2-
from typing import Any
3+
from typing import Any, Literal
34

45
import numpy as np
56
import numpy.typing as npt
@@ -16,13 +17,42 @@
1617
ShardingCodecIndexLocation,
1718
TransposeCodec,
1819
)
20+
from zarr.codecs.bytes import BytesCodec
21+
from zarr.codecs.crc32c_ import Crc32cCodec
22+
from zarr.codecs.sharding import ShardingJSON_V2, ShardingJSON_V3
1923
from zarr.core.buffer import NDArrayLike, default_buffer_prototype
2024
from zarr.storage import StorePath
2125

2226
from ..conftest import ArrayRequest
2327
from .test_codecs import _AsyncArrayProxy, order_from_dim
2428

2529

30+
@pytest.mark.parametrize("index_location", ["start", "end"])
31+
@pytest.mark.parametrize("chunk_shape", [(32, 32), (64, 64)])
32+
@pytest.mark.parametrize('codecs', [(BytesCodec(),)])
33+
@pytest.mark.parametrize('index_codecs', [(Crc32cCodec(),)])
34+
def test_sharding_codec_to_json(index_location: Literal["start", "end"], chunk_shape: tuple[int, ...], codecs: tuple[Codec, ...], index_codecs: tuple[Codec, ...]) -> None:
35+
codec = ShardingCodec(chunk_shape=chunk_shape, codecs=codecs, index_location=index_location, index_codecs=index_codecs)
36+
expected_v2: ShardingJSON_V2 = {
37+
"id": "sharding_indexed",
38+
"chunk_shape": chunk_shape,
39+
"codecs": tuple(c.to_json(zarr_format=2) for c in codecs),
40+
"index_codecs": tuple(c.to_json(zarr_format=2) for c in index_codecs),
41+
"index_location": index_location,
42+
}
43+
expected_v3: ShardingJSON_V3 = {
44+
"name": "sharding_indexed",
45+
"configuration": {
46+
"chunk_shape": chunk_shape,
47+
"codecs": tuple(c.to_json(zarr_format=3) for c in codecs),
48+
"index_codecs": tuple(c.to_json(zarr_format=3) for c in index_codecs),
49+
"index_location": index_location,
50+
},
51+
}
52+
assert codec.to_json(zarr_format=2) == expected_v2
53+
assert codec.to_json(zarr_format=3) == expected_v3
54+
55+
2656
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
2757
@pytest.mark.parametrize("index_location", ["start", "end"])
2858
@pytest.mark.parametrize(

tests/test_codecs/test_transpose.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,24 @@
55
from zarr import AsyncArray, config
66
from zarr.abc.store import Store
77
from zarr.codecs import TransposeCodec
8+
from zarr.codecs.transpose import TransposeJSON_V2, TransposeJSON_V3
89
from zarr.core.common import MemoryOrder
910
from zarr.storage import StorePath
1011

1112
from .test_codecs import _AsyncArrayProxy
1213

1314

15+
@pytest.mark.parametrize("order", [(1,2,3), (2,1,0)])
16+
def test_transpose_to_json(order: tuple[int, ...]) -> None:
17+
codec = TransposeCodec(order=order)
18+
expected_v2: TransposeJSON_V2 = {"id": "transpose", "order": order}
19+
expected_v3: TransposeJSON_V3 = {
20+
"name": "transpose",
21+
"configuration": {"order": order},
22+
}
23+
assert codec.to_json(zarr_format=2) == expected_v2
24+
assert codec.to_json(zarr_format=3) == expected_v3
25+
1426
@pytest.mark.parametrize("input_order", ["F", "C"])
1527
@pytest.mark.parametrize("runtime_write_order", ["F", "C"])
1628
@pytest.mark.parametrize("runtime_read_order", ["F", "C"])

tests/test_codecs/test_vlen.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from zarr.abc.codec import Codec
99
from zarr.abc.store import Store
1010
from zarr.codecs import ZstdCodec
11+
from zarr.codecs.vlen_utf8 import VLenUTF8Codec, VLenUTF8JSON_V2, VLenUTF8JSON_V3
1112
from zarr.core.dtype import get_data_type_from_native_dtype
1213
from zarr.core.dtype.npy.string import _NUMPY_SUPPORTS_VLEN_STRING
1314
from zarr.core.metadata.v3 import ArrayV3Metadata
@@ -21,6 +22,16 @@
2122
else:
2223
expected_array_string_dtype = np.dtype("O")
2324

25+
def test_vlen_utf8_to_json() -> None:
26+
codec = VLenUTF8Codec()
27+
expected_v2: VLenUTF8JSON_V2 = {
28+
"id": "vlen-utf8"}
29+
expected_v3: VLenUTF8JSON_V3 = {
30+
"name": "vlen-utf8",
31+
}
32+
assert codec.to_json(zarr_format=2) == expected_v2
33+
assert codec.to_json(zarr_format=3) == expected_v3
34+
2435

2536
@pytest.mark.filterwarnings("ignore::zarr.core.dtype.common.UnstableSpecificationWarning")
2637
@pytest.mark.parametrize("store", ["memory", "local"], indirect=["store"])

tests/test_codecs/test_zstd.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@
44
import zarr
55
from zarr.abc.store import Store
66
from zarr.codecs import ZstdCodec
7+
from zarr.codecs.zstd import ZstdJSON_V2, ZstdJSON_V3
78
from zarr.storage import StorePath
89

10+
@pytest.mark.parametrize("level", [1, 5, 9])
11+
@pytest.mark.parametrize('checksum', [True, False])
12+
def test_json(level: int, checksum: bool) -> None:
13+
codec = ZstdCodec(level=level, checksum=checksum)
14+
expected_v2: ZstdJSON_V2 = {
15+
"id": "zstd",
16+
"level": level,
17+
}
18+
expected_v3: ZstdJSON_V3 = {
19+
"name": "zstd",
20+
"configuration": {
21+
"level": level,
22+
"checksum": checksum
23+
},
24+
}
25+
assert codec.to_json(zarr_format=2) == expected_v2
26+
assert codec.to_json(zarr_format=3) == expected_v3
927

1028
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
1129
@pytest.mark.parametrize("checksum", [True, False])

0 commit comments

Comments
 (0)