symbol_database_test.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.symbol_database."""
  33. try:
  34. import unittest2 as unittest #PY26
  35. except ImportError:
  36. import unittest
  37. from google.protobuf import unittest_pb2
  38. from google.protobuf import descriptor
  39. from google.protobuf import descriptor_pool
  40. from google.protobuf import symbol_database
  41. class SymbolDatabaseTest(unittest.TestCase):
  42. def _Database(self):
  43. if descriptor._USE_C_DESCRIPTORS:
  44. # The C++ implementation does not allow mixing descriptors from
  45. # different pools.
  46. db = symbol_database.SymbolDatabase(pool=descriptor_pool.Default())
  47. else:
  48. db = symbol_database.SymbolDatabase()
  49. # Register representative types from unittest_pb2.
  50. db.RegisterFileDescriptor(unittest_pb2.DESCRIPTOR)
  51. db.RegisterMessage(unittest_pb2.TestAllTypes)
  52. db.RegisterMessage(unittest_pb2.TestAllTypes.NestedMessage)
  53. db.RegisterMessage(unittest_pb2.TestAllTypes.OptionalGroup)
  54. db.RegisterMessage(unittest_pb2.TestAllTypes.RepeatedGroup)
  55. db.RegisterEnumDescriptor(unittest_pb2.ForeignEnum.DESCRIPTOR)
  56. db.RegisterEnumDescriptor(unittest_pb2.TestAllTypes.NestedEnum.DESCRIPTOR)
  57. return db
  58. def testGetPrototype(self):
  59. instance = self._Database().GetPrototype(
  60. unittest_pb2.TestAllTypes.DESCRIPTOR)
  61. self.assertTrue(instance is unittest_pb2.TestAllTypes)
  62. def testGetMessages(self):
  63. messages = self._Database().GetMessages(
  64. ['google/protobuf/unittest.proto'])
  65. self.assertTrue(
  66. unittest_pb2.TestAllTypes is
  67. messages['protobuf_unittest.TestAllTypes'])
  68. def testGetSymbol(self):
  69. self.assertEqual(
  70. unittest_pb2.TestAllTypes, self._Database().GetSymbol(
  71. 'protobuf_unittest.TestAllTypes'))
  72. self.assertEqual(
  73. unittest_pb2.TestAllTypes.NestedMessage, self._Database().GetSymbol(
  74. 'protobuf_unittest.TestAllTypes.NestedMessage'))
  75. self.assertEqual(
  76. unittest_pb2.TestAllTypes.OptionalGroup, self._Database().GetSymbol(
  77. 'protobuf_unittest.TestAllTypes.OptionalGroup'))
  78. self.assertEqual(
  79. unittest_pb2.TestAllTypes.RepeatedGroup, self._Database().GetSymbol(
  80. 'protobuf_unittest.TestAllTypes.RepeatedGroup'))
  81. def testEnums(self):
  82. # Check registration of types in the pool.
  83. self.assertEqual(
  84. 'protobuf_unittest.ForeignEnum',
  85. self._Database().pool.FindEnumTypeByName(
  86. 'protobuf_unittest.ForeignEnum').full_name)
  87. self.assertEqual(
  88. 'protobuf_unittest.TestAllTypes.NestedEnum',
  89. self._Database().pool.FindEnumTypeByName(
  90. 'protobuf_unittest.TestAllTypes.NestedEnum').full_name)
  91. def testFindMessageTypeByName(self):
  92. self.assertEqual(
  93. 'protobuf_unittest.TestAllTypes',
  94. self._Database().pool.FindMessageTypeByName(
  95. 'protobuf_unittest.TestAllTypes').full_name)
  96. self.assertEqual(
  97. 'protobuf_unittest.TestAllTypes.NestedMessage',
  98. self._Database().pool.FindMessageTypeByName(
  99. 'protobuf_unittest.TestAllTypes.NestedMessage').full_name)
  100. def testFindFindContainingSymbol(self):
  101. # Lookup based on either enum or message.
  102. self.assertEqual(
  103. 'google/protobuf/unittest.proto',
  104. self._Database().pool.FindFileContainingSymbol(
  105. 'protobuf_unittest.TestAllTypes.NestedEnum').name)
  106. self.assertEqual(
  107. 'google/protobuf/unittest.proto',
  108. self._Database().pool.FindFileContainingSymbol(
  109. 'protobuf_unittest.TestAllTypes').name)
  110. def testFindFileByName(self):
  111. self.assertEqual(
  112. 'google/protobuf/unittest.proto',
  113. self._Database().pool.FindFileByName(
  114. 'google/protobuf/unittest.proto').name)
  115. if __name__ == '__main__':
  116. unittest.main()