blob: 396e68d9d027a60521288dfe52c296bb3d393889 [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
Ali-Akber Saifeeb7247872021-12-11 08:39:24 -080034from thrift.TSerialization import serialize, deserialize
35from TestServer.ttypes import Message
36
37
38class TestSerializer(unittest.TestCase):
39 def setUp(self):
40 self.message = Message("hello thrift", 42)
41 self.binary_serialized = b"\x0b\x00\x01\x00\x00\x00\x0chello thrift\n\x00\x02\x00\x00\x00\x00\x00\x00\x00*\x00"
42 self.compact_serialized = b'\x18\x0chello thrift\x16T\x00'
43
44 def verify(self, serialized, factory):
45 self.assertEqual(serialized, serialize(self.message, factory))
46
47 self.assertEqual(
48 "hello thrift",
49 deserialize(Message(), serialized, factory).body,
50 )
51 self.assertEqual(
52 42, deserialize(Message(), serialized, factory).num
53 )
54
Ali-Akber Saifeeb7247872021-12-11 08:39:24 -080055 self.assertRaises(EOFError, deserialize, Message(), b'', factory)
Ali-Akber Saifeeb7247872021-12-11 08:39:24 -080056
57 def test_TBinaryProtocol(self):
Hasnain Lakhani0a760ff2025-08-26 18:02:09 -070058 factory = TBinaryProtocolFactory()
Ali-Akber Saifeeb7247872021-12-11 08:39:24 -080059 self.verify(self.binary_serialized, factory)
60
Ali-Akber Saifeeb7247872021-12-11 08:39:24 -080061 def test_TBinaryProtocolAccelerated(self):
Hasnain Lakhani0a760ff2025-08-26 18:02:09 -070062 factory = TBinaryProtocolAcceleratedFactory()
Ali-Akber Saifeeb7247872021-12-11 08:39:24 -080063 self.verify(self.binary_serialized, factory)
64
65 def test_TCompactProtocol(self):
66 factory = TCompactProtocolFactory()
67 self.verify(self.compact_serialized, factory)
68
69 def test_TCompactProtocolAccelerated(self):
70 factory = TCompactProtocolAcceleratedFactory()
71 self.verify(self.compact_serialized, factory)
72
73
74if __name__ == "__main__":
75 unittest.main()