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.descriptor_database.""" 34 35__author__ = 'matthewtoia@google.com (Matt Toia)' 36 37try: 38 import unittest2 as unittest #PY26 39except ImportError: 40 import unittest 41import warnings 42 43from google.protobuf import unittest_pb2 44from google.protobuf import descriptor_pb2 45from google.protobuf.internal import factory_test2_pb2 46from google.protobuf.internal import no_package_pb2 47from google.protobuf.internal import testing_refleaks 48from google.protobuf import descriptor_database 49 50 51@testing_refleaks.TestCase 52class DescriptorDatabaseTest(unittest.TestCase): 53 54 def testAdd(self): 55 db = descriptor_database.DescriptorDatabase() 56 file_desc_proto = descriptor_pb2.FileDescriptorProto.FromString( 57 factory_test2_pb2.DESCRIPTOR.serialized_pb) 58 file_desc_proto2 = descriptor_pb2.FileDescriptorProto.FromString( 59 no_package_pb2.DESCRIPTOR.serialized_pb) 60 db.Add(file_desc_proto) 61 db.Add(file_desc_proto2) 62 63 self.assertEqual(file_desc_proto, db.FindFileByName( 64 'google/protobuf/internal/factory_test2.proto')) 65 # Can find message type. 66 self.assertEqual(file_desc_proto, db.FindFileContainingSymbol( 67 'google.protobuf.python.internal.Factory2Message')) 68 # Can find nested message type. 69 self.assertEqual(file_desc_proto, db.FindFileContainingSymbol( 70 'google.protobuf.python.internal.Factory2Message.NestedFactory2Message')) 71 # Can find enum type. 72 self.assertEqual(file_desc_proto, db.FindFileContainingSymbol( 73 'google.protobuf.python.internal.Factory2Enum')) 74 # Can find nested enum type. 75 self.assertEqual(file_desc_proto, db.FindFileContainingSymbol( 76 'google.protobuf.python.internal.Factory2Message.NestedFactory2Enum')) 77 self.assertEqual(file_desc_proto, db.FindFileContainingSymbol( 78 'google.protobuf.python.internal.MessageWithNestedEnumOnly.NestedEnum')) 79 # Can find field. 80 self.assertEqual(file_desc_proto, db.FindFileContainingSymbol( 81 'google.protobuf.python.internal.Factory2Message.list_field')) 82 # Can find enum value. 83 self.assertEqual(file_desc_proto, db.FindFileContainingSymbol( 84 'google.protobuf.python.internal.Factory2Enum.FACTORY_2_VALUE_0')) 85 self.assertEqual(file_desc_proto, db.FindFileContainingSymbol( 86 'google.protobuf.python.internal.FACTORY_2_VALUE_0')) 87 self.assertEqual(file_desc_proto2, db.FindFileContainingSymbol( 88 '.NO_PACKAGE_VALUE_0')) 89 # Can find top level extension. 90 self.assertEqual(file_desc_proto, db.FindFileContainingSymbol( 91 'google.protobuf.python.internal.another_field')) 92 # Can find nested extension inside a message. 93 self.assertEqual(file_desc_proto, db.FindFileContainingSymbol( 94 'google.protobuf.python.internal.Factory2Message.one_more_field')) 95 96 # Can find service. 97 file_desc_proto2 = descriptor_pb2.FileDescriptorProto.FromString( 98 unittest_pb2.DESCRIPTOR.serialized_pb) 99 db.Add(file_desc_proto2) 100 self.assertEqual(file_desc_proto2, db.FindFileContainingSymbol( 101 'protobuf_unittest.TestService')) 102 103 # Non-existent field under a valid top level symbol can also be 104 # found. The behavior is the same with protobuf C++. 105 self.assertEqual(file_desc_proto2, db.FindFileContainingSymbol( 106 'protobuf_unittest.TestAllTypes.none_field')) 107 108 with self.assertRaisesRegexp(KeyError, r'\'protobuf_unittest\.NoneMessage\''): 109 db.FindFileContainingSymbol('protobuf_unittest.NoneMessage') 110 111 def testConflictRegister(self): 112 db = descriptor_database.DescriptorDatabase() 113 unittest_fd = descriptor_pb2.FileDescriptorProto.FromString( 114 unittest_pb2.DESCRIPTOR.serialized_pb) 115 db.Add(unittest_fd) 116 conflict_fd = descriptor_pb2.FileDescriptorProto.FromString( 117 unittest_pb2.DESCRIPTOR.serialized_pb) 118 conflict_fd.name = 'other_file2' 119 with warnings.catch_warnings(record=True) as w: 120 # Cause all warnings to always be triggered. 121 warnings.simplefilter('always') 122 db.Add(conflict_fd) 123 self.assertTrue(len(w)) 124 self.assertIs(w[0].category, RuntimeWarning) 125 self.assertIn('Conflict register for file "other_file2": ', 126 str(w[0].message)) 127 self.assertIn('already defined in file ' 128 '"google/protobuf/unittest.proto"', 129 str(w[0].message)) 130 131if __name__ == '__main__': 132 unittest.main() 133