-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathtest_vlen_bytes.py
More file actions
89 lines (69 loc) · 2.3 KB
/
Copy pathtest_vlen_bytes.py
File metadata and controls
89 lines (69 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import numpy as np
import pytest
from tests.common import (
assert_array_items_equal,
check_backwards_compatibility,
check_config,
check_encode_decode_array,
check_repr,
greetings,
)
VLenBytes = pytest.importorskip("numcodecs.vlen").VLenBytes
greetings_bytes = [g.encode('utf-8') for g in greetings]
arrays = [
np.array([b'foo', b'bar', b'baz'] * 300, dtype=object),
np.array(greetings_bytes * 100, dtype=object),
np.array([b'foo', b'bar', b'baz'] * 300, dtype=object).reshape(90, 10),
np.array(greetings_bytes * 1000, dtype=object).reshape(
len(greetings_bytes), 100, 10, order='F'
),
]
def test_encode_decode():
for arr in arrays:
codec = VLenBytes()
check_encode_decode_array(arr, codec)
def test_config():
codec = VLenBytes()
check_config(codec)
def test_repr():
check_repr("VLenBytes()")
def test_backwards_compatibility():
check_backwards_compatibility(VLenBytes.codec_id, arrays, [VLenBytes()])
def test_encode_errors():
codec = VLenBytes()
with pytest.raises(TypeError):
codec.encode(1234)
with pytest.raises(TypeError):
codec.encode([1234, 5678])
with pytest.raises(TypeError):
codec.encode(np.ones(10, dtype='i4'))
def test_decode_errors():
codec = VLenBytes()
with pytest.raises(TypeError):
codec.decode(1234)
# these should look like corrupt data
with pytest.raises(ValueError):
codec.decode(b'foo')
with pytest.raises(ValueError):
codec.decode(np.arange(2, 3, dtype='i4'))
with pytest.raises(ValueError):
codec.decode(np.arange(10, 20, dtype='i4'))
with pytest.raises(TypeError):
codec.decode('foo')
# test out parameter
enc = codec.encode(arrays[0])
with pytest.raises(TypeError):
codec.decode(enc, out=b'foo')
with pytest.raises(TypeError):
codec.decode(enc, out='foo')
with pytest.raises(TypeError):
codec.decode(enc, out=123)
with pytest.raises(ValueError):
codec.decode(enc, out=np.zeros(10, dtype='i4'))
def test_encode_none():
a = np.array([b'foo', None, b'bar'], dtype=object)
codec = VLenBytes()
enc = codec.encode(a)
dec = codec.decode(enc)
expect = np.array([b'foo', b'', b'bar'], dtype=object)
assert_array_items_equal(expect, dec)