店播爬取Python脚本

proto_builder_test.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.proto_builder."""
  33. import collections
  34. try:
  35. import unittest2 as unittest
  36. except ImportError:
  37. import unittest
  38. from google.protobuf import descriptor_pb2 # pylint: disable=g-import-not-at-top
  39. from google.protobuf import descriptor
  40. from google.protobuf import descriptor_pool
  41. from google.protobuf import proto_builder
  42. from google.protobuf import text_format
  43. class ProtoBuilderTest(unittest.TestCase):
  44. def setUp(self):
  45. self.ordered_fields = collections.OrderedDict([
  46. ('foo', descriptor_pb2.FieldDescriptorProto.TYPE_INT64),
  47. ('bar', descriptor_pb2.FieldDescriptorProto.TYPE_STRING),
  48. ])
  49. self._fields = dict(self.ordered_fields)
  50. def testMakeSimpleProtoClass(self):
  51. """Test that we can create a proto class."""
  52. proto_cls = proto_builder.MakeSimpleProtoClass(
  53. self._fields,
  54. full_name='net.proto2.python.public.proto_builder_test.Test')
  55. proto = proto_cls()
  56. proto.foo = 12345
  57. proto.bar = 'asdf'
  58. self.assertMultiLineEqual(
  59. 'bar: "asdf"\nfoo: 12345\n', text_format.MessageToString(proto))
  60. def testOrderedFields(self):
  61. """Test that the field order is maintained when given an OrderedDict."""
  62. proto_cls = proto_builder.MakeSimpleProtoClass(
  63. self.ordered_fields,
  64. full_name='net.proto2.python.public.proto_builder_test.OrderedTest')
  65. proto = proto_cls()
  66. proto.foo = 12345
  67. proto.bar = 'asdf'
  68. self.assertMultiLineEqual(
  69. 'foo: 12345\nbar: "asdf"\n', text_format.MessageToString(proto))
  70. def testMakeSameProtoClassTwice(self):
  71. """Test that the DescriptorPool is used."""
  72. pool = descriptor_pool.DescriptorPool()
  73. proto_cls1 = proto_builder.MakeSimpleProtoClass(
  74. self._fields,
  75. full_name='net.proto2.python.public.proto_builder_test.Test',
  76. pool=pool)
  77. proto_cls2 = proto_builder.MakeSimpleProtoClass(
  78. self._fields,
  79. full_name='net.proto2.python.public.proto_builder_test.Test',
  80. pool=pool)
  81. self.assertIs(proto_cls1.DESCRIPTOR, proto_cls2.DESCRIPTOR)
  82. def testMakeLargeProtoClass(self):
  83. """Test that large created protos don't use reserved field numbers."""
  84. num_fields = 123456
  85. fields = {
  86. 'foo%d' % i: descriptor_pb2.FieldDescriptorProto.TYPE_INT64
  87. for i in range(num_fields)
  88. }
  89. proto_cls = proto_builder.MakeSimpleProtoClass(
  90. fields,
  91. full_name='net.proto2.python.public.proto_builder_test.LargeProtoTest')
  92. reserved_field_numbers = set(
  93. range(descriptor.FieldDescriptor.FIRST_RESERVED_FIELD_NUMBER,
  94. descriptor.FieldDescriptor.LAST_RESERVED_FIELD_NUMBER + 1))
  95. proto_field_numbers = set(proto_cls.DESCRIPTOR.fields_by_number)
  96. self.assertFalse(reserved_field_numbers.intersection(proto_field_numbers))
  97. if __name__ == '__main__':
  98. unittest.main()