Ali-Akber Saifee | b724787 | 2021-12-11 08:39:24 -0800 | [diff] [blame] | 1 | # |
| 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 | |
| 20 | import unittest |
| 21 | import os |
| 22 | import sys |
| 23 | |
| 24 | gen_path = os.path.join( |
| 25 | os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "gen-py" |
| 26 | ) |
| 27 | sys.path.append(gen_path) |
| 28 | |
| 29 | import _import_local_thrift # noqa |
| 30 | from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory |
| 31 | from thrift.protocol.TBinaryProtocol import TBinaryProtocolAcceleratedFactory |
| 32 | from thrift.protocol.TCompactProtocol import TCompactProtocolFactory |
| 33 | from thrift.protocol.TCompactProtocol import TCompactProtocolAcceleratedFactory |
| 34 | from thrift.transport import TTransport |
| 35 | from thrift.TSerialization import serialize, deserialize |
| 36 | from TestServer.ttypes import Message |
| 37 | |
| 38 | |
| 39 | class 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 Saifee | b724787 | 2021-12-11 08:39:24 -0800 | [diff] [blame] | 56 | self.assertRaises(EOFError, deserialize, Message(), b'', factory) |
Ali-Akber Saifee | b724787 | 2021-12-11 08:39:24 -0800 | [diff] [blame] | 57 | |
| 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 Saifee | b724787 | 2021-12-11 08:39:24 -0800 | [diff] [blame] | 64 | 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 | |
| 79 | if __name__ == "__main__": |
| 80 | unittest.main() |