blob: 5f6944f6375e981c0731edaea3dd23ee29feba08 [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
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050030
Roshan Rajan284e6b32023-06-26 18:03:03 -070031def deserialize_immutable(base,
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050032 buf,
33 protocol_factory=TBinaryProtocol.TBinaryProtocolFactory()):
Roshan Rajan284e6b32023-06-26 18:03:03 -070034 transport = TTransport.TMemoryBuffer(buf)
35 protocol = protocol_factory.getProtocol(transport)
36 return base.read(protocol)
37
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050038
Roshan Rajan284e6b32023-06-26 18:03:03 -070039def serialization_deserialization_struct_enum_test():
40 test_obj = TestStruct(param1="test_string", param2=TestEnum.TestEnum1, param3=SharedEnum.SharedEnum1)
41 test_obj_serialized = serialize(test_obj)
42 test_obj2 = deserialize(TestStruct(), test_obj_serialized)
43 assert test_obj.param1 == test_obj2.param1
44 assert test_obj.param2 == test_obj2.param2
45 assert test_obj.param3 == test_obj2.param3
46
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050047
Roshan Rajan284e6b32023-06-26 18:03:03 -070048def serialization_deserialization_struct_enum_as_string_test():
49 test_obj = TestStruct(param1="test_string", param2=TestEnum.TestEnum1.name, param3=SharedEnum.SharedEnum1.name)
50 test_obj_serialized = serialize(test_obj)
51 test_obj2 = deserialize(TestStruct(), test_obj_serialized)
52 assert test_obj.param1 == test_obj2.param1
53 assert test_obj.param2 == test_obj2.param2
54 assert test_obj.param3 == test_obj2.param3
55
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050056
Roshan Rajan284e6b32023-06-26 18:03:03 -070057def serialization_deserialization_exception_enum_as_string_test():
58 test_obj = TestException(whatOp=0, why=SharedEnum.SharedEnum0.name, who=TestEnum.TestEnum0.name)
59 test_obj_serialized = serialize(test_obj)
60 test_obj2 = deserialize_immutable(TestException, test_obj_serialized)
61 assert test_obj.whatOp == test_obj2.whatOp
62 assert test_obj.why == test_obj2.why
63 assert test_obj.who == test_obj2.who
64
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050065
Roshan Rajan284e6b32023-06-26 18:03:03 -070066def serialization_deserialization_exception_enum_test():
67 test_obj = TestException(whatOp=0, why=SharedEnum.SharedEnum0, who=TestEnum.TestEnum0)
68 test_obj_serialized = serialize(test_obj)
69 test_obj2 = deserialize_immutable(TestException, test_obj_serialized)
70 assert test_obj.whatOp == test_obj2.whatOp
71 assert test_obj.why == test_obj2.why
72 assert test_obj.who == test_obj2.who
73
74
Roshan Rajan284e6b32023-06-26 18:03:03 -070075if __name__ == "__main__":
76 args = sys.argv[1:]
77 if args:
78 from test5_slots.test5.ttypes import TestEnum, TestStruct, TestException
79 else:
80 from test5.ttypes import TestEnum, TestStruct, TestException
81 serialization_deserialization_struct_enum_test()
82 serialization_deserialization_struct_enum_as_string_test()
83 serialization_deserialization_exception_enum_as_string_test()
Dmytro Shteflyukacbcf102026-02-13 18:25:55 -050084 serialization_deserialization_exception_enum_test()