blob: 8d82708ae0789f68306983fc15f3af90fd11a3ca [file] [log] [blame]
Roshan Rajan284e6b32023-06-26 18:03:03 -07001#!/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
22from __future__ import annotations
23
24import sys
25from shared_types.ttypes import SharedEnum
26from thrift.TSerialization import serialize, deserialize
27from thrift.protocol import TBinaryProtocol
28from thrift.transport import TTransport
29
30def 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
37def 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
45def 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
53def 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
61def 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
71if __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()