/
test_pyobj_serialize.py
241 lines (201 loc) · 9.09 KB
/
test_pyobj_serialize.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
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import math
import unittest
import paddlehub as hub
import paddle.fluid as fluid
from paddlehub.utils import from_pyobj_to_flexible_data, from_flexible_data_to_pyobj, get_pykey
from paddlehub import module_desc_pb2
from paddlehub.logger import logger
def _compare_float(a, b):
error_value = 1.0e-9
logger.debug("a is %f and b is %f" % (a, b))
# if python version < 3.5
if sys.version_info < (3, 5):
return abs(a - b) < error_value
else:
return math.isclose(a, b)
def _check_none(pyobj, flexible_data):
assert flexible_data.type == module_desc_pb2.NONE, "type conversion error"
def _check_int(pyobj, flexible_data):
assert flexible_data.type == module_desc_pb2.INT, "type conversion error"
assert flexible_data.i == pyobj, "value convesion error"
def _check_float(pyobj, flexible_data):
assert flexible_data.type == module_desc_pb2.FLOAT, "type conversion error"
assert _compare_float(flexible_data.f, pyobj), "value convesion error"
def _check_str(pyobj, flexible_data):
assert flexible_data.type == module_desc_pb2.STRING, "type conversion error"
assert flexible_data.s == pyobj, "value convesion error"
def _check_bool(pyobj, flexible_data):
assert flexible_data.type == module_desc_pb2.BOOLEAN, "type conversion error"
assert flexible_data.b == pyobj, "value convesion error"
class TestPyobj2FlexibleData(unittest.TestCase):
def test_int_2_flexible_data(self):
input = None
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
_check_none(input, flexible_data)
def test_int_2_flexible_data(self):
input = 1
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
_check_int(input, flexible_data)
def test_float_2_flexible_data(self):
input = 2.012
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
_check_float(input, flexible_data)
def test_string_2_flexible_data(self):
input = "123"
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
_check_str(input, flexible_data)
def test_bool_2_flexible_data(self):
input = False
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
_check_bool(input, flexible_data)
def test_list_2_flexible_data(self):
input = [1, 2, 3]
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
assert flexible_data.type == module_desc_pb2.LIST, "type conversion error"
assert len(
flexible_data.list.data) == len(input), "value convesion error"
for index in range(len(input)):
_check_int(input[index], flexible_data.list.data[str(index)])
def test_tuple_2_flexible_data(self):
input = (1, 2, 3)
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
assert flexible_data.type == module_desc_pb2.LIST, "type conversion error"
assert len(
flexible_data.list.data) == len(input), "value convesion error"
for index in range(len(input)):
_check_int(input[index], flexible_data.list.data[str(index)])
def test_set_2_flexible_data(self):
input = {1, 2, 3}
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
assert flexible_data.type == module_desc_pb2.SET, "type conversion error"
assert len(
flexible_data.set.data) == len(input), "value convesion error"
for index in range(len(input)):
assert flexible_data.set.data[str(
index)].i in input, "value convesion error"
def test_dict_2_flexible_data(self):
input = {1: 1, 2: 2, 3: 3}
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
assert flexible_data.type == module_desc_pb2.MAP, "type conversion error"
assert len(
flexible_data.map.data) == len(input), "value convesion error"
for key, value in flexible_data.map.data.items():
realkey = get_pykey(key, flexible_data.map.keyType[key])
assert realkey in input, "key convesion error"
_check_int(input[realkey], flexible_data.map.data[key])
def test_obj_2_flexible_data(self):
class TestObj:
def __init__(self):
self.a = 1
self.b = 2.0
self.c = "str"
self.d = {'a': 123}
input = TestObj()
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
assert flexible_data.type == module_desc_pb2.OBJECT, "type conversion error"
assert len(flexible_data.object.data) == len(
input.__dict__), "value convesion error"
_check_int(input.a, flexible_data.object.data['a'])
_check_float(input.b, flexible_data.object.data['b'])
_check_str(input.c, flexible_data.object.data['c'])
_check_int(input.d['a'], flexible_data.object.data['d'].map.data['a'])
class TestFlexibleData2Pyobj(unittest.TestCase):
def test_flexible_data_2_int(self):
pass
class TestSerializeAndDeSerialize(unittest.TestCase):
def test_convert_none(self):
input = None
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
output = from_flexible_data_to_pyobj(flexible_data)
assert input == output, "none convesion error"
def test_convert_int(self):
input = 1
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
output = from_flexible_data_to_pyobj(flexible_data)
assert input == output, "int convesion error"
def test_convert_float(self):
input = 2.012
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
output = from_flexible_data_to_pyobj(flexible_data)
assert _compare_float(input, output), "float convesion error"
def test_convert_str(self):
input = "123"
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
output = from_flexible_data_to_pyobj(flexible_data)
assert input == output, "str convesion error"
def test_convert_bool(self):
input = False
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
output = from_flexible_data_to_pyobj(flexible_data)
assert input == output, "bool convesion error"
def test_convert_list(self):
input = [1, 2, 3]
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
output = from_flexible_data_to_pyobj(flexible_data)
assert input == output, "list convesion error"
def test_convert_tuple(self):
input = (1, 2, 3)
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
output = from_flexible_data_to_pyobj(flexible_data)
assert list(input) == output, "tuple convesion error"
def test_convert_set(self):
input = {1, 2, 3}
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
output = from_flexible_data_to_pyobj(flexible_data)
assert input == output, "set convesion error"
def test_convert_dict(self):
input = {1: 1, 2: 2, 3: 3}
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
output = from_flexible_data_to_pyobj(flexible_data)
assert input == output, "dict convesion error"
def test_convert_compound_object(self):
input = {
False: 1,
'2': 3,
4.0: [5, 6.0, ['7', {
8: 9
}]],
'set': {10},
'dict': {
11: 12
}
}
flexible_data = module_desc_pb2.FlexibleData()
from_pyobj_to_flexible_data(input, flexible_data)
output = from_flexible_data_to_pyobj(flexible_data)
assert input == output, "dict convesion error"
if __name__ == "__main__":
unittest.main()