1# Protocol Buffers - Google's data interchange format 2# Copyright 2008 Google Inc. All rights reserved. 3# https://developers.google.com/protocol-buffers/ 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are 7# met: 8# 9# * Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# * Redistributions in binary form must reproduce the above 12# copyright notice, this list of conditions and the following disclaimer 13# in the documentation and/or other materials provided with the 14# distribution. 15# * Neither the name of Google Inc. nor the names of its 16# contributors may be used to endorse or promote products derived from 17# this software without specific prior written permission. 18# 19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31"""Utilities for Python proto2 tests. 32 33This is intentionally modeled on C++ code in 34//google/protobuf/test_util.*. 35""" 36 37__author__ = 'robinson@google.com (Will Robinson)' 38 39import os.path 40 41from google.protobuf import unittest_import_pb2 42from google.protobuf import unittest_pb2 43 44 45def SetAllNonLazyFields(message): 46 """Sets every non-lazy field in the message to a unique value. 47 48 Args: 49 message: A unittest_pb2.TestAllTypes instance. 50 """ 51 52 # 53 # Optional fields. 54 # 55 56 message.optional_int32 = 101 57 message.optional_int64 = 102 58 message.optional_uint32 = 103 59 message.optional_uint64 = 104 60 message.optional_sint32 = 105 61 message.optional_sint64 = 106 62 message.optional_fixed32 = 107 63 message.optional_fixed64 = 108 64 message.optional_sfixed32 = 109 65 message.optional_sfixed64 = 110 66 message.optional_float = 111 67 message.optional_double = 112 68 message.optional_bool = True 69 message.optional_string = u'115' 70 message.optional_bytes = b'116' 71 72 message.optionalgroup.a = 117 73 message.optional_nested_message.bb = 118 74 message.optional_foreign_message.c = 119 75 message.optional_import_message.d = 120 76 message.optional_public_import_message.e = 126 77 78 message.optional_nested_enum = unittest_pb2.TestAllTypes.BAZ 79 message.optional_foreign_enum = unittest_pb2.FOREIGN_BAZ 80 message.optional_import_enum = unittest_import_pb2.IMPORT_BAZ 81 82 message.optional_string_piece = u'124' 83 message.optional_cord = u'125' 84 85 # 86 # Repeated fields. 87 # 88 89 message.repeated_int32.append(201) 90 message.repeated_int64.append(202) 91 message.repeated_uint32.append(203) 92 message.repeated_uint64.append(204) 93 message.repeated_sint32.append(205) 94 message.repeated_sint64.append(206) 95 message.repeated_fixed32.append(207) 96 message.repeated_fixed64.append(208) 97 message.repeated_sfixed32.append(209) 98 message.repeated_sfixed64.append(210) 99 message.repeated_float.append(211) 100 message.repeated_double.append(212) 101 message.repeated_bool.append(True) 102 message.repeated_string.append(u'215') 103 message.repeated_bytes.append(b'216') 104 105 message.repeatedgroup.add().a = 217 106 message.repeated_nested_message.add().bb = 218 107 message.repeated_foreign_message.add().c = 219 108 message.repeated_import_message.add().d = 220 109 message.repeated_lazy_message.add().bb = 227 110 111 message.repeated_nested_enum.append(unittest_pb2.TestAllTypes.BAR) 112 message.repeated_foreign_enum.append(unittest_pb2.FOREIGN_BAR) 113 message.repeated_import_enum.append(unittest_import_pb2.IMPORT_BAR) 114 115 message.repeated_string_piece.append(u'224') 116 message.repeated_cord.append(u'225') 117 118 # Add a second one of each field. 119 message.repeated_int32.append(301) 120 message.repeated_int64.append(302) 121 message.repeated_uint32.append(303) 122 message.repeated_uint64.append(304) 123 message.repeated_sint32.append(305) 124 message.repeated_sint64.append(306) 125 message.repeated_fixed32.append(307) 126 message.repeated_fixed64.append(308) 127 message.repeated_sfixed32.append(309) 128 message.repeated_sfixed64.append(310) 129 message.repeated_float.append(311) 130 message.repeated_double.append(312) 131 message.repeated_bool.append(False) 132 message.repeated_string.append(u'315') 133 message.repeated_bytes.append(b'316') 134 135 message.repeatedgroup.add().a = 317 136 message.repeated_nested_message.add().bb = 318 137 message.repeated_foreign_message.add().c = 319 138 message.repeated_import_message.add().d = 320 139 message.repeated_lazy_message.add().bb = 327 140 141 message.repeated_nested_enum.append(unittest_pb2.TestAllTypes.BAZ) 142 message.repeated_foreign_enum.append(unittest_pb2.FOREIGN_BAZ) 143 message.repeated_import_enum.append(unittest_import_pb2.IMPORT_BAZ) 144 145 message.repeated_string_piece.append(u'324') 146 message.repeated_cord.append(u'325') 147 148 # 149 # Fields that have defaults. 150 # 151 152 message.default_int32 = 401 153 message.default_int64 = 402 154 message.default_uint32 = 403 155 message.default_uint64 = 404 156 message.default_sint32 = 405 157 message.default_sint64 = 406 158 message.default_fixed32 = 407 159 message.default_fixed64 = 408 160 message.default_sfixed32 = 409 161 message.default_sfixed64 = 410 162 message.default_float = 411 163 message.default_double = 412 164 message.default_bool = False 165 message.default_string = '415' 166 message.default_bytes = b'416' 167 168 message.default_nested_enum = unittest_pb2.TestAllTypes.FOO 169 message.default_foreign_enum = unittest_pb2.FOREIGN_FOO 170 message.default_import_enum = unittest_import_pb2.IMPORT_FOO 171 172 message.default_string_piece = '424' 173 message.default_cord = '425' 174 175 message.oneof_uint32 = 601 176 message.oneof_nested_message.bb = 602 177 message.oneof_string = '603' 178 message.oneof_bytes = b'604' 179 180 181def SetAllFields(message): 182 SetAllNonLazyFields(message) 183 message.optional_lazy_message.bb = 127 184 185 186def SetAllExtensions(message): 187 """Sets every extension in the message to a unique value. 188 189 Args: 190 message: A unittest_pb2.TestAllExtensions instance. 191 """ 192 193 extensions = message.Extensions 194 pb2 = unittest_pb2 195 import_pb2 = unittest_import_pb2 196 197 # 198 # Optional fields. 199 # 200 201 extensions[pb2.optional_int32_extension] = 101 202 extensions[pb2.optional_int64_extension] = 102 203 extensions[pb2.optional_uint32_extension] = 103 204 extensions[pb2.optional_uint64_extension] = 104 205 extensions[pb2.optional_sint32_extension] = 105 206 extensions[pb2.optional_sint64_extension] = 106 207 extensions[pb2.optional_fixed32_extension] = 107 208 extensions[pb2.optional_fixed64_extension] = 108 209 extensions[pb2.optional_sfixed32_extension] = 109 210 extensions[pb2.optional_sfixed64_extension] = 110 211 extensions[pb2.optional_float_extension] = 111 212 extensions[pb2.optional_double_extension] = 112 213 extensions[pb2.optional_bool_extension] = True 214 extensions[pb2.optional_string_extension] = u'115' 215 extensions[pb2.optional_bytes_extension] = b'116' 216 217 extensions[pb2.optionalgroup_extension].a = 117 218 extensions[pb2.optional_nested_message_extension].bb = 118 219 extensions[pb2.optional_foreign_message_extension].c = 119 220 extensions[pb2.optional_import_message_extension].d = 120 221 extensions[pb2.optional_public_import_message_extension].e = 126 222 extensions[pb2.optional_lazy_message_extension].bb = 127 223 224 extensions[pb2.optional_nested_enum_extension] = pb2.TestAllTypes.BAZ 225 extensions[pb2.optional_nested_enum_extension] = pb2.TestAllTypes.BAZ 226 extensions[pb2.optional_foreign_enum_extension] = pb2.FOREIGN_BAZ 227 extensions[pb2.optional_import_enum_extension] = import_pb2.IMPORT_BAZ 228 229 extensions[pb2.optional_string_piece_extension] = u'124' 230 extensions[pb2.optional_cord_extension] = u'125' 231 232 # 233 # Repeated fields. 234 # 235 236 extensions[pb2.repeated_int32_extension].append(201) 237 extensions[pb2.repeated_int64_extension].append(202) 238 extensions[pb2.repeated_uint32_extension].append(203) 239 extensions[pb2.repeated_uint64_extension].append(204) 240 extensions[pb2.repeated_sint32_extension].append(205) 241 extensions[pb2.repeated_sint64_extension].append(206) 242 extensions[pb2.repeated_fixed32_extension].append(207) 243 extensions[pb2.repeated_fixed64_extension].append(208) 244 extensions[pb2.repeated_sfixed32_extension].append(209) 245 extensions[pb2.repeated_sfixed64_extension].append(210) 246 extensions[pb2.repeated_float_extension].append(211) 247 extensions[pb2.repeated_double_extension].append(212) 248 extensions[pb2.repeated_bool_extension].append(True) 249 extensions[pb2.repeated_string_extension].append(u'215') 250 extensions[pb2.repeated_bytes_extension].append(b'216') 251 252 extensions[pb2.repeatedgroup_extension].add().a = 217 253 extensions[pb2.repeated_nested_message_extension].add().bb = 218 254 extensions[pb2.repeated_foreign_message_extension].add().c = 219 255 extensions[pb2.repeated_import_message_extension].add().d = 220 256 extensions[pb2.repeated_lazy_message_extension].add().bb = 227 257 258 extensions[pb2.repeated_nested_enum_extension].append(pb2.TestAllTypes.BAR) 259 extensions[pb2.repeated_foreign_enum_extension].append(pb2.FOREIGN_BAR) 260 extensions[pb2.repeated_import_enum_extension].append(import_pb2.IMPORT_BAR) 261 262 extensions[pb2.repeated_string_piece_extension].append(u'224') 263 extensions[pb2.repeated_cord_extension].append(u'225') 264 265 # Append a second one of each field. 266 extensions[pb2.repeated_int32_extension].append(301) 267 extensions[pb2.repeated_int64_extension].append(302) 268 extensions[pb2.repeated_uint32_extension].append(303) 269 extensions[pb2.repeated_uint64_extension].append(304) 270 extensions[pb2.repeated_sint32_extension].append(305) 271 extensions[pb2.repeated_sint64_extension].append(306) 272 extensions[pb2.repeated_fixed32_extension].append(307) 273 extensions[pb2.repeated_fixed64_extension].append(308) 274 extensions[pb2.repeated_sfixed32_extension].append(309) 275 extensions[pb2.repeated_sfixed64_extension].append(310) 276 extensions[pb2.repeated_float_extension].append(311) 277 extensions[pb2.repeated_double_extension].append(312) 278 extensions[pb2.repeated_bool_extension].append(False) 279 extensions[pb2.repeated_string_extension].append(u'315') 280 extensions[pb2.repeated_bytes_extension].append(b'316') 281 282 extensions[pb2.repeatedgroup_extension].add().a = 317 283 extensions[pb2.repeated_nested_message_extension].add().bb = 318 284 extensions[pb2.repeated_foreign_message_extension].add().c = 319 285 extensions[pb2.repeated_import_message_extension].add().d = 320 286 extensions[pb2.repeated_lazy_message_extension].add().bb = 327 287 288 extensions[pb2.repeated_nested_enum_extension].append(pb2.TestAllTypes.BAZ) 289 extensions[pb2.repeated_foreign_enum_extension].append(pb2.FOREIGN_BAZ) 290 extensions[pb2.repeated_import_enum_extension].append(import_pb2.IMPORT_BAZ) 291 292 extensions[pb2.repeated_string_piece_extension].append(u'324') 293 extensions[pb2.repeated_cord_extension].append(u'325') 294 295 # 296 # Fields with defaults. 297 # 298 299 extensions[pb2.default_int32_extension] = 401 300 extensions[pb2.default_int64_extension] = 402 301 extensions[pb2.default_uint32_extension] = 403 302 extensions[pb2.default_uint64_extension] = 404 303 extensions[pb2.default_sint32_extension] = 405 304 extensions[pb2.default_sint64_extension] = 406 305 extensions[pb2.default_fixed32_extension] = 407 306 extensions[pb2.default_fixed64_extension] = 408 307 extensions[pb2.default_sfixed32_extension] = 409 308 extensions[pb2.default_sfixed64_extension] = 410 309 extensions[pb2.default_float_extension] = 411 310 extensions[pb2.default_double_extension] = 412 311 extensions[pb2.default_bool_extension] = False 312 extensions[pb2.default_string_extension] = u'415' 313 extensions[pb2.default_bytes_extension] = b'416' 314 315 extensions[pb2.default_nested_enum_extension] = pb2.TestAllTypes.FOO 316 extensions[pb2.default_foreign_enum_extension] = pb2.FOREIGN_FOO 317 extensions[pb2.default_import_enum_extension] = import_pb2.IMPORT_FOO 318 319 extensions[pb2.default_string_piece_extension] = u'424' 320 extensions[pb2.default_cord_extension] = '425' 321 322 extensions[pb2.oneof_uint32_extension] = 601 323 extensions[pb2.oneof_nested_message_extension].bb = 602 324 extensions[pb2.oneof_string_extension] = u'603' 325 extensions[pb2.oneof_bytes_extension] = b'604' 326 327 328def SetAllFieldsAndExtensions(message): 329 """Sets every field and extension in the message to a unique value. 330 331 Args: 332 message: A unittest_pb2.TestAllExtensions message. 333 """ 334 message.my_int = 1 335 message.my_string = 'foo' 336 message.my_float = 1.0 337 message.Extensions[unittest_pb2.my_extension_int] = 23 338 message.Extensions[unittest_pb2.my_extension_string] = 'bar' 339 340 341def ExpectAllFieldsAndExtensionsInOrder(serialized): 342 """Ensures that serialized is the serialization we expect for a message 343 filled with SetAllFieldsAndExtensions(). (Specifically, ensures that the 344 serialization is in canonical, tag-number order). 345 """ 346 my_extension_int = unittest_pb2.my_extension_int 347 my_extension_string = unittest_pb2.my_extension_string 348 expected_strings = [] 349 message = unittest_pb2.TestFieldOrderings() 350 message.my_int = 1 # Field 1. 351 expected_strings.append(message.SerializeToString()) 352 message.Clear() 353 message.Extensions[my_extension_int] = 23 # Field 5. 354 expected_strings.append(message.SerializeToString()) 355 message.Clear() 356 message.my_string = 'foo' # Field 11. 357 expected_strings.append(message.SerializeToString()) 358 message.Clear() 359 message.Extensions[my_extension_string] = 'bar' # Field 50. 360 expected_strings.append(message.SerializeToString()) 361 message.Clear() 362 message.my_float = 1.0 363 expected_strings.append(message.SerializeToString()) 364 message.Clear() 365 expected = b''.join(expected_strings) 366 367 if expected != serialized: 368 raise ValueError('Expected %r, found %r' % (expected, serialized)) 369 370 371def ExpectAllFieldsSet(test_case, message): 372 """Check all fields for correct values have after Set*Fields() is called.""" 373 test_case.assertTrue(message.HasField('optional_int32')) 374 test_case.assertTrue(message.HasField('optional_int64')) 375 test_case.assertTrue(message.HasField('optional_uint32')) 376 test_case.assertTrue(message.HasField('optional_uint64')) 377 test_case.assertTrue(message.HasField('optional_sint32')) 378 test_case.assertTrue(message.HasField('optional_sint64')) 379 test_case.assertTrue(message.HasField('optional_fixed32')) 380 test_case.assertTrue(message.HasField('optional_fixed64')) 381 test_case.assertTrue(message.HasField('optional_sfixed32')) 382 test_case.assertTrue(message.HasField('optional_sfixed64')) 383 test_case.assertTrue(message.HasField('optional_float')) 384 test_case.assertTrue(message.HasField('optional_double')) 385 test_case.assertTrue(message.HasField('optional_bool')) 386 test_case.assertTrue(message.HasField('optional_string')) 387 test_case.assertTrue(message.HasField('optional_bytes')) 388 389 test_case.assertTrue(message.HasField('optionalgroup')) 390 test_case.assertTrue(message.HasField('optional_nested_message')) 391 test_case.assertTrue(message.HasField('optional_foreign_message')) 392 test_case.assertTrue(message.HasField('optional_import_message')) 393 394 test_case.assertTrue(message.optionalgroup.HasField('a')) 395 test_case.assertTrue(message.optional_nested_message.HasField('bb')) 396 test_case.assertTrue(message.optional_foreign_message.HasField('c')) 397 test_case.assertTrue(message.optional_import_message.HasField('d')) 398 399 test_case.assertTrue(message.HasField('optional_nested_enum')) 400 test_case.assertTrue(message.HasField('optional_foreign_enum')) 401 test_case.assertTrue(message.HasField('optional_import_enum')) 402 403 test_case.assertTrue(message.HasField('optional_string_piece')) 404 test_case.assertTrue(message.HasField('optional_cord')) 405 406 test_case.assertEqual(101, message.optional_int32) 407 test_case.assertEqual(102, message.optional_int64) 408 test_case.assertEqual(103, message.optional_uint32) 409 test_case.assertEqual(104, message.optional_uint64) 410 test_case.assertEqual(105, message.optional_sint32) 411 test_case.assertEqual(106, message.optional_sint64) 412 test_case.assertEqual(107, message.optional_fixed32) 413 test_case.assertEqual(108, message.optional_fixed64) 414 test_case.assertEqual(109, message.optional_sfixed32) 415 test_case.assertEqual(110, message.optional_sfixed64) 416 test_case.assertEqual(111, message.optional_float) 417 test_case.assertEqual(112, message.optional_double) 418 test_case.assertEqual(True, message.optional_bool) 419 test_case.assertEqual('115', message.optional_string) 420 test_case.assertEqual(b'116', message.optional_bytes) 421 422 test_case.assertEqual(117, message.optionalgroup.a) 423 test_case.assertEqual(118, message.optional_nested_message.bb) 424 test_case.assertEqual(119, message.optional_foreign_message.c) 425 test_case.assertEqual(120, message.optional_import_message.d) 426 test_case.assertEqual(126, message.optional_public_import_message.e) 427 test_case.assertEqual(127, message.optional_lazy_message.bb) 428 429 test_case.assertEqual(unittest_pb2.TestAllTypes.BAZ, 430 message.optional_nested_enum) 431 test_case.assertEqual(unittest_pb2.FOREIGN_BAZ, 432 message.optional_foreign_enum) 433 test_case.assertEqual(unittest_import_pb2.IMPORT_BAZ, 434 message.optional_import_enum) 435 436 # ----------------------------------------------------------------- 437 438 test_case.assertEqual(2, len(message.repeated_int32)) 439 test_case.assertEqual(2, len(message.repeated_int64)) 440 test_case.assertEqual(2, len(message.repeated_uint32)) 441 test_case.assertEqual(2, len(message.repeated_uint64)) 442 test_case.assertEqual(2, len(message.repeated_sint32)) 443 test_case.assertEqual(2, len(message.repeated_sint64)) 444 test_case.assertEqual(2, len(message.repeated_fixed32)) 445 test_case.assertEqual(2, len(message.repeated_fixed64)) 446 test_case.assertEqual(2, len(message.repeated_sfixed32)) 447 test_case.assertEqual(2, len(message.repeated_sfixed64)) 448 test_case.assertEqual(2, len(message.repeated_float)) 449 test_case.assertEqual(2, len(message.repeated_double)) 450 test_case.assertEqual(2, len(message.repeated_bool)) 451 test_case.assertEqual(2, len(message.repeated_string)) 452 test_case.assertEqual(2, len(message.repeated_bytes)) 453 454 test_case.assertEqual(2, len(message.repeatedgroup)) 455 test_case.assertEqual(2, len(message.repeated_nested_message)) 456 test_case.assertEqual(2, len(message.repeated_foreign_message)) 457 test_case.assertEqual(2, len(message.repeated_import_message)) 458 test_case.assertEqual(2, len(message.repeated_nested_enum)) 459 test_case.assertEqual(2, len(message.repeated_foreign_enum)) 460 test_case.assertEqual(2, len(message.repeated_import_enum)) 461 462 test_case.assertEqual(2, len(message.repeated_string_piece)) 463 test_case.assertEqual(2, len(message.repeated_cord)) 464 465 test_case.assertEqual(201, message.repeated_int32[0]) 466 test_case.assertEqual(202, message.repeated_int64[0]) 467 test_case.assertEqual(203, message.repeated_uint32[0]) 468 test_case.assertEqual(204, message.repeated_uint64[0]) 469 test_case.assertEqual(205, message.repeated_sint32[0]) 470 test_case.assertEqual(206, message.repeated_sint64[0]) 471 test_case.assertEqual(207, message.repeated_fixed32[0]) 472 test_case.assertEqual(208, message.repeated_fixed64[0]) 473 test_case.assertEqual(209, message.repeated_sfixed32[0]) 474 test_case.assertEqual(210, message.repeated_sfixed64[0]) 475 test_case.assertEqual(211, message.repeated_float[0]) 476 test_case.assertEqual(212, message.repeated_double[0]) 477 test_case.assertEqual(True, message.repeated_bool[0]) 478 test_case.assertEqual('215', message.repeated_string[0]) 479 test_case.assertEqual(b'216', message.repeated_bytes[0]) 480 481 test_case.assertEqual(217, message.repeatedgroup[0].a) 482 test_case.assertEqual(218, message.repeated_nested_message[0].bb) 483 test_case.assertEqual(219, message.repeated_foreign_message[0].c) 484 test_case.assertEqual(220, message.repeated_import_message[0].d) 485 test_case.assertEqual(227, message.repeated_lazy_message[0].bb) 486 487 test_case.assertEqual(unittest_pb2.TestAllTypes.BAR, 488 message.repeated_nested_enum[0]) 489 test_case.assertEqual(unittest_pb2.FOREIGN_BAR, 490 message.repeated_foreign_enum[0]) 491 test_case.assertEqual(unittest_import_pb2.IMPORT_BAR, 492 message.repeated_import_enum[0]) 493 494 test_case.assertEqual(301, message.repeated_int32[1]) 495 test_case.assertEqual(302, message.repeated_int64[1]) 496 test_case.assertEqual(303, message.repeated_uint32[1]) 497 test_case.assertEqual(304, message.repeated_uint64[1]) 498 test_case.assertEqual(305, message.repeated_sint32[1]) 499 test_case.assertEqual(306, message.repeated_sint64[1]) 500 test_case.assertEqual(307, message.repeated_fixed32[1]) 501 test_case.assertEqual(308, message.repeated_fixed64[1]) 502 test_case.assertEqual(309, message.repeated_sfixed32[1]) 503 test_case.assertEqual(310, message.repeated_sfixed64[1]) 504 test_case.assertEqual(311, message.repeated_float[1]) 505 test_case.assertEqual(312, message.repeated_double[1]) 506 test_case.assertEqual(False, message.repeated_bool[1]) 507 test_case.assertEqual('315', message.repeated_string[1]) 508 test_case.assertEqual(b'316', message.repeated_bytes[1]) 509 510 test_case.assertEqual(317, message.repeatedgroup[1].a) 511 test_case.assertEqual(318, message.repeated_nested_message[1].bb) 512 test_case.assertEqual(319, message.repeated_foreign_message[1].c) 513 test_case.assertEqual(320, message.repeated_import_message[1].d) 514 test_case.assertEqual(327, message.repeated_lazy_message[1].bb) 515 516 test_case.assertEqual(unittest_pb2.TestAllTypes.BAZ, 517 message.repeated_nested_enum[1]) 518 test_case.assertEqual(unittest_pb2.FOREIGN_BAZ, 519 message.repeated_foreign_enum[1]) 520 test_case.assertEqual(unittest_import_pb2.IMPORT_BAZ, 521 message.repeated_import_enum[1]) 522 523 # ----------------------------------------------------------------- 524 525 test_case.assertTrue(message.HasField('default_int32')) 526 test_case.assertTrue(message.HasField('default_int64')) 527 test_case.assertTrue(message.HasField('default_uint32')) 528 test_case.assertTrue(message.HasField('default_uint64')) 529 test_case.assertTrue(message.HasField('default_sint32')) 530 test_case.assertTrue(message.HasField('default_sint64')) 531 test_case.assertTrue(message.HasField('default_fixed32')) 532 test_case.assertTrue(message.HasField('default_fixed64')) 533 test_case.assertTrue(message.HasField('default_sfixed32')) 534 test_case.assertTrue(message.HasField('default_sfixed64')) 535 test_case.assertTrue(message.HasField('default_float')) 536 test_case.assertTrue(message.HasField('default_double')) 537 test_case.assertTrue(message.HasField('default_bool')) 538 test_case.assertTrue(message.HasField('default_string')) 539 test_case.assertTrue(message.HasField('default_bytes')) 540 541 test_case.assertTrue(message.HasField('default_nested_enum')) 542 test_case.assertTrue(message.HasField('default_foreign_enum')) 543 test_case.assertTrue(message.HasField('default_import_enum')) 544 545 test_case.assertEqual(401, message.default_int32) 546 test_case.assertEqual(402, message.default_int64) 547 test_case.assertEqual(403, message.default_uint32) 548 test_case.assertEqual(404, message.default_uint64) 549 test_case.assertEqual(405, message.default_sint32) 550 test_case.assertEqual(406, message.default_sint64) 551 test_case.assertEqual(407, message.default_fixed32) 552 test_case.assertEqual(408, message.default_fixed64) 553 test_case.assertEqual(409, message.default_sfixed32) 554 test_case.assertEqual(410, message.default_sfixed64) 555 test_case.assertEqual(411, message.default_float) 556 test_case.assertEqual(412, message.default_double) 557 test_case.assertEqual(False, message.default_bool) 558 test_case.assertEqual('415', message.default_string) 559 test_case.assertEqual(b'416', message.default_bytes) 560 561 test_case.assertEqual(unittest_pb2.TestAllTypes.FOO, 562 message.default_nested_enum) 563 test_case.assertEqual(unittest_pb2.FOREIGN_FOO, 564 message.default_foreign_enum) 565 test_case.assertEqual(unittest_import_pb2.IMPORT_FOO, 566 message.default_import_enum) 567 568 569def GoldenFile(filename): 570 """Finds the given golden file and returns a file object representing it.""" 571 572 # Search up the directory tree looking for the C++ protobuf source code. 573 path = '.' 574 while os.path.exists(path): 575 if os.path.exists(os.path.join(path, 'src/google/protobuf')): 576 # Found it. Load the golden file from the testdata directory. 577 full_path = os.path.join(path, 'src/google/protobuf/testdata', filename) 578 return open(full_path, 'rb') 579 path = os.path.join(path, '..') 580 581 raise RuntimeError( 582 'Could not find golden files. This test must be run from within the ' 583 'protobuf source package so that it can read test data files from the ' 584 'C++ source tree.') 585 586 587def GoldenFileData(filename): 588 """Finds the given golden file and returns its contents.""" 589 with GoldenFile(filename) as f: 590 return f.read() 591 592 593def SetAllPackedFields(message): 594 """Sets every field in the message to a unique value. 595 596 Args: 597 message: A unittest_pb2.TestPackedTypes instance. 598 """ 599 message.packed_int32.extend([601, 701]) 600 message.packed_int64.extend([602, 702]) 601 message.packed_uint32.extend([603, 703]) 602 message.packed_uint64.extend([604, 704]) 603 message.packed_sint32.extend([605, 705]) 604 message.packed_sint64.extend([606, 706]) 605 message.packed_fixed32.extend([607, 707]) 606 message.packed_fixed64.extend([608, 708]) 607 message.packed_sfixed32.extend([609, 709]) 608 message.packed_sfixed64.extend([610, 710]) 609 message.packed_float.extend([611.0, 711.0]) 610 message.packed_double.extend([612.0, 712.0]) 611 message.packed_bool.extend([True, False]) 612 message.packed_enum.extend([unittest_pb2.FOREIGN_BAR, 613 unittest_pb2.FOREIGN_BAZ]) 614 615 616def SetAllPackedExtensions(message): 617 """Sets every extension in the message to a unique value. 618 619 Args: 620 message: A unittest_pb2.TestPackedExtensions instance. 621 """ 622 extensions = message.Extensions 623 pb2 = unittest_pb2 624 625 extensions[pb2.packed_int32_extension].extend([601, 701]) 626 extensions[pb2.packed_int64_extension].extend([602, 702]) 627 extensions[pb2.packed_uint32_extension].extend([603, 703]) 628 extensions[pb2.packed_uint64_extension].extend([604, 704]) 629 extensions[pb2.packed_sint32_extension].extend([605, 705]) 630 extensions[pb2.packed_sint64_extension].extend([606, 706]) 631 extensions[pb2.packed_fixed32_extension].extend([607, 707]) 632 extensions[pb2.packed_fixed64_extension].extend([608, 708]) 633 extensions[pb2.packed_sfixed32_extension].extend([609, 709]) 634 extensions[pb2.packed_sfixed64_extension].extend([610, 710]) 635 extensions[pb2.packed_float_extension].extend([611.0, 711.0]) 636 extensions[pb2.packed_double_extension].extend([612.0, 712.0]) 637 extensions[pb2.packed_bool_extension].extend([True, False]) 638 extensions[pb2.packed_enum_extension].extend([unittest_pb2.FOREIGN_BAR, 639 unittest_pb2.FOREIGN_BAZ]) 640 641 642def SetAllUnpackedFields(message): 643 """Sets every field in the message to a unique value. 644 645 Args: 646 message: A unittest_pb2.TestUnpackedTypes instance. 647 """ 648 message.unpacked_int32.extend([601, 701]) 649 message.unpacked_int64.extend([602, 702]) 650 message.unpacked_uint32.extend([603, 703]) 651 message.unpacked_uint64.extend([604, 704]) 652 message.unpacked_sint32.extend([605, 705]) 653 message.unpacked_sint64.extend([606, 706]) 654 message.unpacked_fixed32.extend([607, 707]) 655 message.unpacked_fixed64.extend([608, 708]) 656 message.unpacked_sfixed32.extend([609, 709]) 657 message.unpacked_sfixed64.extend([610, 710]) 658 message.unpacked_float.extend([611.0, 711.0]) 659 message.unpacked_double.extend([612.0, 712.0]) 660 message.unpacked_bool.extend([True, False]) 661 message.unpacked_enum.extend([unittest_pb2.FOREIGN_BAR, 662 unittest_pb2.FOREIGN_BAZ]) 663