minimal_test.py
Go to the documentation of this file.
1 # Copyright (c) 2009-2021, Google LLC
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 # * Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer.
8 # * Redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution.
11 # * Neither the name of Google LLC nor the
12 # names of its contributors may be used to endorse or promote products
13 # derived from this software without specific prior written permission.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 # DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
19 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 
27 """A bare-bones unit test that doesn't load any generated code."""
28 
29 
30 import unittest
31 from google.protobuf.pyext import _message
32 from google.protobuf.internal import api_implementation
33 from google.protobuf import unittest_pb2
34 from google.protobuf import map_unittest_pb2
35 from google.protobuf import descriptor_pool
36 from google.protobuf import text_format
37 from google.protobuf import message_factory
38 from google.protobuf import message
39 from google.protobuf.internal import factory_test1_pb2
40 from google.protobuf.internal import factory_test2_pb2
41 from google.protobuf.internal import more_extensions_pb2
42 from google.protobuf import descriptor_pb2
43 
44 class TestMessageExtension(unittest.TestCase):
45 
47  serialized_desc = b'\n\ntest.proto\"\x0e\n\x02M1*\x08\x08\x01\x10\x80\x80\x80\x80\x02:\x15\n\x08test_ext\x12\x03.M1\x18\x01 \x01(\x05'
48  pool = _message.DescriptorPool()
49  file_desc = pool.AddSerializedFile(serialized_desc)
50  self.assertEqual("test.proto", file_desc.name)
51  ext_desc = pool.FindExtensionByName("test_ext")
52  self.assertEqual(1, ext_desc.number)
53 
54  # Test object cache: repeatedly retrieving the same descriptor
55  # should result in the same object
56  self.assertIs(ext_desc, pool.FindExtensionByName("test_ext"))
57 
58 
59  def test_lib_is_upb(self):
60  # Ensure we are not pulling in a different protobuf library on the
61  # system.
62  print(_message._IS_UPB)
63  self.assertTrue(_message._IS_UPB)
64  self.assertEqual(api_implementation.Type(), "cpp")
65 
67  def test_slice(start, end, step):
68  vals = list(range(20))
69  message = unittest_pb2.TestAllTypes(repeated_int32=vals)
70  del vals[start:end:step]
71  del message.repeated_int32[start:end:step]
72  self.assertEqual(vals, list(message.repeated_int32))
73  test_slice(3, 11, 1)
74  test_slice(3, 11, 2)
75  test_slice(3, 11, 3)
76  test_slice(11, 3, -1)
77  test_slice(11, 3, -2)
78  test_slice(11, 3, -3)
79  test_slice(10, 25, 4)
80 
82  msg = unittest_pb2.TestAllTypes()
83  self.assertRaises(AttributeError, getattr, msg, 'Extensions')
84 
86  msg = map_unittest_pb2.TestMapSubmessage()
87  int32_map = msg.test_map.map_int32_int32
88  msg.test_map.ClearField("map_int32_int32")
89  int32_map[123] = 456
90  self.assertEqual(0, msg.test_map.ByteSize())
91 
93  msg = map_unittest_pb2.TestMap()
94  int32_map = msg.map_int32_int32
95  int32_map[123] = 456
96  msg.ClearField("map_int32_int32")
97  int32_map[111] = 222
98  self.assertEqual(0, msg.ByteSize())
99 
101  msg = unittest_pb2.NestedTestAllTypes()
102  int32_array = msg.payload.repeated_int32
103  msg.payload.ClearField("repeated_int32")
104  int32_array.append(123)
105  self.assertEqual(0, msg.payload.ByteSize())
106 
108  msg = unittest_pb2.TestAllTypes()
109  int32_array = msg.repeated_int32
110  int32_array.append(123)
111  self.assertNotEqual(0, msg.ByteSize())
112  msg.ClearField("repeated_int32")
113  int32_array.append(123)
114  self.assertEqual(0, msg.ByteSize())
115 
116  def testFloatPrinting(self):
117  message = unittest_pb2.TestAllTypes()
118  message.optional_float = -0.0
119  self.assertEqual(str(message), 'optional_float: -0\n')
120 
121 class OversizeProtosTest(unittest.TestCase):
122  def setUp(self):
123  msg = unittest_pb2.NestedTestAllTypes()
124  m = msg
125  for i in range(101):
126  m = m.child
127  m.Clear()
128  self.p_serialized = msg.SerializeToString()
129 
131  from google.protobuf.pyext._message import SetAllowOversizeProtos
133  q = unittest_pb2.NestedTestAllTypes()
134  with self.assertRaises(message.DecodeError):
135  q.ParseFromString(self.p_serialized)
136  print(q)
137 
139  from google.protobuf.pyext._message import SetAllowOversizeProtos
141  q = unittest_pb2.NestedTestAllTypes()
142  q.ParseFromString(self.p_serialized)
143 
144  def testExtensionIter(self):
145  extendee_proto = more_extensions_pb2.ExtendedMessage()
146 
147  extension_int32 = more_extensions_pb2.optional_int_extension
148  extendee_proto.Extensions[extension_int32] = 23
149 
150  extension_repeated = more_extensions_pb2.repeated_int_extension
151  extendee_proto.Extensions[extension_repeated].append(11)
152 
153  extension_msg = more_extensions_pb2.optional_message_extension
154  extendee_proto.Extensions[extension_msg].foreign_message_int = 56
155 
156  # Set some normal fields.
157  extendee_proto.optional_int32 = 1
158  extendee_proto.repeated_string.append('hi')
159 
160  expected = {
161  extension_int32: True,
162  extension_msg: True,
163  extension_repeated: True
164  }
165  count = 0
166  for item in extendee_proto.Extensions:
167  del expected[item]
168  self.assertIn(item, extendee_proto.Extensions)
169  count += 1
170  self.assertEqual(count, 3)
171  self.assertEqual(len(expected), 0)
172 
174  proto = unittest_pb2.TestRequiredForeign()
175  self.assertTrue(proto.IsInitialized())
176  self.assertFalse(proto.optional_message.IsInitialized())
177  errors = []
178  self.assertFalse(proto.optional_message.IsInitialized(errors))
179  self.assertEqual(['a', 'b', 'c'], errors)
180  self.assertRaises(message.EncodeError, proto.optional_message.SerializeToString)
181 
182 if __name__ == '__main__':
183  unittest.main(verbosity=2)
xds_interop_client.str
str
Definition: xds_interop_client.py:487
minimal_test.TestMessageExtension
Definition: minimal_test.py:44
minimal_test.OversizeProtosTest.setUp
def setUp(self)
Definition: minimal_test.py:122
minimal_test.TestMessageExtension.testExtensionsErrors
def testExtensionsErrors(self)
Definition: minimal_test.py:81
minimal_test.OversizeProtosTest.testSucceedOversizeProto
def testSucceedOversizeProto(self)
Definition: minimal_test.py:138
capstone.range
range
Definition: third_party/bloaty/third_party/capstone/bindings/python/capstone/__init__.py:6
minimal_test.OversizeProtosTest.p_serialized
p_serialized
Definition: minimal_test.py:128
google::protobuf.pyext
Definition: third_party/bloaty/third_party/protobuf/python/google/protobuf/pyext/__init__.py:1
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
minimal_test.OversizeProtosTest.testExtensionIter
def testExtensionIter(self)
Definition: minimal_test.py:144
minimal_test.TestMessageExtension.testClearStubMapField
def testClearStubMapField(self)
Definition: minimal_test.py:85
google::protobuf.internal
Definition: third_party/bloaty/third_party/protobuf/python/google/protobuf/internal/__init__.py:1
minimal_test.OversizeProtosTest
Definition: minimal_test.py:121
minimal_test.TestMessageExtension.test_descriptor_pool
def test_descriptor_pool(self)
Definition: minimal_test.py:46
minimal_test.TestMessageExtension.testClearReifiedMapField
def testClearReifiedMapField(self)
Definition: minimal_test.py:92
minimal_test.TestMessageExtension.test_repeated_field_slice_delete
def test_repeated_field_slice_delete(self)
Definition: minimal_test.py:66
google::protobuf::python::cmessage::SetAllowOversizeProtos
PyObject * SetAllowOversizeProtos(PyObject *m, PyObject *arg)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.cc:1914
minimal_test.TestMessageExtension.test_lib_is_upb
def test_lib_is_upb(self)
Definition: minimal_test.py:59
minimal_test.OversizeProtosTest.testIsInitializedStub
def testIsInitializedStub(self)
Definition: minimal_test.py:173
minimal_test.TestMessageExtension.testClearStubRepeatedField
def testClearStubRepeatedField(self)
Definition: minimal_test.py:100
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
minimal_test.TestMessageExtension.testClearReifiedRepeatdField
def testClearReifiedRepeatdField(self)
Definition: minimal_test.py:107
minimal_test.OversizeProtosTest.testAssertOversizeProto
def testAssertOversizeProto(self)
Definition: minimal_test.py:130
minimal_test.TestMessageExtension.testFloatPrinting
def testFloatPrinting(self)
Definition: minimal_test.py:116


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:40