Repository navigation
Expand file tree
/
Copy pathtest_obj.py
More file actions
110 lines (74 loc) · 2.95 KB
/
Copy pathtest_obj.py
File metadata and controls
110 lines (74 loc) · 2.95 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
from pytest import raises
from msgpack import ExtType, packb, unpackb
def _decode_complex(obj):
if b"__complex__" in obj:
return complex(obj[b"real"], obj[b"imag"])
return obj
def _encode_complex(obj):
if isinstance(obj, complex):
return {b"__complex__": True, b"real": 1, b"imag": 2}
return obj
def test_encode_hook():
packed = packb([3, 1 + 2j], default=_encode_complex)
unpacked = unpackb(packed, use_list=1)
assert unpacked[1] == {b"__complex__": True, b"real": 1, b"imag": 2}
def test_decode_hook():
packed = packb([3, {b"__complex__": True, b"real": 1, b"imag": 2}])
unpacked = unpackb(packed, object_hook=_decode_complex, use_list=1)
assert unpacked[1] == 1 + 2j
def test_decode_pairs_hook():
packed = packb([3, {1: 2, 3: 4}])
prod_sum = 1 * 2 + 3 * 4
unpacked = unpackb(
packed,
object_pairs_hook=lambda lst: sum(k * v for k, v in lst),
use_list=1,
strict_map_key=False,
)
assert unpacked[1] == prod_sum
def test_decode_pairs_hook_receives_list():
def reject_duplicate_keys(pairs):
keys = [k for k, _ in pairs]
assert len(keys) == len(set(keys))
return dict(pairs)
packed = packb([{"a": 1, "b": 2}, 3])
assert unpackb(packed, object_pairs_hook=reject_duplicate_keys) == [{"a": 1, "b": 2}, 3]
assert unpackb(packed, object_pairs_hook=lambda pairs: pairs[0]) == [("a", 1), 3]
assert unpackb(packed, object_pairs_hook=lambda pairs: None) == [None, 3]
def test_only_one_obj_hook():
with raises(TypeError):
unpackb(b"", object_hook=lambda x: x, object_pairs_hook=lambda x: x)
def test_bad_hook():
with raises(TypeError):
packed = packb([3, 1 + 2j], default=lambda o: o)
unpackb(packed, use_list=1)
def _arr_to_str(arr):
return "".join(str(c) for c in arr)
def test_array_hook():
packed = packb([1, 2, 3])
unpacked = unpackb(packed, list_hook=_arr_to_str, use_list=1)
assert unpacked == "123"
class DecodeError(Exception):
pass
def bad_complex_decoder(o):
raise DecodeError("Ooops!")
def test_an_exception_in_objecthook1():
with raises(DecodeError):
packed = packb({1: {"__complex__": True, "real": 1, "imag": 2}})
unpackb(packed, object_hook=bad_complex_decoder, strict_map_key=False)
def test_an_exception_in_objecthook2():
with raises(DecodeError):
packed = packb({1: [{"__complex__": True, "real": 1, "imag": 2}]})
unpackb(packed, list_hook=bad_complex_decoder, use_list=1, strict_map_key=False)
def test_str_subclass_map_key_from_ext_hook():
class Key(str):
pass
key = Key("name")
packed = packb({ExtType(1, b"name"): 42})
options = {"ext_hook": lambda code, data: key}
result = unpackb(packed, strict_map_key=False, **options)
assert result == {key: 42}
assert next(iter(result)) is key
with raises(ValueError):
unpackb(packed, strict_map_key=True, **options)