blob: ce7425f88fb14f6104e940a896e6b373db375ec8 [file] [log] [blame]
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +09001#!/usr/bin/env python
2
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
Elvis Pranskevichus9c439622019-12-11 16:47:52 -050022from DebugProtoTest import Srv
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090023from DebugProtoTest.ttypes import CompactProtoTestStruct, Empty, Wrapper
Elvis Pranskevichus9c439622019-12-11 16:47:52 -050024from DebugProtoTest.ttypes import ExceptionWithAMap, MutableException
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090025from thrift.Thrift import TFrozenDict
26from thrift.transport import TTransport
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090027from thrift.protocol import TBinaryProtocol, TCompactProtocol
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090028import collections
29import unittest
30
31
32class TestFrozenBase(unittest.TestCase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090033 def _roundtrip(self, src, dst):
34 otrans = TTransport.TMemoryBuffer()
35 optoro = self.protocol(otrans)
36 src.write(optoro)
37 itrans = TTransport.TMemoryBuffer(otrans.getvalue())
38 iproto = self.protocol(itrans)
39 return dst.read(iproto) or dst
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090040
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090041 def test_dict_is_hashable_only_after_frozen(self):
42 d0 = {}
43 self.assertFalse(isinstance(d0, collections.Hashable))
44 d1 = TFrozenDict(d0)
45 self.assertTrue(isinstance(d1, collections.Hashable))
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090046
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090047 def test_struct_with_collection_fields(self):
48 pass
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090049
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090050 def test_set(self):
51 """Test that annotated set field can be serialized and deserialized"""
52 x = CompactProtoTestStruct(set_byte_map={
53 frozenset([42, 100, -100]): 99,
54 frozenset([0]): 100,
55 frozenset([]): 0,
56 })
57 x2 = self._roundtrip(x, CompactProtoTestStruct())
58 self.assertEqual(x2.set_byte_map[frozenset([42, 100, -100])], 99)
59 self.assertEqual(x2.set_byte_map[frozenset([0])], 100)
60 self.assertEqual(x2.set_byte_map[frozenset([])], 0)
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090061
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090062 def test_map(self):
63 """Test that annotated map field can be serialized and deserialized"""
64 x = CompactProtoTestStruct(map_byte_map={
65 TFrozenDict({42: 42, 100: -100}): 99,
66 TFrozenDict({0: 0}): 100,
67 TFrozenDict({}): 0,
68 })
69 x2 = self._roundtrip(x, CompactProtoTestStruct())
70 self.assertEqual(x2.map_byte_map[TFrozenDict({42: 42, 100: -100})], 99)
71 self.assertEqual(x2.map_byte_map[TFrozenDict({0: 0})], 100)
72 self.assertEqual(x2.map_byte_map[TFrozenDict({})], 0)
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090073
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090074 def test_list(self):
75 """Test that annotated list field can be serialized and deserialized"""
76 x = CompactProtoTestStruct(list_byte_map={
77 (42, 100, -100): 99,
78 (0,): 100,
79 (): 0,
80 })
81 x2 = self._roundtrip(x, CompactProtoTestStruct())
82 self.assertEqual(x2.list_byte_map[(42, 100, -100)], 99)
83 self.assertEqual(x2.list_byte_map[(0,)], 100)
84 self.assertEqual(x2.list_byte_map[()], 0)
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090085
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090086 def test_empty_struct(self):
87 """Test that annotated empty struct can be serialized and deserialized"""
88 x = CompactProtoTestStruct(empty_struct_field=Empty())
89 x2 = self._roundtrip(x, CompactProtoTestStruct())
90 self.assertEqual(x2.empty_struct_field, Empty())
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090091
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090092 def test_struct(self):
93 """Test that annotated struct can be serialized and deserialized"""
94 x = Wrapper(foo=Empty())
95 self.assertEqual(x.foo, Empty())
96 x2 = self._roundtrip(x, Wrapper)
97 self.assertEqual(x2.foo, Empty())
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090098
Elvis Pranskevichus9c439622019-12-11 16:47:52 -050099 def test_frozen_exception(self):
100 exc = ExceptionWithAMap(blah='foo')
101 with self.assertRaises(TypeError):
102 exc.blah = 'bar'
103 mutexc = MutableException(msg='foo')
104 mutexc.msg = 'bar'
105 self.assertEqual(mutexc.msg, 'bar')
106
107 def test_frozen_exception_serialization(self):
108 result = Srv.declaredExceptionMethod_result(
109 xwamap=ExceptionWithAMap(blah="error"))
110 deserialized = self._roundtrip(
111 result, Srv.declaredExceptionMethod_result())
112 self.assertEqual(result, deserialized)
113
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900114
115class TestFrozen(TestFrozenBase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900116 def protocol(self, trans):
117 return TBinaryProtocol.TBinaryProtocolFactory().getProtocol(trans)
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900118
119
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900120class TestFrozenAcceleratedBinary(TestFrozenBase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900121 def protocol(self, trans):
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900122 return TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(trans)
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900123
124
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900125class TestFrozenAcceleratedCompact(TestFrozenBase):
126 def protocol(self, trans):
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900127 return TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(trans)
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900128
129
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900130def suite():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900131 suite = unittest.TestSuite()
132 loader = unittest.TestLoader()
133 suite.addTest(loader.loadTestsFromTestCase(TestFrozen))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900134 suite.addTest(loader.loadTestsFromTestCase(TestFrozenAcceleratedBinary))
135 suite.addTest(loader.loadTestsFromTestCase(TestFrozenAcceleratedCompact))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900136 return suite
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900137
James E. King, III0ad20bd2017-09-30 15:44:16 -0700138
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900139if __name__ == "__main__":
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900140 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))