店播爬取Python脚本

keywords_test.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.internal.keywords."""
  33. import unittest
  34. from google.protobuf.internal import more_messages_pb2
  35. from google.protobuf import descriptor_pool
  36. class KeywordsConflictTest(unittest.TestCase):
  37. def setUp(self):
  38. super(KeywordsConflictTest, self).setUp()
  39. self.pool = descriptor_pool.Default()
  40. def testMessage(self):
  41. message = getattr(more_messages_pb2, 'class')()
  42. message.int_field = 123
  43. self.assertEqual(message.int_field, 123)
  44. des = self.pool.FindMessageTypeByName('google.protobuf.internal.class')
  45. self.assertEqual(des.name, 'class')
  46. def testNestedMessage(self):
  47. message = getattr(more_messages_pb2, 'class')()
  48. message.nested_message.field = 234
  49. self.assertEqual(message.nested_message.field, 234)
  50. des = self.pool.FindMessageTypeByName('google.protobuf.internal.class.try')
  51. self.assertEqual(des.name, 'try')
  52. def testField(self):
  53. message = getattr(more_messages_pb2, 'class')()
  54. setattr(message, 'if', 123)
  55. setattr(message, 'as', 1)
  56. self.assertEqual(getattr(message, 'if'), 123)
  57. self.assertEqual(getattr(message, 'as'), 1)
  58. def testEnum(self):
  59. class_ = getattr(more_messages_pb2, 'class')
  60. message = class_()
  61. # Normal enum value.
  62. message.enum_field = more_messages_pb2.default
  63. self.assertEqual(message.enum_field, more_messages_pb2.default)
  64. # Top level enum value.
  65. message.enum_field = getattr(more_messages_pb2, 'else')
  66. self.assertEqual(message.enum_field, 1)
  67. # Nested enum value
  68. message.nested_enum_field = getattr(class_, 'True')
  69. self.assertEqual(message.nested_enum_field, 1)
  70. def testExtension(self):
  71. message = getattr(more_messages_pb2, 'class')()
  72. # Top level extension
  73. extension1 = getattr(more_messages_pb2, 'continue')
  74. message.Extensions[extension1] = 456
  75. self.assertEqual(message.Extensions[extension1], 456)
  76. # None top level extension
  77. extension2 = getattr(more_messages_pb2.ExtendClass, 'return')
  78. message.Extensions[extension2] = 789
  79. self.assertEqual(message.Extensions[extension2], 789)
  80. def testExtensionForNestedMessage(self):
  81. message = getattr(more_messages_pb2, 'class')()
  82. extension = getattr(more_messages_pb2, 'with')
  83. message.nested_message.Extensions[extension] = 999
  84. self.assertEqual(message.nested_message.Extensions[extension], 999)
  85. def TestFullKeywordUsed(self):
  86. message = more_messages_pb2.TestFullKeyword()
  87. message.field2.int_field = 123
  88. if __name__ == '__main__':
  89. unittest.main()