Roshan Rajan | 284e6b3 | 2023-06-26 18:03:03 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 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 | |
| 22 | from __future__ import annotations |
| 23 | |
| 24 | import sys |
| 25 | from shared_types.ttypes import SharedEnum |
| 26 | from thrift.TSerialization import serialize, deserialize |
| 27 | from thrift.protocol import TBinaryProtocol |
| 28 | from thrift.transport import TTransport |
| 29 | |
| 30 | def deserialize_immutable(base, |
| 31 | buf, |
| 32 | protocol_factory=TBinaryProtocol.TBinaryProtocolFactory()): |
| 33 | transport = TTransport.TMemoryBuffer(buf) |
| 34 | protocol = protocol_factory.getProtocol(transport) |
| 35 | return base.read(protocol) |
| 36 | |
| 37 | def serialization_deserialization_struct_enum_test(): |
| 38 | test_obj = TestStruct(param1="test_string", param2=TestEnum.TestEnum1, param3=SharedEnum.SharedEnum1) |
| 39 | test_obj_serialized = serialize(test_obj) |
| 40 | test_obj2 = deserialize(TestStruct(), test_obj_serialized) |
| 41 | assert test_obj.param1 == test_obj2.param1 |
| 42 | assert test_obj.param2 == test_obj2.param2 |
| 43 | assert test_obj.param3 == test_obj2.param3 |
| 44 | |
| 45 | def serialization_deserialization_struct_enum_as_string_test(): |
| 46 | test_obj = TestStruct(param1="test_string", param2=TestEnum.TestEnum1.name, param3=SharedEnum.SharedEnum1.name) |
| 47 | test_obj_serialized = serialize(test_obj) |
| 48 | test_obj2 = deserialize(TestStruct(), test_obj_serialized) |
| 49 | assert test_obj.param1 == test_obj2.param1 |
| 50 | assert test_obj.param2 == test_obj2.param2 |
| 51 | assert test_obj.param3 == test_obj2.param3 |
| 52 | |
| 53 | def serialization_deserialization_exception_enum_as_string_test(): |
| 54 | test_obj = TestException(whatOp=0, why=SharedEnum.SharedEnum0.name, who=TestEnum.TestEnum0.name) |
| 55 | test_obj_serialized = serialize(test_obj) |
| 56 | test_obj2 = deserialize_immutable(TestException, test_obj_serialized) |
| 57 | assert test_obj.whatOp == test_obj2.whatOp |
| 58 | assert test_obj.why == test_obj2.why |
| 59 | assert test_obj.who == test_obj2.who |
| 60 | |
| 61 | def serialization_deserialization_exception_enum_test(): |
| 62 | test_obj = TestException(whatOp=0, why=SharedEnum.SharedEnum0, who=TestEnum.TestEnum0) |
| 63 | test_obj_serialized = serialize(test_obj) |
| 64 | test_obj2 = deserialize_immutable(TestException, test_obj_serialized) |
| 65 | assert test_obj.whatOp == test_obj2.whatOp |
| 66 | assert test_obj.why == test_obj2.why |
| 67 | assert test_obj.who == test_obj2.who |
| 68 | |
| 69 | |
| 70 | |
| 71 | if __name__ == "__main__": |
| 72 | args = sys.argv[1:] |
| 73 | if args: |
| 74 | from test5_slots.test5.ttypes import TestEnum, TestStruct, TestException |
| 75 | else: |
| 76 | from test5.ttypes import TestEnum, TestStruct, TestException |
| 77 | serialization_deserialization_struct_enum_test() |
| 78 | serialization_deserialization_struct_enum_as_string_test() |
| 79 | serialization_deserialization_exception_enum_as_string_test() |
| 80 | serialization_deserialization_exception_enum_test() |