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 return db 64 65 def testGetPrototype(self): 66 instance = self._Database().GetPrototype( 67 unittest_pb2.TestAllTypes.DESCRIPTOR) 68 self.assertTrue(instance is unittest_pb2.TestAllTypes) 69 70 def testGetMessages(self): 71 messages = self._Database().GetMessages( 72 ['google/protobuf/unittest.proto']) 73 self.assertTrue( 74 unittest_pb2.TestAllTypes is 75 messages['protobuf_unittest.TestAllTypes']) 76 77 def testGetSymbol(self): 78 self.assertEqual( 79 unittest_pb2.TestAllTypes, self._Database().GetSymbol( 80 'protobuf_unittest.TestAllTypes')) 81 self.assertEqual( 82 unittest_pb2.TestAllTypes.NestedMessage, self._Database().GetSymbol( 83 'protobuf_unittest.TestAllTypes.NestedMessage')) 84 self.assertEqual( 85 unittest_pb2.TestAllTypes.OptionalGroup, self._Database().GetSymbol( 86 'protobuf_unittest.TestAllTypes.OptionalGroup')) 87 self.assertEqual( 88 unittest_pb2.TestAllTypes.RepeatedGroup, self._Database().GetSymbol( 89 'protobuf_unittest.TestAllTypes.RepeatedGroup')) 90 91 def testEnums(self): 92 # Check registration of types in the pool. 93 self.assertEqual( 94 'protobuf_unittest.ForeignEnum', 95 self._Database().pool.FindEnumTypeByName( 96 'protobuf_unittest.ForeignEnum').full_name) 97 self.assertEqual( 98 'protobuf_unittest.TestAllTypes.NestedEnum', 99 self._Database().pool.FindEnumTypeByName( 100 'protobuf_unittest.TestAllTypes.NestedEnum').full_name) 101 102 def testFindMessageTypeByName(self): 103 self.assertEqual( 104 'protobuf_unittest.TestAllTypes', 105 self._Database().pool.FindMessageTypeByName( 106 'protobuf_unittest.TestAllTypes').full_name) 107 self.assertEqual( 108 'protobuf_unittest.TestAllTypes.NestedMessage', 109 self._Database().pool.FindMessageTypeByName( 110 'protobuf_unittest.TestAllTypes.NestedMessage').full_name) 111 112 def testFindFindContainingSymbol(self): 113 # Lookup based on either enum or message. 114 self.assertEqual( 115 'google/protobuf/unittest.proto', 116 self._Database().pool.FindFileContainingSymbol( 117 'protobuf_unittest.TestAllTypes.NestedEnum').name) 118 self.assertEqual( 119 'google/protobuf/unittest.proto', 120 self._Database().pool.FindFileContainingSymbol( 121 'protobuf_unittest.TestAllTypes').name) 122 123 def testFindFileByName(self): 124 self.assertEqual( 125 'google/protobuf/unittest.proto', 126 self._Database().pool.FindFileByName( 127 'google/protobuf/unittest.proto').name) 128 129 130if __name__ == '__main__': 131 unittest.main() 132