protobuf/python/google/protobuf/internal/proto_builder_test.py
Go to the documentation of this file.
1 # Protocol Buffers - Google's data interchange format
2 # Copyright 2008 Google Inc. All rights reserved.
3 # https://developers.google.com/protocol-buffers/
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
8 #
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
14 # distribution.
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 """Tests for google.protobuf.proto_builder."""
32 
33 import collections
34 try:
35  import unittest2 as unittest
36 except ImportError:
37  import unittest
38 
39 from google.protobuf import descriptor_pb2 # pylint: disable=g-import-not-at-top
40 from google.protobuf import descriptor
41 from google.protobuf import descriptor_pool
42 from google.protobuf import proto_builder
43 from google.protobuf import text_format
44 
45 
46 class ProtoBuilderTest(unittest.TestCase):
47 
48  def setUp(self):
49  self.ordered_fields = collections.OrderedDict([
50  ('foo', descriptor_pb2.FieldDescriptorProto.TYPE_INT64),
51  ('bar', descriptor_pb2.FieldDescriptorProto.TYPE_STRING),
52  ])
53  self._fields = dict(self.ordered_fields)
54 
56  """Test that we can create a proto class."""
57  proto_cls = proto_builder.MakeSimpleProtoClass(
58  self._fields,
59  full_name='net.proto2.python.public.proto_builder_test.Test')
60  proto = proto_cls()
61  proto.foo = 12345
62  proto.bar = 'asdf'
63  self.assertMultiLineEqual(
64  'bar: "asdf"\nfoo: 12345\n', text_format.MessageToString(proto))
65 
66  def testOrderedFields(self):
67  """Test that the field order is maintained when given an OrderedDict."""
68  proto_cls = proto_builder.MakeSimpleProtoClass(
69  self.ordered_fields,
70  full_name='net.proto2.python.public.proto_builder_test.OrderedTest')
71  proto = proto_cls()
72  proto.foo = 12345
73  proto.bar = 'asdf'
74  self.assertMultiLineEqual(
75  'foo: 12345\nbar: "asdf"\n', text_format.MessageToString(proto))
76 
78  """Test that the DescriptorPool is used."""
80  proto_cls1 = proto_builder.MakeSimpleProtoClass(
81  self._fields,
82  full_name='net.proto2.python.public.proto_builder_test.Test',
83  pool=pool)
84  proto_cls2 = proto_builder.MakeSimpleProtoClass(
85  self._fields,
86  full_name='net.proto2.python.public.proto_builder_test.Test',
87  pool=pool)
88  self.assertIs(proto_cls1.DESCRIPTOR, proto_cls2.DESCRIPTOR)
89 
91  """Test that large created protos don't use reserved field numbers."""
92  num_fields = 123456
93  fields = {
94  'foo%d' % i: descriptor_pb2.FieldDescriptorProto.TYPE_INT64
95  for i in range(num_fields)
96  }
97  proto_cls = proto_builder.MakeSimpleProtoClass(
98  fields,
99  full_name='net.proto2.python.public.proto_builder_test.LargeProtoTest')
100 
101  reserved_field_numbers = set(
102  range(descriptor.FieldDescriptor.FIRST_RESERVED_FIELD_NUMBER,
103  descriptor.FieldDescriptor.LAST_RESERVED_FIELD_NUMBER + 1))
104  proto_field_numbers = set(proto_cls.DESCRIPTOR.fields_by_number)
105  self.assertFalse(reserved_field_numbers.intersection(proto_field_numbers))
106 
107 
108 if __name__ == '__main__':
109  unittest.main()
capstone.range
range
Definition: third_party/bloaty/third_party/capstone/bindings/python/capstone/__init__.py:6
google::protobuf.internal.proto_builder_test.ProtoBuilderTest.testOrderedFields
def testOrderedFields(self)
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/proto_builder_test.py:70
google::protobuf.internal.proto_builder_test.ProtoBuilderTest.ordered_fields
ordered_fields
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/proto_builder_test.py:53
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf.internal.proto_builder_test.ProtoBuilderTest.testMakeSameProtoClassTwice
def testMakeSameProtoClassTwice(self)
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/proto_builder_test.py:81
google::protobuf.internal.proto_builder_test.ProtoBuilderTest._fields
_fields
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/proto_builder_test.py:57
google::protobuf.internal.proto_builder_test.ProtoBuilderTest.testMakeSimpleProtoClass
def testMakeSimpleProtoClass(self)
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/proto_builder_test.py:59
google::protobuf.internal.proto_builder_test.ProtoBuilderTest.testMakeLargeProtoClass
def testMakeLargeProtoClass(self)
Definition: protobuf/python/google/protobuf/internal/proto_builder_test.py:90
google::protobuf.descriptor_pool.DescriptorPool
Definition: bloaty/third_party/protobuf/python/google/protobuf/descriptor_pool.py:118
cpp.gmock_class.set
set
Definition: bloaty/third_party/googletest/googlemock/scripts/generator/cpp/gmock_class.py:44
google::protobuf.internal.proto_builder_test.ProtoBuilderTest.setUp
def setUp(self)
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/proto_builder_test.py:52


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:47