1
1
# -*- coding: utf-8 -*-
2
2
3
- from io import BytesIO
3
+ # from io import BytesIO
4
4
5
5
from nose .tools import assert_equal
6
6
7
7
from thriftpy ._compat import u
8
8
from thriftpy .thrift import TType , TPayload
9
9
from thriftpy .utils import hexlify
10
- from thriftpy .protocol import cybinary as proto
10
+ from thriftpy .protocol import cybin as proto
11
+ from thriftpy .transport import TMemoryBuffer
11
12
12
13
13
14
class TItem (TPayload ):
@@ -19,103 +20,127 @@ class TItem(TPayload):
19
20
20
21
21
22
def test_pack_i8 ():
22
- b = BytesIO ()
23
- proto .write_val (b , TType .I08 , 123 )
23
+ b = TMemoryBuffer ()
24
+ p = proto .TCyBinaryProtocol (b )
25
+ p .write_val (TType .I08 , 123 )
26
+ p .write_message_end ()
24
27
assert_equal ("7b" , hexlify (b .getvalue ()))
25
28
26
29
27
30
def test_unpack_i8 ():
28
- b = BytesIO (b"{" )
29
- assert_equal (123 , proto .read_val (b , TType .I08 ))
31
+ b = TMemoryBuffer (b"{" )
32
+ p = proto .TCyBinaryProtocol (b )
33
+ assert_equal (123 , p .read_val (TType .I08 ))
30
34
31
35
32
36
def test_pack_i16 ():
33
- b = BytesIO ()
34
- proto .write_val (b , TType .I16 , 12345 )
37
+ b = TMemoryBuffer ()
38
+ p = proto .TCyBinaryProtocol (b )
39
+ p .write_val (TType .I16 , 12345 )
40
+ p .write_message_end ()
35
41
assert_equal ("30 39" , hexlify (b .getvalue ()))
36
42
37
43
38
44
def test_unpack_i16 ():
39
- b = BytesIO (b"09" )
40
- assert_equal (12345 , proto .read_val (b , TType .I16 ))
45
+ b = TMemoryBuffer (b"09" )
46
+ p = proto .TCyBinaryProtocol (b )
47
+ assert_equal (12345 , p .read_val (TType .I16 ))
41
48
42
49
43
50
def test_pack_i32 ():
44
- b = BytesIO ()
45
- proto .write_val (b , TType .I32 , 1234567890 )
51
+ b = TMemoryBuffer ()
52
+ p = proto .TCyBinaryProtocol (b )
53
+ p .write_val (TType .I32 , 1234567890 )
54
+ p .write_message_end ()
46
55
assert_equal ("49 96 02 d2" , hexlify (b .getvalue ()))
47
56
48
57
49
58
def test_unpack_i32 ():
50
- b = BytesIO (b'I\x96 \x02 \xd2 ' )
51
- assert_equal (1234567890 , proto .read_val (b , TType .I32 ))
59
+ b = TMemoryBuffer (b'I\x96 \x02 \xd2 ' )
60
+ p = proto .TCyBinaryProtocol (b )
61
+ assert_equal (1234567890 , p .read_val (TType .I32 ))
52
62
53
63
54
64
def test_pack_i64 ():
55
- b = BytesIO ()
56
- proto .write_val (b , TType .I64 , 1234567890123456789 )
65
+ b = TMemoryBuffer ()
66
+ p = proto .TCyBinaryProtocol (b )
67
+ p .write_val (TType .I64 , 1234567890123456789 )
68
+ p .write_message_end ()
57
69
assert_equal ("11 22 10 f4 7d e9 81 15" , hexlify (b .getvalue ()))
58
70
59
71
60
72
def test_unpack_i64 ():
61
- b = BytesIO (b'\x11 "\x10 \xf4 }\xe9 \x81 \x15 ' )
62
- assert_equal (1234567890123456789 , proto .read_val (b , TType .I64 ))
73
+ b = TMemoryBuffer (b'\x11 "\x10 \xf4 }\xe9 \x81 \x15 ' )
74
+ p = proto .TCyBinaryProtocol (b )
75
+ assert_equal (1234567890123456789 , p .read_val (TType .I64 ))
63
76
64
77
65
78
def test_pack_double ():
66
- b = BytesIO ()
67
- proto .write_val (b , TType .DOUBLE , 1234567890.1234567890 )
79
+ b = TMemoryBuffer ()
80
+ p = proto .TCyBinaryProtocol (b )
81
+ p .write_val (TType .DOUBLE , 1234567890.1234567890 )
82
+ p .write_message_end ()
68
83
assert_equal ("41 d2 65 80 b4 87 e6 b7" , hexlify (b .getvalue ()))
69
84
70
85
71
86
def test_unpack_double ():
72
- b = BytesIO (b'A\xd2 e\x80 \xb4 \x87 \xe6 \xb7 ' )
73
- assert_equal (1234567890.1234567890 , proto .read_val (b , TType .DOUBLE ))
87
+ b = TMemoryBuffer (b'A\xd2 e\x80 \xb4 \x87 \xe6 \xb7 ' )
88
+ p = proto .TCyBinaryProtocol (b )
89
+ assert_equal (1234567890.1234567890 , p .read_val (TType .DOUBLE ))
74
90
75
91
76
92
def test_pack_string ():
77
- b = BytesIO ()
78
- proto .write_val (b , TType .STRING , "hello world!" )
93
+ b = TMemoryBuffer ()
94
+ p = proto .TCyBinaryProtocol (b )
95
+ p .write_val (TType .STRING , "hello world!" )
96
+ p .write_message_end ()
79
97
assert_equal ("00 00 00 0c 68 65 6c 6c 6f 20 77 6f 72 6c 64 21" ,
80
98
hexlify (b .getvalue ()))
81
99
82
- b = BytesIO ()
83
- proto .write_val (b , TType .STRING , u ("你好世界" ))
100
+ b = TMemoryBuffer ()
101
+ p = proto .TCyBinaryProtocol (b )
102
+ p .write_val (TType .STRING , u ("你好世界" ))
103
+ p .write_message_end ()
84
104
assert_equal ("00 00 00 0c e4 bd a0 e5 a5 bd e4 b8 96 e7 95 8c" ,
85
105
hexlify (b .getvalue ()))
86
106
87
107
88
108
def test_unpack_string ():
89
- b = BytesIO (b'\x00 \x00 \x00 \x0c '
90
- b'\xe4 \xbd \xa0 \xe5 \xa5 \xbd \xe4 \xb8 \x96 \xe7 \x95 \x8c ' )
91
- assert_equal (u ("你好世界" ), proto .read_val (b , TType .STRING ))
109
+ b = TMemoryBuffer (b'\x00 \x00 \x00 \x0c '
110
+ b'\xe4 \xbd \xa0 \xe5 \xa5 \xbd \xe4 \xb8 \x96 \xe7 \x95 \x8c ' )
111
+ p = proto .TCyBinaryProtocol (b )
112
+ assert_equal (u ("你好世界" ), p .read_val (TType .STRING ))
92
113
93
114
94
115
def test_write_message_begin ():
95
- b = BytesIO ()
96
- proto .TCyBinaryProtocol (b ).write_message_begin ('test' , TType .STRING , 1 )
116
+ b = TMemoryBuffer ()
117
+ p = proto .TCyBinaryProtocol (b )
118
+ p .write_message_begin ('test' , TType .STRING , 1 )
119
+ p .write_message_end ()
97
120
assert_equal ("80 01 00 0b 00 00 00 04 74 65 73 74 00 00 00 01" ,
98
121
hexlify (b .getvalue ()))
99
122
100
123
101
124
def test_read_message_begin ():
102
- b = BytesIO (b'\x80 \x01 \x00 \x0b \x00 \x00 \x00 \x04 test\x00 \x00 \x00 \x01 ' )
125
+ b = TMemoryBuffer (b'\x80 \x01 \x00 \x0b \x00 \x00 \x00 \x04 test\x00 \x00 \x00 \x01 ' )
103
126
res = proto .TCyBinaryProtocol (b ).read_message_begin ()
104
127
assert_equal (res , ("test" , TType .STRING , 1 ))
105
128
106
129
107
130
def test_write_struct ():
108
- b = BytesIO ()
131
+ b = TMemoryBuffer ()
109
132
item = TItem (id = 123 , phones = ['123456' , 'abcdef' ])
110
- proto .TCyBinaryProtocol (b ).write_struct (item )
133
+ p = proto .TCyBinaryProtocol (b )
134
+ p .write_struct (item )
135
+ p .write_message_end ()
111
136
assert_equal ("08 00 01 00 00 00 7b 0f 00 02 0b 00 00 00 02 00 00 00 "
112
137
"06 31 32 33 34 35 36 00 00 00 06 61 62 63 64 65 66 00" ,
113
138
hexlify (b .getvalue ()))
114
139
115
140
116
141
def test_read_struct ():
117
- b = BytesIO (b'\x08 \x00 \x01 \x00 \x00 \x00 {\x0f \x00 \x02 \x0b \x00 \x00 \x00 '
118
- b'\x02 \x00 \x00 \x00 \x06 123456\x00 \x00 \x00 \x06 abcdef\x00 ' )
142
+ b = TMemoryBuffer (b'\x08 \x00 \x01 \x00 \x00 \x00 {\x0f \x00 \x02 \x0b \x00 \x00 \x00 '
143
+ b'\x02 \x00 \x00 \x00 \x06 123456\x00 \x00 \x00 \x06 abcdef\x00 ' )
119
144
_item = TItem (id = 123 , phones = ['123456' , 'abcdef' ])
120
145
_item2 = TItem ()
121
146
proto .TCyBinaryProtocol (b ).read_struct (_item2 )
0 commit comments