• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Protocol Buffers - Google's data interchange format
2# Copyright 2008 Google Inc.  All rights reserved.
3#
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file or at
6# https://developers.google.com/open-source/licenses/bsd
7
8"""Tests for google.protobuf.descriptor_database."""
9
10__author__ = 'matthewtoia@google.com (Matt Toia)'
11
12import unittest
13import warnings
14
15from google.protobuf import descriptor_pb2
16from google.protobuf.internal import factory_test2_pb2
17from google.protobuf.internal import no_package_pb2
18from google.protobuf.internal import testing_refleaks
19from google.protobuf import descriptor_database
20from google.protobuf import unittest_pb2
21
22
23@testing_refleaks.TestCase
24class DescriptorDatabaseTest(unittest.TestCase):
25
26  def testAdd(self):
27    db = descriptor_database.DescriptorDatabase()
28    file_desc_proto = descriptor_pb2.FileDescriptorProto.FromString(
29        factory_test2_pb2.DESCRIPTOR.serialized_pb)
30    file_desc_proto2 = descriptor_pb2.FileDescriptorProto.FromString(
31        no_package_pb2.DESCRIPTOR.serialized_pb)
32    db.Add(file_desc_proto)
33    db.Add(file_desc_proto2)
34
35    self.assertEqual(file_desc_proto, db.FindFileByName(
36        'google/protobuf/internal/factory_test2.proto'))
37    # Can find message type.
38    self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
39        'google.protobuf.python.internal.Factory2Message'))
40    # Can find nested message type.
41    self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
42        'google.protobuf.python.internal.Factory2Message.NestedFactory2Message'))
43    # Can find enum type.
44    self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
45        'google.protobuf.python.internal.Factory2Enum'))
46    # Can find nested enum type.
47    self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
48        'google.protobuf.python.internal.Factory2Message.NestedFactory2Enum'))
49    self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
50        'google.protobuf.python.internal.MessageWithNestedEnumOnly.NestedEnum'))
51    # Can find field.
52    self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
53        'google.protobuf.python.internal.Factory2Message.list_field'))
54    # Can find enum value.
55    self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
56        'google.protobuf.python.internal.Factory2Enum.FACTORY_2_VALUE_0'))
57    self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
58        'google.protobuf.python.internal.FACTORY_2_VALUE_0'))
59    self.assertEqual(file_desc_proto2, db.FindFileContainingSymbol(
60        '.NO_PACKAGE_VALUE_0'))
61    # Can find top level extension.
62    self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
63        'google.protobuf.python.internal.another_field'))
64    # Can find nested extension inside a message.
65    self.assertEqual(file_desc_proto, db.FindFileContainingSymbol(
66        'google.protobuf.python.internal.Factory2Message.one_more_field'))
67
68    # Can find service.
69    file_desc_proto2 = descriptor_pb2.FileDescriptorProto.FromString(
70        unittest_pb2.DESCRIPTOR.serialized_pb)
71    db.Add(file_desc_proto2)
72    self.assertEqual(file_desc_proto2, db.FindFileContainingSymbol(
73        'protobuf_unittest.TestService'))
74
75    # Non-existent field under a valid top level symbol can also be
76    # found. The behavior is the same with protobuf C++.
77    self.assertEqual(file_desc_proto2, db.FindFileContainingSymbol(
78        'protobuf_unittest.TestAllTypes.none_field'))
79
80    with self.assertRaisesRegex(KeyError, r'\'protobuf_unittest\.NoneMessage\''):
81      db.FindFileContainingSymbol('protobuf_unittest.NoneMessage')
82
83  def testConflictRegister(self):
84    db = descriptor_database.DescriptorDatabase()
85    unittest_fd = descriptor_pb2.FileDescriptorProto.FromString(
86        unittest_pb2.DESCRIPTOR.serialized_pb)
87    db.Add(unittest_fd)
88    conflict_fd = descriptor_pb2.FileDescriptorProto.FromString(
89        unittest_pb2.DESCRIPTOR.serialized_pb)
90    conflict_fd.name = 'other_file2'
91    with warnings.catch_warnings(record=True) as w:
92      # Cause all warnings to always be triggered.
93      warnings.simplefilter('always')
94      db.Add(conflict_fd)
95      self.assertTrue(len(w))
96      self.assertIs(w[0].category, RuntimeWarning)
97      self.assertIn('Conflict register for file "other_file2": ',
98                    str(w[0].message))
99      self.assertIn(
100          'already defined in file '
101          '"google/protobuf/unittest.proto"', str(w[0].message))
102
103if __name__ == '__main__':
104  unittest.main()
105