David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | # |
| 4 | # Licensed to the Apache Software Foundation (ASF) under one |
| 5 | # or more contributor license agreements. See the NOTICE file |
| 6 | # distributed with this work for additional information |
| 7 | # regarding copyright ownership. The ASF licenses this file |
| 8 | # to you under the Apache License, Version 2.0 (the |
| 9 | # "License"); you may not use this file except in compliance |
| 10 | # with the License. You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, |
| 15 | # software distributed under the License is distributed on an |
| 16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | # KIND, either express or implied. See the License for the |
| 18 | # specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # |
| 21 | |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 22 | from ThriftTest.ttypes import ( |
| 23 | Bonk, |
| 24 | Bools, |
| 25 | LargeDeltas, |
| 26 | ListBonks, |
| 27 | NestedListsBonk, |
| 28 | NestedListsI32x2, |
| 29 | NestedListsI32x3, |
| 30 | NestedMixedx2, |
| 31 | Numberz, |
| 32 | VersioningTestV1, |
| 33 | VersioningTestV2, |
| 34 | Xtruct, |
| 35 | Xtruct2, |
| 36 | ) |
| 37 | |
Bryan Duxbury | df4cffd | 2011-03-15 17:16:09 +0000 | [diff] [blame] | 38 | from DebugProtoTest.ttypes import CompactProtoTestStruct, Empty |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 39 | from thrift.transport import TTransport |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 40 | from thrift.protocol import TBinaryProtocol, TCompactProtocol, TJSONProtocol |
David Reiss | 6acc269 | 2010-02-26 00:56:02 +0000 | [diff] [blame] | 41 | from thrift.TSerialization import serialize, deserialize |
Nobuaki Sukegawa | 33744b0 | 2016-01-03 14:24:39 +0900 | [diff] [blame] | 42 | import sys |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 43 | import unittest |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 44 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 45 | |
| 46 | class AbstractTest(unittest.TestCase): |
| 47 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 48 | def setUp(self): |
| 49 | self.v1obj = VersioningTestV1( |
| 50 | begin_in_both=12345, |
| 51 | old_string='aaa', |
| 52 | end_in_both=54321, |
| 53 | ) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 54 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 55 | self.v2obj = VersioningTestV2( |
| 56 | begin_in_both=12345, |
| 57 | newint=1, |
| 58 | newbyte=2, |
| 59 | newshort=3, |
| 60 | newlong=4, |
| 61 | newdouble=5.0, |
| 62 | newstruct=Bonk(message="Hello!", type=123), |
| 63 | newlist=[7, 8, 9], |
| 64 | newset=set([42, 1, 8]), |
| 65 | newmap={1: 2, 2: 3}, |
| 66 | newstring="Hola!", |
| 67 | end_in_both=54321, |
| 68 | ) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 69 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 70 | self.bools = Bools(im_true=True, im_false=False) |
| 71 | self.bools_flipped = Bools(im_true=False, im_false=True) |
Bryan Duxbury | df4cffd | 2011-03-15 17:16:09 +0000 | [diff] [blame] | 72 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 73 | self.large_deltas = LargeDeltas( |
| 74 | b1=self.bools, |
| 75 | b10=self.bools_flipped, |
| 76 | b100=self.bools, |
| 77 | check_true=True, |
| 78 | b1000=self.bools_flipped, |
| 79 | check_false=False, |
| 80 | vertwo2000=VersioningTestV2(newstruct=Bonk(message='World!', type=314)), |
| 81 | a_set2500=set(['lazy', 'brown', 'cow']), |
| 82 | vertwo3000=VersioningTestV2(newset=set([2, 3, 5, 7, 11])), |
Nobuaki Sukegawa | d479e23 | 2016-02-28 11:28:19 +0900 | [diff] [blame] | 83 | big_numbers=[2 ** 8, 2 ** 16, 2 ** 31 - 1, -(2 ** 31 - 1)] |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 84 | ) |
Bryan Duxbury | df4cffd | 2011-03-15 17:16:09 +0000 | [diff] [blame] | 85 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 86 | self.compact_struct = CompactProtoTestStruct( |
| 87 | a_byte=127, |
| 88 | a_i16=32000, |
| 89 | a_i32=1000000000, |
| 90 | a_i64=0xffffffffff, |
| 91 | a_double=5.6789, |
| 92 | a_string="my string", |
| 93 | true_field=True, |
| 94 | false_field=False, |
| 95 | empty_struct_field=Empty(), |
| 96 | byte_list=[-127, -1, 0, 1, 127], |
| 97 | i16_list=[-1, 0, 1, 0x7fff], |
| 98 | i32_list=[-1, 0, 0xff, 0xffff, 0xffffff, 0x7fffffff], |
| 99 | i64_list=[-1, 0, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0x7fffffffffffffff], |
| 100 | double_list=[0.1, 0.2, 0.3], |
| 101 | string_list=["first", "second", "third"], |
| 102 | boolean_list=[True, True, True, False, False, False], |
| 103 | struct_list=[Empty(), Empty()], |
| 104 | byte_set=set([-127, -1, 0, 1, 127]), |
| 105 | i16_set=set([-1, 0, 1, 0x7fff]), |
| 106 | i32_set=set([1, 2, 3]), |
| 107 | i64_set=set([-1, 0, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0x7fffffffffffffff]), |
| 108 | double_set=set([0.1, 0.2, 0.3]), |
| 109 | string_set=set(["first", "second", "third"]), |
| 110 | boolean_set=set([True, False]), |
| 111 | # struct_set=set([Empty()]), # unhashable instance |
| 112 | byte_byte_map={1: 2}, |
| 113 | i16_byte_map={1: 1, -1: 1, 0x7fff: 1}, |
| 114 | i32_byte_map={1: 1, -1: 1, 0x7fffffff: 1}, |
| 115 | i64_byte_map={0: 1, 1: 1, -1: 1, 0x7fffffffffffffff: 1}, |
| 116 | double_byte_map={-1.1: 1, 1.1: 1}, |
| 117 | string_byte_map={"first": 1, "second": 2, "third": 3, "": 0}, |
| 118 | boolean_byte_map={True: 1, False: 0}, |
| 119 | byte_i16_map={1: 1, 2: -1, 3: 0x7fff}, |
| 120 | byte_i32_map={1: 1, 2: -1, 3: 0x7fffffff}, |
| 121 | byte_i64_map={1: 1, 2: -1, 3: 0x7fffffffffffffff}, |
| 122 | byte_double_map={1: 0.1, 2: -0.1, 3: 1000000.1}, |
| 123 | byte_string_map={1: "", 2: "blah", 3: "loooooooooooooong string"}, |
| 124 | byte_boolean_map={1: True, 2: False}, |
| 125 | # list_byte_map # unhashable |
| 126 | # set_byte_map={set([1, 2, 3]) : 1, set([0, 1]) : 2, set([]) : 0}, # unhashable |
| 127 | # map_byte_map # unhashable |
| 128 | byte_map_map={0: {}, 1: {1: 1}, 2: {1: 1, 2: 2}}, |
| 129 | byte_set_map={0: set([]), 1: set([1]), 2: set([1, 2])}, |
| 130 | byte_list_map={0: [], 1: [1], 2: [1, 2]}, |
| 131 | ) |
Bryan Duxbury | df4cffd | 2011-03-15 17:16:09 +0000 | [diff] [blame] | 132 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 133 | self.nested_lists_i32x2 = NestedListsI32x2( |
| 134 | [ |
| 135 | [1, 1, 2], |
| 136 | [2, 7, 9], |
| 137 | [3, 5, 8] |
| 138 | ] |
| 139 | ) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 140 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 141 | self.nested_lists_i32x3 = NestedListsI32x3( |
| 142 | [ |
| 143 | [ |
| 144 | [2, 7, 9], |
| 145 | [3, 5, 8] |
| 146 | ], |
| 147 | [ |
| 148 | [1, 1, 2], |
| 149 | [1, 4, 9] |
Konrad Grochowski | d5f3be5 | 2014-10-08 15:32:21 +0200 | [diff] [blame] | 150 | ] |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 151 | ] |
| 152 | ) |
Konrad Grochowski | d5f3be5 | 2014-10-08 15:32:21 +0200 | [diff] [blame] | 153 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 154 | self.nested_mixedx2 = NestedMixedx2(int_set_list=[ |
| 155 | set([1, 2, 3]), |
| 156 | set([1, 4, 9]), |
| 157 | set([1, 2, 3, 5, 8, 13, 21]), |
| 158 | set([-1, 0, 1]) |
| 159 | ], |
| 160 | # note, the sets below are sets of chars, since the strings are iterated |
| 161 | map_int_strset={10: set('abc'), 20: set('def'), 30: set('GHI')}, |
| 162 | map_int_strset_list=[ |
Nobuaki Sukegawa | d479e23 | 2016-02-28 11:28:19 +0900 | [diff] [blame] | 163 | {10: set('abc'), 20: set('def'), 30: set('GHI')}, |
| 164 | {100: set('lmn'), 200: set('opq'), 300: set('RST')}, |
| 165 | {1000: set('uvw'), 2000: set('wxy'), 3000: set('XYZ')}] |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 166 | ) |
| 167 | |
| 168 | self.nested_lists_bonk = NestedListsBonk( |
| 169 | [ |
| 170 | [ |
| 171 | [ |
| 172 | Bonk(message='inner A first', type=1), |
| 173 | Bonk(message='inner A second', type=1) |
| 174 | ], |
| 175 | [ |
| 176 | Bonk(message='inner B first', type=2), |
| 177 | Bonk(message='inner B second', type=2) |
| 178 | ] |
| 179 | ] |
| 180 | ] |
| 181 | ) |
| 182 | |
| 183 | self.list_bonks = ListBonks( |
| 184 | [ |
| 185 | Bonk(message='inner A', type=1), |
| 186 | Bonk(message='inner B', type=2), |
| 187 | Bonk(message='inner C', type=0) |
| 188 | ] |
| 189 | ) |
| 190 | |
| 191 | def _serialize(self, obj): |
| 192 | trans = TTransport.TMemoryBuffer() |
| 193 | prot = self.protocol_factory.getProtocol(trans) |
| 194 | obj.write(prot) |
| 195 | return trans.getvalue() |
| 196 | |
| 197 | def _deserialize(self, objtype, data): |
| 198 | prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data)) |
| 199 | ret = objtype() |
| 200 | ret.read(prot) |
| 201 | return ret |
| 202 | |
| 203 | def testForwards(self): |
| 204 | obj = self._deserialize(VersioningTestV2, self._serialize(self.v1obj)) |
| 205 | self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both) |
| 206 | self.assertEquals(obj.end_in_both, self.v1obj.end_in_both) |
| 207 | |
| 208 | def testBackwards(self): |
| 209 | obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj)) |
| 210 | self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both) |
| 211 | self.assertEquals(obj.end_in_both, self.v2obj.end_in_both) |
| 212 | |
| 213 | def testSerializeV1(self): |
| 214 | obj = self._deserialize(VersioningTestV1, self._serialize(self.v1obj)) |
| 215 | self.assertEquals(obj, self.v1obj) |
| 216 | |
| 217 | def testSerializeV2(self): |
| 218 | obj = self._deserialize(VersioningTestV2, self._serialize(self.v2obj)) |
| 219 | self.assertEquals(obj, self.v2obj) |
| 220 | |
| 221 | def testBools(self): |
| 222 | self.assertNotEquals(self.bools, self.bools_flipped) |
| 223 | self.assertNotEquals(self.bools, self.v1obj) |
| 224 | obj = self._deserialize(Bools, self._serialize(self.bools)) |
| 225 | self.assertEquals(obj, self.bools) |
| 226 | obj = self._deserialize(Bools, self._serialize(self.bools_flipped)) |
| 227 | self.assertEquals(obj, self.bools_flipped) |
| 228 | rep = repr(self.bools) |
| 229 | self.assertTrue(len(rep) > 0) |
| 230 | |
| 231 | def testLargeDeltas(self): |
| 232 | # test large field deltas (meaningful in CompactProto only) |
| 233 | obj = self._deserialize(LargeDeltas, self._serialize(self.large_deltas)) |
| 234 | self.assertEquals(obj, self.large_deltas) |
| 235 | rep = repr(self.large_deltas) |
| 236 | self.assertTrue(len(rep) > 0) |
| 237 | |
| 238 | def testNestedListsI32x2(self): |
| 239 | obj = self._deserialize(NestedListsI32x2, self._serialize(self.nested_lists_i32x2)) |
| 240 | self.assertEquals(obj, self.nested_lists_i32x2) |
| 241 | rep = repr(self.nested_lists_i32x2) |
| 242 | self.assertTrue(len(rep) > 0) |
| 243 | |
| 244 | def testNestedListsI32x3(self): |
| 245 | obj = self._deserialize(NestedListsI32x3, self._serialize(self.nested_lists_i32x3)) |
| 246 | self.assertEquals(obj, self.nested_lists_i32x3) |
| 247 | rep = repr(self.nested_lists_i32x3) |
| 248 | self.assertTrue(len(rep) > 0) |
| 249 | |
| 250 | def testNestedMixedx2(self): |
| 251 | obj = self._deserialize(NestedMixedx2, self._serialize(self.nested_mixedx2)) |
| 252 | self.assertEquals(obj, self.nested_mixedx2) |
| 253 | rep = repr(self.nested_mixedx2) |
| 254 | self.assertTrue(len(rep) > 0) |
| 255 | |
| 256 | def testNestedListsBonk(self): |
| 257 | obj = self._deserialize(NestedListsBonk, self._serialize(self.nested_lists_bonk)) |
| 258 | self.assertEquals(obj, self.nested_lists_bonk) |
| 259 | rep = repr(self.nested_lists_bonk) |
| 260 | self.assertTrue(len(rep) > 0) |
| 261 | |
| 262 | def testListBonks(self): |
| 263 | obj = self._deserialize(ListBonks, self._serialize(self.list_bonks)) |
| 264 | self.assertEquals(obj, self.list_bonks) |
| 265 | rep = repr(self.list_bonks) |
| 266 | self.assertTrue(len(rep) > 0) |
| 267 | |
| 268 | def testCompactStruct(self): |
| 269 | # test large field deltas (meaningful in CompactProto only) |
| 270 | obj = self._deserialize(CompactProtoTestStruct, self._serialize(self.compact_struct)) |
| 271 | self.assertEquals(obj, self.compact_struct) |
| 272 | rep = repr(self.compact_struct) |
| 273 | self.assertTrue(len(rep) > 0) |
| 274 | |
| 275 | def testIntegerLimits(self): |
| 276 | if (sys.version_info[0] == 2 and sys.version_info[1] <= 6): |
| 277 | print('Skipping testIntegerLimits for Python 2.6') |
| 278 | return |
| 279 | bad_values = [CompactProtoTestStruct(a_byte=128), CompactProtoTestStruct(a_byte=-129), |
| 280 | CompactProtoTestStruct(a_i16=32768), CompactProtoTestStruct(a_i16=-32769), |
| 281 | CompactProtoTestStruct(a_i32=2147483648), CompactProtoTestStruct(a_i32=-2147483649), |
| 282 | CompactProtoTestStruct(a_i64=9223372036854775808), CompactProtoTestStruct(a_i64=-9223372036854775809) |
| 283 | ] |
| 284 | |
| 285 | for value in bad_values: |
| 286 | self.assertRaises(Exception, self._serialize, value) |
Konrad Grochowski | d5f3be5 | 2014-10-08 15:32:21 +0200 | [diff] [blame] | 287 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 288 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 289 | class NormalBinaryTest(AbstractTest): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 290 | protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 291 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 292 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 293 | class AcceleratedBinaryTest(AbstractTest): |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 294 | protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False) |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 295 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 296 | |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 297 | class CompactProtocolTest(AbstractTest): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 298 | protocol_factory = TCompactProtocol.TCompactProtocolFactory() |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 299 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 300 | |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 301 | class AcceleratedCompactTest(AbstractTest): |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 302 | protocol_factory = TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 303 | |
| 304 | |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 305 | class JSONProtocolTest(AbstractTest): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 306 | protocol_factory = TJSONProtocol.TJSONProtocolFactory() |
Roger Meier | ad8154a | 2012-12-18 21:02:16 +0100 | [diff] [blame] | 307 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 308 | |
David Reiss | 4c591c9 | 2009-01-31 21:39:25 +0000 | [diff] [blame] | 309 | class AcceleratedFramedTest(unittest.TestCase): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 310 | def testSplit(self): |
| 311 | """Test FramedTransport and BinaryProtocolAccelerated |
David Reiss | 4c591c9 | 2009-01-31 21:39:25 +0000 | [diff] [blame] | 312 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 313 | Tests that TBinaryProtocolAccelerated and TFramedTransport |
| 314 | play nicely together when a read spans a frame""" |
David Reiss | 4c591c9 | 2009-01-31 21:39:25 +0000 | [diff] [blame] | 315 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 316 | protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() |
| 317 | bigstring = "".join(chr(byte) for byte in range(ord("a"), ord("z") + 1)) |
David Reiss | 4c591c9 | 2009-01-31 21:39:25 +0000 | [diff] [blame] | 318 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 319 | databuf = TTransport.TMemoryBuffer() |
| 320 | prot = protocol_factory.getProtocol(databuf) |
| 321 | prot.writeI32(42) |
| 322 | prot.writeString(bigstring) |
| 323 | prot.writeI16(24) |
| 324 | data = databuf.getvalue() |
| 325 | cutpoint = len(data) // 2 |
| 326 | parts = [data[:cutpoint], data[cutpoint:]] |
David Reiss | 4c591c9 | 2009-01-31 21:39:25 +0000 | [diff] [blame] | 327 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 328 | framed_buffer = TTransport.TMemoryBuffer() |
| 329 | framed_writer = TTransport.TFramedTransport(framed_buffer) |
| 330 | for part in parts: |
| 331 | framed_writer.write(part) |
| 332 | framed_writer.flush() |
| 333 | self.assertEquals(len(framed_buffer.getvalue()), len(data) + 8) |
David Reiss | 4c591c9 | 2009-01-31 21:39:25 +0000 | [diff] [blame] | 334 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 335 | # Recreate framed_buffer so we can read from it. |
| 336 | framed_buffer = TTransport.TMemoryBuffer(framed_buffer.getvalue()) |
| 337 | framed_reader = TTransport.TFramedTransport(framed_buffer) |
| 338 | prot = protocol_factory.getProtocol(framed_reader) |
| 339 | self.assertEqual(prot.readI32(), 42) |
| 340 | self.assertEqual(prot.readString(), bigstring) |
| 341 | self.assertEqual(prot.readI16(), 24) |
| 342 | |
David Reiss | 4c591c9 | 2009-01-31 21:39:25 +0000 | [diff] [blame] | 343 | |
David Reiss | 6acc269 | 2010-02-26 00:56:02 +0000 | [diff] [blame] | 344 | class SerializersTest(unittest.TestCase): |
| 345 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 346 | def testSerializeThenDeserialize(self): |
| 347 | obj = Xtruct2(i32_thing=1, |
| 348 | struct_thing=Xtruct(string_thing="foo")) |
David Reiss | 6acc269 | 2010-02-26 00:56:02 +0000 | [diff] [blame] | 349 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 350 | s1 = serialize(obj) |
| 351 | for i in range(10): |
| 352 | self.assertEquals(s1, serialize(obj)) |
| 353 | objcopy = Xtruct2() |
| 354 | deserialize(objcopy, serialize(obj)) |
| 355 | self.assertEquals(obj, objcopy) |
David Reiss | 6acc269 | 2010-02-26 00:56:02 +0000 | [diff] [blame] | 356 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 357 | obj = Xtruct(string_thing="bar") |
| 358 | objcopy = Xtruct() |
| 359 | deserialize(objcopy, serialize(obj)) |
| 360 | self.assertEquals(obj, objcopy) |
David Reiss | 4c591c9 | 2009-01-31 21:39:25 +0000 | [diff] [blame] | 361 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 362 | # test booleans |
| 363 | obj = Bools(im_true=True, im_false=False) |
| 364 | objcopy = Bools() |
| 365 | deserialize(objcopy, serialize(obj)) |
| 366 | self.assertEquals(obj, objcopy) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 367 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 368 | # test enums |
| 369 | for num, name in Numberz._VALUES_TO_NAMES.items(): |
| 370 | obj = Bonk(message='enum Numberz value %d is string %s' % (num, name), type=num) |
| 371 | objcopy = Bonk() |
| 372 | deserialize(objcopy, serialize(obj)) |
| 373 | self.assertEquals(obj, objcopy) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 374 | |
David Reiss | 4c591c9 | 2009-01-31 21:39:25 +0000 | [diff] [blame] | 375 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 376 | def suite(): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 377 | suite = unittest.TestSuite() |
| 378 | loader = unittest.TestLoader() |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 379 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 380 | suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest)) |
| 381 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest)) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 382 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedCompactTest)) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 383 | suite.addTest(loader.loadTestsFromTestCase(CompactProtocolTest)) |
| 384 | suite.addTest(loader.loadTestsFromTestCase(JSONProtocolTest)) |
| 385 | suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest)) |
| 386 | suite.addTest(loader.loadTestsFromTestCase(SerializersTest)) |
| 387 | return suite |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 388 | |
| 389 | if __name__ == "__main__": |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 390 | unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) |