1# Protocol Buffers - Google's data interchange format 2# Copyright 2008 Google Inc. All rights reserved. 3# https://developers.google.com/protocol-buffers/ 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are 7# met: 8# 9# * Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# * Redistributions in binary form must reproduce the above 12# copyright notice, this list of conditions and the following disclaimer 13# in the documentation and/or other materials provided with the 14# distribution. 15# * Neither the name of Google Inc. nor the names of its 16# contributors may be used to endorse or promote products derived from 17# this software without specific prior written permission. 18# 19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31"""Tests for google.protobuf.symbol_database.""" 32 33import unittest 34 35from google.protobuf import unittest_pb2 36from google.protobuf import descriptor 37from google.protobuf import descriptor_pool 38from google.protobuf import symbol_database 39 40 41class SymbolDatabaseTest(unittest.TestCase): 42 43 def _Database(self): 44 if descriptor._USE_C_DESCRIPTORS: 45 # The C++ implementation does not allow mixing descriptors from 46 # different pools. 47 db = symbol_database.SymbolDatabase(pool=descriptor_pool.Default()) 48 else: 49 db = symbol_database.SymbolDatabase() 50 # Register representative types from unittest_pb2. 51 db.RegisterFileDescriptor(unittest_pb2.DESCRIPTOR) 52 db.RegisterMessage(unittest_pb2.TestAllTypes) 53 db.RegisterMessage(unittest_pb2.TestAllTypes.NestedMessage) 54 db.RegisterMessage(unittest_pb2.TestAllTypes.OptionalGroup) 55 db.RegisterMessage(unittest_pb2.TestAllTypes.RepeatedGroup) 56 db.RegisterEnumDescriptor(unittest_pb2.ForeignEnum.DESCRIPTOR) 57 db.RegisterEnumDescriptor(unittest_pb2.TestAllTypes.NestedEnum.DESCRIPTOR) 58 db.RegisterServiceDescriptor(unittest_pb2._TESTSERVICE) 59 return db 60 61 def testGetPrototype(self): 62 instance = self._Database().GetPrototype( 63 unittest_pb2.TestAllTypes.DESCRIPTOR) 64 self.assertTrue(instance is unittest_pb2.TestAllTypes) 65 66 def testGetMessages(self): 67 messages = self._Database().GetMessages( 68 ['google/protobuf/unittest.proto']) 69 self.assertTrue( 70 unittest_pb2.TestAllTypes is 71 messages['protobuf_unittest.TestAllTypes']) 72 73 def testGetSymbol(self): 74 self.assertEqual( 75 unittest_pb2.TestAllTypes, self._Database().GetSymbol( 76 'protobuf_unittest.TestAllTypes')) 77 self.assertEqual( 78 unittest_pb2.TestAllTypes.NestedMessage, self._Database().GetSymbol( 79 'protobuf_unittest.TestAllTypes.NestedMessage')) 80 self.assertEqual( 81 unittest_pb2.TestAllTypes.OptionalGroup, self._Database().GetSymbol( 82 'protobuf_unittest.TestAllTypes.OptionalGroup')) 83 self.assertEqual( 84 unittest_pb2.TestAllTypes.RepeatedGroup, self._Database().GetSymbol( 85 'protobuf_unittest.TestAllTypes.RepeatedGroup')) 86 87 def testEnums(self): 88 # Check registration of types in the pool. 89 self.assertEqual( 90 'protobuf_unittest.ForeignEnum', 91 self._Database().pool.FindEnumTypeByName( 92 'protobuf_unittest.ForeignEnum').full_name) 93 self.assertEqual( 94 'protobuf_unittest.TestAllTypes.NestedEnum', 95 self._Database().pool.FindEnumTypeByName( 96 'protobuf_unittest.TestAllTypes.NestedEnum').full_name) 97 98 def testFindMessageTypeByName(self): 99 self.assertEqual( 100 'protobuf_unittest.TestAllTypes', 101 self._Database().pool.FindMessageTypeByName( 102 'protobuf_unittest.TestAllTypes').full_name) 103 self.assertEqual( 104 'protobuf_unittest.TestAllTypes.NestedMessage', 105 self._Database().pool.FindMessageTypeByName( 106 'protobuf_unittest.TestAllTypes.NestedMessage').full_name) 107 108 def testFindServiceByName(self): 109 self.assertEqual( 110 'protobuf_unittest.TestService', 111 self._Database().pool.FindServiceByName( 112 'protobuf_unittest.TestService').full_name) 113 114 def testFindFileContainingSymbol(self): 115 # Lookup based on either enum or message. 116 self.assertEqual( 117 'google/protobuf/unittest.proto', 118 self._Database().pool.FindFileContainingSymbol( 119 'protobuf_unittest.TestAllTypes.NestedEnum').name) 120 self.assertEqual( 121 'google/protobuf/unittest.proto', 122 self._Database().pool.FindFileContainingSymbol( 123 'protobuf_unittest.TestAllTypes').name) 124 125 def testFindFileByName(self): 126 self.assertEqual( 127 'google/protobuf/unittest.proto', 128 self._Database().pool.FindFileByName( 129 'google/protobuf/unittest.proto').name) 130 131 132if __name__ == '__main__': 133 unittest.main() 134