forked from Thriftpy/thriftpy2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_type_mismatch.py
114 lines (86 loc) · 3.36 KB
/
test_type_mismatch.py
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
111
112
113
114
from unittest import TestCase
from thriftpy2.thrift import TType, TPayload
from thriftpy2.transport.memory import TMemoryBuffer
from thriftpy2.protocol.binary import TBinaryProtocol
from thriftpy2._compat import CYTHON
class Struct(TPayload):
thrift_spec = {
1: (TType.I32, 'a', False),
2: (TType.STRING, 'b', False),
3: (TType.DOUBLE, 'c', False)
}
default_spec = [('a', None), ('b', None), ('c', None)]
class TItem(TPayload):
thrift_spec = {
1: (TType.I32, "id", False),
2: (TType.LIST, "phones", TType.STRING, False),
3: (TType.MAP, "addr", (TType.I32, TType.STRING), False),
4: (TType.LIST, "data", (TType.STRUCT, Struct), False)
}
default_spec = [("id", None), ("phones", None), ("addr", None),
("data", None)]
class MismatchTestCase(TestCase):
BUFFER = TMemoryBuffer
PROTO = TBinaryProtocol
def test_list_type_mismatch(self):
class TMismatchItem(TPayload):
thrift_spec = {
1: (TType.I32, "id", False),
2: (TType.LIST, "phones", (TType.I32, False), False),
}
default_spec = [("id", None), ("phones", None)]
t = self.BUFFER()
p = self.PROTO(t)
item = TItem(id=37, phones=["23424", "235125"])
p.write_struct(item)
p.write_message_end()
item2 = TMismatchItem()
p.read_struct(item2)
assert item2.phones == []
def test_map_type_mismatch(self):
class TMismatchItem(TPayload):
thrift_spec = {
1: (TType.I32, "id", False),
3: (TType.MAP, "addr", (TType.STRING, TType.STRING), False)
}
default_spec = [("id", None), ("addr", None)]
t = self.BUFFER()
p = self.PROTO(t)
item = TItem(id=37, addr={1: "hello", 2: "world"})
p.write_struct(item)
p.write_message_end()
item2 = TMismatchItem()
p.read_struct(item2)
assert item2.addr == {}
def test_struct_mismatch(self):
class MismatchStruct(TPayload):
thrift_spec = {
1: (TType.STRING, 'a', False),
2: (TType.STRING, 'b', False)
}
default_spec = [('a', None), ('b', None)]
class TMismatchItem(TPayload):
thrift_spec = {
1: (TType.I32, "id", False),
2: (TType.LIST, "phones", TType.STRING, False),
3: (TType.MAP, "addr", (TType.I32, TType.STRING), False),
4: (TType.LIST, "data", (TType.STRUCT, MismatchStruct), False)
}
default_spec = [("id", None), ("phones", None), ("addr", None)]
t = self.BUFFER()
p = self.PROTO(t)
item = TItem(id=37, data=[Struct(a=1, b="hello", c=0.123),
Struct(a=2, b="world", c=34.342346),
Struct(a=3, b="when", c=25235.14)])
p.write_struct(item)
p.write_message_end()
item2 = TMismatchItem()
p.read_struct(item2)
assert len(item2.data) == 3
assert all([i.b for i in item2.data])
if CYTHON:
from thriftpy2.transport.memory import TCyMemoryBuffer
from thriftpy2.protocol.cybin import TCyBinaryProtocol
class CyMismatchTestCase(MismatchTestCase):
BUFFER = TCyMemoryBuffer
PROTO = TCyBinaryProtocol