/
test_convertor.py
266 lines (236 loc) · 8.9 KB
/
test_convertor.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Module Description:
Class ConvertorTest
Authors: Li Yuanjian (liyuanjian@baidu.com)
Date: 2015-12-02 21:19:11
Convert json:
{
"test_simple_map": {
"a": 1,
"b": 2
},
"name": "abc",
"add_more": {
"more": "more"
},
"error_type": "SUCCESS",
"in_list": [
{
"l_name": "l_name1"
},
{
"l_name": "l_name2"
},
{
"l_name": "l_name3"
}
],
"test_struct_map": {
"a": {
"value": "abc"
},
"b": {
"value": "abc1"
}
},
"num": 128,
"simple_list": [
1,
2,
3,
4
],
"desc": "for_lyj_test"
}
To thrift struct:
meta(test_simple_map={u'a': 1, u'b': 2}, name=u'abc', add_more=in_meta(in_list=None, more=u'more'), error_type=1, in_list=[list_test(l_name=u'l_name1'), list_test(l_name=u'l_name2'), list_test(l_name=u'l_name3')], test_struct_map={u'a': test_map_value(value=u'abc'), u'b': test_map_value(value=u'abc1')}, num=128, simple_list=[1, 2, 3, 4], desc=u'for_lyj_test')
.....
Convert thrift struct:
{'test_simple_map': {'a': 1, 'b': 2}, 'name': 'abc', 'add_more': {'more': 'more'}, 'error_type': 'SUCCESS', 'in_list': [{'l_name': 'l_name1'}, {'l_name': 'l_name2'}, {'l_name': 'l_name3'}, {'l_name': 'l_name4'}], 'test_struct_map': {'a': {'value': 'abc'}, 'b': {'value': 'abc1'}}, 'num': 128, 'simple_list': [1, 2, 3, 4], 'desc': 'for_lyj_test'}
To json:
{
"test_simple_map": {
"a": 1,
"b": 2
},
"name": "abc",
"add_more": {
"more": "more"
},
"error_type": "SUCCESS",
"in_list": [
{
"l_name": "l_name1"
},
{
"l_name": "l_name2"
},
{
"l_name": "l_name3"
},
{
"l_name": "l_name4"
}
],
"test_struct_map": {
"a": {
"value": "abc"
},
"b": {
"value": "abc1"
}
},
"num": 128,
"simple_list": [
1,
2,
3,
4
],
"desc": "for_lyj_test"
}
"""
import sys
import unittest
import json
import thriftpy
from convertor import json_thrift_convertor, thrift_json_convertor
from convertor import UnknowInputFieldsError
class Json2ThfirtConvertorTest(unittest.TestCase):
""" Test class for Convertor"""
def setUp(self):
self._input_json = '{ \
"name": "abc", \
"num": 128, \
"desc": "for_lyj_test", \
"simple_list": [1,2,3,4], \
"add_more": {"more": "more"}, \
"in_list": [{"l_name": "l_name1"}, {"l_name": "l_name2"}, {"l_name": "l_name3"}], \
"error_type": "SUCCESS", \
"test_struct_map" : {\
"a": {"value": "abc"}, \
"b": {"value": "abc1"} \
}, \
"test_simple_map" : {"a": 1, "b": 2} \
}'
test_thrift = thriftpy.load("test.thrift", module_name="test_thrift")
self._json_struct = json.loads(self._input_json)
self._thrift_struct = test_thrift.meta()
json_thrift_convertor(self._json_struct, self._thrift_struct)
def test_normal_fields_json2thrift(self):
""" Normal test for conver from json to thrift struct"""
self.assertEqual(self._json_struct['name'], self._thrift_struct.name)
self.assertEqual(self._json_struct['num'], self._thrift_struct.num)
self.assertEqual(self._json_struct['desc'], self._thrift_struct.desc)
def test_enum_fields_json2thrift(self):
""" Enum fields test """
self.assertEqual(self._thrift_struct.error_type, 1)
def test_nested_fields_json2thrift(self):
""" Nested fields test """
self.assertEqual(self._thrift_struct.add_more.more,
self._json_struct['add_more']['more'])
def test_list_with_struct_json2thrift(self):
""" Test complex list with struct"""
for i in xrange(0, len(self._json_struct["in_list"])):
self.assertEqual(self._thrift_struct.in_list[i].l_name,
self._json_struct["in_list"][i]["l_name"])
def test_list_with_basetype_json2thrift(self):
""" Test list with basetype """
for i in xrange(0, len(self._json_struct["simple_list"])):
self.assertEqual(self._thrift_struct.simple_list[i],
self._json_struct["simple_list"][i])
def test_noexist_fields_error_json2thrift(self):
""" Test no exist fields """
input_json = '{"name": "abc", "num": 128, "desc": "for_lyj_test", \
"add_more": {"more": "more"}, \
"not_exists_list": [{"l_name": "l_name1"}, \
{"l_name": "l_name2"}, {"l_name": "l_name3"}]}'
json_struct = json.loads(input_json)
self.assertRaises(UnknowInputFieldsError, json_thrift_convertor,
json_struct, self._thrift_struct)
def test_map_of_basetype_json2thrift(self):
""" Test map of basetype """
for (k, v) in self._json_struct["test_simple_map"].items():
self.assertEqual(self._thrift_struct.test_simple_map[k], v)
def test_map_of_struct_json2thrift(self):
""" Test map of basetype """
for (k, v) in self._json_struct["test_struct_map"].items():
self.assertEqual(self._thrift_struct.test_struct_map[k].value,
v["value"])
print "\nConvert json:"
print json.dumps(self._json_struct, indent=4)
print "To thrift struct:"
print self._thrift_struct
class Thrift2JsonConvertorTest(unittest.TestCase):
"""docstring for Thrift2JsonConvertorTest"""
def setUp(self):
test_thrift = thriftpy.load("test.thrift", module_name="test_thrift")
self._output_dict = dict()
self._thrift_struct = test_thrift.meta()
self._thrift_struct.name = "abc"
self._thrift_struct.num = 128
self._thrift_struct.desc = "for_lyj_test"
self._thrift_struct.error_type = 1
self._thrift_struct.simple_list = [1, 2, 3, 4]
tmp_list = []
for i in xrange(1,5):
tmp = test_thrift.list_test()
tmp.l_name = "l_name%d" % i
tmp_list.append(tmp)
self._thrift_struct.in_list = tmp_list
tmp = test_thrift.in_meta()
tmp.more = "more"
self._thrift_struct.add_more = tmp
tmp_map = dict()
map_value = test_thrift.test_map_value()
map_value.value = "abc"
tmp_map["a"] = map_value
map_value = test_thrift.test_map_value()
map_value.value = "abc1"
tmp_map["b"] = map_value
self._thrift_struct.test_struct_map = tmp_map
tmp_map = {"a": 1, "b": 2}
self._thrift_struct.test_simple_map = tmp_map
thrift_json_convertor(self._thrift_struct, self._output_dict)
def test_normal_fields_thrift2json(self):
""" Normal test for conver from json to thrift struct"""
self.assertEqual(self._output_dict['name'], self._thrift_struct.name)
self.assertEqual(self._output_dict['num'], self._thrift_struct.num)
self.assertEqual(self._output_dict['desc'], self._thrift_struct.desc)
def test_enum_fields_thrift2json(self):
""" Enum fields test """
self.assertEqual(self._thrift_struct.error_type, 1)
def test_nested_fields_thrift2json(self):
""" Nested fields test """
self.assertEqual(self._thrift_struct.add_more.more,
self._output_dict['add_more']['more'])
def test_list_with_struct_thrift2json(self):
""" Test complex list with struct"""
for i in xrange(0, len(self._output_dict["in_list"])):
self.assertEqual(self._thrift_struct.in_list[i].l_name,
self._output_dict["in_list"][i]["l_name"])
def test_list_with_basetype_thrift2json(self):
""" Test list with basetype """
for i in xrange(0, len(self._output_dict["simple_list"])):
self.assertEqual(self._thrift_struct.simple_list[i],
self._output_dict["simple_list"][i])
def test_map_with_basetype_thrift2json(self):
""" Test map with basetype """
for (k, v) in self._output_dict["test_simple_map"].items():
self.assertEqual(self._thrift_struct.test_simple_map[k], v)
def test_map_with_struct_thrift2json(self):
""" Test map with struct """
for (k, v) in self._output_dict["test_struct_map"].items():
#print k,v, self._thrift_struct.test_struct_map[k]
self.assertEqual(self._thrift_struct.test_struct_map[k].value, v["value"])
def test_json_loads(self):
""" Just test for _output_dict can dumps by json (it means all struct
of thrift is successfully translate) """
print "\nConvert thrift struct:"
print self._output_dict
print "To json:"
print json.dumps(self._output_dict, indent=4)
if __name__ == '__main__':
unittest.main()