Skip to content

Commit cc005a4

Browse files
Properly skip tests if crc32c isn't installed (#740)
Co-authored-by: Davis Bennett <[email protected]>
1 parent fedb0cb commit cc005a4

File tree

1 file changed

+60
-34
lines changed

1 file changed

+60
-34
lines changed

numcodecs/tests/test_checksum32.py

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,54 +39,59 @@
3939
np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('m8[m]'),
4040
]
4141

42-
codecs = [
42+
base_codecs = [
4343
CRC32(),
4444
CRC32(location="end"),
4545
Adler32(),
4646
Adler32(location="end"),
4747
]
48-
if has_crc32c:
49-
codecs.extend(
50-
[
51-
CRC32C(location="start"),
52-
CRC32C(),
53-
]
54-
)
5548

5649

57-
@pytest.mark.parametrize(("codec", "arr"), itertools.product(codecs, arrays))
50+
def get_all_codecs():
51+
codecs = base_codecs.copy()
52+
if has_crc32c:
53+
codecs.extend(
54+
[
55+
CRC32C(location="start"),
56+
CRC32C(),
57+
]
58+
)
59+
return codecs
60+
61+
62+
@pytest.mark.parametrize(("codec", "arr"), itertools.product(get_all_codecs(), arrays))
5863
def test_encode_decode(codec, arr):
5964
check_encode_decode(arr, codec)
6065

6166

62-
@pytest.mark.parametrize(("codec", "arr"), itertools.product(codecs, arrays))
67+
@pytest.mark.parametrize(("codec", "arr"), itertools.product(get_all_codecs(), arrays))
6368
def test_errors(codec, arr):
6469
enc = codec.encode(arr)
6570
with pytest.raises(RuntimeError):
6671
codec.decode(enc[:-1])
6772

6873

69-
@pytest.mark.parametrize("codec", codecs)
74+
@pytest.mark.parametrize("codec", get_all_codecs())
7075
def test_config(codec):
7176
check_config(codec)
7277

7378

74-
@pytest.mark.parametrize("codec", codecs)
79+
@pytest.mark.parametrize("codec", get_all_codecs())
7580
def test_err_input_too_small(codec):
7681
buf = b'000' # 3 bytes are too little for a 32-bit checksum
7782
with pytest.raises(ValueError):
7883
codec.decode(buf)
7984

8085

81-
@pytest.mark.parametrize("codec", codecs)
86+
@pytest.mark.parametrize("codec", get_all_codecs())
8287
def test_err_encode_non_contiguous(codec):
8388
# non-contiguous memory
8489
arr = np.arange(1000, dtype='i4')[::2]
8590
with pytest.raises(ValueError):
8691
codec.encode(arr)
8792

8893

89-
@pytest.mark.parametrize("codec", codecs)
94+
@pytest.mark.parametrize("codec", get_all_codecs())
9095
def test_err_encode_list(codec):
9196
data = ['foo', 'bar', 'baz']
9297
with pytest.raises(TypeError):
@@ -98,39 +103,60 @@ def test_err_location():
98103
CRC32(location="foo")
99104
with pytest.raises(ValueError):
100105
Adler32(location="foo")
101-
if has_crc32c:
102-
with pytest.raises(ValueError):
103-
CRC32C(location="foo")
106+
if not has_crc32c:
107+
pytest.skip("Needs `crc32c` installed")
108+
with pytest.raises(ValueError):
109+
CRC32C(location="foo")
110+
111+
112+
@pytest.mark.parametrize(
113+
"repr_str",
114+
[
115+
"CRC32(location='start')",
116+
"CRC32(location='end')",
117+
"Adler32(location='start')",
118+
"Adler32(location='end')",
119+
pytest.param(
120+
"CRC32C(location='start')",
121+
marks=pytest.mark.skipif(not has_crc32c, reason="Needs `crc32c` installed"),
122+
),
123+
pytest.param(
124+
"CRC32C(location='end')",
125+
marks=pytest.mark.skipif(not has_crc32c, reason="Needs `crc32c` installed"),
126+
),
127+
],
128+
)
129+
def test_repr(repr_str):
130+
check_repr(repr_str)
104131

105132

106-
def test_repr():
107-
check_repr("CRC32(location='start')")
108-
check_repr("CRC32(location='end')")
109-
check_repr("Adler32(location='start')")
110-
check_repr("Adler32(location='end')")
111-
if has_crc32c:
112-
check_repr("CRC32C(location='start')")
113-
check_repr("CRC32C(location='end')")
133+
@pytest.mark.parametrize(
134+
('codec_id', 'codec_instance'),
135+
[
136+
(CRC32.codec_id, CRC32()),
137+
(Adler32.codec_id, Adler32()),
138+
],
139+
)
140+
def test_backwards_compatibility(codec_id, codec_instance):
141+
check_backwards_compatibility(codec_id, arrays, [codec_instance])
114142

115143

116-
def test_backwards_compatibility():
117-
check_backwards_compatibility(CRC32.codec_id, arrays, [CRC32()])
118-
check_backwards_compatibility(Adler32.codec_id, arrays, [Adler32()])
119-
if has_crc32c:
120-
check_backwards_compatibility(CRC32C.codec_id, arrays, [CRC32C()])
144+
@pytest.mark.skipif(not has_crc32c, reason="Needs `crc32c` installed")
145+
def test_backwards_compatibility_crc32c():
146+
check_backwards_compatibility(CRC32C.codec_id, arrays, [CRC32C()])
121147

122148

123-
@pytest.mark.parametrize("codec", codecs)
149+
@pytest.mark.parametrize("codec", get_all_codecs())
124150
def test_err_encode_object_buffer(codec):
125151
check_err_encode_object_buffer(codec)
126152

127153

128-
@pytest.mark.parametrize("codec", codecs)
154+
@pytest.mark.parametrize("codec", get_all_codecs())
129155
def test_err_decode_object_buffer(codec):
130156
check_err_decode_object_buffer(codec)
131157

132158

133-
@pytest.mark.parametrize("codec", codecs)
159+
@pytest.mark.parametrize("codec", get_all_codecs())
134160
def test_err_out_too_small(codec):
135161
arr = np.arange(10, dtype='i4')
136162
out = np.empty_like(arr)[:-1]
@@ -164,7 +190,7 @@ def test_crc32c_incremental():
164190
assert checksum_full == checksum_part2
165191

166192

167-
@pytest.mark.parametrize("codec", codecs)
193+
@pytest.mark.parametrize("codec", get_all_codecs())
168194
def test_err_checksum(codec):
169195
arr = np.arange(0, 64, dtype="uint8")
170196
buf = bytearray(codec.encode(arr))

0 commit comments

Comments
 (0)