aboutsummaryrefslogtreecommitdiffhomepage
path: root/python/google/protobuf/internal/descriptor_pool_test.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/google/protobuf/internal/descriptor_pool_test.py')
-rw-r--r--python/google/protobuf/internal/descriptor_pool_test.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/python/google/protobuf/internal/descriptor_pool_test.py b/python/google/protobuf/internal/descriptor_pool_test.py
index 15c857bb..2cbf7813 100644
--- a/python/google/protobuf/internal/descriptor_pool_test.py
+++ b/python/google/protobuf/internal/descriptor_pool_test.py
@@ -34,8 +34,10 @@
__author__ = 'matthewtoia@google.com (Matt Toia)'
+import copy
import os
import sys
+import warnings
try:
import unittest2 as unittest #PY26
@@ -119,6 +121,14 @@ class DescriptorPoolTestBase(object):
self.assertEqual('google/protobuf/unittest.proto',
file_desc5.name)
+ # Tests the generated pool.
+ assert descriptor_pool.Default().FindFileContainingSymbol(
+ 'google.protobuf.python.internal.Factory2Message.one_more_field')
+ assert descriptor_pool.Default().FindFileContainingSymbol(
+ 'google.protobuf.python.internal.another_field')
+ assert descriptor_pool.Default().FindFileContainingSymbol(
+ 'protobuf_unittest.TestService')
+
def testFindFileContainingSymbolFailure(self):
with self.assertRaises(KeyError):
self.pool.FindFileContainingSymbol('Does not exist')
@@ -492,6 +502,52 @@ class DescriptorPoolTestBase(object):
TEST1_FILE.CheckFile(self, self.pool)
TEST2_FILE.CheckFile(self, self.pool)
+ def testConflictRegister(self):
+ if isinstance(self, SecondaryDescriptorFromDescriptorDB):
+ if api_implementation.Type() == 'cpp':
+ # Cpp extension cannot call Add on a DescriptorPool
+ # that uses a DescriptorDatabase.
+ # TODO(jieluo): Fix python and cpp extension diff.
+ return
+ unittest_fd = descriptor_pb2.FileDescriptorProto.FromString(
+ unittest_pb2.DESCRIPTOR.serialized_pb)
+ conflict_fd = copy.deepcopy(unittest_fd)
+ conflict_fd.name = 'other_file'
+ if api_implementation.Type() == 'cpp':
+ try:
+ self.pool.Add(unittest_fd)
+ self.pool.Add(conflict_fd)
+ except TypeError:
+ pass
+ else:
+ with warnings.catch_warnings(record=True) as w:
+ # Cause all warnings to always be triggered.
+ warnings.simplefilter('always')
+ pool = copy.deepcopy(self.pool)
+ # No warnings to add the same descriptors.
+ file_descriptor = unittest_pb2.DESCRIPTOR
+ pool.AddDescriptor(
+ file_descriptor.message_types_by_name['TestAllTypes'])
+ pool.AddEnumDescriptor(
+ file_descriptor.enum_types_by_name['ForeignEnum'])
+ pool.AddServiceDescriptor(
+ file_descriptor.services_by_name['TestService'])
+ pool.AddExtensionDescriptor(
+ file_descriptor.extensions_by_name['optional_int32_extension'])
+ self.assertEqual(len(w), 0)
+ # Check warnings for conflict descriptors with the same name.
+ pool.Add(unittest_fd)
+ pool.Add(conflict_fd)
+ pool.FindFileByName(unittest_fd.name)
+ pool.FindFileByName(conflict_fd.name)
+ self.assertTrue(len(w))
+ self.assertIs(w[0].category, RuntimeWarning)
+ self.assertIn('Conflict register for file "other_file": ',
+ str(w[0].message))
+ self.assertIn('already defined in file '
+ '"google/protobuf/unittest.proto"',
+ str(w[0].message))
+
class DefaultDescriptorPoolTest(DescriptorPoolTestBase, unittest.TestCase):