1 # Protocol Buffers - Google's data interchange format 2 # Copyright 2008 Google Inc. All rights reserved. 3 # http://code.google.com/p/protobuf/ 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 33 This is intentionally modeled on C++ code in 34 //google/protobuf/test_util.*. 35 """ 36 37 __author__ = 'robinson (at] google.com (Will Robinson)' 38 39 import os.path 40 41 from google.protobuf import unittest_import_pb2 42 from google.protobuf import unittest_pb2 43 44 45 def SetAllFields(message): 46 """Sets every 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 # TODO(robinson): Firmly spec out and test how 70 # protos interact with unicode. One specific example: 71 # what happens if we change the literal below to 72 # u'115'? What *should* happen? Still some discussion 73 # to finish with Kenton about bytes vs. strings 74 # and forcing everything to be utf8. :-/ 75 message.optional_string = '115' 76 message.optional_bytes = '116' 77 78 message.optionalgroup.a = 117 79 message.optional_nested_message.bb = 118 80 message.optional_foreign_message.c = 119 81 message.optional_import_message.d = 120 82 83 message.optional_nested_enum = unittest_pb2.TestAllTypes.BAZ 84 message.optional_foreign_enum = unittest_pb2.FOREIGN_BAZ 85 message.optional_import_enum = unittest_import_pb2.IMPORT_BAZ 86 87 message.optional_string_piece = '124' 88 message.optional_cord = '125' 89 90 # 91 # Repeated fields. 92 # 93 94 message.repeated_int32.append(201) 95 message.repeated_int64.append(202) 96 message.repeated_uint32.append(203) 97 message.repeated_uint64.append(204) 98 message.repeated_sint32.append(205) 99 message.repeated_sint64.append(206) 100 message.repeated_fixed32.append(207) 101 message.repeated_fixed64.append(208) 102 message.repeated_sfixed32.append(209) 103 message.repeated_sfixed64.append(210) 104 message.repeated_float.append(211) 105 message.repeated_double.append(212) 106 message.repeated_bool.append(True) 107 message.repeated_string.append('215') 108 message.repeated_bytes.append('216') 109 110 message.repeatedgroup.add().a = 217 111 message.repeated_nested_message.add().bb = 218 112 message.repeated_foreign_message.add().c = 219 113 message.repeated_import_message.add().d = 220 114 115 message.repeated_nested_enum.append(unittest_pb2.TestAllTypes.BAR) 116 message.repeated_foreign_enum.append(unittest_pb2.FOREIGN_BAR) 117 message.repeated_import_enum.append(unittest_import_pb2.IMPORT_BAR) 118 119 message.repeated_string_piece.append('224') 120 message.repeated_cord.append('225') 121 122 # Add a second one of each field. 123 message.repeated_int32.append(301) 124 message.repeated_int64.append(302) 125 message.repeated_uint32.append(303) 126 message.repeated_uint64.append(304) 127 message.repeated_sint32.append(305) 128 message.repeated_sint64.append(306) 129 message.repeated_fixed32.append(307) 130 message.repeated_fixed64.append(308) 131 message.repeated_sfixed32.append(309) 132 message.repeated_sfixed64.append(310) 133 message.repeated_float.append(311) 134 message.repeated_double.append(312) 135 message.repeated_bool.append(False) 136 message.repeated_string.append('315') 137 message.repeated_bytes.append('316') 138 139 message.repeatedgroup.add().a = 317 140 message.repeated_nested_message.add().bb = 318 141 message.repeated_foreign_message.add().c = 319 142 message.repeated_import_message.add().d = 320 143 144 message.repeated_nested_enum.append(unittest_pb2.TestAllTypes.BAZ) 145 message.repeated_foreign_enum.append(unittest_pb2.FOREIGN_BAZ) 146 message.repeated_import_enum.append(unittest_import_pb2.IMPORT_BAZ) 147 148 message.repeated_string_piece.append('324') 149 message.repeated_cord.append('325') 150 151 # 152 # Fields that have defaults. 153 # 154 155 message.default_int32 = 401 156 message.default_int64 = 402 157 message.default_uint32 = 403 158 message.default_uint64 = 404 159 message.default_sint32 = 405 160 message.default_sint64 = 406 161 message.default_fixed32 = 407 162 message.default_fixed64 = 408 163 message.default_sfixed32 = 409 164 message.default_sfixed64 = 410 165 message.default_float = 411 166 message.default_double = 412 167 message.default_bool = False 168 message.default_string = '415' 169 message.default_bytes = '416' 170 171 message.default_nested_enum = unittest_pb2.TestAllTypes.FOO 172 message.default_foreign_enum = unittest_pb2.FOREIGN_FOO 173 message.default_import_enum = unittest_import_pb2.IMPORT_FOO 174 175 message.default_string_piece = '424' 176 message.default_cord = '425' 177 178 179 def SetAllExtensions(message): 180 """Sets every extension in the message to a unique value. 181 182 Args: 183 message: A unittest_pb2.TestAllExtensions instance. 184 """ 185 186 extensions = message.Extensions 187 pb2 = unittest_pb2 188 import_pb2 = unittest_import_pb2 189 190 # 191 # Optional fields. 192 # 193 194 extensions[pb2.optional_int32_extension] = 101 195 extensions[pb2.optional_int64_extension] = 102 196 extensions[pb2.optional_uint32_extension] = 103 197 extensions[pb2.optional_uint64_extension] = 104 198 extensions[pb2.optional_sint32_extension] = 105 199 extensions[pb2.optional_sint64_extension] = 106 200 extensions[pb2.optional_fixed32_extension] = 107 201 extensions[pb2.optional_fixed64_extension] = 108 202 extensions[pb2.optional_sfixed32_extension] = 109 203 extensions[pb2.optional_sfixed64_extension] = 110 204 extensions[pb2.optional_float_extension] = 111 205 extensions[pb2.optional_double_extension] = 112 206 extensions[pb2.optional_bool_extension] = True 207 extensions[pb2.optional_string_extension] = '115' 208 extensions[pb2.optional_bytes_extension] = '116' 209 210 extensions[pb2.optionalgroup_extension].a = 117 211 extensions[pb2.optional_nested_message_extension].bb = 118 212 extensions[pb2.optional_foreign_message_extension].c = 119 213 extensions[pb2.optional_import_message_extension].d = 120 214 215 extensions[pb2.optional_nested_enum_extension] = pb2.TestAllTypes.BAZ 216 extensions[pb2.optional_nested_enum_extension] = pb2.TestAllTypes.BAZ 217 extensions[pb2.optional_foreign_enum_extension] = pb2.FOREIGN_BAZ 218 extensions[pb2.optional_import_enum_extension] = import_pb2.IMPORT_BAZ 219 220 extensions[pb2.optional_string_piece_extension] = '124' 221 extensions[pb2.optional_cord_extension] = '125' 222 223 # 224 # Repeated fields. 225 # 226 227 extensions[pb2.repeated_int32_extension].append(201) 228 extensions[pb2.repeated_int64_extension].append(202) 229 extensions[pb2.repeated_uint32_extension].append(203) 230 extensions[pb2.repeated_uint64_extension].append(204) 231 extensions[pb2.repeated_sint32_extension].append(205) 232 extensions[pb2.repeated_sint64_extension].append(206) 233 extensions[pb2.repeated_fixed32_extension].append(207) 234 extensions[pb2.repeated_fixed64_extension].append(208) 235 extensions[pb2.repeated_sfixed32_extension].append(209) 236 extensions[pb2.repeated_sfixed64_extension].append(210) 237 extensions[pb2.repeated_float_extension].append(211) 238 extensions[pb2.repeated_double_extension].append(212) 239 extensions[pb2.repeated_bool_extension].append(True) 240 extensions[pb2.repeated_string_extension].append('215') 241 extensions[pb2.repeated_bytes_extension].append('216') 242 243 extensions[pb2.repeatedgroup_extension].add().a = 217 244 extensions[pb2.repeated_nested_message_extension].add().bb = 218 245 extensions[pb2.repeated_foreign_message_extension].add().c = 219 246 extensions[pb2.repeated_import_message_extension].add().d = 220 247 248 extensions[pb2.repeated_nested_enum_extension].append(pb2.TestAllTypes.BAR) 249 extensions[pb2.repeated_foreign_enum_extension].append(pb2.FOREIGN_BAR) 250 extensions[pb2.repeated_import_enum_extension].append(import_pb2.IMPORT_BAR) 251 252 extensions[pb2.repeated_string_piece_extension].append('224') 253 extensions[pb2.repeated_cord_extension].append('225') 254 255 # Append a second one of each field. 256 extensions[pb2.repeated_int32_extension].append(301) 257 extensions[pb2.repeated_int64_extension].append(302) 258 extensions[pb2.repeated_uint32_extension].append(303) 259 extensions[pb2.repeated_uint64_extension].append(304) 260 extensions[pb2.repeated_sint32_extension].append(305) 261 extensions[pb2.repeated_sint64_extension].append(306) 262 extensions[pb2.repeated_fixed32_extension].append(307) 263 extensions[pb2.repeated_fixed64_extension].append(308) 264 extensions[pb2.repeated_sfixed32_extension].append(309) 265 extensions[pb2.repeated_sfixed64_extension].append(310) 266 extensions[pb2.repeated_float_extension].append(311) 267 extensions[pb2.repeated_double_extension].append(312) 268 extensions[pb2.repeated_bool_extension].append(False) 269 extensions[pb2.repeated_string_extension].append('315') 270 extensions[pb2.repeated_bytes_extension].append('316') 271 272 extensions[pb2.repeatedgroup_extension].add().a = 317 273 extensions[pb2.repeated_nested_message_extension].add().bb = 318 274 extensions[pb2.repeated_foreign_message_extension].add().c = 319 275 extensions[pb2.repeated_import_message_extension].add().d = 320 276 277 extensions[pb2.repeated_nested_enum_extension].append(pb2.TestAllTypes.BAZ) 278 extensions[pb2.repeated_foreign_enum_extension].append(pb2.FOREIGN_BAZ) 279 extensions[pb2.repeated_import_enum_extension].append(import_pb2.IMPORT_BAZ) 280 281 extensions[pb2.repeated_string_piece_extension].append('324') 282 extensions[pb2.repeated_cord_extension].append('325') 283 284 # 285 # Fields with defaults. 286 # 287 288 extensions[pb2.default_int32_extension] = 401 289 extensions[pb2.default_int64_extension] = 402 290 extensions[pb2.default_uint32_extension] = 403 291 extensions[pb2.default_uint64_extension] = 404 292 extensions[pb2.default_sint32_extension] = 405 293 extensions[pb2.default_sint64_extension] = 406 294 extensions[pb2.default_fixed32_extension] = 407 295 extensions[pb2.default_fixed64_extension] = 408 296 extensions[pb2.default_sfixed32_extension] = 409 297 extensions[pb2.default_sfixed64_extension] = 410 298 extensions[pb2.default_float_extension] = 411 299 extensions[pb2.default_double_extension] = 412 300 extensions[pb2.default_bool_extension] = False 301 extensions[pb2.default_string_extension] = '415' 302 extensions[pb2.default_bytes_extension] = '416' 303 304 extensions[pb2.default_nested_enum_extension] = pb2.TestAllTypes.FOO 305 extensions[pb2.default_foreign_enum_extension] = pb2.FOREIGN_FOO 306 extensions[pb2.default_import_enum_extension] = import_pb2.IMPORT_FOO 307 308 extensions[pb2.default_string_piece_extension] = '424' 309 extensions[pb2.default_cord_extension] = '425' 310 311 312 def SetAllFieldsAndExtensions(message): 313 """Sets every field and extension in the message to a unique value. 314 315 Args: 316 message: A unittest_pb2.TestAllExtensions message. 317 """ 318 message.my_int = 1 319 message.my_string = 'foo' 320 message.my_float = 1.0 321 message.Extensions[unittest_pb2.my_extension_int] = 23 322 message.Extensions[unittest_pb2.my_extension_string] = 'bar' 323 324 325 def ExpectAllFieldsAndExtensionsInOrder(serialized): 326 """Ensures that serialized is the serialization we expect for a message 327 filled with SetAllFieldsAndExtensions(). (Specifically, ensures that the 328 serialization is in canonical, tag-number order). 329 """ 330 my_extension_int = unittest_pb2.my_extension_int 331 my_extension_string = unittest_pb2.my_extension_string 332 expected_strings = [] 333 message = unittest_pb2.TestFieldOrderings() 334 message.my_int = 1 # Field 1. 335 expected_strings.append(message.SerializeToString()) 336 message.Clear() 337 message.Extensions[my_extension_int] = 23 # Field 5. 338 expected_strings.append(message.SerializeToString()) 339 message.Clear() 340 message.my_string = 'foo' # Field 11. 341 expected_strings.append(message.SerializeToString()) 342 message.Clear() 343 message.Extensions[my_extension_string] = 'bar' # Field 50. 344 expected_strings.append(message.SerializeToString()) 345 message.Clear() 346 message.my_float = 1.0 347 expected_strings.append(message.SerializeToString()) 348 message.Clear() 349 expected = ''.join(expected_strings) 350 351 if expected != serialized: 352 raise ValueError('Expected %r, found %r' % (expected, serialized)) 353 354 355 def ExpectAllFieldsSet(test_case, message): 356 """Check all fields for correct values have after Set*Fields() is called.""" 357 test_case.assertTrue(message.HasField('optional_int32')) 358 test_case.assertTrue(message.HasField('optional_int64')) 359 test_case.assertTrue(message.HasField('optional_uint32')) 360 test_case.assertTrue(message.HasField('optional_uint64')) 361 test_case.assertTrue(message.HasField('optional_sint32')) 362 test_case.assertTrue(message.HasField('optional_sint64')) 363 test_case.assertTrue(message.HasField('optional_fixed32')) 364 test_case.assertTrue(message.HasField('optional_fixed64')) 365 test_case.assertTrue(message.HasField('optional_sfixed32')) 366 test_case.assertTrue(message.HasField('optional_sfixed64')) 367 test_case.assertTrue(message.HasField('optional_float')) 368 test_case.assertTrue(message.HasField('optional_double')) 369 test_case.assertTrue(message.HasField('optional_bool')) 370 test_case.assertTrue(message.HasField('optional_string')) 371 test_case.assertTrue(message.HasField('optional_bytes')) 372 373 test_case.assertTrue(message.HasField('optionalgroup')) 374 test_case.assertTrue(message.HasField('optional_nested_message')) 375 test_case.assertTrue(message.HasField('optional_foreign_message')) 376 test_case.assertTrue(message.HasField('optional_import_message')) 377 378 test_case.assertTrue(message.optionalgroup.HasField('a')) 379 test_case.assertTrue(message.optional_nested_message.HasField('bb')) 380 test_case.assertTrue(message.optional_foreign_message.HasField('c')) 381 test_case.assertTrue(message.optional_import_message.HasField('d')) 382 383 test_case.assertTrue(message.HasField('optional_nested_enum')) 384 test_case.assertTrue(message.HasField('optional_foreign_enum')) 385 test_case.assertTrue(message.HasField('optional_import_enum')) 386 387 test_case.assertTrue(message.HasField('optional_string_piece')) 388 test_case.assertTrue(message.HasField('optional_cord')) 389 390 test_case.assertEqual(101, message.optional_int32) 391 test_case.assertEqual(102, message.optional_int64) 392 test_case.assertEqual(103, message.optional_uint32) 393 test_case.assertEqual(104, message.optional_uint64) 394 test_case.assertEqual(105, message.optional_sint32) 395 test_case.assertEqual(106, message.optional_sint64) 396 test_case.assertEqual(107, message.optional_fixed32) 397 test_case.assertEqual(108, message.optional_fixed64) 398 test_case.assertEqual(109, message.optional_sfixed32) 399 test_case.assertEqual(110, message.optional_sfixed64) 400 test_case.assertEqual(111, message.optional_float) 401 test_case.assertEqual(112, message.optional_double) 402 test_case.assertEqual(True, message.optional_bool) 403 test_case.assertEqual('115', message.optional_string) 404 test_case.assertEqual('116', message.optional_bytes) 405 406 test_case.assertEqual(117, message.optionalgroup.a) 407 test_case.assertEqual(118, message.optional_nested_message.bb) 408 test_case.assertEqual(119, message.optional_foreign_message.c) 409 test_case.assertEqual(120, message.optional_import_message.d) 410 411 test_case.assertEqual(unittest_pb2.TestAllTypes.BAZ, 412 message.optional_nested_enum) 413 test_case.assertEqual(unittest_pb2.FOREIGN_BAZ, 414 message.optional_foreign_enum) 415 test_case.assertEqual(unittest_import_pb2.IMPORT_BAZ, 416 message.optional_import_enum) 417 418 # ----------------------------------------------------------------- 419 420 test_case.assertEqual(2, len(message.repeated_int32)) 421 test_case.assertEqual(2, len(message.repeated_int64)) 422 test_case.assertEqual(2, len(message.repeated_uint32)) 423 test_case.assertEqual(2, len(message.repeated_uint64)) 424 test_case.assertEqual(2, len(message.repeated_sint32)) 425 test_case.assertEqual(2, len(message.repeated_sint64)) 426 test_case.assertEqual(2, len(message.repeated_fixed32)) 427 test_case.assertEqual(2, len(message.repeated_fixed64)) 428 test_case.assertEqual(2, len(message.repeated_sfixed32)) 429 test_case.assertEqual(2, len(message.repeated_sfixed64)) 430 test_case.assertEqual(2, len(message.repeated_float)) 431 test_case.assertEqual(2, len(message.repeated_double)) 432 test_case.assertEqual(2, len(message.repeated_bool)) 433 test_case.assertEqual(2, len(message.repeated_string)) 434 test_case.assertEqual(2, len(message.repeated_bytes)) 435 436 test_case.assertEqual(2, len(message.repeatedgroup)) 437 test_case.assertEqual(2, len(message.repeated_nested_message)) 438 test_case.assertEqual(2, len(message.repeated_foreign_message)) 439 test_case.assertEqual(2, len(message.repeated_import_message)) 440 test_case.assertEqual(2, len(message.repeated_nested_enum)) 441 test_case.assertEqual(2, len(message.repeated_foreign_enum)) 442 test_case.assertEqual(2, len(message.repeated_import_enum)) 443 444 test_case.assertEqual(2, len(message.repeated_string_piece)) 445 test_case.assertEqual(2, len(message.repeated_cord)) 446 447 test_case.assertEqual(201, message.repeated_int32[0]) 448 test_case.assertEqual(202, message.repeated_int64[0]) 449 test_case.assertEqual(203, message.repeated_uint32[0]) 450 test_case.assertEqual(204, message.repeated_uint64[0]) 451 test_case.assertEqual(205, message.repeated_sint32[0]) 452 test_case.assertEqual(206, message.repeated_sint64[0]) 453 test_case.assertEqual(207, message.repeated_fixed32[0]) 454 test_case.assertEqual(208, message.repeated_fixed64[0]) 455 test_case.assertEqual(209, message.repeated_sfixed32[0]) 456 test_case.assertEqual(210, message.repeated_sfixed64[0]) 457 test_case.assertEqual(211, message.repeated_float[0]) 458 test_case.assertEqual(212, message.repeated_double[0]) 459 test_case.assertEqual(True, message.repeated_bool[0]) 460 test_case.assertEqual('215', message.repeated_string[0]) 461 test_case.assertEqual('216', message.repeated_bytes[0]) 462 463 test_case.assertEqual(217, message.repeatedgroup[0].a) 464 test_case.assertEqual(218, message.repeated_nested_message[0].bb) 465 test_case.assertEqual(219, message.repeated_foreign_message[0].c) 466 test_case.assertEqual(220, message.repeated_import_message[0].d) 467 468 test_case.assertEqual(unittest_pb2.TestAllTypes.BAR, 469 message.repeated_nested_enum[0]) 470 test_case.assertEqual(unittest_pb2.FOREIGN_BAR, 471 message.repeated_foreign_enum[0]) 472 test_case.assertEqual(unittest_import_pb2.IMPORT_BAR, 473 message.repeated_import_enum[0]) 474 475 test_case.assertEqual(301, message.repeated_int32[1]) 476 test_case.assertEqual(302, message.repeated_int64[1]) 477 test_case.assertEqual(303, message.repeated_uint32[1]) 478 test_case.assertEqual(304, message.repeated_uint64[1]) 479 test_case.assertEqual(305, message.repeated_sint32[1]) 480 test_case.assertEqual(306, message.repeated_sint64[1]) 481 test_case.assertEqual(307, message.repeated_fixed32[1]) 482 test_case.assertEqual(308, message.repeated_fixed64[1]) 483 test_case.assertEqual(309, message.repeated_sfixed32[1]) 484 test_case.assertEqual(310, message.repeated_sfixed64[1]) 485 test_case.assertEqual(311, message.repeated_float[1]) 486 test_case.assertEqual(312, message.repeated_double[1]) 487 test_case.assertEqual(False, message.repeated_bool[1]) 488 test_case.assertEqual('315', message.repeated_string[1]) 489 test_case.assertEqual('316', message.repeated_bytes[1]) 490 491 test_case.assertEqual(317, message.repeatedgroup[1].a) 492 test_case.assertEqual(318, message.repeated_nested_message[1].bb) 493 test_case.assertEqual(319, message.repeated_foreign_message[1].c) 494 test_case.assertEqual(320, message.repeated_import_message[1].d) 495 496 test_case.assertEqual(unittest_pb2.TestAllTypes.BAZ, 497 message.repeated_nested_enum[1]) 498 test_case.assertEqual(unittest_pb2.FOREIGN_BAZ, 499 message.repeated_foreign_enum[1]) 500 test_case.assertEqual(unittest_import_pb2.IMPORT_BAZ, 501 message.repeated_import_enum[1]) 502 503 # ----------------------------------------------------------------- 504 505 test_case.assertTrue(message.HasField('default_int32')) 506 test_case.assertTrue(message.HasField('default_int64')) 507 test_case.assertTrue(message.HasField('default_uint32')) 508 test_case.assertTrue(message.HasField('default_uint64')) 509 test_case.assertTrue(message.HasField('default_sint32')) 510 test_case.assertTrue(message.HasField('default_sint64')) 511 test_case.assertTrue(message.HasField('default_fixed32')) 512 test_case.assertTrue(message.HasField('default_fixed64')) 513 test_case.assertTrue(message.HasField('default_sfixed32')) 514 test_case.assertTrue(message.HasField('default_sfixed64')) 515 test_case.assertTrue(message.HasField('default_float')) 516 test_case.assertTrue(message.HasField('default_double')) 517 test_case.assertTrue(message.HasField('default_bool')) 518 test_case.assertTrue(message.HasField('default_string')) 519 test_case.assertTrue(message.HasField('default_bytes')) 520 521 test_case.assertTrue(message.HasField('default_nested_enum')) 522 test_case.assertTrue(message.HasField('default_foreign_enum')) 523 test_case.assertTrue(message.HasField('default_import_enum')) 524 525 test_case.assertEqual(401, message.default_int32) 526 test_case.assertEqual(402, message.default_int64) 527 test_case.assertEqual(403, message.default_uint32) 528 test_case.assertEqual(404, message.default_uint64) 529 test_case.assertEqual(405, message.default_sint32) 530 test_case.assertEqual(406, message.default_sint64) 531 test_case.assertEqual(407, message.default_fixed32) 532 test_case.assertEqual(408, message.default_fixed64) 533 test_case.assertEqual(409, message.default_sfixed32) 534 test_case.assertEqual(410, message.default_sfixed64) 535 test_case.assertEqual(411, message.default_float) 536 test_case.assertEqual(412, message.default_double) 537 test_case.assertEqual(False, message.default_bool) 538 test_case.assertEqual('415', message.default_string) 539 test_case.assertEqual('416', message.default_bytes) 540 541 test_case.assertEqual(unittest_pb2.TestAllTypes.FOO, 542 message.default_nested_enum) 543 test_case.assertEqual(unittest_pb2.FOREIGN_FOO, 544 message.default_foreign_enum) 545 test_case.assertEqual(unittest_import_pb2.IMPORT_FOO, 546 message.default_import_enum) 547 548 def GoldenFile(filename): 549 """Finds the given golden file and returns a file object representing it.""" 550 551 # Search up the directory tree looking for the C++ protobuf source code. 552 path = '.' 553 while os.path.exists(path): 554 if os.path.exists(os.path.join(path, 'src/google/protobuf')): 555 # Found it. Load the golden file from the testdata directory. 556 full_path = os.path.join(path, 'src/google/protobuf/testdata', filename) 557 return open(full_path, 'rb') 558 path = os.path.join(path, '..') 559 560 raise RuntimeError( 561 'Could not find golden files. This test must be run from within the ' 562 'protobuf source package so that it can read test data files from the ' 563 'C++ source tree.') 564 565 566 def SetAllPackedFields(message): 567 """Sets every field in the message to a unique value. 568 569 Args: 570 message: A unittest_pb2.TestPackedTypes instance. 571 """ 572 message.packed_int32.extend([601, 701]) 573 message.packed_int64.extend([602, 702]) 574 message.packed_uint32.extend([603, 703]) 575 message.packed_uint64.extend([604, 704]) 576 message.packed_sint32.extend([605, 705]) 577 message.packed_sint64.extend([606, 706]) 578 message.packed_fixed32.extend([607, 707]) 579 message.packed_fixed64.extend([608, 708]) 580 message.packed_sfixed32.extend([609, 709]) 581 message.packed_sfixed64.extend([610, 710]) 582 message.packed_float.extend([611.0, 711.0]) 583 message.packed_double.extend([612.0, 712.0]) 584 message.packed_bool.extend([True, False]) 585 message.packed_enum.extend([unittest_pb2.FOREIGN_BAR, 586 unittest_pb2.FOREIGN_BAZ]) 587 588 589 def SetAllPackedExtensions(message): 590 """Sets every extension in the message to a unique value. 591 592 Args: 593 message: A unittest_pb2.TestPackedExtensions instance. 594 """ 595 extensions = message.Extensions 596 pb2 = unittest_pb2 597 598 extensions[pb2.packed_int32_extension].extend([601, 701]) 599 extensions[pb2.packed_int64_extension].extend([602, 702]) 600 extensions[pb2.packed_uint32_extension].extend([603, 703]) 601 extensions[pb2.packed_uint64_extension].extend([604, 704]) 602 extensions[pb2.packed_sint32_extension].extend([605, 705]) 603 extensions[pb2.packed_sint64_extension].extend([606, 706]) 604 extensions[pb2.packed_fixed32_extension].extend([607, 707]) 605 extensions[pb2.packed_fixed64_extension].extend([608, 708]) 606 extensions[pb2.packed_sfixed32_extension].extend([609, 709]) 607 extensions[pb2.packed_sfixed64_extension].extend([610, 710]) 608 extensions[pb2.packed_float_extension].extend([611.0, 711.0]) 609 extensions[pb2.packed_double_extension].extend([612.0, 712.0]) 610 extensions[pb2.packed_bool_extension].extend([True, False]) 611 extensions[pb2.packed_enum_extension].extend([unittest_pb2.FOREIGN_BAR, 612 unittest_pb2.FOREIGN_BAZ]) 613 614 615 def SetAllUnpackedFields(message): 616 """Sets every field in the message to a unique value. 617 618 Args: 619 message: A unittest_pb2.TestUnpackedTypes instance. 620 """ 621 message.unpacked_int32.extend([601, 701]) 622 message.unpacked_int64.extend([602, 702]) 623 message.unpacked_uint32.extend([603, 703]) 624 message.unpacked_uint64.extend([604, 704]) 625 message.unpacked_sint32.extend([605, 705]) 626 message.unpacked_sint64.extend([606, 706]) 627 message.unpacked_fixed32.extend([607, 707]) 628 message.unpacked_fixed64.extend([608, 708]) 629 message.unpacked_sfixed32.extend([609, 709]) 630 message.unpacked_sfixed64.extend([610, 710]) 631 message.unpacked_float.extend([611.0, 711.0]) 632 message.unpacked_double.extend([612.0, 712.0]) 633 message.unpacked_bool.extend([True, False]) 634 message.unpacked_enum.extend([unittest_pb2.FOREIGN_BAR, 635 unittest_pb2.FOREIGN_BAZ]) 636