|
18 | 18 | BloscCodec, |
19 | 19 | BytesCodec, |
20 | 20 | Crc32cCodec, |
| 21 | + GzipCodec, |
21 | 22 | ShardingCodec, |
22 | 23 | TransposeCodec, |
23 | 24 | ) |
| 25 | +from zarr.codecs.numcodecs import CRC32 |
24 | 26 | from zarr.codecs.sharding import ( |
25 | 27 | INDEX_LOCATION, |
26 | 28 | MAX_UINT_64, |
@@ -1119,6 +1121,44 @@ def test_sharding_codec_rejects_unknown_index_location() -> None: |
1119 | 1121 | ShardingCodec(**kwargs) |
1120 | 1122 |
|
1121 | 1123 |
|
| 1124 | +@pytest.mark.parametrize( |
| 1125 | + "index_codecs", |
| 1126 | + [ |
| 1127 | + (BytesCodec(),), |
| 1128 | + (BytesCodec(), Crc32cCodec()), |
| 1129 | + (TransposeCodec(order=(1, 0)), BytesCodec(), Crc32cCodec()), |
| 1130 | + (BytesCodec(), CRC32()), |
| 1131 | + ], |
| 1132 | +) |
| 1133 | +def test_sharding_fixed_size_index_codecs_roundtrip(index_codecs: tuple[Any, ...]) -> None: |
| 1134 | + """ |
| 1135 | + Any chain of fixed-size codecs is accepted as `index_codecs`, and data |
| 1136 | + written through it reads back unchanged. |
| 1137 | + """ |
| 1138 | + data = np.arange(16, dtype="uint16") |
| 1139 | + arr = zarr.create_array( |
| 1140 | + MemoryStore(), |
| 1141 | + shape=data.shape, |
| 1142 | + dtype=data.dtype, |
| 1143 | + chunks=(2,), |
| 1144 | + shards=(8,), |
| 1145 | + compressors=None, |
| 1146 | + serializer=ShardingCodec(chunk_shape=(2,), index_codecs=index_codecs), |
| 1147 | + ) |
| 1148 | + arr[:] = data |
| 1149 | + np.testing.assert_array_equal(arr[:], data) |
| 1150 | + |
| 1151 | + |
| 1152 | +@pytest.mark.parametrize("compressor", [GzipCodec(), BloscCodec()]) |
| 1153 | +def test_sharding_codec_rejects_variable_size_index_codecs(compressor: Any) -> None: |
| 1154 | + """ |
| 1155 | + ShardingCodec rejects an `index_codecs` chain containing a codec whose |
| 1156 | + encoded size is not fixed, as the spec requires. |
| 1157 | + """ |
| 1158 | + with pytest.raises(ValueError, match="must produce a fixed-size encoding"): |
| 1159 | + ShardingCodec(chunk_shape=(2,), index_codecs=(BytesCodec(), compressor)) |
| 1160 | + |
| 1161 | + |
1122 | 1162 | def test_sharding_index_location_attribute_error_for_unknown_member() -> None: |
1123 | 1163 | """ |
1124 | 1164 | Attribute access for a name that is not a known member of the deprecated |
|
0 commit comments