descriptor_database_test.py 5.4 KB

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