店播爬取Python脚本

symbol_database_test.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. db.RegisterServiceDescriptor(unittest_pb2._TESTSERVICE)
  58. return db
  59. def testGetPrototype(self):
  60. instance = self._Database().GetPrototype(
  61. unittest_pb2.TestAllTypes.DESCRIPTOR)
  62. self.assertTrue(instance is unittest_pb2.TestAllTypes)
  63. def testGetMessages(self):
  64. messages = self._Database().GetMessages(
  65. ['google/protobuf/unittest.proto'])
  66. self.assertTrue(
  67. unittest_pb2.TestAllTypes is
  68. messages['protobuf_unittest.TestAllTypes'])
  69. def testGetSymbol(self):
  70. self.assertEqual(
  71. unittest_pb2.TestAllTypes, self._Database().GetSymbol(
  72. 'protobuf_unittest.TestAllTypes'))
  73. self.assertEqual(
  74. unittest_pb2.TestAllTypes.NestedMessage, self._Database().GetSymbol(
  75. 'protobuf_unittest.TestAllTypes.NestedMessage'))
  76. self.assertEqual(
  77. unittest_pb2.TestAllTypes.OptionalGroup, self._Database().GetSymbol(
  78. 'protobuf_unittest.TestAllTypes.OptionalGroup'))
  79. self.assertEqual(
  80. unittest_pb2.TestAllTypes.RepeatedGroup, self._Database().GetSymbol(
  81. 'protobuf_unittest.TestAllTypes.RepeatedGroup'))
  82. def testEnums(self):
  83. # Check registration of types in the pool.
  84. self.assertEqual(
  85. 'protobuf_unittest.ForeignEnum',
  86. self._Database().pool.FindEnumTypeByName(
  87. 'protobuf_unittest.ForeignEnum').full_name)
  88. self.assertEqual(
  89. 'protobuf_unittest.TestAllTypes.NestedEnum',
  90. self._Database().pool.FindEnumTypeByName(
  91. 'protobuf_unittest.TestAllTypes.NestedEnum').full_name)
  92. def testFindMessageTypeByName(self):
  93. self.assertEqual(
  94. 'protobuf_unittest.TestAllTypes',
  95. self._Database().pool.FindMessageTypeByName(
  96. 'protobuf_unittest.TestAllTypes').full_name)
  97. self.assertEqual(
  98. 'protobuf_unittest.TestAllTypes.NestedMessage',
  99. self._Database().pool.FindMessageTypeByName(
  100. 'protobuf_unittest.TestAllTypes.NestedMessage').full_name)
  101. def testFindServiceByName(self):
  102. self.assertEqual(
  103. 'protobuf_unittest.TestService',
  104. self._Database().pool.FindServiceByName(
  105. 'protobuf_unittest.TestService').full_name)
  106. def testFindFileContainingSymbol(self):
  107. # Lookup based on either enum or message.
  108. self.assertEqual(
  109. 'google/protobuf/unittest.proto',
  110. self._Database().pool.FindFileContainingSymbol(
  111. 'protobuf_unittest.TestAllTypes.NestedEnum').name)
  112. self.assertEqual(
  113. 'google/protobuf/unittest.proto',
  114. self._Database().pool.FindFileContainingSymbol(
  115. 'protobuf_unittest.TestAllTypes').name)
  116. def testFindFileByName(self):
  117. self.assertEqual(
  118. 'google/protobuf/unittest.proto',
  119. self._Database().pool.FindFileByName(
  120. 'google/protobuf/unittest.proto').name)
  121. if __name__ == '__main__':
  122. unittest.main()