blob: 7b9d3398b6efbb7e545ca2d993b983d39a07ddef [file] [log] [blame]
kenton@google.com26bd9ee2008-11-21 00:06:27 +00001#! /usr/bin/python
kenton@google.coma41a9dd2008-10-03 17:27:48 +00002# -*- coding: utf-8 -*-
kenton@google.com26bd9ee2008-11-21 00:06:27 +00003#
temporal40ee5512008-07-10 02:12:20 +00004# Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00005# Copyright 2008 Google Inc. All rights reserved.
temporal40ee5512008-07-10 02:12:20 +00006# http://code.google.com/p/protobuf/
7#
kenton@google.com24bf56f2008-09-24 20:31:01 +00008# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions are
10# met:
temporal40ee5512008-07-10 02:12:20 +000011#
kenton@google.com24bf56f2008-09-24 20:31:01 +000012# * Redistributions of source code must retain the above copyright
13# notice, this list of conditions and the following disclaimer.
14# * Redistributions in binary form must reproduce the above
15# copyright notice, this list of conditions and the following disclaimer
16# in the documentation and/or other materials provided with the
17# distribution.
18# * Neither the name of Google Inc. nor the names of its
19# contributors may be used to endorse or promote products derived from
20# this software without specific prior written permission.
temporal40ee5512008-07-10 02:12:20 +000021#
kenton@google.com24bf56f2008-09-24 20:31:01 +000022# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
temporal40ee5512008-07-10 02:12:20 +000034"""Unittest for reflection.py, which also indirectly tests the output of the
35pure-Python protocol compiler.
36"""
37
38__author__ = 'robinson@google.com (Will Robinson)'
39
40import operator
kenton@google.comfccb1462009-12-18 02:11:36 +000041import struct
temporal40ee5512008-07-10 02:12:20 +000042
43import unittest
temporal40ee5512008-07-10 02:12:20 +000044from google.protobuf import unittest_import_pb2
45from google.protobuf import unittest_mset_pb2
46from google.protobuf import unittest_pb2
47from google.protobuf import descriptor_pb2
48from google.protobuf import descriptor
49from google.protobuf import message
50from google.protobuf import reflection
liujisi@google.com33165fe2010-11-02 13:14:58 +000051from google.protobuf.internal import api_implementation
temporal40ee5512008-07-10 02:12:20 +000052from google.protobuf.internal import more_extensions_pb2
53from google.protobuf.internal import more_messages_pb2
54from google.protobuf.internal import wire_format
55from google.protobuf.internal import test_util
56from google.protobuf.internal import decoder
57
58
kenton@google.comfccb1462009-12-18 02:11:36 +000059class _MiniDecoder(object):
60 """Decodes a stream of values from a string.
61
62 Once upon a time we actually had a class called decoder.Decoder. Then we
63 got rid of it during a redesign that made decoding much, much faster overall.
64 But a couple tests in this file used it to check that the serialized form of
65 a message was correct. So, this class implements just the methods that were
66 used by said tests, so that we don't have to rewrite the tests.
67 """
68
69 def __init__(self, bytes):
70 self._bytes = bytes
71 self._pos = 0
72
73 def ReadVarint(self):
74 result, self._pos = decoder._DecodeVarint(self._bytes, self._pos)
75 return result
76
77 ReadInt32 = ReadVarint
78 ReadInt64 = ReadVarint
79 ReadUInt32 = ReadVarint
80 ReadUInt64 = ReadVarint
81
82 def ReadSInt64(self):
83 return wire_format.ZigZagDecode(self.ReadVarint())
84
85 ReadSInt32 = ReadSInt64
86
87 def ReadFieldNumberAndWireType(self):
88 return wire_format.UnpackTag(self.ReadVarint())
89
90 def ReadFloat(self):
91 result = struct.unpack("<f", self._bytes[self._pos:self._pos+4])[0]
92 self._pos += 4
93 return result
94
95 def ReadDouble(self):
96 result = struct.unpack("<d", self._bytes[self._pos:self._pos+8])[0]
97 self._pos += 8
98 return result
99
100 def EndOfStream(self):
101 return self._pos == len(self._bytes)
102
103
pesho.petrov87e64e12008-12-24 01:07:22 +0000104class ReflectionTest(unittest.TestCase):
105
liujisi@google.com33165fe2010-11-02 13:14:58 +0000106 def assertListsEqual(self, values, others):
pesho.petrov87e64e12008-12-24 01:07:22 +0000107 self.assertEqual(len(values), len(others))
108 for i in range(len(values)):
liujisi@google.com33165fe2010-11-02 13:14:58 +0000109 self.assertEqual(values[i], others[i])
temporal40ee5512008-07-10 02:12:20 +0000110
kenton@google.comfccb1462009-12-18 02:11:36 +0000111 def testScalarConstructor(self):
112 # Constructor with only scalar types should succeed.
113 proto = unittest_pb2.TestAllTypes(
114 optional_int32=24,
115 optional_double=54.321,
116 optional_string='optional_string')
117
118 self.assertEqual(24, proto.optional_int32)
119 self.assertEqual(54.321, proto.optional_double)
120 self.assertEqual('optional_string', proto.optional_string)
121
122 def testRepeatedScalarConstructor(self):
123 # Constructor with only repeated scalar types should succeed.
124 proto = unittest_pb2.TestAllTypes(
125 repeated_int32=[1, 2, 3, 4],
126 repeated_double=[1.23, 54.321],
127 repeated_bool=[True, False, False],
128 repeated_string=["optional_string"])
129
130 self.assertEquals([1, 2, 3, 4], list(proto.repeated_int32))
131 self.assertEquals([1.23, 54.321], list(proto.repeated_double))
132 self.assertEquals([True, False, False], list(proto.repeated_bool))
133 self.assertEquals(["optional_string"], list(proto.repeated_string))
134
135 def testRepeatedCompositeConstructor(self):
136 # Constructor with only repeated composite types should succeed.
137 proto = unittest_pb2.TestAllTypes(
138 repeated_nested_message=[
139 unittest_pb2.TestAllTypes.NestedMessage(
140 bb=unittest_pb2.TestAllTypes.FOO),
141 unittest_pb2.TestAllTypes.NestedMessage(
142 bb=unittest_pb2.TestAllTypes.BAR)],
143 repeated_foreign_message=[
144 unittest_pb2.ForeignMessage(c=-43),
145 unittest_pb2.ForeignMessage(c=45324),
146 unittest_pb2.ForeignMessage(c=12)],
147 repeatedgroup=[
148 unittest_pb2.TestAllTypes.RepeatedGroup(),
149 unittest_pb2.TestAllTypes.RepeatedGroup(a=1),
150 unittest_pb2.TestAllTypes.RepeatedGroup(a=2)])
151
152 self.assertEquals(
153 [unittest_pb2.TestAllTypes.NestedMessage(
154 bb=unittest_pb2.TestAllTypes.FOO),
155 unittest_pb2.TestAllTypes.NestedMessage(
156 bb=unittest_pb2.TestAllTypes.BAR)],
157 list(proto.repeated_nested_message))
158 self.assertEquals(
159 [unittest_pb2.ForeignMessage(c=-43),
160 unittest_pb2.ForeignMessage(c=45324),
161 unittest_pb2.ForeignMessage(c=12)],
162 list(proto.repeated_foreign_message))
163 self.assertEquals(
164 [unittest_pb2.TestAllTypes.RepeatedGroup(),
165 unittest_pb2.TestAllTypes.RepeatedGroup(a=1),
166 unittest_pb2.TestAllTypes.RepeatedGroup(a=2)],
167 list(proto.repeatedgroup))
168
169 def testMixedConstructor(self):
170 # Constructor with only mixed types should succeed.
171 proto = unittest_pb2.TestAllTypes(
172 optional_int32=24,
173 optional_string='optional_string',
174 repeated_double=[1.23, 54.321],
175 repeated_bool=[True, False, False],
176 repeated_nested_message=[
177 unittest_pb2.TestAllTypes.NestedMessage(
178 bb=unittest_pb2.TestAllTypes.FOO),
179 unittest_pb2.TestAllTypes.NestedMessage(
180 bb=unittest_pb2.TestAllTypes.BAR)],
181 repeated_foreign_message=[
182 unittest_pb2.ForeignMessage(c=-43),
183 unittest_pb2.ForeignMessage(c=45324),
184 unittest_pb2.ForeignMessage(c=12)])
185
186 self.assertEqual(24, proto.optional_int32)
187 self.assertEqual('optional_string', proto.optional_string)
188 self.assertEquals([1.23, 54.321], list(proto.repeated_double))
189 self.assertEquals([True, False, False], list(proto.repeated_bool))
190 self.assertEquals(
191 [unittest_pb2.TestAllTypes.NestedMessage(
192 bb=unittest_pb2.TestAllTypes.FOO),
193 unittest_pb2.TestAllTypes.NestedMessage(
194 bb=unittest_pb2.TestAllTypes.BAR)],
195 list(proto.repeated_nested_message))
196 self.assertEquals(
197 [unittest_pb2.ForeignMessage(c=-43),
198 unittest_pb2.ForeignMessage(c=45324),
199 unittest_pb2.ForeignMessage(c=12)],
200 list(proto.repeated_foreign_message))
201
kenton@google.com27028bc2010-07-27 21:19:59 +0000202 def testConstructorTypeError(self):
liujisi@google.com33165fe2010-11-02 13:14:58 +0000203 self.assertRaises(
204 TypeError, unittest_pb2.TestAllTypes, optional_int32="foo")
205 self.assertRaises(
206 TypeError, unittest_pb2.TestAllTypes, optional_string=1234)
207 self.assertRaises(
208 TypeError, unittest_pb2.TestAllTypes, optional_nested_message=1234)
209 self.assertRaises(
210 TypeError, unittest_pb2.TestAllTypes, repeated_int32=1234)
211 self.assertRaises(
212 TypeError, unittest_pb2.TestAllTypes, repeated_int32=["foo"])
213 self.assertRaises(
214 TypeError, unittest_pb2.TestAllTypes, repeated_string=1234)
215 self.assertRaises(
216 TypeError, unittest_pb2.TestAllTypes, repeated_string=[1234])
217 self.assertRaises(
218 TypeError, unittest_pb2.TestAllTypes, repeated_nested_message=1234)
219 self.assertRaises(
220 TypeError, unittest_pb2.TestAllTypes, repeated_nested_message=[1234])
kenton@google.com27028bc2010-07-27 21:19:59 +0000221
222 def testConstructorInvalidatesCachedByteSize(self):
223 message = unittest_pb2.TestAllTypes(optional_int32 = 12)
224 self.assertEquals(2, message.ByteSize())
225
226 message = unittest_pb2.TestAllTypes(
227 optional_nested_message = unittest_pb2.TestAllTypes.NestedMessage())
228 self.assertEquals(3, message.ByteSize())
229
230 message = unittest_pb2.TestAllTypes(repeated_int32 = [12])
231 self.assertEquals(3, message.ByteSize())
232
233 message = unittest_pb2.TestAllTypes(
234 repeated_nested_message = [unittest_pb2.TestAllTypes.NestedMessage()])
235 self.assertEquals(3, message.ByteSize())
236
temporal40ee5512008-07-10 02:12:20 +0000237 def testSimpleHasBits(self):
238 # Test a scalar.
239 proto = unittest_pb2.TestAllTypes()
240 self.assertTrue(not proto.HasField('optional_int32'))
241 self.assertEqual(0, proto.optional_int32)
242 # HasField() shouldn't be true if all we've done is
243 # read the default value.
244 self.assertTrue(not proto.HasField('optional_int32'))
245 proto.optional_int32 = 1
246 # Setting a value however *should* set the "has" bit.
247 self.assertTrue(proto.HasField('optional_int32'))
248 proto.ClearField('optional_int32')
249 # And clearing that value should unset the "has" bit.
250 self.assertTrue(not proto.HasField('optional_int32'))
251
252 def testHasBitsWithSinglyNestedScalar(self):
253 # Helper used to test foreign messages and groups.
254 #
255 # composite_field_name should be the name of a non-repeated
256 # composite (i.e., foreign or group) field in TestAllTypes,
257 # and scalar_field_name should be the name of an integer-valued
258 # scalar field within that composite.
259 #
260 # I never thought I'd miss C++ macros and templates so much. :(
261 # This helper is semantically just:
262 #
263 # assert proto.composite_field.scalar_field == 0
264 # assert not proto.composite_field.HasField('scalar_field')
265 # assert not proto.HasField('composite_field')
266 #
267 # proto.composite_field.scalar_field = 10
268 # old_composite_field = proto.composite_field
269 #
270 # assert proto.composite_field.scalar_field == 10
271 # assert proto.composite_field.HasField('scalar_field')
272 # assert proto.HasField('composite_field')
273 #
274 # proto.ClearField('composite_field')
275 #
276 # assert not proto.composite_field.HasField('scalar_field')
277 # assert not proto.HasField('composite_field')
278 # assert proto.composite_field.scalar_field == 0
279 #
280 # # Now ensure that ClearField('composite_field') disconnected
281 # # the old field object from the object tree...
282 # assert old_composite_field is not proto.composite_field
283 # old_composite_field.scalar_field = 20
284 # assert not proto.composite_field.HasField('scalar_field')
285 # assert not proto.HasField('composite_field')
286 def TestCompositeHasBits(composite_field_name, scalar_field_name):
287 proto = unittest_pb2.TestAllTypes()
288 # First, check that we can get the scalar value, and see that it's the
289 # default (0), but that proto.HasField('omposite') and
290 # proto.composite.HasField('scalar') will still return False.
291 composite_field = getattr(proto, composite_field_name)
292 original_scalar_value = getattr(composite_field, scalar_field_name)
293 self.assertEqual(0, original_scalar_value)
294 # Assert that the composite object does not "have" the scalar.
295 self.assertTrue(not composite_field.HasField(scalar_field_name))
296 # Assert that proto does not "have" the composite field.
297 self.assertTrue(not proto.HasField(composite_field_name))
298
299 # Now set the scalar within the composite field. Ensure that the setting
300 # is reflected, and that proto.HasField('composite') and
301 # proto.composite.HasField('scalar') now both return True.
302 new_val = 20
303 setattr(composite_field, scalar_field_name, new_val)
304 self.assertEqual(new_val, getattr(composite_field, scalar_field_name))
305 # Hold on to a reference to the current composite_field object.
306 old_composite_field = composite_field
307 # Assert that the has methods now return true.
308 self.assertTrue(composite_field.HasField(scalar_field_name))
309 self.assertTrue(proto.HasField(composite_field_name))
310
311 # Now call the clear method...
312 proto.ClearField(composite_field_name)
313
314 # ...and ensure that the "has" bits are all back to False...
315 composite_field = getattr(proto, composite_field_name)
316 self.assertTrue(not composite_field.HasField(scalar_field_name))
317 self.assertTrue(not proto.HasField(composite_field_name))
318 # ...and ensure that the scalar field has returned to its default.
319 self.assertEqual(0, getattr(composite_field, scalar_field_name))
320
321 # Finally, ensure that modifications to the old composite field object
liujisi@google.com33165fe2010-11-02 13:14:58 +0000322 # don't have any effect on the parent. Possible only with the pure-python
323 # implementation of the API.
temporal40ee5512008-07-10 02:12:20 +0000324 #
325 # (NOTE that when we clear the composite field in the parent, we actually
326 # don't recursively clear down the tree. Instead, we just disconnect the
327 # cleared composite from the tree.)
liujisi@google.com33165fe2010-11-02 13:14:58 +0000328 if api_implementation.Type() != 'python':
329 return
temporal40ee5512008-07-10 02:12:20 +0000330 self.assertTrue(old_composite_field is not composite_field)
331 setattr(old_composite_field, scalar_field_name, new_val)
332 self.assertTrue(not composite_field.HasField(scalar_field_name))
333 self.assertTrue(not proto.HasField(composite_field_name))
334 self.assertEqual(0, getattr(composite_field, scalar_field_name))
335
336 # Test simple, single-level nesting when we set a scalar.
337 TestCompositeHasBits('optionalgroup', 'a')
338 TestCompositeHasBits('optional_nested_message', 'bb')
339 TestCompositeHasBits('optional_foreign_message', 'c')
340 TestCompositeHasBits('optional_import_message', 'd')
341
342 def testReferencesToNestedMessage(self):
343 proto = unittest_pb2.TestAllTypes()
344 nested = proto.optional_nested_message
345 del proto
346 # A previous version had a bug where this would raise an exception when
347 # hitting a now-dead weak reference.
348 nested.bb = 23
349
350 def testDisconnectingNestedMessageBeforeSettingField(self):
liujisi@google.com33165fe2010-11-02 13:14:58 +0000351 if api_implementation.Type() != 'python':
352 return
temporal40ee5512008-07-10 02:12:20 +0000353 proto = unittest_pb2.TestAllTypes()
354 nested = proto.optional_nested_message
355 proto.ClearField('optional_nested_message') # Should disconnect from parent
356 self.assertTrue(nested is not proto.optional_nested_message)
357 nested.bb = 23
358 self.assertTrue(not proto.HasField('optional_nested_message'))
359 self.assertEqual(0, proto.optional_nested_message.bb)
360
361 def testHasBitsWhenModifyingRepeatedFields(self):
362 # Test nesting when we add an element to a repeated field in a submessage.
363 proto = unittest_pb2.TestNestedMessageHasBits()
364 proto.optional_nested_message.nestedmessage_repeated_int32.append(5)
365 self.assertEqual(
366 [5], proto.optional_nested_message.nestedmessage_repeated_int32)
367 self.assertTrue(proto.HasField('optional_nested_message'))
368
369 # Do the same test, but with a repeated composite field within the
370 # submessage.
371 proto.ClearField('optional_nested_message')
372 self.assertTrue(not proto.HasField('optional_nested_message'))
373 proto.optional_nested_message.nestedmessage_repeated_foreignmessage.add()
374 self.assertTrue(proto.HasField('optional_nested_message'))
375
376 def testHasBitsForManyLevelsOfNesting(self):
377 # Test nesting many levels deep.
378 recursive_proto = unittest_pb2.TestMutualRecursionA()
379 self.assertTrue(not recursive_proto.HasField('bb'))
380 self.assertEqual(0, recursive_proto.bb.a.bb.a.bb.optional_int32)
381 self.assertTrue(not recursive_proto.HasField('bb'))
382 recursive_proto.bb.a.bb.a.bb.optional_int32 = 5
383 self.assertEqual(5, recursive_proto.bb.a.bb.a.bb.optional_int32)
384 self.assertTrue(recursive_proto.HasField('bb'))
385 self.assertTrue(recursive_proto.bb.HasField('a'))
386 self.assertTrue(recursive_proto.bb.a.HasField('bb'))
387 self.assertTrue(recursive_proto.bb.a.bb.HasField('a'))
388 self.assertTrue(recursive_proto.bb.a.bb.a.HasField('bb'))
389 self.assertTrue(not recursive_proto.bb.a.bb.a.bb.HasField('a'))
390 self.assertTrue(recursive_proto.bb.a.bb.a.bb.HasField('optional_int32'))
391
392 def testSingularListFields(self):
393 proto = unittest_pb2.TestAllTypes()
394 proto.optional_fixed32 = 1
395 proto.optional_int32 = 5
396 proto.optional_string = 'foo'
kenton@google.comfccb1462009-12-18 02:11:36 +0000397 # Access sub-message but don't set it yet.
398 nested_message = proto.optional_nested_message
temporal40ee5512008-07-10 02:12:20 +0000399 self.assertEqual(
400 [ (proto.DESCRIPTOR.fields_by_name['optional_int32' ], 5),
401 (proto.DESCRIPTOR.fields_by_name['optional_fixed32'], 1),
402 (proto.DESCRIPTOR.fields_by_name['optional_string' ], 'foo') ],
403 proto.ListFields())
404
kenton@google.comfccb1462009-12-18 02:11:36 +0000405 proto.optional_nested_message.bb = 123
406 self.assertEqual(
407 [ (proto.DESCRIPTOR.fields_by_name['optional_int32' ], 5),
408 (proto.DESCRIPTOR.fields_by_name['optional_fixed32'], 1),
409 (proto.DESCRIPTOR.fields_by_name['optional_string' ], 'foo'),
410 (proto.DESCRIPTOR.fields_by_name['optional_nested_message' ],
411 nested_message) ],
412 proto.ListFields())
413
temporal40ee5512008-07-10 02:12:20 +0000414 def testRepeatedListFields(self):
415 proto = unittest_pb2.TestAllTypes()
416 proto.repeated_fixed32.append(1)
417 proto.repeated_int32.append(5)
418 proto.repeated_int32.append(11)
kenton@google.com2d6daa72009-01-22 01:27:00 +0000419 proto.repeated_string.extend(['foo', 'bar'])
420 proto.repeated_string.extend([])
temporal40ee5512008-07-10 02:12:20 +0000421 proto.repeated_string.append('baz')
kenton@google.com80b1d622009-07-29 01:13:20 +0000422 proto.repeated_string.extend(str(x) for x in xrange(2))
temporal40ee5512008-07-10 02:12:20 +0000423 proto.optional_int32 = 21
kenton@google.comfccb1462009-12-18 02:11:36 +0000424 proto.repeated_bool # Access but don't set anything; should not be listed.
temporal40ee5512008-07-10 02:12:20 +0000425 self.assertEqual(
426 [ (proto.DESCRIPTOR.fields_by_name['optional_int32' ], 21),
427 (proto.DESCRIPTOR.fields_by_name['repeated_int32' ], [5, 11]),
428 (proto.DESCRIPTOR.fields_by_name['repeated_fixed32'], [1]),
429 (proto.DESCRIPTOR.fields_by_name['repeated_string' ],
kenton@google.com80b1d622009-07-29 01:13:20 +0000430 ['foo', 'bar', 'baz', '0', '1']) ],
temporal40ee5512008-07-10 02:12:20 +0000431 proto.ListFields())
432
433 def testSingularListExtensions(self):
434 proto = unittest_pb2.TestAllExtensions()
435 proto.Extensions[unittest_pb2.optional_fixed32_extension] = 1
436 proto.Extensions[unittest_pb2.optional_int32_extension ] = 5
437 proto.Extensions[unittest_pb2.optional_string_extension ] = 'foo'
438 self.assertEqual(
439 [ (unittest_pb2.optional_int32_extension , 5),
440 (unittest_pb2.optional_fixed32_extension, 1),
441 (unittest_pb2.optional_string_extension , 'foo') ],
442 proto.ListFields())
443
444 def testRepeatedListExtensions(self):
445 proto = unittest_pb2.TestAllExtensions()
446 proto.Extensions[unittest_pb2.repeated_fixed32_extension].append(1)
447 proto.Extensions[unittest_pb2.repeated_int32_extension ].append(5)
448 proto.Extensions[unittest_pb2.repeated_int32_extension ].append(11)
449 proto.Extensions[unittest_pb2.repeated_string_extension ].append('foo')
450 proto.Extensions[unittest_pb2.repeated_string_extension ].append('bar')
451 proto.Extensions[unittest_pb2.repeated_string_extension ].append('baz')
452 proto.Extensions[unittest_pb2.optional_int32_extension ] = 21
453 self.assertEqual(
454 [ (unittest_pb2.optional_int32_extension , 21),
455 (unittest_pb2.repeated_int32_extension , [5, 11]),
456 (unittest_pb2.repeated_fixed32_extension, [1]),
457 (unittest_pb2.repeated_string_extension , ['foo', 'bar', 'baz']) ],
458 proto.ListFields())
459
460 def testListFieldsAndExtensions(self):
461 proto = unittest_pb2.TestFieldOrderings()
462 test_util.SetAllFieldsAndExtensions(proto)
463 unittest_pb2.my_extension_int
464 self.assertEqual(
465 [ (proto.DESCRIPTOR.fields_by_name['my_int' ], 1),
466 (unittest_pb2.my_extension_int , 23),
467 (proto.DESCRIPTOR.fields_by_name['my_string'], 'foo'),
468 (unittest_pb2.my_extension_string , 'bar'),
469 (proto.DESCRIPTOR.fields_by_name['my_float' ], 1.0) ],
470 proto.ListFields())
471
472 def testDefaultValues(self):
473 proto = unittest_pb2.TestAllTypes()
474 self.assertEqual(0, proto.optional_int32)
475 self.assertEqual(0, proto.optional_int64)
476 self.assertEqual(0, proto.optional_uint32)
477 self.assertEqual(0, proto.optional_uint64)
478 self.assertEqual(0, proto.optional_sint32)
479 self.assertEqual(0, proto.optional_sint64)
480 self.assertEqual(0, proto.optional_fixed32)
481 self.assertEqual(0, proto.optional_fixed64)
482 self.assertEqual(0, proto.optional_sfixed32)
483 self.assertEqual(0, proto.optional_sfixed64)
484 self.assertEqual(0.0, proto.optional_float)
485 self.assertEqual(0.0, proto.optional_double)
486 self.assertEqual(False, proto.optional_bool)
487 self.assertEqual('', proto.optional_string)
488 self.assertEqual('', proto.optional_bytes)
489
490 self.assertEqual(41, proto.default_int32)
491 self.assertEqual(42, proto.default_int64)
492 self.assertEqual(43, proto.default_uint32)
493 self.assertEqual(44, proto.default_uint64)
494 self.assertEqual(-45, proto.default_sint32)
495 self.assertEqual(46, proto.default_sint64)
496 self.assertEqual(47, proto.default_fixed32)
497 self.assertEqual(48, proto.default_fixed64)
498 self.assertEqual(49, proto.default_sfixed32)
499 self.assertEqual(-50, proto.default_sfixed64)
500 self.assertEqual(51.5, proto.default_float)
501 self.assertEqual(52e3, proto.default_double)
502 self.assertEqual(True, proto.default_bool)
503 self.assertEqual('hello', proto.default_string)
504 self.assertEqual('world', proto.default_bytes)
505 self.assertEqual(unittest_pb2.TestAllTypes.BAR, proto.default_nested_enum)
506 self.assertEqual(unittest_pb2.FOREIGN_BAR, proto.default_foreign_enum)
507 self.assertEqual(unittest_import_pb2.IMPORT_BAR,
508 proto.default_import_enum)
509
kenton@google.com24bf56f2008-09-24 20:31:01 +0000510 proto = unittest_pb2.TestExtremeDefaultValues()
511 self.assertEqual(u'\u1234', proto.utf8_string)
512
temporal40ee5512008-07-10 02:12:20 +0000513 def testHasFieldWithUnknownFieldName(self):
514 proto = unittest_pb2.TestAllTypes()
515 self.assertRaises(ValueError, proto.HasField, 'nonexistent_field')
516
517 def testClearFieldWithUnknownFieldName(self):
518 proto = unittest_pb2.TestAllTypes()
519 self.assertRaises(ValueError, proto.ClearField, 'nonexistent_field')
520
521 def testDisallowedAssignments(self):
522 # It's illegal to assign values directly to repeated fields
523 # or to nonrepeated composite fields. Ensure that this fails.
524 proto = unittest_pb2.TestAllTypes()
525 # Repeated fields.
526 self.assertRaises(AttributeError, setattr, proto, 'repeated_int32', 10)
527 # Lists shouldn't work, either.
528 self.assertRaises(AttributeError, setattr, proto, 'repeated_int32', [10])
529 # Composite fields.
530 self.assertRaises(AttributeError, setattr, proto,
531 'optional_nested_message', 23)
kenton@google.com24bf56f2008-09-24 20:31:01 +0000532 # Assignment to a repeated nested message field without specifying
533 # the index in the array of nested messages.
534 self.assertRaises(AttributeError, setattr, proto.repeated_nested_message,
535 'bb', 34)
536 # Assignment to an attribute of a repeated field.
537 self.assertRaises(AttributeError, setattr, proto.repeated_float,
538 'some_attribute', 34)
temporal40ee5512008-07-10 02:12:20 +0000539 # proto.nonexistent_field = 23 should fail as well.
540 self.assertRaises(AttributeError, setattr, proto, 'nonexistent_field', 23)
541
temporal40ee5512008-07-10 02:12:20 +0000542 def testSingleScalarTypeSafety(self):
543 proto = unittest_pb2.TestAllTypes()
544 self.assertRaises(TypeError, setattr, proto, 'optional_int32', 1.1)
545 self.assertRaises(TypeError, setattr, proto, 'optional_int32', 'foo')
546 self.assertRaises(TypeError, setattr, proto, 'optional_string', 10)
547 self.assertRaises(TypeError, setattr, proto, 'optional_bytes', 10)
548
549 def testSingleScalarBoundsChecking(self):
550 def TestMinAndMaxIntegers(field_name, expected_min, expected_max):
551 pb = unittest_pb2.TestAllTypes()
552 setattr(pb, field_name, expected_min)
liujisi@google.com33165fe2010-11-02 13:14:58 +0000553 self.assertEqual(expected_min, getattr(pb, field_name))
temporal40ee5512008-07-10 02:12:20 +0000554 setattr(pb, field_name, expected_max)
liujisi@google.com33165fe2010-11-02 13:14:58 +0000555 self.assertEqual(expected_max, getattr(pb, field_name))
temporal40ee5512008-07-10 02:12:20 +0000556 self.assertRaises(ValueError, setattr, pb, field_name, expected_min - 1)
557 self.assertRaises(ValueError, setattr, pb, field_name, expected_max + 1)
558
559 TestMinAndMaxIntegers('optional_int32', -(1 << 31), (1 << 31) - 1)
560 TestMinAndMaxIntegers('optional_uint32', 0, 0xffffffff)
561 TestMinAndMaxIntegers('optional_int64', -(1 << 63), (1 << 63) - 1)
562 TestMinAndMaxIntegers('optional_uint64', 0, 0xffffffffffffffff)
liujisi@google.com33165fe2010-11-02 13:14:58 +0000563
564 pb = unittest_pb2.TestAllTypes()
565 pb.optional_nested_enum = 1
566 self.assertEqual(1, pb.optional_nested_enum)
567
568 # Invalid enum values.
569 pb.optional_nested_enum = 0
570 self.assertEqual(0, pb.optional_nested_enum)
571
572 bytes_size_before = pb.ByteSize()
573
574 pb.optional_nested_enum = 4
575 self.assertEqual(4, pb.optional_nested_enum)
576
577 pb.optional_nested_enum = 0
578 self.assertEqual(0, pb.optional_nested_enum)
579
580 # Make sure that setting the same enum field doesn't just add unknown
581 # fields (but overwrites them).
582 self.assertEqual(bytes_size_before, pb.ByteSize())
583
584 # Is the invalid value preserved after serialization?
585 serialized = pb.SerializeToString()
586 pb2 = unittest_pb2.TestAllTypes()
587 pb2.ParseFromString(serialized)
588 self.assertEqual(0, pb2.optional_nested_enum)
589 self.assertEqual(pb, pb2)
temporal40ee5512008-07-10 02:12:20 +0000590
591 def testRepeatedScalarTypeSafety(self):
592 proto = unittest_pb2.TestAllTypes()
593 self.assertRaises(TypeError, proto.repeated_int32.append, 1.1)
594 self.assertRaises(TypeError, proto.repeated_int32.append, 'foo')
595 self.assertRaises(TypeError, proto.repeated_string, 10)
596 self.assertRaises(TypeError, proto.repeated_bytes, 10)
597
598 proto.repeated_int32.append(10)
599 proto.repeated_int32[0] = 23
600 self.assertRaises(IndexError, proto.repeated_int32.__setitem__, 500, 23)
601 self.assertRaises(TypeError, proto.repeated_int32.__setitem__, 0, 'abc')
602
liujisi@google.com33165fe2010-11-02 13:14:58 +0000603 # Repeated enums tests.
604 #proto.repeated_nested_enum.append(0)
605
temporal40ee5512008-07-10 02:12:20 +0000606 def testSingleScalarGettersAndSetters(self):
607 proto = unittest_pb2.TestAllTypes()
608 self.assertEqual(0, proto.optional_int32)
609 proto.optional_int32 = 1
610 self.assertEqual(1, proto.optional_int32)
liujisi@google.com33165fe2010-11-02 13:14:58 +0000611
612 proto.optional_uint64 = 0xffffffffffff
613 self.assertEqual(0xffffffffffff, proto.optional_uint64)
614 proto.optional_uint64 = 0xffffffffffffffff
615 self.assertEqual(0xffffffffffffffff, proto.optional_uint64)
temporal40ee5512008-07-10 02:12:20 +0000616 # TODO(robinson): Test all other scalar field types.
617
618 def testSingleScalarClearField(self):
619 proto = unittest_pb2.TestAllTypes()
620 # Should be allowed to clear something that's not there (a no-op).
621 proto.ClearField('optional_int32')
622 proto.optional_int32 = 1
623 self.assertTrue(proto.HasField('optional_int32'))
624 proto.ClearField('optional_int32')
625 self.assertEqual(0, proto.optional_int32)
626 self.assertTrue(not proto.HasField('optional_int32'))
627 # TODO(robinson): Test all other scalar field types.
628
629 def testEnums(self):
630 proto = unittest_pb2.TestAllTypes()
631 self.assertEqual(1, proto.FOO)
632 self.assertEqual(1, unittest_pb2.TestAllTypes.FOO)
633 self.assertEqual(2, proto.BAR)
634 self.assertEqual(2, unittest_pb2.TestAllTypes.BAR)
635 self.assertEqual(3, proto.BAZ)
636 self.assertEqual(3, unittest_pb2.TestAllTypes.BAZ)
637
638 def testRepeatedScalars(self):
639 proto = unittest_pb2.TestAllTypes()
640
641 self.assertTrue(not proto.repeated_int32)
642 self.assertEqual(0, len(proto.repeated_int32))
pesho.petrov87e64e12008-12-24 01:07:22 +0000643 proto.repeated_int32.append(5)
644 proto.repeated_int32.append(10)
645 proto.repeated_int32.append(15)
temporal40ee5512008-07-10 02:12:20 +0000646 self.assertTrue(proto.repeated_int32)
pesho.petrov87e64e12008-12-24 01:07:22 +0000647 self.assertEqual(3, len(proto.repeated_int32))
temporal40ee5512008-07-10 02:12:20 +0000648
pesho.petrov87e64e12008-12-24 01:07:22 +0000649 self.assertEqual([5, 10, 15], proto.repeated_int32)
650
651 # Test single retrieval.
temporal40ee5512008-07-10 02:12:20 +0000652 self.assertEqual(5, proto.repeated_int32[0])
pesho.petrov87e64e12008-12-24 01:07:22 +0000653 self.assertEqual(15, proto.repeated_int32[-1])
temporal40ee5512008-07-10 02:12:20 +0000654 # Test out-of-bounds indices.
655 self.assertRaises(IndexError, proto.repeated_int32.__getitem__, 1234)
656 self.assertRaises(IndexError, proto.repeated_int32.__getitem__, -1234)
657 # Test incorrect types passed to __getitem__.
658 self.assertRaises(TypeError, proto.repeated_int32.__getitem__, 'foo')
659 self.assertRaises(TypeError, proto.repeated_int32.__getitem__, None)
660
pesho.petrov87e64e12008-12-24 01:07:22 +0000661 # Test single assignment.
662 proto.repeated_int32[1] = 20
663 self.assertEqual([5, 20, 15], proto.repeated_int32)
664
665 # Test insertion.
666 proto.repeated_int32.insert(1, 25)
667 self.assertEqual([5, 25, 20, 15], proto.repeated_int32)
668
669 # Test slice retrieval.
670 proto.repeated_int32.append(30)
671 self.assertEqual([25, 20, 15], proto.repeated_int32[1:4])
672 self.assertEqual([5, 25, 20, 15, 30], proto.repeated_int32[:])
673
kenton@google.com80b1d622009-07-29 01:13:20 +0000674 # Test slice assignment with an iterator
675 proto.repeated_int32[1:4] = (i for i in xrange(3))
676 self.assertEqual([5, 0, 1, 2, 30], proto.repeated_int32)
677
pesho.petrov87e64e12008-12-24 01:07:22 +0000678 # Test slice assignment.
679 proto.repeated_int32[1:4] = [35, 40, 45]
680 self.assertEqual([5, 35, 40, 45, 30], proto.repeated_int32)
681
temporal40ee5512008-07-10 02:12:20 +0000682 # Test that we can use the field as an iterator.
683 result = []
684 for i in proto.repeated_int32:
685 result.append(i)
pesho.petrov87e64e12008-12-24 01:07:22 +0000686 self.assertEqual([5, 35, 40, 45, 30], result)
687
688 # Test single deletion.
689 del proto.repeated_int32[2]
690 self.assertEqual([5, 35, 45, 30], proto.repeated_int32)
691
692 # Test slice deletion.
693 del proto.repeated_int32[2:]
694 self.assertEqual([5, 35], proto.repeated_int32)
temporal40ee5512008-07-10 02:12:20 +0000695
liujisi@google.com33165fe2010-11-02 13:14:58 +0000696 # Test extending.
697 proto.repeated_int32.extend([3, 13])
698 self.assertEqual([5, 35, 3, 13], proto.repeated_int32)
699
temporal40ee5512008-07-10 02:12:20 +0000700 # Test clearing.
701 proto.ClearField('repeated_int32')
702 self.assertTrue(not proto.repeated_int32)
703 self.assertEqual(0, len(proto.repeated_int32))
704
liujisi@google.com33165fe2010-11-02 13:14:58 +0000705 proto.repeated_int32.append(1)
706 self.assertEqual(1, proto.repeated_int32[-1])
707 # Test assignment to a negative index.
708 proto.repeated_int32[-1] = 2
709 self.assertEqual(2, proto.repeated_int32[-1])
710
711 # Test deletion at negative indices.
712 proto.repeated_int32[:] = [0, 1, 2, 3]
713 del proto.repeated_int32[-1]
714 self.assertEqual([0, 1, 2], proto.repeated_int32)
715
716 del proto.repeated_int32[-2]
717 self.assertEqual([0, 2], proto.repeated_int32)
718
719 self.assertRaises(IndexError, proto.repeated_int32.__delitem__, -3)
720 self.assertRaises(IndexError, proto.repeated_int32.__delitem__, 300)
721
722 del proto.repeated_int32[-2:-1]
723 self.assertEqual([2], proto.repeated_int32)
724
725 del proto.repeated_int32[100:10000]
726 self.assertEqual([2], proto.repeated_int32)
727
kenton@google.com24bf56f2008-09-24 20:31:01 +0000728 def testRepeatedScalarsRemove(self):
729 proto = unittest_pb2.TestAllTypes()
730
731 self.assertTrue(not proto.repeated_int32)
732 self.assertEqual(0, len(proto.repeated_int32))
733 proto.repeated_int32.append(5)
734 proto.repeated_int32.append(10)
735 proto.repeated_int32.append(5)
736 proto.repeated_int32.append(5)
737
738 self.assertEqual(4, len(proto.repeated_int32))
739 proto.repeated_int32.remove(5)
740 self.assertEqual(3, len(proto.repeated_int32))
741 self.assertEqual(10, proto.repeated_int32[0])
742 self.assertEqual(5, proto.repeated_int32[1])
743 self.assertEqual(5, proto.repeated_int32[2])
744
745 proto.repeated_int32.remove(5)
746 self.assertEqual(2, len(proto.repeated_int32))
747 self.assertEqual(10, proto.repeated_int32[0])
748 self.assertEqual(5, proto.repeated_int32[1])
749
750 proto.repeated_int32.remove(10)
751 self.assertEqual(1, len(proto.repeated_int32))
752 self.assertEqual(5, proto.repeated_int32[0])
753
754 # Remove a non-existent element.
755 self.assertRaises(ValueError, proto.repeated_int32.remove, 123)
756
temporal40ee5512008-07-10 02:12:20 +0000757 def testRepeatedComposites(self):
758 proto = unittest_pb2.TestAllTypes()
759 self.assertTrue(not proto.repeated_nested_message)
760 self.assertEqual(0, len(proto.repeated_nested_message))
761 m0 = proto.repeated_nested_message.add()
762 m1 = proto.repeated_nested_message.add()
763 self.assertTrue(proto.repeated_nested_message)
764 self.assertEqual(2, len(proto.repeated_nested_message))
liujisi@google.com33165fe2010-11-02 13:14:58 +0000765 self.assertListsEqual([m0, m1], proto.repeated_nested_message)
temporal40ee5512008-07-10 02:12:20 +0000766 self.assertTrue(isinstance(m0, unittest_pb2.TestAllTypes.NestedMessage))
767
768 # Test out-of-bounds indices.
769 self.assertRaises(IndexError, proto.repeated_nested_message.__getitem__,
770 1234)
771 self.assertRaises(IndexError, proto.repeated_nested_message.__getitem__,
772 -1234)
773
774 # Test incorrect types passed to __getitem__.
775 self.assertRaises(TypeError, proto.repeated_nested_message.__getitem__,
776 'foo')
777 self.assertRaises(TypeError, proto.repeated_nested_message.__getitem__,
778 None)
779
pesho.petrov87e64e12008-12-24 01:07:22 +0000780 # Test slice retrieval.
781 m2 = proto.repeated_nested_message.add()
782 m3 = proto.repeated_nested_message.add()
783 m4 = proto.repeated_nested_message.add()
liujisi@google.com33165fe2010-11-02 13:14:58 +0000784 self.assertListsEqual(
785 [m1, m2, m3], proto.repeated_nested_message[1:4])
786 self.assertListsEqual(
787 [m0, m1, m2, m3, m4], proto.repeated_nested_message[:])
788 self.assertListsEqual(
789 [m0, m1], proto.repeated_nested_message[:2])
790 self.assertListsEqual(
791 [m2, m3, m4], proto.repeated_nested_message[2:])
792 self.assertEqual(
793 m0, proto.repeated_nested_message[0])
794 self.assertListsEqual(
795 [m0], proto.repeated_nested_message[:1])
pesho.petrov87e64e12008-12-24 01:07:22 +0000796
temporal40ee5512008-07-10 02:12:20 +0000797 # Test that we can use the field as an iterator.
798 result = []
799 for i in proto.repeated_nested_message:
800 result.append(i)
liujisi@google.com33165fe2010-11-02 13:14:58 +0000801 self.assertListsEqual([m0, m1, m2, m3, m4], result)
temporal40ee5512008-07-10 02:12:20 +0000802
pesho.petrov87e64e12008-12-24 01:07:22 +0000803 # Test single deletion.
804 del proto.repeated_nested_message[2]
liujisi@google.com33165fe2010-11-02 13:14:58 +0000805 self.assertListsEqual([m0, m1, m3, m4], proto.repeated_nested_message)
pesho.petrov87e64e12008-12-24 01:07:22 +0000806
807 # Test slice deletion.
808 del proto.repeated_nested_message[2:]
liujisi@google.com33165fe2010-11-02 13:14:58 +0000809 self.assertListsEqual([m0, m1], proto.repeated_nested_message)
810
811 # Test extending.
812 n1 = unittest_pb2.TestAllTypes.NestedMessage(bb=1)
813 n2 = unittest_pb2.TestAllTypes.NestedMessage(bb=2)
814 proto.repeated_nested_message.extend([n1,n2])
815 self.assertEqual(4, len(proto.repeated_nested_message))
816 self.assertEqual(n1, proto.repeated_nested_message[2])
817 self.assertEqual(n2, proto.repeated_nested_message[3])
kenton@google.com24bf56f2008-09-24 20:31:01 +0000818
temporal40ee5512008-07-10 02:12:20 +0000819 # Test clearing.
820 proto.ClearField('repeated_nested_message')
821 self.assertTrue(not proto.repeated_nested_message)
822 self.assertEqual(0, len(proto.repeated_nested_message))
823
liujisi@google.com33165fe2010-11-02 13:14:58 +0000824 # Test constructing an element while adding it.
825 proto.repeated_nested_message.add(bb=23)
826 self.assertEqual(1, len(proto.repeated_nested_message))
827 self.assertEqual(23, proto.repeated_nested_message[0].bb)
828
temporal40ee5512008-07-10 02:12:20 +0000829 def testHandWrittenReflection(self):
liujisi@google.com33165fe2010-11-02 13:14:58 +0000830 # Hand written extensions are only supported by the pure-Python
831 # implementation of the API.
832 if api_implementation.Type() != 'python':
833 return
834
temporal40ee5512008-07-10 02:12:20 +0000835 FieldDescriptor = descriptor.FieldDescriptor
836 foo_field_descriptor = FieldDescriptor(
837 name='foo_field', full_name='MyProto.foo_field',
838 index=0, number=1, type=FieldDescriptor.TYPE_INT64,
839 cpp_type=FieldDescriptor.CPPTYPE_INT64,
840 label=FieldDescriptor.LABEL_OPTIONAL, default_value=0,
841 containing_type=None, message_type=None, enum_type=None,
842 is_extension=False, extension_scope=None,
843 options=descriptor_pb2.FieldOptions())
844 mydescriptor = descriptor.Descriptor(
845 name='MyProto', full_name='MyProto', filename='ignored',
846 containing_type=None, nested_types=[], enum_types=[],
847 fields=[foo_field_descriptor], extensions=[],
848 options=descriptor_pb2.MessageOptions())
849 class MyProtoClass(message.Message):
850 DESCRIPTOR = mydescriptor
851 __metaclass__ = reflection.GeneratedProtocolMessageType
852 myproto_instance = MyProtoClass()
853 self.assertEqual(0, myproto_instance.foo_field)
854 self.assertTrue(not myproto_instance.HasField('foo_field'))
855 myproto_instance.foo_field = 23
856 self.assertEqual(23, myproto_instance.foo_field)
857 self.assertTrue(myproto_instance.HasField('foo_field'))
858
859 def testTopLevelExtensionsForOptionalScalar(self):
860 extendee_proto = unittest_pb2.TestAllExtensions()
861 extension = unittest_pb2.optional_int32_extension
862 self.assertTrue(not extendee_proto.HasExtension(extension))
863 self.assertEqual(0, extendee_proto.Extensions[extension])
864 # As with normal scalar fields, just doing a read doesn't actually set the
865 # "has" bit.
866 self.assertTrue(not extendee_proto.HasExtension(extension))
867 # Actually set the thing.
868 extendee_proto.Extensions[extension] = 23
869 self.assertEqual(23, extendee_proto.Extensions[extension])
870 self.assertTrue(extendee_proto.HasExtension(extension))
871 # Ensure that clearing works as well.
872 extendee_proto.ClearExtension(extension)
873 self.assertEqual(0, extendee_proto.Extensions[extension])
874 self.assertTrue(not extendee_proto.HasExtension(extension))
875
876 def testTopLevelExtensionsForRepeatedScalar(self):
877 extendee_proto = unittest_pb2.TestAllExtensions()
878 extension = unittest_pb2.repeated_string_extension
879 self.assertEqual(0, len(extendee_proto.Extensions[extension]))
880 extendee_proto.Extensions[extension].append('foo')
881 self.assertEqual(['foo'], extendee_proto.Extensions[extension])
882 string_list = extendee_proto.Extensions[extension]
883 extendee_proto.ClearExtension(extension)
884 self.assertEqual(0, len(extendee_proto.Extensions[extension]))
885 self.assertTrue(string_list is not extendee_proto.Extensions[extension])
886 # Shouldn't be allowed to do Extensions[extension] = 'a'
887 self.assertRaises(TypeError, operator.setitem, extendee_proto.Extensions,
888 extension, 'a')
889
890 def testTopLevelExtensionsForOptionalMessage(self):
891 extendee_proto = unittest_pb2.TestAllExtensions()
892 extension = unittest_pb2.optional_foreign_message_extension
893 self.assertTrue(not extendee_proto.HasExtension(extension))
894 self.assertEqual(0, extendee_proto.Extensions[extension].c)
895 # As with normal (non-extension) fields, merely reading from the
896 # thing shouldn't set the "has" bit.
897 self.assertTrue(not extendee_proto.HasExtension(extension))
898 extendee_proto.Extensions[extension].c = 23
899 self.assertEqual(23, extendee_proto.Extensions[extension].c)
900 self.assertTrue(extendee_proto.HasExtension(extension))
901 # Save a reference here.
902 foreign_message = extendee_proto.Extensions[extension]
903 extendee_proto.ClearExtension(extension)
904 self.assertTrue(foreign_message is not extendee_proto.Extensions[extension])
905 # Setting a field on foreign_message now shouldn't set
906 # any "has" bits on extendee_proto.
907 foreign_message.c = 42
908 self.assertEqual(42, foreign_message.c)
909 self.assertTrue(foreign_message.HasField('c'))
910 self.assertTrue(not extendee_proto.HasExtension(extension))
911 # Shouldn't be allowed to do Extensions[extension] = 'a'
912 self.assertRaises(TypeError, operator.setitem, extendee_proto.Extensions,
913 extension, 'a')
914
915 def testTopLevelExtensionsForRepeatedMessage(self):
916 extendee_proto = unittest_pb2.TestAllExtensions()
917 extension = unittest_pb2.repeatedgroup_extension
918 self.assertEqual(0, len(extendee_proto.Extensions[extension]))
919 group = extendee_proto.Extensions[extension].add()
920 group.a = 23
921 self.assertEqual(23, extendee_proto.Extensions[extension][0].a)
922 group.a = 42
923 self.assertEqual(42, extendee_proto.Extensions[extension][0].a)
924 group_list = extendee_proto.Extensions[extension]
925 extendee_proto.ClearExtension(extension)
926 self.assertEqual(0, len(extendee_proto.Extensions[extension]))
927 self.assertTrue(group_list is not extendee_proto.Extensions[extension])
928 # Shouldn't be allowed to do Extensions[extension] = 'a'
929 self.assertRaises(TypeError, operator.setitem, extendee_proto.Extensions,
930 extension, 'a')
931
932 def testNestedExtensions(self):
933 extendee_proto = unittest_pb2.TestAllExtensions()
934 extension = unittest_pb2.TestRequired.single
935
936 # We just test the non-repeated case.
937 self.assertTrue(not extendee_proto.HasExtension(extension))
938 required = extendee_proto.Extensions[extension]
939 self.assertEqual(0, required.a)
940 self.assertTrue(not extendee_proto.HasExtension(extension))
941 required.a = 23
942 self.assertEqual(23, extendee_proto.Extensions[extension].a)
943 self.assertTrue(extendee_proto.HasExtension(extension))
944 extendee_proto.ClearExtension(extension)
945 self.assertTrue(required is not extendee_proto.Extensions[extension])
946 self.assertTrue(not extendee_proto.HasExtension(extension))
947
948 # If message A directly contains message B, and
949 # a.HasField('b') is currently False, then mutating any
950 # extension in B should change a.HasField('b') to True
951 # (and so on up the object tree).
952 def testHasBitsForAncestorsOfExtendedMessage(self):
953 # Optional scalar extension.
954 toplevel = more_extensions_pb2.TopLevelMessage()
955 self.assertTrue(not toplevel.HasField('submessage'))
956 self.assertEqual(0, toplevel.submessage.Extensions[
957 more_extensions_pb2.optional_int_extension])
958 self.assertTrue(not toplevel.HasField('submessage'))
959 toplevel.submessage.Extensions[
960 more_extensions_pb2.optional_int_extension] = 23
961 self.assertEqual(23, toplevel.submessage.Extensions[
962 more_extensions_pb2.optional_int_extension])
963 self.assertTrue(toplevel.HasField('submessage'))
964
965 # Repeated scalar extension.
966 toplevel = more_extensions_pb2.TopLevelMessage()
967 self.assertTrue(not toplevel.HasField('submessage'))
968 self.assertEqual([], toplevel.submessage.Extensions[
969 more_extensions_pb2.repeated_int_extension])
970 self.assertTrue(not toplevel.HasField('submessage'))
971 toplevel.submessage.Extensions[
972 more_extensions_pb2.repeated_int_extension].append(23)
973 self.assertEqual([23], toplevel.submessage.Extensions[
974 more_extensions_pb2.repeated_int_extension])
975 self.assertTrue(toplevel.HasField('submessage'))
976
977 # Optional message extension.
978 toplevel = more_extensions_pb2.TopLevelMessage()
979 self.assertTrue(not toplevel.HasField('submessage'))
980 self.assertEqual(0, toplevel.submessage.Extensions[
981 more_extensions_pb2.optional_message_extension].foreign_message_int)
982 self.assertTrue(not toplevel.HasField('submessage'))
983 toplevel.submessage.Extensions[
984 more_extensions_pb2.optional_message_extension].foreign_message_int = 23
985 self.assertEqual(23, toplevel.submessage.Extensions[
986 more_extensions_pb2.optional_message_extension].foreign_message_int)
987 self.assertTrue(toplevel.HasField('submessage'))
988
989 # Repeated message extension.
990 toplevel = more_extensions_pb2.TopLevelMessage()
991 self.assertTrue(not toplevel.HasField('submessage'))
992 self.assertEqual(0, len(toplevel.submessage.Extensions[
993 more_extensions_pb2.repeated_message_extension]))
994 self.assertTrue(not toplevel.HasField('submessage'))
995 foreign = toplevel.submessage.Extensions[
996 more_extensions_pb2.repeated_message_extension].add()
liujisi@google.com33165fe2010-11-02 13:14:58 +0000997 self.assertEqual(foreign, toplevel.submessage.Extensions[
temporal40ee5512008-07-10 02:12:20 +0000998 more_extensions_pb2.repeated_message_extension][0])
999 self.assertTrue(toplevel.HasField('submessage'))
1000
1001 def testDisconnectionAfterClearingEmptyMessage(self):
1002 toplevel = more_extensions_pb2.TopLevelMessage()
1003 extendee_proto = toplevel.submessage
1004 extension = more_extensions_pb2.optional_message_extension
1005 extension_proto = extendee_proto.Extensions[extension]
1006 extendee_proto.ClearExtension(extension)
1007 extension_proto.foreign_message_int = 23
1008
temporal40ee5512008-07-10 02:12:20 +00001009 self.assertTrue(extension_proto is not extendee_proto.Extensions[extension])
1010
1011 def testExtensionFailureModes(self):
1012 extendee_proto = unittest_pb2.TestAllExtensions()
1013
1014 # Try non-extension-handle arguments to HasExtension,
1015 # ClearExtension(), and Extensions[]...
1016 self.assertRaises(KeyError, extendee_proto.HasExtension, 1234)
1017 self.assertRaises(KeyError, extendee_proto.ClearExtension, 1234)
1018 self.assertRaises(KeyError, extendee_proto.Extensions.__getitem__, 1234)
1019 self.assertRaises(KeyError, extendee_proto.Extensions.__setitem__, 1234, 5)
1020
1021 # Try something that *is* an extension handle, just not for
1022 # this message...
1023 unknown_handle = more_extensions_pb2.optional_int_extension
1024 self.assertRaises(KeyError, extendee_proto.HasExtension,
1025 unknown_handle)
1026 self.assertRaises(KeyError, extendee_proto.ClearExtension,
1027 unknown_handle)
1028 self.assertRaises(KeyError, extendee_proto.Extensions.__getitem__,
1029 unknown_handle)
1030 self.assertRaises(KeyError, extendee_proto.Extensions.__setitem__,
1031 unknown_handle, 5)
1032
1033 # Try call HasExtension() with a valid handle, but for a
1034 # *repeated* field. (Just as with non-extension repeated
1035 # fields, Has*() isn't supported for extension repeated fields).
1036 self.assertRaises(KeyError, extendee_proto.HasExtension,
1037 unittest_pb2.repeated_string_extension)
1038
kenton@google.com2d6daa72009-01-22 01:27:00 +00001039 def testStaticParseFrom(self):
1040 proto1 = unittest_pb2.TestAllTypes()
1041 test_util.SetAllFields(proto1)
1042
1043 string1 = proto1.SerializeToString()
1044 proto2 = unittest_pb2.TestAllTypes.FromString(string1)
1045
1046 # Messages should be equal.
1047 self.assertEqual(proto2, proto1)
1048
temporal779f61c2008-08-13 03:15:00 +00001049 def testMergeFromSingularField(self):
1050 # Test merge with just a singular field.
1051 proto1 = unittest_pb2.TestAllTypes()
1052 proto1.optional_int32 = 1
1053
1054 proto2 = unittest_pb2.TestAllTypes()
1055 # This shouldn't get overwritten.
1056 proto2.optional_string = 'value'
1057
1058 proto2.MergeFrom(proto1)
1059 self.assertEqual(1, proto2.optional_int32)
1060 self.assertEqual('value', proto2.optional_string)
1061
1062 def testMergeFromRepeatedField(self):
1063 # Test merge with just a repeated field.
1064 proto1 = unittest_pb2.TestAllTypes()
1065 proto1.repeated_int32.append(1)
1066 proto1.repeated_int32.append(2)
1067
1068 proto2 = unittest_pb2.TestAllTypes()
1069 proto2.repeated_int32.append(0)
1070 proto2.MergeFrom(proto1)
1071
1072 self.assertEqual(0, proto2.repeated_int32[0])
1073 self.assertEqual(1, proto2.repeated_int32[1])
1074 self.assertEqual(2, proto2.repeated_int32[2])
1075
1076 def testMergeFromOptionalGroup(self):
1077 # Test merge with an optional group.
1078 proto1 = unittest_pb2.TestAllTypes()
1079 proto1.optionalgroup.a = 12
1080 proto2 = unittest_pb2.TestAllTypes()
1081 proto2.MergeFrom(proto1)
1082 self.assertEqual(12, proto2.optionalgroup.a)
1083
1084 def testMergeFromRepeatedNestedMessage(self):
1085 # Test merge with a repeated nested message.
1086 proto1 = unittest_pb2.TestAllTypes()
1087 m = proto1.repeated_nested_message.add()
1088 m.bb = 123
1089 m = proto1.repeated_nested_message.add()
1090 m.bb = 321
1091
1092 proto2 = unittest_pb2.TestAllTypes()
1093 m = proto2.repeated_nested_message.add()
1094 m.bb = 999
1095 proto2.MergeFrom(proto1)
1096 self.assertEqual(999, proto2.repeated_nested_message[0].bb)
1097 self.assertEqual(123, proto2.repeated_nested_message[1].bb)
1098 self.assertEqual(321, proto2.repeated_nested_message[2].bb)
1099
liujisi@google.com33165fe2010-11-02 13:14:58 +00001100 proto3 = unittest_pb2.TestAllTypes()
1101 proto3.repeated_nested_message.MergeFrom(proto2.repeated_nested_message)
1102 self.assertEqual(999, proto3.repeated_nested_message[0].bb)
1103 self.assertEqual(123, proto3.repeated_nested_message[1].bb)
1104 self.assertEqual(321, proto3.repeated_nested_message[2].bb)
1105
temporal779f61c2008-08-13 03:15:00 +00001106 def testMergeFromAllFields(self):
1107 # With all fields set.
1108 proto1 = unittest_pb2.TestAllTypes()
1109 test_util.SetAllFields(proto1)
1110 proto2 = unittest_pb2.TestAllTypes()
1111 proto2.MergeFrom(proto1)
1112
1113 # Messages should be equal.
1114 self.assertEqual(proto2, proto1)
1115
1116 # Serialized string should be equal too.
1117 string1 = proto1.SerializeToString()
1118 string2 = proto2.SerializeToString()
1119 self.assertEqual(string1, string2)
1120
1121 def testMergeFromExtensionsSingular(self):
1122 proto1 = unittest_pb2.TestAllExtensions()
1123 proto1.Extensions[unittest_pb2.optional_int32_extension] = 1
1124
1125 proto2 = unittest_pb2.TestAllExtensions()
1126 proto2.MergeFrom(proto1)
1127 self.assertEqual(
1128 1, proto2.Extensions[unittest_pb2.optional_int32_extension])
1129
1130 def testMergeFromExtensionsRepeated(self):
1131 proto1 = unittest_pb2.TestAllExtensions()
1132 proto1.Extensions[unittest_pb2.repeated_int32_extension].append(1)
1133 proto1.Extensions[unittest_pb2.repeated_int32_extension].append(2)
1134
1135 proto2 = unittest_pb2.TestAllExtensions()
1136 proto2.Extensions[unittest_pb2.repeated_int32_extension].append(0)
1137 proto2.MergeFrom(proto1)
1138 self.assertEqual(
1139 3, len(proto2.Extensions[unittest_pb2.repeated_int32_extension]))
1140 self.assertEqual(
1141 0, proto2.Extensions[unittest_pb2.repeated_int32_extension][0])
1142 self.assertEqual(
1143 1, proto2.Extensions[unittest_pb2.repeated_int32_extension][1])
1144 self.assertEqual(
1145 2, proto2.Extensions[unittest_pb2.repeated_int32_extension][2])
1146
1147 def testMergeFromExtensionsNestedMessage(self):
1148 proto1 = unittest_pb2.TestAllExtensions()
1149 ext1 = proto1.Extensions[
1150 unittest_pb2.repeated_nested_message_extension]
1151 m = ext1.add()
1152 m.bb = 222
1153 m = ext1.add()
1154 m.bb = 333
1155
1156 proto2 = unittest_pb2.TestAllExtensions()
1157 ext2 = proto2.Extensions[
1158 unittest_pb2.repeated_nested_message_extension]
1159 m = ext2.add()
1160 m.bb = 111
1161
1162 proto2.MergeFrom(proto1)
1163 ext2 = proto2.Extensions[
1164 unittest_pb2.repeated_nested_message_extension]
1165 self.assertEqual(3, len(ext2))
1166 self.assertEqual(111, ext2[0].bb)
1167 self.assertEqual(222, ext2[1].bb)
1168 self.assertEqual(333, ext2[2].bb)
1169
kenton@google.com0c293de2010-07-27 20:45:09 +00001170 def testMergeFromBug(self):
1171 message1 = unittest_pb2.TestAllTypes()
1172 message2 = unittest_pb2.TestAllTypes()
kenton@google.com27028bc2010-07-27 21:19:59 +00001173
kenton@google.com0c293de2010-07-27 20:45:09 +00001174 # Cause optional_nested_message to be instantiated within message1, even
1175 # though it is not considered to be "present".
1176 message1.optional_nested_message
1177 self.assertFalse(message1.HasField('optional_nested_message'))
kenton@google.com27028bc2010-07-27 21:19:59 +00001178
kenton@google.com0c293de2010-07-27 20:45:09 +00001179 # Merge into message2. This should not instantiate the field is message2.
1180 message2.MergeFrom(message1)
1181 self.assertFalse(message2.HasField('optional_nested_message'))
1182
temporal779f61c2008-08-13 03:15:00 +00001183 def testCopyFromSingularField(self):
1184 # Test copy with just a singular field.
1185 proto1 = unittest_pb2.TestAllTypes()
1186 proto1.optional_int32 = 1
1187 proto1.optional_string = 'important-text'
1188
1189 proto2 = unittest_pb2.TestAllTypes()
1190 proto2.optional_string = 'value'
1191
1192 proto2.CopyFrom(proto1)
1193 self.assertEqual(1, proto2.optional_int32)
1194 self.assertEqual('important-text', proto2.optional_string)
1195
1196 def testCopyFromRepeatedField(self):
1197 # Test copy with a repeated field.
1198 proto1 = unittest_pb2.TestAllTypes()
1199 proto1.repeated_int32.append(1)
1200 proto1.repeated_int32.append(2)
1201
1202 proto2 = unittest_pb2.TestAllTypes()
1203 proto2.repeated_int32.append(0)
1204 proto2.CopyFrom(proto1)
1205
1206 self.assertEqual(1, proto2.repeated_int32[0])
1207 self.assertEqual(2, proto2.repeated_int32[1])
1208
1209 def testCopyFromAllFields(self):
1210 # With all fields set.
1211 proto1 = unittest_pb2.TestAllTypes()
1212 test_util.SetAllFields(proto1)
1213 proto2 = unittest_pb2.TestAllTypes()
1214 proto2.CopyFrom(proto1)
1215
1216 # Messages should be equal.
1217 self.assertEqual(proto2, proto1)
1218
1219 # Serialized string should be equal too.
1220 string1 = proto1.SerializeToString()
1221 string2 = proto2.SerializeToString()
1222 self.assertEqual(string1, string2)
1223
1224 def testCopyFromSelf(self):
1225 proto1 = unittest_pb2.TestAllTypes()
1226 proto1.repeated_int32.append(1)
1227 proto1.optional_int32 = 2
1228 proto1.optional_string = 'important-text'
1229
1230 proto1.CopyFrom(proto1)
1231 self.assertEqual(1, proto1.repeated_int32[0])
1232 self.assertEqual(2, proto1.optional_int32)
1233 self.assertEqual('important-text', proto1.optional_string)
temporal40ee5512008-07-10 02:12:20 +00001234
liujisi@google.com33165fe2010-11-02 13:14:58 +00001235 def testCopyFromBadType(self):
1236 # The python implementation doesn't raise an exception in this
1237 # case. In theory it should.
1238 if api_implementation.Type() == 'python':
1239 return
1240 proto1 = unittest_pb2.TestAllTypes()
1241 proto2 = unittest_pb2.TestAllExtensions()
1242 self.assertRaises(TypeError, proto1.CopyFrom, proto2)
1243
temporal40ee5512008-07-10 02:12:20 +00001244 def testClear(self):
1245 proto = unittest_pb2.TestAllTypes()
1246 test_util.SetAllFields(proto)
1247 # Clear the message.
1248 proto.Clear()
1249 self.assertEquals(proto.ByteSize(), 0)
1250 empty_proto = unittest_pb2.TestAllTypes()
1251 self.assertEquals(proto, empty_proto)
1252
1253 # Test if extensions which were set are cleared.
1254 proto = unittest_pb2.TestAllExtensions()
1255 test_util.SetAllExtensions(proto)
1256 # Clear the message.
1257 proto.Clear()
1258 self.assertEquals(proto.ByteSize(), 0)
1259 empty_proto = unittest_pb2.TestAllExtensions()
1260 self.assertEquals(proto, empty_proto)
1261
kenton@google.comfccb1462009-12-18 02:11:36 +00001262 def assertInitialized(self, proto):
1263 self.assertTrue(proto.IsInitialized())
1264 # Neither method should raise an exception.
1265 proto.SerializeToString()
1266 proto.SerializePartialToString()
1267
1268 def assertNotInitialized(self, proto):
1269 self.assertFalse(proto.IsInitialized())
1270 self.assertRaises(message.EncodeError, proto.SerializeToString)
1271 # "Partial" serialization doesn't care if message is uninitialized.
1272 proto.SerializePartialToString()
1273
temporal40ee5512008-07-10 02:12:20 +00001274 def testIsInitialized(self):
1275 # Trivial cases - all optional fields and extensions.
1276 proto = unittest_pb2.TestAllTypes()
kenton@google.comfccb1462009-12-18 02:11:36 +00001277 self.assertInitialized(proto)
temporal40ee5512008-07-10 02:12:20 +00001278 proto = unittest_pb2.TestAllExtensions()
kenton@google.comfccb1462009-12-18 02:11:36 +00001279 self.assertInitialized(proto)
temporal40ee5512008-07-10 02:12:20 +00001280
1281 # The case of uninitialized required fields.
1282 proto = unittest_pb2.TestRequired()
kenton@google.comfccb1462009-12-18 02:11:36 +00001283 self.assertNotInitialized(proto)
temporal40ee5512008-07-10 02:12:20 +00001284 proto.a = proto.b = proto.c = 2
kenton@google.comfccb1462009-12-18 02:11:36 +00001285 self.assertInitialized(proto)
temporal40ee5512008-07-10 02:12:20 +00001286
1287 # The case of uninitialized submessage.
1288 proto = unittest_pb2.TestRequiredForeign()
kenton@google.comfccb1462009-12-18 02:11:36 +00001289 self.assertInitialized(proto)
temporal40ee5512008-07-10 02:12:20 +00001290 proto.optional_message.a = 1
kenton@google.comfccb1462009-12-18 02:11:36 +00001291 self.assertNotInitialized(proto)
temporal40ee5512008-07-10 02:12:20 +00001292 proto.optional_message.b = 0
1293 proto.optional_message.c = 0
kenton@google.comfccb1462009-12-18 02:11:36 +00001294 self.assertInitialized(proto)
temporal40ee5512008-07-10 02:12:20 +00001295
1296 # Uninitialized repeated submessage.
1297 message1 = proto.repeated_message.add()
kenton@google.comfccb1462009-12-18 02:11:36 +00001298 self.assertNotInitialized(proto)
temporal40ee5512008-07-10 02:12:20 +00001299 message1.a = message1.b = message1.c = 0
kenton@google.comfccb1462009-12-18 02:11:36 +00001300 self.assertInitialized(proto)
temporal40ee5512008-07-10 02:12:20 +00001301
1302 # Uninitialized repeated group in an extension.
1303 proto = unittest_pb2.TestAllExtensions()
1304 extension = unittest_pb2.TestRequired.multi
1305 message1 = proto.Extensions[extension].add()
1306 message2 = proto.Extensions[extension].add()
kenton@google.comfccb1462009-12-18 02:11:36 +00001307 self.assertNotInitialized(proto)
temporal40ee5512008-07-10 02:12:20 +00001308 message1.a = 1
1309 message1.b = 1
1310 message1.c = 1
kenton@google.comfccb1462009-12-18 02:11:36 +00001311 self.assertNotInitialized(proto)
temporal40ee5512008-07-10 02:12:20 +00001312 message2.a = 2
1313 message2.b = 2
1314 message2.c = 2
kenton@google.comfccb1462009-12-18 02:11:36 +00001315 self.assertInitialized(proto)
temporal40ee5512008-07-10 02:12:20 +00001316
1317 # Uninitialized nonrepeated message in an extension.
1318 proto = unittest_pb2.TestAllExtensions()
1319 extension = unittest_pb2.TestRequired.single
1320 proto.Extensions[extension].a = 1
kenton@google.comfccb1462009-12-18 02:11:36 +00001321 self.assertNotInitialized(proto)
temporal40ee5512008-07-10 02:12:20 +00001322 proto.Extensions[extension].b = 2
1323 proto.Extensions[extension].c = 3
kenton@google.comfccb1462009-12-18 02:11:36 +00001324 self.assertInitialized(proto)
1325
1326 # Try passing an errors list.
1327 errors = []
1328 proto = unittest_pb2.TestRequired()
1329 self.assertFalse(proto.IsInitialized(errors))
1330 self.assertEqual(errors, ['a', 'b', 'c'])
temporal40ee5512008-07-10 02:12:20 +00001331
kenton@google.com24bf56f2008-09-24 20:31:01 +00001332 def testStringUTF8Encoding(self):
1333 proto = unittest_pb2.TestAllTypes()
1334
1335 # Assignment of a unicode object to a field of type 'bytes' is not allowed.
1336 self.assertRaises(TypeError,
1337 setattr, proto, 'optional_bytes', u'unicode object')
1338
1339 # Check that the default value is of python's 'unicode' type.
1340 self.assertEqual(type(proto.optional_string), unicode)
1341
1342 proto.optional_string = unicode('Testing')
1343 self.assertEqual(proto.optional_string, str('Testing'))
1344
1345 # Assign a value of type 'str' which can be encoded in UTF-8.
1346 proto.optional_string = str('Testing')
1347 self.assertEqual(proto.optional_string, unicode('Testing'))
1348
liujisi@google.com33165fe2010-11-02 13:14:58 +00001349 if api_implementation.Type() == 'python':
1350 # Values of type 'str' are also accepted as long as they can be
1351 # encoded in UTF-8.
1352 self.assertEqual(type(proto.optional_string), str)
kenton@google.com24bf56f2008-09-24 20:31:01 +00001353
1354 # Try to assign a 'str' value which contains bytes that aren't 7-bit ASCII.
1355 self.assertRaises(ValueError,
1356 setattr, proto, 'optional_string', str('a\x80a'))
1357 # Assign a 'str' object which contains a UTF-8 encoded string.
1358 self.assertRaises(ValueError,
1359 setattr, proto, 'optional_string', 'Тест')
1360 # No exception thrown.
1361 proto.optional_string = 'abc'
1362
1363 def testStringUTF8Serialization(self):
1364 proto = unittest_mset_pb2.TestMessageSet()
1365 extension_message = unittest_mset_pb2.TestMessageSetExtension2
1366 extension = extension_message.message_set_extension
1367
1368 test_utf8 = u'Тест'
1369 test_utf8_bytes = test_utf8.encode('utf-8')
1370
1371 # 'Test' in another language, using UTF-8 charset.
1372 proto.Extensions[extension].str = test_utf8
1373
1374 # Serialize using the MessageSet wire format (this is specified in the
1375 # .proto file).
1376 serialized = proto.SerializeToString()
1377
1378 # Check byte size.
1379 self.assertEqual(proto.ByteSize(), len(serialized))
1380
1381 raw = unittest_mset_pb2.RawMessageSet()
1382 raw.MergeFromString(serialized)
1383
1384 message2 = unittest_mset_pb2.TestMessageSetExtension2()
1385
1386 self.assertEqual(1, len(raw.item))
1387 # Check that the type_id is the same as the tag ID in the .proto file.
1388 self.assertEqual(raw.item[0].type_id, 1547769)
1389
liujisi@google.com33165fe2010-11-02 13:14:58 +00001390 # Check the actual bytes on the wire.
kenton@google.com24bf56f2008-09-24 20:31:01 +00001391 self.assertTrue(
1392 raw.item[0].message.endswith(test_utf8_bytes))
1393 message2.MergeFromString(raw.item[0].message)
1394
1395 self.assertEqual(type(message2.str), unicode)
1396 self.assertEqual(message2.str, test_utf8)
1397
liujisi@google.com33165fe2010-11-02 13:14:58 +00001398 # The pure Python API throws an exception on MergeFromString(),
1399 # if any of the string fields of the message can't be UTF-8 decoded.
1400 # The C++ implementation of the API has no way to check that on
1401 # MergeFromString and thus has no way to throw the exception.
1402 #
1403 # The pure Python API always returns objects of type 'unicode' (UTF-8
1404 # encoded), or 'str' (in 7 bit ASCII).
kenton@google.com24bf56f2008-09-24 20:31:01 +00001405 bytes = raw.item[0].message.replace(
1406 test_utf8_bytes, len(test_utf8_bytes) * '\xff')
liujisi@google.com33165fe2010-11-02 13:14:58 +00001407
1408 unicode_decode_failed = False
1409 try:
1410 message2.MergeFromString(bytes)
1411 except UnicodeDecodeError, e:
1412 unicode_decode_failed = True
1413 string_field = message2.str
1414 self.assertTrue(unicode_decode_failed or type(string_field) == str)
kenton@google.com24bf56f2008-09-24 20:31:01 +00001415
kenton@google.comfccb1462009-12-18 02:11:36 +00001416 def testEmptyNestedMessage(self):
1417 proto = unittest_pb2.TestAllTypes()
1418 proto.optional_nested_message.MergeFrom(
1419 unittest_pb2.TestAllTypes.NestedMessage())
1420 self.assertTrue(proto.HasField('optional_nested_message'))
1421
1422 proto = unittest_pb2.TestAllTypes()
1423 proto.optional_nested_message.CopyFrom(
1424 unittest_pb2.TestAllTypes.NestedMessage())
1425 self.assertTrue(proto.HasField('optional_nested_message'))
1426
1427 proto = unittest_pb2.TestAllTypes()
1428 proto.optional_nested_message.MergeFromString('')
1429 self.assertTrue(proto.HasField('optional_nested_message'))
1430
1431 proto = unittest_pb2.TestAllTypes()
1432 proto.optional_nested_message.ParseFromString('')
1433 self.assertTrue(proto.HasField('optional_nested_message'))
1434
1435 serialized = proto.SerializeToString()
1436 proto2 = unittest_pb2.TestAllTypes()
1437 proto2.MergeFromString(serialized)
1438 self.assertTrue(proto2.HasField('optional_nested_message'))
1439
1440 def testSetInParent(self):
1441 proto = unittest_pb2.TestAllTypes()
1442 self.assertFalse(proto.HasField('optionalgroup'))
1443 proto.optionalgroup.SetInParent()
1444 self.assertTrue(proto.HasField('optionalgroup'))
1445
temporal40ee5512008-07-10 02:12:20 +00001446
1447# Since we had so many tests for protocol buffer equality, we broke these out
1448# into separate TestCase classes.
1449
1450
1451class TestAllTypesEqualityTest(unittest.TestCase):
1452
1453 def setUp(self):
1454 self.first_proto = unittest_pb2.TestAllTypes()
1455 self.second_proto = unittest_pb2.TestAllTypes()
1456
liujisi@google.com33165fe2010-11-02 13:14:58 +00001457 def testNotHashable(self):
1458 self.assertRaises(TypeError, hash, self.first_proto)
1459
temporal40ee5512008-07-10 02:12:20 +00001460 def testSelfEquality(self):
1461 self.assertEqual(self.first_proto, self.first_proto)
1462
1463 def testEmptyProtosEqual(self):
1464 self.assertEqual(self.first_proto, self.second_proto)
1465
1466
1467class FullProtosEqualityTest(unittest.TestCase):
1468
1469 """Equality tests using completely-full protos as a starting point."""
1470
1471 def setUp(self):
1472 self.first_proto = unittest_pb2.TestAllTypes()
1473 self.second_proto = unittest_pb2.TestAllTypes()
1474 test_util.SetAllFields(self.first_proto)
1475 test_util.SetAllFields(self.second_proto)
1476
liujisi@google.com33165fe2010-11-02 13:14:58 +00001477 def testNotHashable(self):
1478 self.assertRaises(TypeError, hash, self.first_proto)
1479
kenton@google.comd37d46d2009-04-25 02:53:47 +00001480 def testNoneNotEqual(self):
1481 self.assertNotEqual(self.first_proto, None)
1482 self.assertNotEqual(None, self.second_proto)
1483
1484 def testNotEqualToOtherMessage(self):
1485 third_proto = unittest_pb2.TestRequired()
1486 self.assertNotEqual(self.first_proto, third_proto)
1487 self.assertNotEqual(third_proto, self.second_proto)
1488
temporal40ee5512008-07-10 02:12:20 +00001489 def testAllFieldsFilledEquality(self):
1490 self.assertEqual(self.first_proto, self.second_proto)
1491
1492 def testNonRepeatedScalar(self):
1493 # Nonrepeated scalar field change should cause inequality.
1494 self.first_proto.optional_int32 += 1
1495 self.assertNotEqual(self.first_proto, self.second_proto)
1496 # ...as should clearing a field.
1497 self.first_proto.ClearField('optional_int32')
1498 self.assertNotEqual(self.first_proto, self.second_proto)
1499
1500 def testNonRepeatedComposite(self):
1501 # Change a nonrepeated composite field.
1502 self.first_proto.optional_nested_message.bb += 1
1503 self.assertNotEqual(self.first_proto, self.second_proto)
1504 self.first_proto.optional_nested_message.bb -= 1
1505 self.assertEqual(self.first_proto, self.second_proto)
1506 # Clear a field in the nested message.
1507 self.first_proto.optional_nested_message.ClearField('bb')
1508 self.assertNotEqual(self.first_proto, self.second_proto)
1509 self.first_proto.optional_nested_message.bb = (
1510 self.second_proto.optional_nested_message.bb)
1511 self.assertEqual(self.first_proto, self.second_proto)
1512 # Remove the nested message entirely.
1513 self.first_proto.ClearField('optional_nested_message')
1514 self.assertNotEqual(self.first_proto, self.second_proto)
1515
1516 def testRepeatedScalar(self):
1517 # Change a repeated scalar field.
1518 self.first_proto.repeated_int32.append(5)
1519 self.assertNotEqual(self.first_proto, self.second_proto)
1520 self.first_proto.ClearField('repeated_int32')
1521 self.assertNotEqual(self.first_proto, self.second_proto)
1522
1523 def testRepeatedComposite(self):
1524 # Change value within a repeated composite field.
1525 self.first_proto.repeated_nested_message[0].bb += 1
1526 self.assertNotEqual(self.first_proto, self.second_proto)
1527 self.first_proto.repeated_nested_message[0].bb -= 1
1528 self.assertEqual(self.first_proto, self.second_proto)
1529 # Add a value to a repeated composite field.
1530 self.first_proto.repeated_nested_message.add()
1531 self.assertNotEqual(self.first_proto, self.second_proto)
1532 self.second_proto.repeated_nested_message.add()
1533 self.assertEqual(self.first_proto, self.second_proto)
1534
1535 def testNonRepeatedScalarHasBits(self):
1536 # Ensure that we test "has" bits as well as value for
1537 # nonrepeated scalar field.
1538 self.first_proto.ClearField('optional_int32')
1539 self.second_proto.optional_int32 = 0
1540 self.assertNotEqual(self.first_proto, self.second_proto)
1541
1542 def testNonRepeatedCompositeHasBits(self):
1543 # Ensure that we test "has" bits as well as value for
1544 # nonrepeated composite field.
1545 self.first_proto.ClearField('optional_nested_message')
1546 self.second_proto.optional_nested_message.ClearField('bb')
1547 self.assertNotEqual(self.first_proto, self.second_proto)
temporal40ee5512008-07-10 02:12:20 +00001548 self.first_proto.optional_nested_message.bb = 0
1549 self.first_proto.optional_nested_message.ClearField('bb')
1550 self.assertEqual(self.first_proto, self.second_proto)
1551
1552
1553class ExtensionEqualityTest(unittest.TestCase):
1554
1555 def testExtensionEquality(self):
1556 first_proto = unittest_pb2.TestAllExtensions()
1557 second_proto = unittest_pb2.TestAllExtensions()
1558 self.assertEqual(first_proto, second_proto)
1559 test_util.SetAllExtensions(first_proto)
1560 self.assertNotEqual(first_proto, second_proto)
1561 test_util.SetAllExtensions(second_proto)
1562 self.assertEqual(first_proto, second_proto)
1563
1564 # Ensure that we check value equality.
1565 first_proto.Extensions[unittest_pb2.optional_int32_extension] += 1
1566 self.assertNotEqual(first_proto, second_proto)
1567 first_proto.Extensions[unittest_pb2.optional_int32_extension] -= 1
1568 self.assertEqual(first_proto, second_proto)
1569
1570 # Ensure that we also look at "has" bits.
1571 first_proto.ClearExtension(unittest_pb2.optional_int32_extension)
1572 second_proto.Extensions[unittest_pb2.optional_int32_extension] = 0
1573 self.assertNotEqual(first_proto, second_proto)
1574 first_proto.Extensions[unittest_pb2.optional_int32_extension] = 0
1575 self.assertEqual(first_proto, second_proto)
1576
1577 # Ensure that differences in cached values
1578 # don't matter if "has" bits are both false.
1579 first_proto = unittest_pb2.TestAllExtensions()
1580 second_proto = unittest_pb2.TestAllExtensions()
1581 self.assertEqual(
1582 0, first_proto.Extensions[unittest_pb2.optional_int32_extension])
1583 self.assertEqual(first_proto, second_proto)
1584
1585
1586class MutualRecursionEqualityTest(unittest.TestCase):
1587
1588 def testEqualityWithMutualRecursion(self):
1589 first_proto = unittest_pb2.TestMutualRecursionA()
1590 second_proto = unittest_pb2.TestMutualRecursionA()
1591 self.assertEqual(first_proto, second_proto)
1592 first_proto.bb.a.bb.optional_int32 = 23
1593 self.assertNotEqual(first_proto, second_proto)
1594 second_proto.bb.a.bb.optional_int32 = 23
1595 self.assertEqual(first_proto, second_proto)
1596
1597
1598class ByteSizeTest(unittest.TestCase):
1599
1600 def setUp(self):
1601 self.proto = unittest_pb2.TestAllTypes()
1602 self.extended_proto = more_extensions_pb2.ExtendedMessage()
kenton@google.com2d6daa72009-01-22 01:27:00 +00001603 self.packed_proto = unittest_pb2.TestPackedTypes()
1604 self.packed_extended_proto = unittest_pb2.TestPackedExtensions()
temporal40ee5512008-07-10 02:12:20 +00001605
1606 def Size(self):
1607 return self.proto.ByteSize()
1608
1609 def testEmptyMessage(self):
1610 self.assertEqual(0, self.proto.ByteSize())
1611
liujisi@google.com33165fe2010-11-02 13:14:58 +00001612 def testSizedOnKwargs(self):
1613 # Use a separate message to ensure testing right after creation.
1614 proto = unittest_pb2.TestAllTypes()
1615 self.assertEqual(0, proto.ByteSize())
1616 proto_kwargs = unittest_pb2.TestAllTypes(optional_int64 = 1)
1617 # One byte for the tag, one to encode varint 1.
1618 self.assertEqual(2, proto_kwargs.ByteSize())
1619
temporal40ee5512008-07-10 02:12:20 +00001620 def testVarints(self):
1621 def Test(i, expected_varint_size):
1622 self.proto.Clear()
1623 self.proto.optional_int64 = i
1624 # Add one to the varint size for the tag info
1625 # for tag 1.
1626 self.assertEqual(expected_varint_size + 1, self.Size())
1627 Test(0, 1)
1628 Test(1, 1)
1629 for i, num_bytes in zip(range(7, 63, 7), range(1, 10000)):
1630 Test((1 << i) - 1, num_bytes)
1631 Test(-1, 10)
1632 Test(-2, 10)
1633 Test(-(1 << 63), 10)
1634
1635 def testStrings(self):
1636 self.proto.optional_string = ''
1637 # Need one byte for tag info (tag #14), and one byte for length.
1638 self.assertEqual(2, self.Size())
1639
1640 self.proto.optional_string = 'abc'
1641 # Need one byte for tag info (tag #14), and one byte for length.
1642 self.assertEqual(2 + len(self.proto.optional_string), self.Size())
1643
1644 self.proto.optional_string = 'x' * 128
1645 # Need one byte for tag info (tag #14), and TWO bytes for length.
1646 self.assertEqual(3 + len(self.proto.optional_string), self.Size())
1647
1648 def testOtherNumerics(self):
1649 self.proto.optional_fixed32 = 1234
1650 # One byte for tag and 4 bytes for fixed32.
1651 self.assertEqual(5, self.Size())
1652 self.proto = unittest_pb2.TestAllTypes()
1653
1654 self.proto.optional_fixed64 = 1234
1655 # One byte for tag and 8 bytes for fixed64.
1656 self.assertEqual(9, self.Size())
1657 self.proto = unittest_pb2.TestAllTypes()
1658
1659 self.proto.optional_float = 1.234
1660 # One byte for tag and 4 bytes for float.
1661 self.assertEqual(5, self.Size())
1662 self.proto = unittest_pb2.TestAllTypes()
1663
1664 self.proto.optional_double = 1.234
1665 # One byte for tag and 8 bytes for float.
1666 self.assertEqual(9, self.Size())
1667 self.proto = unittest_pb2.TestAllTypes()
1668
1669 self.proto.optional_sint32 = 64
1670 # One byte for tag and 2 bytes for zig-zag-encoded 64.
1671 self.assertEqual(3, self.Size())
1672 self.proto = unittest_pb2.TestAllTypes()
1673
1674 def testComposites(self):
1675 # 3 bytes.
1676 self.proto.optional_nested_message.bb = (1 << 14)
1677 # Plus one byte for bb tag.
1678 # Plus 1 byte for optional_nested_message serialized size.
1679 # Plus two bytes for optional_nested_message tag.
1680 self.assertEqual(3 + 1 + 1 + 2, self.Size())
1681
1682 def testGroups(self):
1683 # 4 bytes.
1684 self.proto.optionalgroup.a = (1 << 21)
1685 # Plus two bytes for |a| tag.
1686 # Plus 2 * two bytes for START_GROUP and END_GROUP tags.
1687 self.assertEqual(4 + 2 + 2*2, self.Size())
1688
1689 def testRepeatedScalars(self):
1690 self.proto.repeated_int32.append(10) # 1 byte.
1691 self.proto.repeated_int32.append(128) # 2 bytes.
1692 # Also need 2 bytes for each entry for tag.
1693 self.assertEqual(1 + 2 + 2*2, self.Size())
1694
kenton@google.com2d6daa72009-01-22 01:27:00 +00001695 def testRepeatedScalarsExtend(self):
1696 self.proto.repeated_int32.extend([10, 128]) # 3 bytes.
1697 # Also need 2 bytes for each entry for tag.
1698 self.assertEqual(1 + 2 + 2*2, self.Size())
1699
kenton@google.com24bf56f2008-09-24 20:31:01 +00001700 def testRepeatedScalarsRemove(self):
1701 self.proto.repeated_int32.append(10) # 1 byte.
1702 self.proto.repeated_int32.append(128) # 2 bytes.
1703 # Also need 2 bytes for each entry for tag.
1704 self.assertEqual(1 + 2 + 2*2, self.Size())
1705 self.proto.repeated_int32.remove(128)
1706 self.assertEqual(1 + 2, self.Size())
1707
temporal40ee5512008-07-10 02:12:20 +00001708 def testRepeatedComposites(self):
1709 # Empty message. 2 bytes tag plus 1 byte length.
1710 foreign_message_0 = self.proto.repeated_nested_message.add()
1711 # 2 bytes tag plus 1 byte length plus 1 byte bb tag 1 byte int.
1712 foreign_message_1 = self.proto.repeated_nested_message.add()
1713 foreign_message_1.bb = 7
1714 self.assertEqual(2 + 1 + 2 + 1 + 1 + 1, self.Size())
1715
kenton@google.com24bf56f2008-09-24 20:31:01 +00001716 def testRepeatedCompositesDelete(self):
1717 # Empty message. 2 bytes tag plus 1 byte length.
1718 foreign_message_0 = self.proto.repeated_nested_message.add()
1719 # 2 bytes tag plus 1 byte length plus 1 byte bb tag 1 byte int.
1720 foreign_message_1 = self.proto.repeated_nested_message.add()
1721 foreign_message_1.bb = 9
1722 self.assertEqual(2 + 1 + 2 + 1 + 1 + 1, self.Size())
1723
1724 # 2 bytes tag plus 1 byte length plus 1 byte bb tag 1 byte int.
1725 del self.proto.repeated_nested_message[0]
1726 self.assertEqual(2 + 1 + 1 + 1, self.Size())
1727
1728 # Now add a new message.
1729 foreign_message_2 = self.proto.repeated_nested_message.add()
1730 foreign_message_2.bb = 12
1731
1732 # 2 bytes tag plus 1 byte length plus 1 byte bb tag 1 byte int.
1733 # 2 bytes tag plus 1 byte length plus 1 byte bb tag 1 byte int.
1734 self.assertEqual(2 + 1 + 1 + 1 + 2 + 1 + 1 + 1, self.Size())
1735
1736 # 2 bytes tag plus 1 byte length plus 1 byte bb tag 1 byte int.
1737 del self.proto.repeated_nested_message[1]
1738 self.assertEqual(2 + 1 + 1 + 1, self.Size())
1739
1740 del self.proto.repeated_nested_message[0]
1741 self.assertEqual(0, self.Size())
1742
temporal40ee5512008-07-10 02:12:20 +00001743 def testRepeatedGroups(self):
1744 # 2-byte START_GROUP plus 2-byte END_GROUP.
1745 group_0 = self.proto.repeatedgroup.add()
1746 # 2-byte START_GROUP plus 2-byte |a| tag + 1-byte |a|
1747 # plus 2-byte END_GROUP.
1748 group_1 = self.proto.repeatedgroup.add()
1749 group_1.a = 7
1750 self.assertEqual(2 + 2 + 2 + 2 + 1 + 2, self.Size())
1751
1752 def testExtensions(self):
1753 proto = unittest_pb2.TestAllExtensions()
1754 self.assertEqual(0, proto.ByteSize())
1755 extension = unittest_pb2.optional_int32_extension # Field #1, 1 byte.
1756 proto.Extensions[extension] = 23
1757 # 1 byte for tag, 1 byte for value.
1758 self.assertEqual(2, proto.ByteSize())
1759
1760 def testCacheInvalidationForNonrepeatedScalar(self):
1761 # Test non-extension.
1762 self.proto.optional_int32 = 1
1763 self.assertEqual(2, self.proto.ByteSize())
1764 self.proto.optional_int32 = 128
1765 self.assertEqual(3, self.proto.ByteSize())
1766 self.proto.ClearField('optional_int32')
1767 self.assertEqual(0, self.proto.ByteSize())
1768
1769 # Test within extension.
1770 extension = more_extensions_pb2.optional_int_extension
1771 self.extended_proto.Extensions[extension] = 1
1772 self.assertEqual(2, self.extended_proto.ByteSize())
1773 self.extended_proto.Extensions[extension] = 128
1774 self.assertEqual(3, self.extended_proto.ByteSize())
1775 self.extended_proto.ClearExtension(extension)
1776 self.assertEqual(0, self.extended_proto.ByteSize())
1777
1778 def testCacheInvalidationForRepeatedScalar(self):
1779 # Test non-extension.
1780 self.proto.repeated_int32.append(1)
1781 self.assertEqual(3, self.proto.ByteSize())
1782 self.proto.repeated_int32.append(1)
1783 self.assertEqual(6, self.proto.ByteSize())
1784 self.proto.repeated_int32[1] = 128
1785 self.assertEqual(7, self.proto.ByteSize())
1786 self.proto.ClearField('repeated_int32')
1787 self.assertEqual(0, self.proto.ByteSize())
1788
1789 # Test within extension.
1790 extension = more_extensions_pb2.repeated_int_extension
1791 repeated = self.extended_proto.Extensions[extension]
1792 repeated.append(1)
1793 self.assertEqual(2, self.extended_proto.ByteSize())
1794 repeated.append(1)
1795 self.assertEqual(4, self.extended_proto.ByteSize())
1796 repeated[1] = 128
1797 self.assertEqual(5, self.extended_proto.ByteSize())
1798 self.extended_proto.ClearExtension(extension)
1799 self.assertEqual(0, self.extended_proto.ByteSize())
1800
1801 def testCacheInvalidationForNonrepeatedMessage(self):
1802 # Test non-extension.
1803 self.proto.optional_foreign_message.c = 1
1804 self.assertEqual(5, self.proto.ByteSize())
1805 self.proto.optional_foreign_message.c = 128
1806 self.assertEqual(6, self.proto.ByteSize())
1807 self.proto.optional_foreign_message.ClearField('c')
1808 self.assertEqual(3, self.proto.ByteSize())
1809 self.proto.ClearField('optional_foreign_message')
1810 self.assertEqual(0, self.proto.ByteSize())
liujisi@google.com33165fe2010-11-02 13:14:58 +00001811
1812 if api_implementation.Type() == 'python':
1813 # This is only possible in pure-Python implementation of the API.
1814 child = self.proto.optional_foreign_message
1815 self.proto.ClearField('optional_foreign_message')
1816 child.c = 128
1817 self.assertEqual(0, self.proto.ByteSize())
temporal40ee5512008-07-10 02:12:20 +00001818
1819 # Test within extension.
1820 extension = more_extensions_pb2.optional_message_extension
1821 child = self.extended_proto.Extensions[extension]
1822 self.assertEqual(0, self.extended_proto.ByteSize())
1823 child.foreign_message_int = 1
1824 self.assertEqual(4, self.extended_proto.ByteSize())
1825 child.foreign_message_int = 128
1826 self.assertEqual(5, self.extended_proto.ByteSize())
1827 self.extended_proto.ClearExtension(extension)
1828 self.assertEqual(0, self.extended_proto.ByteSize())
1829
1830 def testCacheInvalidationForRepeatedMessage(self):
1831 # Test non-extension.
1832 child0 = self.proto.repeated_foreign_message.add()
1833 self.assertEqual(3, self.proto.ByteSize())
1834 self.proto.repeated_foreign_message.add()
1835 self.assertEqual(6, self.proto.ByteSize())
1836 child0.c = 1
1837 self.assertEqual(8, self.proto.ByteSize())
1838 self.proto.ClearField('repeated_foreign_message')
1839 self.assertEqual(0, self.proto.ByteSize())
1840
1841 # Test within extension.
1842 extension = more_extensions_pb2.repeated_message_extension
1843 child_list = self.extended_proto.Extensions[extension]
1844 child0 = child_list.add()
1845 self.assertEqual(2, self.extended_proto.ByteSize())
1846 child_list.add()
1847 self.assertEqual(4, self.extended_proto.ByteSize())
1848 child0.foreign_message_int = 1
1849 self.assertEqual(6, self.extended_proto.ByteSize())
1850 child0.ClearField('foreign_message_int')
1851 self.assertEqual(4, self.extended_proto.ByteSize())
1852 self.extended_proto.ClearExtension(extension)
1853 self.assertEqual(0, self.extended_proto.ByteSize())
1854
kenton@google.com2d6daa72009-01-22 01:27:00 +00001855 def testPackedRepeatedScalars(self):
1856 self.assertEqual(0, self.packed_proto.ByteSize())
1857
1858 self.packed_proto.packed_int32.append(10) # 1 byte.
1859 self.packed_proto.packed_int32.append(128) # 2 bytes.
1860 # The tag is 2 bytes (the field number is 90), and the varint
1861 # storing the length is 1 byte.
1862 int_size = 1 + 2 + 3
1863 self.assertEqual(int_size, self.packed_proto.ByteSize())
1864
1865 self.packed_proto.packed_double.append(4.2) # 8 bytes
1866 self.packed_proto.packed_double.append(3.25) # 8 bytes
1867 # 2 more tag bytes, 1 more length byte.
1868 double_size = 8 + 8 + 3
1869 self.assertEqual(int_size+double_size, self.packed_proto.ByteSize())
1870
1871 self.packed_proto.ClearField('packed_int32')
1872 self.assertEqual(double_size, self.packed_proto.ByteSize())
1873
1874 def testPackedExtensions(self):
1875 self.assertEqual(0, self.packed_extended_proto.ByteSize())
1876 extension = self.packed_extended_proto.Extensions[
1877 unittest_pb2.packed_fixed32_extension]
1878 extension.extend([1, 2, 3, 4]) # 16 bytes
1879 # Tag is 3 bytes.
1880 self.assertEqual(19, self.packed_extended_proto.ByteSize())
1881
temporal40ee5512008-07-10 02:12:20 +00001882
temporal40ee5512008-07-10 02:12:20 +00001883# Issues to be sure to cover include:
1884# * Handling of unrecognized tags ("uninterpreted_bytes").
1885# * Handling of MessageSets.
1886# * Consistent ordering of tags in the wire format,
1887# including ordering between extensions and non-extension
1888# fields.
1889# * Consistent serialization of negative numbers, especially
1890# negative int32s.
1891# * Handling of empty submessages (with and without "has"
1892# bits set).
1893
1894class SerializationTest(unittest.TestCase):
1895
1896 def testSerializeEmtpyMessage(self):
1897 first_proto = unittest_pb2.TestAllTypes()
1898 second_proto = unittest_pb2.TestAllTypes()
1899 serialized = first_proto.SerializeToString()
1900 self.assertEqual(first_proto.ByteSize(), len(serialized))
1901 second_proto.MergeFromString(serialized)
1902 self.assertEqual(first_proto, second_proto)
1903
1904 def testSerializeAllFields(self):
1905 first_proto = unittest_pb2.TestAllTypes()
1906 second_proto = unittest_pb2.TestAllTypes()
1907 test_util.SetAllFields(first_proto)
1908 serialized = first_proto.SerializeToString()
1909 self.assertEqual(first_proto.ByteSize(), len(serialized))
1910 second_proto.MergeFromString(serialized)
1911 self.assertEqual(first_proto, second_proto)
1912
1913 def testSerializeAllExtensions(self):
1914 first_proto = unittest_pb2.TestAllExtensions()
1915 second_proto = unittest_pb2.TestAllExtensions()
1916 test_util.SetAllExtensions(first_proto)
1917 serialized = first_proto.SerializeToString()
1918 second_proto.MergeFromString(serialized)
1919 self.assertEqual(first_proto, second_proto)
1920
kenton@google.comfccb1462009-12-18 02:11:36 +00001921 def testSerializeNegativeValues(self):
1922 first_proto = unittest_pb2.TestAllTypes()
1923
1924 first_proto.optional_int32 = -1
1925 first_proto.optional_int64 = -(2 << 40)
1926 first_proto.optional_sint32 = -3
1927 first_proto.optional_sint64 = -(4 << 40)
1928 first_proto.optional_sfixed32 = -5
1929 first_proto.optional_sfixed64 = -(6 << 40)
1930
1931 second_proto = unittest_pb2.TestAllTypes.FromString(
1932 first_proto.SerializeToString())
1933
1934 self.assertEqual(first_proto, second_proto)
1935
1936 def testParseTruncated(self):
liujisi@google.com33165fe2010-11-02 13:14:58 +00001937 # This test is only applicable for the Python implementation of the API.
1938 if api_implementation.Type() != 'python':
1939 return
1940
kenton@google.comfccb1462009-12-18 02:11:36 +00001941 first_proto = unittest_pb2.TestAllTypes()
1942 test_util.SetAllFields(first_proto)
1943 serialized = first_proto.SerializeToString()
1944
1945 for truncation_point in xrange(len(serialized) + 1):
1946 try:
1947 second_proto = unittest_pb2.TestAllTypes()
1948 unknown_fields = unittest_pb2.TestEmptyMessage()
1949 pos = second_proto._InternalParse(serialized, 0, truncation_point)
1950 # If we didn't raise an error then we read exactly the amount expected.
1951 self.assertEqual(truncation_point, pos)
1952
1953 # Parsing to unknown fields should not throw if parsing to known fields
1954 # did not.
1955 try:
1956 pos2 = unknown_fields._InternalParse(serialized, 0, truncation_point)
1957 self.assertEqual(truncation_point, pos2)
1958 except message.DecodeError:
1959 self.fail('Parsing unknown fields failed when parsing known fields '
1960 'did not.')
1961 except message.DecodeError:
1962 # Parsing unknown fields should also fail.
1963 self.assertRaises(message.DecodeError, unknown_fields._InternalParse,
1964 serialized, 0, truncation_point)
1965
temporal40ee5512008-07-10 02:12:20 +00001966 def testCanonicalSerializationOrder(self):
1967 proto = more_messages_pb2.OutOfOrderFields()
1968 # These are also their tag numbers. Even though we're setting these in
1969 # reverse-tag order AND they're listed in reverse tag-order in the .proto
1970 # file, they should nonetheless be serialized in tag order.
1971 proto.optional_sint32 = 5
1972 proto.Extensions[more_messages_pb2.optional_uint64] = 4
1973 proto.optional_uint32 = 3
1974 proto.Extensions[more_messages_pb2.optional_int64] = 2
1975 proto.optional_int32 = 1
1976 serialized = proto.SerializeToString()
1977 self.assertEqual(proto.ByteSize(), len(serialized))
kenton@google.comfccb1462009-12-18 02:11:36 +00001978 d = _MiniDecoder(serialized)
temporal40ee5512008-07-10 02:12:20 +00001979 ReadTag = d.ReadFieldNumberAndWireType
1980 self.assertEqual((1, wire_format.WIRETYPE_VARINT), ReadTag())
1981 self.assertEqual(1, d.ReadInt32())
1982 self.assertEqual((2, wire_format.WIRETYPE_VARINT), ReadTag())
1983 self.assertEqual(2, d.ReadInt64())
1984 self.assertEqual((3, wire_format.WIRETYPE_VARINT), ReadTag())
1985 self.assertEqual(3, d.ReadUInt32())
1986 self.assertEqual((4, wire_format.WIRETYPE_VARINT), ReadTag())
1987 self.assertEqual(4, d.ReadUInt64())
1988 self.assertEqual((5, wire_format.WIRETYPE_VARINT), ReadTag())
1989 self.assertEqual(5, d.ReadSInt32())
1990
1991 def testCanonicalSerializationOrderSameAsCpp(self):
1992 # Copy of the same test we use for C++.
1993 proto = unittest_pb2.TestFieldOrderings()
1994 test_util.SetAllFieldsAndExtensions(proto)
1995 serialized = proto.SerializeToString()
1996 test_util.ExpectAllFieldsAndExtensionsInOrder(serialized)
1997
1998 def testMergeFromStringWhenFieldsAlreadySet(self):
1999 first_proto = unittest_pb2.TestAllTypes()
2000 first_proto.repeated_string.append('foobar')
2001 first_proto.optional_int32 = 23
2002 first_proto.optional_nested_message.bb = 42
2003 serialized = first_proto.SerializeToString()
2004
2005 second_proto = unittest_pb2.TestAllTypes()
2006 second_proto.repeated_string.append('baz')
2007 second_proto.optional_int32 = 100
2008 second_proto.optional_nested_message.bb = 999
2009
2010 second_proto.MergeFromString(serialized)
2011 # Ensure that we append to repeated fields.
2012 self.assertEqual(['baz', 'foobar'], list(second_proto.repeated_string))
2013 # Ensure that we overwrite nonrepeatd scalars.
2014 self.assertEqual(23, second_proto.optional_int32)
2015 # Ensure that we recursively call MergeFromString() on
2016 # submessages.
2017 self.assertEqual(42, second_proto.optional_nested_message.bb)
2018
2019 def testMessageSetWireFormat(self):
2020 proto = unittest_mset_pb2.TestMessageSet()
2021 extension_message1 = unittest_mset_pb2.TestMessageSetExtension1
2022 extension_message2 = unittest_mset_pb2.TestMessageSetExtension2
2023 extension1 = extension_message1.message_set_extension
2024 extension2 = extension_message2.message_set_extension
2025 proto.Extensions[extension1].i = 123
2026 proto.Extensions[extension2].str = 'foo'
2027
2028 # Serialize using the MessageSet wire format (this is specified in the
2029 # .proto file).
2030 serialized = proto.SerializeToString()
2031
2032 raw = unittest_mset_pb2.RawMessageSet()
2033 self.assertEqual(False,
2034 raw.DESCRIPTOR.GetOptions().message_set_wire_format)
2035 raw.MergeFromString(serialized)
2036 self.assertEqual(2, len(raw.item))
2037
2038 message1 = unittest_mset_pb2.TestMessageSetExtension1()
2039 message1.MergeFromString(raw.item[0].message)
2040 self.assertEqual(123, message1.i)
2041
2042 message2 = unittest_mset_pb2.TestMessageSetExtension2()
2043 message2.MergeFromString(raw.item[1].message)
2044 self.assertEqual('foo', message2.str)
2045
2046 # Deserialize using the MessageSet wire format.
2047 proto2 = unittest_mset_pb2.TestMessageSet()
2048 proto2.MergeFromString(serialized)
2049 self.assertEqual(123, proto2.Extensions[extension1].i)
2050 self.assertEqual('foo', proto2.Extensions[extension2].str)
2051
2052 # Check byte size.
2053 self.assertEqual(proto2.ByteSize(), len(serialized))
2054 self.assertEqual(proto.ByteSize(), len(serialized))
2055
2056 def testMessageSetWireFormatUnknownExtension(self):
2057 # Create a message using the message set wire format with an unknown
2058 # message.
2059 raw = unittest_mset_pb2.RawMessageSet()
2060
2061 # Add an item.
2062 item = raw.item.add()
2063 item.type_id = 1545008
2064 extension_message1 = unittest_mset_pb2.TestMessageSetExtension1
2065 message1 = unittest_mset_pb2.TestMessageSetExtension1()
2066 message1.i = 12345
2067 item.message = message1.SerializeToString()
2068
2069 # Add a second, unknown extension.
2070 item = raw.item.add()
2071 item.type_id = 1545009
2072 extension_message1 = unittest_mset_pb2.TestMessageSetExtension1
2073 message1 = unittest_mset_pb2.TestMessageSetExtension1()
2074 message1.i = 12346
2075 item.message = message1.SerializeToString()
2076
2077 # Add another unknown extension.
2078 item = raw.item.add()
2079 item.type_id = 1545010
2080 message1 = unittest_mset_pb2.TestMessageSetExtension2()
2081 message1.str = 'foo'
2082 item.message = message1.SerializeToString()
2083
2084 serialized = raw.SerializeToString()
2085
2086 # Parse message using the message set wire format.
2087 proto = unittest_mset_pb2.TestMessageSet()
2088 proto.MergeFromString(serialized)
2089
2090 # Check that the message parsed well.
2091 extension_message1 = unittest_mset_pb2.TestMessageSetExtension1
2092 extension1 = extension_message1.message_set_extension
2093 self.assertEquals(12345, proto.Extensions[extension1].i)
2094
2095 def testUnknownFields(self):
2096 proto = unittest_pb2.TestAllTypes()
2097 test_util.SetAllFields(proto)
2098
2099 serialized = proto.SerializeToString()
2100
2101 # The empty message should be parsable with all of the fields
2102 # unknown.
2103 proto2 = unittest_pb2.TestEmptyMessage()
2104
2105 # Parsing this message should succeed.
2106 proto2.MergeFromString(serialized)
2107
kenton@google.com24bf56f2008-09-24 20:31:01 +00002108 # Now test with a int64 field set.
2109 proto = unittest_pb2.TestAllTypes()
2110 proto.optional_int64 = 0x0fffffffffffffff
2111 serialized = proto.SerializeToString()
2112 # The empty message should be parsable with all of the fields
2113 # unknown.
2114 proto2 = unittest_pb2.TestEmptyMessage()
2115 # Parsing this message should succeed.
2116 proto2.MergeFromString(serialized)
2117
temporal779f61c2008-08-13 03:15:00 +00002118 def _CheckRaises(self, exc_class, callable_obj, exception):
2119 """This method checks if the excpetion type and message are as expected."""
2120 try:
2121 callable_obj()
2122 except exc_class, ex:
2123 # Check if the exception message is the right one.
2124 self.assertEqual(exception, str(ex))
2125 return
2126 else:
2127 raise self.failureException('%s not raised' % str(exc_class))
2128
2129 def testSerializeUninitialized(self):
2130 proto = unittest_pb2.TestRequired()
2131 self._CheckRaises(
2132 message.EncodeError,
2133 proto.SerializeToString,
kenton@google.comfccb1462009-12-18 02:11:36 +00002134 'Message is missing required fields: a,b,c')
temporal779f61c2008-08-13 03:15:00 +00002135 # Shouldn't raise exceptions.
2136 partial = proto.SerializePartialToString()
2137
2138 proto.a = 1
2139 self._CheckRaises(
2140 message.EncodeError,
2141 proto.SerializeToString,
kenton@google.comfccb1462009-12-18 02:11:36 +00002142 'Message is missing required fields: b,c')
temporal779f61c2008-08-13 03:15:00 +00002143 # Shouldn't raise exceptions.
2144 partial = proto.SerializePartialToString()
2145
2146 proto.b = 2
2147 self._CheckRaises(
2148 message.EncodeError,
2149 proto.SerializeToString,
kenton@google.comfccb1462009-12-18 02:11:36 +00002150 'Message is missing required fields: c')
temporal779f61c2008-08-13 03:15:00 +00002151 # Shouldn't raise exceptions.
2152 partial = proto.SerializePartialToString()
2153
2154 proto.c = 3
2155 serialized = proto.SerializeToString()
2156 # Shouldn't raise exceptions.
2157 partial = proto.SerializePartialToString()
2158
2159 proto2 = unittest_pb2.TestRequired()
2160 proto2.MergeFromString(serialized)
2161 self.assertEqual(1, proto2.a)
2162 self.assertEqual(2, proto2.b)
2163 self.assertEqual(3, proto2.c)
2164 proto2.ParseFromString(partial)
2165 self.assertEqual(1, proto2.a)
2166 self.assertEqual(2, proto2.b)
2167 self.assertEqual(3, proto2.c)
2168
kenton@google.comfccb1462009-12-18 02:11:36 +00002169 def testSerializeUninitializedSubMessage(self):
2170 proto = unittest_pb2.TestRequiredForeign()
2171
2172 # Sub-message doesn't exist yet, so this succeeds.
2173 proto.SerializeToString()
2174
2175 proto.optional_message.a = 1
2176 self._CheckRaises(
2177 message.EncodeError,
2178 proto.SerializeToString,
2179 'Message is missing required fields: '
2180 'optional_message.b,optional_message.c')
2181
2182 proto.optional_message.b = 2
2183 proto.optional_message.c = 3
2184 proto.SerializeToString()
2185
2186 proto.repeated_message.add().a = 1
2187 proto.repeated_message.add().b = 2
2188 self._CheckRaises(
2189 message.EncodeError,
2190 proto.SerializeToString,
2191 'Message is missing required fields: '
2192 'repeated_message[0].b,repeated_message[0].c,'
2193 'repeated_message[1].a,repeated_message[1].c')
2194
2195 proto.repeated_message[0].b = 2
2196 proto.repeated_message[0].c = 3
2197 proto.repeated_message[1].a = 1
2198 proto.repeated_message[1].c = 3
2199 proto.SerializeToString()
2200
kenton@google.com80b1d622009-07-29 01:13:20 +00002201 def testSerializeAllPackedFields(self):
kenton@google.com2d6daa72009-01-22 01:27:00 +00002202 first_proto = unittest_pb2.TestPackedTypes()
2203 second_proto = unittest_pb2.TestPackedTypes()
2204 test_util.SetAllPackedFields(first_proto)
2205 serialized = first_proto.SerializeToString()
2206 self.assertEqual(first_proto.ByteSize(), len(serialized))
kenton@google.com80b1d622009-07-29 01:13:20 +00002207 bytes_read = second_proto.MergeFromString(serialized)
2208 self.assertEqual(second_proto.ByteSize(), bytes_read)
kenton@google.com2d6daa72009-01-22 01:27:00 +00002209 self.assertEqual(first_proto, second_proto)
2210
2211 def testSerializeAllPackedExtensions(self):
2212 first_proto = unittest_pb2.TestPackedExtensions()
2213 second_proto = unittest_pb2.TestPackedExtensions()
2214 test_util.SetAllPackedExtensions(first_proto)
2215 serialized = first_proto.SerializeToString()
kenton@google.com80b1d622009-07-29 01:13:20 +00002216 bytes_read = second_proto.MergeFromString(serialized)
2217 self.assertEqual(second_proto.ByteSize(), bytes_read)
kenton@google.com2d6daa72009-01-22 01:27:00 +00002218 self.assertEqual(first_proto, second_proto)
2219
2220 def testMergePackedFromStringWhenSomeFieldsAlreadySet(self):
2221 first_proto = unittest_pb2.TestPackedTypes()
2222 first_proto.packed_int32.extend([1, 2])
2223 first_proto.packed_double.append(3.0)
2224 serialized = first_proto.SerializeToString()
2225
2226 second_proto = unittest_pb2.TestPackedTypes()
2227 second_proto.packed_int32.append(3)
2228 second_proto.packed_double.extend([1.0, 2.0])
2229 second_proto.packed_sint32.append(4)
2230
2231 second_proto.MergeFromString(serialized)
2232 self.assertEqual([3, 1, 2], second_proto.packed_int32)
2233 self.assertEqual([1.0, 2.0, 3.0], second_proto.packed_double)
2234 self.assertEqual([4], second_proto.packed_sint32)
2235
2236 def testPackedFieldsWireFormat(self):
2237 proto = unittest_pb2.TestPackedTypes()
2238 proto.packed_int32.extend([1, 2, 150, 3]) # 1 + 1 + 2 + 1 bytes
2239 proto.packed_double.extend([1.0, 1000.0]) # 8 + 8 bytes
2240 proto.packed_float.append(2.0) # 4 bytes, will be before double
2241 serialized = proto.SerializeToString()
2242 self.assertEqual(proto.ByteSize(), len(serialized))
kenton@google.comfccb1462009-12-18 02:11:36 +00002243 d = _MiniDecoder(serialized)
kenton@google.com2d6daa72009-01-22 01:27:00 +00002244 ReadTag = d.ReadFieldNumberAndWireType
2245 self.assertEqual((90, wire_format.WIRETYPE_LENGTH_DELIMITED), ReadTag())
2246 self.assertEqual(1+1+1+2, d.ReadInt32())
2247 self.assertEqual(1, d.ReadInt32())
2248 self.assertEqual(2, d.ReadInt32())
2249 self.assertEqual(150, d.ReadInt32())
2250 self.assertEqual(3, d.ReadInt32())
2251 self.assertEqual((100, wire_format.WIRETYPE_LENGTH_DELIMITED), ReadTag())
2252 self.assertEqual(4, d.ReadInt32())
2253 self.assertEqual(2.0, d.ReadFloat())
2254 self.assertEqual((101, wire_format.WIRETYPE_LENGTH_DELIMITED), ReadTag())
2255 self.assertEqual(8+8, d.ReadInt32())
2256 self.assertEqual(1.0, d.ReadDouble())
2257 self.assertEqual(1000.0, d.ReadDouble())
2258 self.assertTrue(d.EndOfStream())
2259
kenton@google.comfccb1462009-12-18 02:11:36 +00002260 def testParsePackedFromUnpacked(self):
2261 unpacked = unittest_pb2.TestUnpackedTypes()
2262 test_util.SetAllUnpackedFields(unpacked)
2263 packed = unittest_pb2.TestPackedTypes()
2264 packed.MergeFromString(unpacked.SerializeToString())
2265 expected = unittest_pb2.TestPackedTypes()
2266 test_util.SetAllPackedFields(expected)
2267 self.assertEqual(expected, packed)
2268
2269 def testParseUnpackedFromPacked(self):
2270 packed = unittest_pb2.TestPackedTypes()
2271 test_util.SetAllPackedFields(packed)
2272 unpacked = unittest_pb2.TestUnpackedTypes()
2273 unpacked.MergeFromString(packed.SerializeToString())
2274 expected = unittest_pb2.TestUnpackedTypes()
2275 test_util.SetAllUnpackedFields(expected)
2276 self.assertEqual(expected, unpacked)
2277
kenton@google.comcfa2d8a2009-04-18 00:02:12 +00002278 def testFieldNumbers(self):
2279 proto = unittest_pb2.TestAllTypes()
2280 self.assertEqual(unittest_pb2.TestAllTypes.NestedMessage.BB_FIELD_NUMBER, 1)
2281 self.assertEqual(unittest_pb2.TestAllTypes.OPTIONAL_INT32_FIELD_NUMBER, 1)
2282 self.assertEqual(unittest_pb2.TestAllTypes.OPTIONALGROUP_FIELD_NUMBER, 16)
2283 self.assertEqual(
2284 unittest_pb2.TestAllTypes.OPTIONAL_NESTED_MESSAGE_FIELD_NUMBER, 18)
2285 self.assertEqual(
2286 unittest_pb2.TestAllTypes.OPTIONAL_NESTED_ENUM_FIELD_NUMBER, 21)
2287 self.assertEqual(unittest_pb2.TestAllTypes.REPEATED_INT32_FIELD_NUMBER, 31)
2288 self.assertEqual(unittest_pb2.TestAllTypes.REPEATEDGROUP_FIELD_NUMBER, 46)
2289 self.assertEqual(
2290 unittest_pb2.TestAllTypes.REPEATED_NESTED_MESSAGE_FIELD_NUMBER, 48)
2291 self.assertEqual(
2292 unittest_pb2.TestAllTypes.REPEATED_NESTED_ENUM_FIELD_NUMBER, 51)
2293
2294 def testExtensionFieldNumbers(self):
2295 self.assertEqual(unittest_pb2.TestRequired.single.number, 1000)
2296 self.assertEqual(unittest_pb2.TestRequired.SINGLE_FIELD_NUMBER, 1000)
2297 self.assertEqual(unittest_pb2.TestRequired.multi.number, 1001)
2298 self.assertEqual(unittest_pb2.TestRequired.MULTI_FIELD_NUMBER, 1001)
2299 self.assertEqual(unittest_pb2.optional_int32_extension.number, 1)
2300 self.assertEqual(unittest_pb2.OPTIONAL_INT32_EXTENSION_FIELD_NUMBER, 1)
2301 self.assertEqual(unittest_pb2.optionalgroup_extension.number, 16)
2302 self.assertEqual(unittest_pb2.OPTIONALGROUP_EXTENSION_FIELD_NUMBER, 16)
2303 self.assertEqual(unittest_pb2.optional_nested_message_extension.number, 18)
2304 self.assertEqual(
2305 unittest_pb2.OPTIONAL_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER, 18)
2306 self.assertEqual(unittest_pb2.optional_nested_enum_extension.number, 21)
2307 self.assertEqual(unittest_pb2.OPTIONAL_NESTED_ENUM_EXTENSION_FIELD_NUMBER,
2308 21)
2309 self.assertEqual(unittest_pb2.repeated_int32_extension.number, 31)
2310 self.assertEqual(unittest_pb2.REPEATED_INT32_EXTENSION_FIELD_NUMBER, 31)
2311 self.assertEqual(unittest_pb2.repeatedgroup_extension.number, 46)
2312 self.assertEqual(unittest_pb2.REPEATEDGROUP_EXTENSION_FIELD_NUMBER, 46)
2313 self.assertEqual(unittest_pb2.repeated_nested_message_extension.number, 48)
2314 self.assertEqual(
2315 unittest_pb2.REPEATED_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER, 48)
2316 self.assertEqual(unittest_pb2.repeated_nested_enum_extension.number, 51)
2317 self.assertEqual(unittest_pb2.REPEATED_NESTED_ENUM_EXTENSION_FIELD_NUMBER,
2318 51)
temporal40ee5512008-07-10 02:12:20 +00002319
kenton@google.com80b1d622009-07-29 01:13:20 +00002320 def testInitKwargs(self):
2321 proto = unittest_pb2.TestAllTypes(
2322 optional_int32=1,
2323 optional_string='foo',
2324 optional_bool=True,
2325 optional_bytes='bar',
2326 optional_nested_message=unittest_pb2.TestAllTypes.NestedMessage(bb=1),
2327 optional_foreign_message=unittest_pb2.ForeignMessage(c=1),
2328 optional_nested_enum=unittest_pb2.TestAllTypes.FOO,
2329 optional_foreign_enum=unittest_pb2.FOREIGN_FOO,
2330 repeated_int32=[1, 2, 3])
2331 self.assertTrue(proto.IsInitialized())
2332 self.assertTrue(proto.HasField('optional_int32'))
2333 self.assertTrue(proto.HasField('optional_string'))
2334 self.assertTrue(proto.HasField('optional_bool'))
2335 self.assertTrue(proto.HasField('optional_bytes'))
2336 self.assertTrue(proto.HasField('optional_nested_message'))
2337 self.assertTrue(proto.HasField('optional_foreign_message'))
2338 self.assertTrue(proto.HasField('optional_nested_enum'))
2339 self.assertTrue(proto.HasField('optional_foreign_enum'))
2340 self.assertEqual(1, proto.optional_int32)
2341 self.assertEqual('foo', proto.optional_string)
2342 self.assertEqual(True, proto.optional_bool)
2343 self.assertEqual('bar', proto.optional_bytes)
2344 self.assertEqual(1, proto.optional_nested_message.bb)
2345 self.assertEqual(1, proto.optional_foreign_message.c)
2346 self.assertEqual(unittest_pb2.TestAllTypes.FOO,
2347 proto.optional_nested_enum)
2348 self.assertEqual(unittest_pb2.FOREIGN_FOO, proto.optional_foreign_enum)
2349 self.assertEqual([1, 2, 3], proto.repeated_int32)
2350
2351 def testInitArgsUnknownFieldName(self):
2352 def InitalizeEmptyMessageWithExtraKeywordArg():
2353 unused_proto = unittest_pb2.TestEmptyMessage(unknown='unknown')
2354 self._CheckRaises(ValueError,
2355 InitalizeEmptyMessageWithExtraKeywordArg,
2356 'Protocol message has no "unknown" field.')
2357
2358 def testInitRequiredKwargs(self):
2359 proto = unittest_pb2.TestRequired(a=1, b=1, c=1)
2360 self.assertTrue(proto.IsInitialized())
2361 self.assertTrue(proto.HasField('a'))
2362 self.assertTrue(proto.HasField('b'))
2363 self.assertTrue(proto.HasField('c'))
2364 self.assertTrue(not proto.HasField('dummy2'))
2365 self.assertEqual(1, proto.a)
2366 self.assertEqual(1, proto.b)
2367 self.assertEqual(1, proto.c)
2368
2369 def testInitRequiredForeignKwargs(self):
2370 proto = unittest_pb2.TestRequiredForeign(
2371 optional_message=unittest_pb2.TestRequired(a=1, b=1, c=1))
2372 self.assertTrue(proto.IsInitialized())
2373 self.assertTrue(proto.HasField('optional_message'))
2374 self.assertTrue(proto.optional_message.IsInitialized())
2375 self.assertTrue(proto.optional_message.HasField('a'))
2376 self.assertTrue(proto.optional_message.HasField('b'))
2377 self.assertTrue(proto.optional_message.HasField('c'))
2378 self.assertTrue(not proto.optional_message.HasField('dummy2'))
2379 self.assertEqual(unittest_pb2.TestRequired(a=1, b=1, c=1),
2380 proto.optional_message)
2381 self.assertEqual(1, proto.optional_message.a)
2382 self.assertEqual(1, proto.optional_message.b)
2383 self.assertEqual(1, proto.optional_message.c)
2384
2385 def testInitRepeatedKwargs(self):
2386 proto = unittest_pb2.TestAllTypes(repeated_int32=[1, 2, 3])
2387 self.assertTrue(proto.IsInitialized())
2388 self.assertEqual(1, proto.repeated_int32[0])
2389 self.assertEqual(2, proto.repeated_int32[1])
2390 self.assertEqual(3, proto.repeated_int32[2])
2391
2392
temporal40ee5512008-07-10 02:12:20 +00002393class OptionsTest(unittest.TestCase):
2394
2395 def testMessageOptions(self):
2396 proto = unittest_mset_pb2.TestMessageSet()
2397 self.assertEqual(True,
2398 proto.DESCRIPTOR.GetOptions().message_set_wire_format)
2399 proto = unittest_pb2.TestAllTypes()
2400 self.assertEqual(False,
2401 proto.DESCRIPTOR.GetOptions().message_set_wire_format)
2402
kenton@google.com2d6daa72009-01-22 01:27:00 +00002403 def testPackedOptions(self):
2404 proto = unittest_pb2.TestAllTypes()
2405 proto.optional_int32 = 1
2406 proto.optional_double = 3.0
2407 for field_descriptor, _ in proto.ListFields():
2408 self.assertEqual(False, field_descriptor.GetOptions().packed)
2409
2410 proto = unittest_pb2.TestPackedTypes()
2411 proto.packed_int32.append(1)
2412 proto.packed_double.append(3.0)
2413 for field_descriptor, _ in proto.ListFields():
2414 self.assertEqual(True, field_descriptor.GetOptions().packed)
2415 self.assertEqual(reflection._FieldDescriptor.LABEL_REPEATED,
2416 field_descriptor.label)
2417
temporal40ee5512008-07-10 02:12:20 +00002418
temporal40ee5512008-07-10 02:12:20 +00002419
2420if __name__ == '__main__':
2421 unittest.main()