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