Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 1 | #!/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 Pranskevichus | 9c43962 | 2019-12-11 16:47:52 -0500 | [diff] [blame] | 22 | from DebugProtoTest import Srv |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 23 | from DebugProtoTest.ttypes import CompactProtoTestStruct, Empty, Wrapper |
Elvis Pranskevichus | 9c43962 | 2019-12-11 16:47:52 -0500 | [diff] [blame] | 24 | from DebugProtoTest.ttypes import ExceptionWithAMap, MutableException |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 25 | from thrift.Thrift import TFrozenDict |
| 26 | from thrift.transport import TTransport |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 27 | from thrift.protocol import TBinaryProtocol, TCompactProtocol |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 28 | import collections |
| 29 | import unittest |
| 30 | |
| 31 | |
| 32 | class TestFrozenBase(unittest.TestCase): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 33 | 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 Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 40 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 41 | 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 Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 46 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 47 | def test_struct_with_collection_fields(self): |
| 48 | pass |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 49 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 50 | 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 Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 61 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 62 | 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 Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 73 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 74 | 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 Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 85 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 86 | 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 Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 91 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 92 | 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 Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 98 | |
Elvis Pranskevichus | 9c43962 | 2019-12-11 16:47:52 -0500 | [diff] [blame] | 99 | 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 Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 114 | |
| 115 | class TestFrozen(TestFrozenBase): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 116 | def protocol(self, trans): |
| 117 | return TBinaryProtocol.TBinaryProtocolFactory().getProtocol(trans) |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 118 | |
| 119 | |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 120 | class TestFrozenAcceleratedBinary(TestFrozenBase): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 121 | def protocol(self, trans): |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 122 | return TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False).getProtocol(trans) |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 123 | |
| 124 | |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 125 | class TestFrozenAcceleratedCompact(TestFrozenBase): |
| 126 | def protocol(self, trans): |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 127 | return TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False).getProtocol(trans) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 128 | |
| 129 | |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 130 | def suite(): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 131 | suite = unittest.TestSuite() |
| 132 | loader = unittest.TestLoader() |
| 133 | suite.addTest(loader.loadTestsFromTestCase(TestFrozen)) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 134 | suite.addTest(loader.loadTestsFromTestCase(TestFrozenAcceleratedBinary)) |
| 135 | suite.addTest(loader.loadTestsFromTestCase(TestFrozenAcceleratedCompact)) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 136 | return suite |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 137 | |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 138 | |
Nobuaki Sukegawa | e841b3d | 2015-11-17 11:01:17 +0900 | [diff] [blame] | 139 | if __name__ == "__main__": |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 140 | unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) |