Skip to content

Commit 0a983e6

Browse files
committed
add tests for compressors and filters kwargs to create_array
1 parent ae76bb3 commit 0a983e6

File tree

2 files changed

+111
-36
lines changed

2 files changed

+111
-36
lines changed

tests/test_api.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
save_array,
2424
save_group,
2525
)
26-
from zarr.codecs.transpose import TransposeCodec
27-
from zarr.codecs.zstd import ZstdCodec
2826
from zarr.core.common import MemoryOrder, ZarrFormat
2927
from zarr.errors import MetadataValidationError
3028
from zarr.storage._utils import normalize_path
@@ -1116,36 +1114,3 @@ def test_open_array_with_mode_r_plus(store: Store) -> None:
11161114
assert isinstance(z2, Array)
11171115
assert (z2[:] == 1).all()
11181116
z2[:] = 3
1119-
1120-
1121-
@pytest.mark.parametrize("store", ["memory"], indirect=True)
1122-
async def test_create_array_v3(store: MemoryStore) -> None:
1123-
# TODO: fill in
1124-
_ = zarr.create_array(
1125-
store=store,
1126-
dtype="uint8",
1127-
shape=(10,),
1128-
shards=(4,),
1129-
chunks=(4,),
1130-
zarr_format=3,
1131-
filters=(TransposeCodec(order=(0,)),),
1132-
compressors=ZstdCodec(level=3),
1133-
)
1134-
1135-
1136-
@pytest.mark.parametrize("store", ["memory"], indirect=True)
1137-
async def test_create_array_v2(store: MemoryStore) -> None:
1138-
from numcodecs import Delta, Zstd
1139-
1140-
# TODO: fill in
1141-
dtype = "uint8"
1142-
_ = zarr.create_array(
1143-
store=store,
1144-
dtype=dtype,
1145-
shape=(10,),
1146-
shards=None,
1147-
chunks=(4,),
1148-
zarr_format=2,
1149-
filters=(Delta(dtype=dtype),),
1150-
compressors=Zstd(level=3),
1151-
)

tests/test_array.py

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@
1212
import zarr.api.asynchronous
1313
from zarr import Array, AsyncArray, Group
1414
from zarr.codecs import BytesCodec, VLenBytesCodec, ZstdCodec
15+
from zarr.codecs.gzip import GzipCodec
1516
from zarr.codecs.sharding import ShardingCodec
17+
from zarr.codecs.transpose import TransposeCodec
1618
from zarr.core._info import ArrayInfo
17-
from zarr.core.array import chunks_initialized
19+
from zarr.core.array import (
20+
CompressorsParam,
21+
FiltersParam,
22+
_parse_chunk_encoding_v3,
23+
chunks_initialized,
24+
)
1825
from zarr.core.buffer import default_buffer_prototype
1926
from zarr.core.buffer.cpu import NDBuffer
2027
from zarr.core.chunk_grids import _auto_partition
@@ -957,3 +964,106 @@ def test_chunks_and_shards() -> None:
957964
)
958965
assert arr_v2.chunks == chunks
959966
assert arr_v2.shards is None
967+
968+
969+
@pytest.mark.parametrize("store", ["memory"], indirect=True)
970+
@pytest.mark.parametrize(
971+
"compressors",
972+
[
973+
"auto",
974+
(ZstdCodec(level=3),),
975+
(ZstdCodec(level=3), GzipCodec(level=0)),
976+
ZstdCodec(level=3),
977+
{"name": "zstd", "configuration": {"level": 3}},
978+
({"name": "zstd", "configuration": {"level": 3}},),
979+
],
980+
)
981+
async def test_create_array_v3_compressors(
982+
store: MemoryStore, compressors: CompressorsParam
983+
) -> None:
984+
"""
985+
Test various possibilities for the compressors parameter to create_array
986+
"""
987+
dtype = "uint8"
988+
arr = zarr.create_array(
989+
store=store,
990+
dtype=dtype,
991+
shape=(10,),
992+
zarr_format=3,
993+
compressors=compressors,
994+
)
995+
_, _, bb_codecs_expected = _parse_chunk_encoding_v3(
996+
filters=(), compressors=compressors, dtype=np.dtype(dtype)
997+
)
998+
# TODO: find a better way to get the compressors from the array.
999+
assert tuple(arr._async_array.metadata.codecs[-len(bb_codecs_expected) :]) == bb_codecs_expected # type: ignore[union-attr]
1000+
1001+
1002+
@pytest.mark.parametrize("store", ["memory"], indirect=True)
1003+
@pytest.mark.parametrize(
1004+
"filters",
1005+
[
1006+
"auto",
1007+
(
1008+
TransposeCodec(
1009+
order=[
1010+
0,
1011+
]
1012+
),
1013+
),
1014+
(
1015+
TransposeCodec(
1016+
order=[
1017+
0,
1018+
]
1019+
),
1020+
TransposeCodec(
1021+
order=[
1022+
0,
1023+
]
1024+
),
1025+
),
1026+
TransposeCodec(
1027+
order=[
1028+
0,
1029+
]
1030+
),
1031+
{"name": "transpose", "configuration": {"order": [0]}},
1032+
({"name": "transpose", "configuration": {"order": [0]}},),
1033+
],
1034+
)
1035+
async def test_create_array_v3_filters(store: MemoryStore, filters: FiltersParam) -> None:
1036+
"""
1037+
Test various possibilities for the filters parameter to create_array
1038+
"""
1039+
dtype = "uint8"
1040+
arr = zarr.create_array(
1041+
store=store,
1042+
dtype=dtype,
1043+
shape=(10,),
1044+
zarr_format=3,
1045+
filters=filters,
1046+
)
1047+
aa_codecs_expected, _, _ = _parse_chunk_encoding_v3(
1048+
filters=filters, compressors=(), dtype=np.dtype(dtype)
1049+
)
1050+
# TODO: find a better way to get the filters from the array.
1051+
assert tuple(arr._async_array.metadata.codecs[: len(aa_codecs_expected)]) == aa_codecs_expected # type: ignore[union-attr]
1052+
1053+
1054+
@pytest.mark.parametrize("store", ["memory"], indirect=True)
1055+
async def test_create_array_v2(store: MemoryStore) -> None:
1056+
from numcodecs import Delta, Zstd
1057+
1058+
# TODO: fill in
1059+
dtype = "uint8"
1060+
_ = zarr.create_array(
1061+
store=store,
1062+
dtype=dtype,
1063+
shape=(10,),
1064+
shards=None,
1065+
chunks=(4,),
1066+
zarr_format=2,
1067+
filters=(Delta(dtype=dtype),),
1068+
compressors=Zstd(level=3),
1069+
)

0 commit comments

Comments
 (0)