blob: 0a66b9255fec42bb943ab1e743694f6ffc6a862c [file] [log] [blame]
Ali-Akber Saifeeb7247872021-12-11 08:39:24 -08001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
20import unittest
21import os
22import sys
23
24gen_path = os.path.join(
25 os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "gen-py"
26)
27sys.path.append(gen_path)
28
29import _import_local_thrift # noqa
30from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory
31from thrift.protocol.TBinaryProtocol import TBinaryProtocolAcceleratedFactory
32from thrift.protocol.TCompactProtocol import TCompactProtocolFactory
33from thrift.protocol.TCompactProtocol import TCompactProtocolAcceleratedFactory
34from thrift.transport import TTransport
35from thrift.TSerialization import serialize, deserialize
36from TestServer.ttypes import Message
37
38
39class TestSerializer(unittest.TestCase):
40 def setUp(self):
41 self.message = Message("hello thrift", 42)
42 self.binary_serialized = b"\x0b\x00\x01\x00\x00\x00\x0chello thrift\n\x00\x02\x00\x00\x00\x00\x00\x00\x00*\x00"
43 self.compact_serialized = b'\x18\x0chello thrift\x16T\x00'
44
45 def verify(self, serialized, factory):
46 self.assertEqual(serialized, serialize(self.message, factory))
47
48 self.assertEqual(
49 "hello thrift",
50 deserialize(Message(), serialized, factory).body,
51 )
52 self.assertEqual(
53 42, deserialize(Message(), serialized, factory).num
54 )
55
Ali-Akber Saifeeb7247872021-12-11 08:39:24 -080056 self.assertRaises(EOFError, deserialize, Message(), b'', factory)
Ali-Akber Saifeeb7247872021-12-11 08:39:24 -080057
58 def test_TBinaryProtocol(self):
59 buf = TTransport.TMemoryBuffer()
60 transport = TTransport.TBufferedTransportFactory().getTransport(buf)
61 factory = TBinaryProtocolFactory(transport)
62 self.verify(self.binary_serialized, factory)
63
Ali-Akber Saifeeb7247872021-12-11 08:39:24 -080064 def test_TBinaryProtocolAccelerated(self):
65 buf = TTransport.TMemoryBuffer()
66 transport = TTransport.TBufferedTransportFactory().getTransport(buf)
67 factory = TBinaryProtocolAcceleratedFactory(transport)
68 self.verify(self.binary_serialized, factory)
69
70 def test_TCompactProtocol(self):
71 factory = TCompactProtocolFactory()
72 self.verify(self.compact_serialized, factory)
73
74 def test_TCompactProtocolAccelerated(self):
75 factory = TCompactProtocolAcceleratedFactory()
76 self.verify(self.compact_serialized, factory)
77
78
79if __name__ == "__main__":
80 unittest.main()