descriptor_database_test.py 5.9 KB

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