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