proto_builder_test.py
Go to the documentation of this file.
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 
33 """Tests for google.protobuf.proto_builder."""
34 
35 try:
36  from collections import OrderedDict
37 except ImportError:
38  from ordereddict import OrderedDict #PY26
39 try:
40  import unittest2 as unittest
41 except ImportError:
42  import unittest
43 
44 from google.protobuf import descriptor_pb2
45 from google.protobuf import descriptor_pool
46 from google.protobuf import proto_builder
47 from google.protobuf import text_format
48 
49 
50 class ProtoBuilderTest(unittest.TestCase):
51 
52  def setUp(self):
53  self.ordered_fields = OrderedDict([
54  ('foo', descriptor_pb2.FieldDescriptorProto.TYPE_INT64),
55  ('bar', descriptor_pb2.FieldDescriptorProto.TYPE_STRING),
56  ])
57  self._fields = dict(self.ordered_fields)
58 
60  """Test that we can create a proto class."""
61  proto_cls = proto_builder.MakeSimpleProtoClass(
62  self._fields,
63  full_name='net.proto2.python.public.proto_builder_test.Test')
64  proto = proto_cls()
65  proto.foo = 12345
66  proto.bar = 'asdf'
67  self.assertMultiLineEqual(
68  'bar: "asdf"\nfoo: 12345\n', text_format.MessageToString(proto))
69 
70  def testOrderedFields(self):
71  """Test that the field order is maintained when given an OrderedDict."""
72  proto_cls = proto_builder.MakeSimpleProtoClass(
73  self.ordered_fields,
74  full_name='net.proto2.python.public.proto_builder_test.OrderedTest')
75  proto = proto_cls()
76  proto.foo = 12345
77  proto.bar = 'asdf'
78  self.assertMultiLineEqual(
79  'foo: 12345\nbar: "asdf"\n', text_format.MessageToString(proto))
80 
82  """Test that the DescriptorPool is used."""
84  proto_cls1 = proto_builder.MakeSimpleProtoClass(
85  self._fields,
86  full_name='net.proto2.python.public.proto_builder_test.Test',
87  pool=pool)
88  proto_cls2 = proto_builder.MakeSimpleProtoClass(
89  self._fields,
90  full_name='net.proto2.python.public.proto_builder_test.Test',
91  pool=pool)
92  self.assertIs(proto_cls1.DESCRIPTOR, proto_cls2.DESCRIPTOR)
93 
94 
95 if __name__ == '__main__':
96  unittest.main()
google::protobuf.internal.proto_builder_test.ProtoBuilderTest.testOrderedFields
def testOrderedFields(self)
Definition: proto_builder_test.py:70
google::protobuf.internal.proto_builder_test.ProtoBuilderTest.ordered_fields
ordered_fields
Definition: proto_builder_test.py:53
google::protobuf
Definition: data_proto2_to_proto3_util.h:12
google::protobuf.internal.proto_builder_test.ProtoBuilderTest.testMakeSameProtoClassTwice
def testMakeSameProtoClassTwice(self)
Definition: proto_builder_test.py:81
google::protobuf.internal.proto_builder_test.ProtoBuilderTest._fields
_fields
Definition: proto_builder_test.py:57
google::protobuf.internal.proto_builder_test.ProtoBuilderTest.testMakeSimpleProtoClass
def testMakeSimpleProtoClass(self)
Definition: proto_builder_test.py:59
google::protobuf.internal.proto_builder_test.ProtoBuilderTest
Definition: proto_builder_test.py:50
google::protobuf.descriptor_pool.DescriptorPool
Definition: descriptor_pool.py:102
google::protobuf.internal.proto_builder_test.ProtoBuilderTest.setUp
def setUp(self)
Definition: proto_builder_test.py:52


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57