店播爬取Python脚本

test_util.py 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. """Utilities for Python proto2 tests.
  31. This is intentionally modeled on C++ code in
  32. //google/protobuf/test_util.*.
  33. """
  34. __author__ = 'robinson@google.com (Will Robinson)'
  35. import numbers
  36. import operator
  37. import os.path
  38. from google.protobuf import unittest_import_pb2
  39. from google.protobuf import unittest_pb2
  40. try:
  41. long # Python 2
  42. except NameError:
  43. long = int # Python 3
  44. # Tests whether the given TestAllTypes message is proto2 or not.
  45. # This is used to gate several fields/features that only exist
  46. # for the proto2 version of the message.
  47. def IsProto2(message):
  48. return message.DESCRIPTOR.syntax == "proto2"
  49. def SetAllNonLazyFields(message):
  50. """Sets every non-lazy field in the message to a unique value.
  51. Args:
  52. message: A TestAllTypes instance.
  53. """
  54. #
  55. # Optional fields.
  56. #
  57. message.optional_int32 = 101
  58. message.optional_int64 = 102
  59. message.optional_uint32 = 103
  60. message.optional_uint64 = 104
  61. message.optional_sint32 = 105
  62. message.optional_sint64 = 106
  63. message.optional_fixed32 = 107
  64. message.optional_fixed64 = 108
  65. message.optional_sfixed32 = 109
  66. message.optional_sfixed64 = 110
  67. message.optional_float = 111
  68. message.optional_double = 112
  69. message.optional_bool = True
  70. message.optional_string = u'115'
  71. message.optional_bytes = b'116'
  72. if IsProto2(message):
  73. message.optionalgroup.a = 117
  74. message.optional_nested_message.bb = 118
  75. message.optional_foreign_message.c = 119
  76. message.optional_import_message.d = 120
  77. message.optional_public_import_message.e = 126
  78. message.optional_nested_enum = unittest_pb2.TestAllTypes.BAZ
  79. message.optional_foreign_enum = unittest_pb2.FOREIGN_BAZ
  80. if IsProto2(message):
  81. message.optional_import_enum = unittest_import_pb2.IMPORT_BAZ
  82. message.optional_string_piece = u'124'
  83. message.optional_cord = u'125'
  84. #
  85. # Repeated fields.
  86. #
  87. message.repeated_int32.append(201)
  88. message.repeated_int64.append(202)
  89. message.repeated_uint32.append(203)
  90. message.repeated_uint64.append(204)
  91. message.repeated_sint32.append(205)
  92. message.repeated_sint64.append(206)
  93. message.repeated_fixed32.append(207)
  94. message.repeated_fixed64.append(208)
  95. message.repeated_sfixed32.append(209)
  96. message.repeated_sfixed64.append(210)
  97. message.repeated_float.append(211)
  98. message.repeated_double.append(212)
  99. message.repeated_bool.append(True)
  100. message.repeated_string.append(u'215')
  101. message.repeated_bytes.append(b'216')
  102. if IsProto2(message):
  103. message.repeatedgroup.add().a = 217
  104. message.repeated_nested_message.add().bb = 218
  105. message.repeated_foreign_message.add().c = 219
  106. message.repeated_import_message.add().d = 220
  107. message.repeated_lazy_message.add().bb = 227
  108. message.repeated_nested_enum.append(unittest_pb2.TestAllTypes.BAR)
  109. message.repeated_foreign_enum.append(unittest_pb2.FOREIGN_BAR)
  110. if IsProto2(message):
  111. message.repeated_import_enum.append(unittest_import_pb2.IMPORT_BAR)
  112. message.repeated_string_piece.append(u'224')
  113. message.repeated_cord.append(u'225')
  114. # Add a second one of each field and set value by index.
  115. message.repeated_int32.append(0)
  116. message.repeated_int64.append(0)
  117. message.repeated_uint32.append(0)
  118. message.repeated_uint64.append(0)
  119. message.repeated_sint32.append(0)
  120. message.repeated_sint64.append(0)
  121. message.repeated_fixed32.append(0)
  122. message.repeated_fixed64.append(0)
  123. message.repeated_sfixed32.append(0)
  124. message.repeated_sfixed64.append(0)
  125. message.repeated_float.append(0)
  126. message.repeated_double.append(0)
  127. message.repeated_bool.append(True)
  128. message.repeated_string.append(u'0')
  129. message.repeated_bytes.append(b'0')
  130. message.repeated_int32[1] = 301
  131. message.repeated_int64[1] = 302
  132. message.repeated_uint32[1] = 303
  133. message.repeated_uint64[1] = 304
  134. message.repeated_sint32[1] = 305
  135. message.repeated_sint64[1] = 306
  136. message.repeated_fixed32[1] = 307
  137. message.repeated_fixed64[1] = 308
  138. message.repeated_sfixed32[1] = 309
  139. message.repeated_sfixed64[1] = 310
  140. message.repeated_float[1] = 311
  141. message.repeated_double[1] = 312
  142. message.repeated_bool[1] = False
  143. message.repeated_string[1] = u'315'
  144. message.repeated_bytes[1] = b'316'
  145. if IsProto2(message):
  146. message.repeatedgroup.add().a = 317
  147. message.repeated_nested_message.add().bb = 318
  148. message.repeated_foreign_message.add().c = 319
  149. message.repeated_import_message.add().d = 320
  150. message.repeated_lazy_message.add().bb = 327
  151. message.repeated_nested_enum.append(unittest_pb2.TestAllTypes.BAR)
  152. message.repeated_nested_enum[1] = unittest_pb2.TestAllTypes.BAZ
  153. message.repeated_foreign_enum.append(unittest_pb2.FOREIGN_BAZ)
  154. if IsProto2(message):
  155. message.repeated_import_enum.append(unittest_import_pb2.IMPORT_BAZ)
  156. message.repeated_string_piece.append(u'324')
  157. message.repeated_cord.append(u'325')
  158. #
  159. # Fields that have defaults.
  160. #
  161. if IsProto2(message):
  162. message.default_int32 = 401
  163. message.default_int64 = 402
  164. message.default_uint32 = 403
  165. message.default_uint64 = 404
  166. message.default_sint32 = 405
  167. message.default_sint64 = 406
  168. message.default_fixed32 = 407
  169. message.default_fixed64 = 408
  170. message.default_sfixed32 = 409
  171. message.default_sfixed64 = 410
  172. message.default_float = 411
  173. message.default_double = 412
  174. message.default_bool = False
  175. message.default_string = '415'
  176. message.default_bytes = b'416'
  177. message.default_nested_enum = unittest_pb2.TestAllTypes.FOO
  178. message.default_foreign_enum = unittest_pb2.FOREIGN_FOO
  179. message.default_import_enum = unittest_import_pb2.IMPORT_FOO
  180. message.default_string_piece = '424'
  181. message.default_cord = '425'
  182. message.oneof_uint32 = 601
  183. message.oneof_nested_message.bb = 602
  184. message.oneof_string = '603'
  185. message.oneof_bytes = b'604'
  186. def SetAllFields(message):
  187. SetAllNonLazyFields(message)
  188. message.optional_lazy_message.bb = 127
  189. def SetAllExtensions(message):
  190. """Sets every extension in the message to a unique value.
  191. Args:
  192. message: A unittest_pb2.TestAllExtensions instance.
  193. """
  194. extensions = message.Extensions
  195. pb2 = unittest_pb2
  196. import_pb2 = unittest_import_pb2
  197. #
  198. # Optional fields.
  199. #
  200. extensions[pb2.optional_int32_extension] = 101
  201. extensions[pb2.optional_int64_extension] = 102
  202. extensions[pb2.optional_uint32_extension] = 103
  203. extensions[pb2.optional_uint64_extension] = 104
  204. extensions[pb2.optional_sint32_extension] = 105
  205. extensions[pb2.optional_sint64_extension] = 106
  206. extensions[pb2.optional_fixed32_extension] = 107
  207. extensions[pb2.optional_fixed64_extension] = 108
  208. extensions[pb2.optional_sfixed32_extension] = 109
  209. extensions[pb2.optional_sfixed64_extension] = 110
  210. extensions[pb2.optional_float_extension] = 111
  211. extensions[pb2.optional_double_extension] = 112
  212. extensions[pb2.optional_bool_extension] = True
  213. extensions[pb2.optional_string_extension] = u'115'
  214. extensions[pb2.optional_bytes_extension] = b'116'
  215. extensions[pb2.optionalgroup_extension].a = 117
  216. extensions[pb2.optional_nested_message_extension].bb = 118
  217. extensions[pb2.optional_foreign_message_extension].c = 119
  218. extensions[pb2.optional_import_message_extension].d = 120
  219. extensions[pb2.optional_public_import_message_extension].e = 126
  220. extensions[pb2.optional_lazy_message_extension].bb = 127
  221. extensions[pb2.optional_nested_enum_extension] = pb2.TestAllTypes.BAZ
  222. extensions[pb2.optional_nested_enum_extension] = pb2.TestAllTypes.BAZ
  223. extensions[pb2.optional_foreign_enum_extension] = pb2.FOREIGN_BAZ
  224. extensions[pb2.optional_import_enum_extension] = import_pb2.IMPORT_BAZ
  225. extensions[pb2.optional_string_piece_extension] = u'124'
  226. extensions[pb2.optional_cord_extension] = u'125'
  227. #
  228. # Repeated fields.
  229. #
  230. extensions[pb2.repeated_int32_extension].append(201)
  231. extensions[pb2.repeated_int64_extension].append(202)
  232. extensions[pb2.repeated_uint32_extension].append(203)
  233. extensions[pb2.repeated_uint64_extension].append(204)
  234. extensions[pb2.repeated_sint32_extension].append(205)
  235. extensions[pb2.repeated_sint64_extension].append(206)
  236. extensions[pb2.repeated_fixed32_extension].append(207)
  237. extensions[pb2.repeated_fixed64_extension].append(208)
  238. extensions[pb2.repeated_sfixed32_extension].append(209)
  239. extensions[pb2.repeated_sfixed64_extension].append(210)
  240. extensions[pb2.repeated_float_extension].append(211)
  241. extensions[pb2.repeated_double_extension].append(212)
  242. extensions[pb2.repeated_bool_extension].append(True)
  243. extensions[pb2.repeated_string_extension].append(u'215')
  244. extensions[pb2.repeated_bytes_extension].append(b'216')
  245. extensions[pb2.repeatedgroup_extension].add().a = 217
  246. extensions[pb2.repeated_nested_message_extension].add().bb = 218
  247. extensions[pb2.repeated_foreign_message_extension].add().c = 219
  248. extensions[pb2.repeated_import_message_extension].add().d = 220
  249. extensions[pb2.repeated_lazy_message_extension].add().bb = 227
  250. extensions[pb2.repeated_nested_enum_extension].append(pb2.TestAllTypes.BAR)
  251. extensions[pb2.repeated_foreign_enum_extension].append(pb2.FOREIGN_BAR)
  252. extensions[pb2.repeated_import_enum_extension].append(import_pb2.IMPORT_BAR)
  253. extensions[pb2.repeated_string_piece_extension].append(u'224')
  254. extensions[pb2.repeated_cord_extension].append(u'225')
  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(u'315')
  270. extensions[pb2.repeated_bytes_extension].append(b'316')
  271. extensions[pb2.repeatedgroup_extension].add().a = 317
  272. extensions[pb2.repeated_nested_message_extension].add().bb = 318
  273. extensions[pb2.repeated_foreign_message_extension].add().c = 319
  274. extensions[pb2.repeated_import_message_extension].add().d = 320
  275. extensions[pb2.repeated_lazy_message_extension].add().bb = 327
  276. extensions[pb2.repeated_nested_enum_extension].append(pb2.TestAllTypes.BAZ)
  277. extensions[pb2.repeated_foreign_enum_extension].append(pb2.FOREIGN_BAZ)
  278. extensions[pb2.repeated_import_enum_extension].append(import_pb2.IMPORT_BAZ)
  279. extensions[pb2.repeated_string_piece_extension].append(u'324')
  280. extensions[pb2.repeated_cord_extension].append(u'325')
  281. #
  282. # Fields with defaults.
  283. #
  284. extensions[pb2.default_int32_extension] = 401
  285. extensions[pb2.default_int64_extension] = 402
  286. extensions[pb2.default_uint32_extension] = 403
  287. extensions[pb2.default_uint64_extension] = 404
  288. extensions[pb2.default_sint32_extension] = 405
  289. extensions[pb2.default_sint64_extension] = 406
  290. extensions[pb2.default_fixed32_extension] = 407
  291. extensions[pb2.default_fixed64_extension] = 408
  292. extensions[pb2.default_sfixed32_extension] = 409
  293. extensions[pb2.default_sfixed64_extension] = 410
  294. extensions[pb2.default_float_extension] = 411
  295. extensions[pb2.default_double_extension] = 412
  296. extensions[pb2.default_bool_extension] = False
  297. extensions[pb2.default_string_extension] = u'415'
  298. extensions[pb2.default_bytes_extension] = b'416'
  299. extensions[pb2.default_nested_enum_extension] = pb2.TestAllTypes.FOO
  300. extensions[pb2.default_foreign_enum_extension] = pb2.FOREIGN_FOO
  301. extensions[pb2.default_import_enum_extension] = import_pb2.IMPORT_FOO
  302. extensions[pb2.default_string_piece_extension] = u'424'
  303. extensions[pb2.default_cord_extension] = '425'
  304. extensions[pb2.oneof_uint32_extension] = 601
  305. extensions[pb2.oneof_nested_message_extension].bb = 602
  306. extensions[pb2.oneof_string_extension] = u'603'
  307. extensions[pb2.oneof_bytes_extension] = b'604'
  308. def SetAllFieldsAndExtensions(message):
  309. """Sets every field and extension in the message to a unique value.
  310. Args:
  311. message: A unittest_pb2.TestAllExtensions message.
  312. """
  313. message.my_int = 1
  314. message.my_string = 'foo'
  315. message.my_float = 1.0
  316. message.Extensions[unittest_pb2.my_extension_int] = 23
  317. message.Extensions[unittest_pb2.my_extension_string] = 'bar'
  318. def ExpectAllFieldsAndExtensionsInOrder(serialized):
  319. """Ensures that serialized is the serialization we expect for a message
  320. filled with SetAllFieldsAndExtensions(). (Specifically, ensures that the
  321. serialization is in canonical, tag-number order).
  322. """
  323. my_extension_int = unittest_pb2.my_extension_int
  324. my_extension_string = unittest_pb2.my_extension_string
  325. expected_strings = []
  326. message = unittest_pb2.TestFieldOrderings()
  327. message.my_int = 1 # Field 1.
  328. expected_strings.append(message.SerializeToString())
  329. message.Clear()
  330. message.Extensions[my_extension_int] = 23 # Field 5.
  331. expected_strings.append(message.SerializeToString())
  332. message.Clear()
  333. message.my_string = 'foo' # Field 11.
  334. expected_strings.append(message.SerializeToString())
  335. message.Clear()
  336. message.Extensions[my_extension_string] = 'bar' # Field 50.
  337. expected_strings.append(message.SerializeToString())
  338. message.Clear()
  339. message.my_float = 1.0
  340. expected_strings.append(message.SerializeToString())
  341. message.Clear()
  342. expected = b''.join(expected_strings)
  343. if expected != serialized:
  344. raise ValueError('Expected %r, found %r' % (expected, serialized))
  345. def ExpectAllFieldsSet(test_case, message):
  346. """Check all fields for correct values have after Set*Fields() is called."""
  347. test_case.assertTrue(message.HasField('optional_int32'))
  348. test_case.assertTrue(message.HasField('optional_int64'))
  349. test_case.assertTrue(message.HasField('optional_uint32'))
  350. test_case.assertTrue(message.HasField('optional_uint64'))
  351. test_case.assertTrue(message.HasField('optional_sint32'))
  352. test_case.assertTrue(message.HasField('optional_sint64'))
  353. test_case.assertTrue(message.HasField('optional_fixed32'))
  354. test_case.assertTrue(message.HasField('optional_fixed64'))
  355. test_case.assertTrue(message.HasField('optional_sfixed32'))
  356. test_case.assertTrue(message.HasField('optional_sfixed64'))
  357. test_case.assertTrue(message.HasField('optional_float'))
  358. test_case.assertTrue(message.HasField('optional_double'))
  359. test_case.assertTrue(message.HasField('optional_bool'))
  360. test_case.assertTrue(message.HasField('optional_string'))
  361. test_case.assertTrue(message.HasField('optional_bytes'))
  362. if IsProto2(message):
  363. test_case.assertTrue(message.HasField('optionalgroup'))
  364. test_case.assertTrue(message.HasField('optional_nested_message'))
  365. test_case.assertTrue(message.HasField('optional_foreign_message'))
  366. test_case.assertTrue(message.HasField('optional_import_message'))
  367. test_case.assertTrue(message.optionalgroup.HasField('a'))
  368. test_case.assertTrue(message.optional_nested_message.HasField('bb'))
  369. test_case.assertTrue(message.optional_foreign_message.HasField('c'))
  370. test_case.assertTrue(message.optional_import_message.HasField('d'))
  371. test_case.assertTrue(message.HasField('optional_nested_enum'))
  372. test_case.assertTrue(message.HasField('optional_foreign_enum'))
  373. if IsProto2(message):
  374. test_case.assertTrue(message.HasField('optional_import_enum'))
  375. test_case.assertTrue(message.HasField('optional_string_piece'))
  376. test_case.assertTrue(message.HasField('optional_cord'))
  377. test_case.assertEqual(101, message.optional_int32)
  378. test_case.assertEqual(102, message.optional_int64)
  379. test_case.assertEqual(103, message.optional_uint32)
  380. test_case.assertEqual(104, message.optional_uint64)
  381. test_case.assertEqual(105, message.optional_sint32)
  382. test_case.assertEqual(106, message.optional_sint64)
  383. test_case.assertEqual(107, message.optional_fixed32)
  384. test_case.assertEqual(108, message.optional_fixed64)
  385. test_case.assertEqual(109, message.optional_sfixed32)
  386. test_case.assertEqual(110, message.optional_sfixed64)
  387. test_case.assertEqual(111, message.optional_float)
  388. test_case.assertEqual(112, message.optional_double)
  389. test_case.assertEqual(True, message.optional_bool)
  390. test_case.assertEqual('115', message.optional_string)
  391. test_case.assertEqual(b'116', message.optional_bytes)
  392. if IsProto2(message):
  393. test_case.assertEqual(117, message.optionalgroup.a)
  394. test_case.assertEqual(118, message.optional_nested_message.bb)
  395. test_case.assertEqual(119, message.optional_foreign_message.c)
  396. test_case.assertEqual(120, message.optional_import_message.d)
  397. test_case.assertEqual(126, message.optional_public_import_message.e)
  398. test_case.assertEqual(127, message.optional_lazy_message.bb)
  399. test_case.assertEqual(unittest_pb2.TestAllTypes.BAZ,
  400. message.optional_nested_enum)
  401. test_case.assertEqual(unittest_pb2.FOREIGN_BAZ,
  402. message.optional_foreign_enum)
  403. if IsProto2(message):
  404. test_case.assertEqual(unittest_import_pb2.IMPORT_BAZ,
  405. message.optional_import_enum)
  406. # -----------------------------------------------------------------
  407. test_case.assertEqual(2, len(message.repeated_int32))
  408. test_case.assertEqual(2, len(message.repeated_int64))
  409. test_case.assertEqual(2, len(message.repeated_uint32))
  410. test_case.assertEqual(2, len(message.repeated_uint64))
  411. test_case.assertEqual(2, len(message.repeated_sint32))
  412. test_case.assertEqual(2, len(message.repeated_sint64))
  413. test_case.assertEqual(2, len(message.repeated_fixed32))
  414. test_case.assertEqual(2, len(message.repeated_fixed64))
  415. test_case.assertEqual(2, len(message.repeated_sfixed32))
  416. test_case.assertEqual(2, len(message.repeated_sfixed64))
  417. test_case.assertEqual(2, len(message.repeated_float))
  418. test_case.assertEqual(2, len(message.repeated_double))
  419. test_case.assertEqual(2, len(message.repeated_bool))
  420. test_case.assertEqual(2, len(message.repeated_string))
  421. test_case.assertEqual(2, len(message.repeated_bytes))
  422. if IsProto2(message):
  423. test_case.assertEqual(2, len(message.repeatedgroup))
  424. test_case.assertEqual(2, len(message.repeated_nested_message))
  425. test_case.assertEqual(2, len(message.repeated_foreign_message))
  426. test_case.assertEqual(2, len(message.repeated_import_message))
  427. test_case.assertEqual(2, len(message.repeated_nested_enum))
  428. test_case.assertEqual(2, len(message.repeated_foreign_enum))
  429. if IsProto2(message):
  430. test_case.assertEqual(2, len(message.repeated_import_enum))
  431. test_case.assertEqual(2, len(message.repeated_string_piece))
  432. test_case.assertEqual(2, len(message.repeated_cord))
  433. test_case.assertEqual(201, message.repeated_int32[0])
  434. test_case.assertEqual(202, message.repeated_int64[0])
  435. test_case.assertEqual(203, message.repeated_uint32[0])
  436. test_case.assertEqual(204, message.repeated_uint64[0])
  437. test_case.assertEqual(205, message.repeated_sint32[0])
  438. test_case.assertEqual(206, message.repeated_sint64[0])
  439. test_case.assertEqual(207, message.repeated_fixed32[0])
  440. test_case.assertEqual(208, message.repeated_fixed64[0])
  441. test_case.assertEqual(209, message.repeated_sfixed32[0])
  442. test_case.assertEqual(210, message.repeated_sfixed64[0])
  443. test_case.assertEqual(211, message.repeated_float[0])
  444. test_case.assertEqual(212, message.repeated_double[0])
  445. test_case.assertEqual(True, message.repeated_bool[0])
  446. test_case.assertEqual('215', message.repeated_string[0])
  447. test_case.assertEqual(b'216', message.repeated_bytes[0])
  448. if IsProto2(message):
  449. test_case.assertEqual(217, message.repeatedgroup[0].a)
  450. test_case.assertEqual(218, message.repeated_nested_message[0].bb)
  451. test_case.assertEqual(219, message.repeated_foreign_message[0].c)
  452. test_case.assertEqual(220, message.repeated_import_message[0].d)
  453. test_case.assertEqual(227, message.repeated_lazy_message[0].bb)
  454. test_case.assertEqual(unittest_pb2.TestAllTypes.BAR,
  455. message.repeated_nested_enum[0])
  456. test_case.assertEqual(unittest_pb2.FOREIGN_BAR,
  457. message.repeated_foreign_enum[0])
  458. if IsProto2(message):
  459. test_case.assertEqual(unittest_import_pb2.IMPORT_BAR,
  460. message.repeated_import_enum[0])
  461. test_case.assertEqual(301, message.repeated_int32[1])
  462. test_case.assertEqual(302, message.repeated_int64[1])
  463. test_case.assertEqual(303, message.repeated_uint32[1])
  464. test_case.assertEqual(304, message.repeated_uint64[1])
  465. test_case.assertEqual(305, message.repeated_sint32[1])
  466. test_case.assertEqual(306, message.repeated_sint64[1])
  467. test_case.assertEqual(307, message.repeated_fixed32[1])
  468. test_case.assertEqual(308, message.repeated_fixed64[1])
  469. test_case.assertEqual(309, message.repeated_sfixed32[1])
  470. test_case.assertEqual(310, message.repeated_sfixed64[1])
  471. test_case.assertEqual(311, message.repeated_float[1])
  472. test_case.assertEqual(312, message.repeated_double[1])
  473. test_case.assertEqual(False, message.repeated_bool[1])
  474. test_case.assertEqual('315', message.repeated_string[1])
  475. test_case.assertEqual(b'316', message.repeated_bytes[1])
  476. if IsProto2(message):
  477. test_case.assertEqual(317, message.repeatedgroup[1].a)
  478. test_case.assertEqual(318, message.repeated_nested_message[1].bb)
  479. test_case.assertEqual(319, message.repeated_foreign_message[1].c)
  480. test_case.assertEqual(320, message.repeated_import_message[1].d)
  481. test_case.assertEqual(327, message.repeated_lazy_message[1].bb)
  482. test_case.assertEqual(unittest_pb2.TestAllTypes.BAZ,
  483. message.repeated_nested_enum[1])
  484. test_case.assertEqual(unittest_pb2.FOREIGN_BAZ,
  485. message.repeated_foreign_enum[1])
  486. if IsProto2(message):
  487. test_case.assertEqual(unittest_import_pb2.IMPORT_BAZ,
  488. message.repeated_import_enum[1])
  489. # -----------------------------------------------------------------
  490. if IsProto2(message):
  491. test_case.assertTrue(message.HasField('default_int32'))
  492. test_case.assertTrue(message.HasField('default_int64'))
  493. test_case.assertTrue(message.HasField('default_uint32'))
  494. test_case.assertTrue(message.HasField('default_uint64'))
  495. test_case.assertTrue(message.HasField('default_sint32'))
  496. test_case.assertTrue(message.HasField('default_sint64'))
  497. test_case.assertTrue(message.HasField('default_fixed32'))
  498. test_case.assertTrue(message.HasField('default_fixed64'))
  499. test_case.assertTrue(message.HasField('default_sfixed32'))
  500. test_case.assertTrue(message.HasField('default_sfixed64'))
  501. test_case.assertTrue(message.HasField('default_float'))
  502. test_case.assertTrue(message.HasField('default_double'))
  503. test_case.assertTrue(message.HasField('default_bool'))
  504. test_case.assertTrue(message.HasField('default_string'))
  505. test_case.assertTrue(message.HasField('default_bytes'))
  506. test_case.assertTrue(message.HasField('default_nested_enum'))
  507. test_case.assertTrue(message.HasField('default_foreign_enum'))
  508. test_case.assertTrue(message.HasField('default_import_enum'))
  509. test_case.assertEqual(401, message.default_int32)
  510. test_case.assertEqual(402, message.default_int64)
  511. test_case.assertEqual(403, message.default_uint32)
  512. test_case.assertEqual(404, message.default_uint64)
  513. test_case.assertEqual(405, message.default_sint32)
  514. test_case.assertEqual(406, message.default_sint64)
  515. test_case.assertEqual(407, message.default_fixed32)
  516. test_case.assertEqual(408, message.default_fixed64)
  517. test_case.assertEqual(409, message.default_sfixed32)
  518. test_case.assertEqual(410, message.default_sfixed64)
  519. test_case.assertEqual(411, message.default_float)
  520. test_case.assertEqual(412, message.default_double)
  521. test_case.assertEqual(False, message.default_bool)
  522. test_case.assertEqual('415', message.default_string)
  523. test_case.assertEqual(b'416', message.default_bytes)
  524. test_case.assertEqual(unittest_pb2.TestAllTypes.FOO,
  525. message.default_nested_enum)
  526. test_case.assertEqual(unittest_pb2.FOREIGN_FOO,
  527. message.default_foreign_enum)
  528. test_case.assertEqual(unittest_import_pb2.IMPORT_FOO,
  529. message.default_import_enum)
  530. def GoldenFile(filename):
  531. """Finds the given golden file and returns a file object representing it."""
  532. # Search up the directory tree looking for the C++ protobuf source code.
  533. path = '.'
  534. while os.path.exists(path):
  535. if os.path.exists(os.path.join(path, 'src/google/protobuf')):
  536. # Found it. Load the golden file from the testdata directory.
  537. full_path = os.path.join(path, 'src/google/protobuf/testdata', filename)
  538. return open(full_path, 'rb')
  539. path = os.path.join(path, '..')
  540. # Search internally.
  541. path = '.'
  542. full_path = os.path.join(path, 'third_party/py/google/protobuf/testdata',
  543. filename)
  544. if os.path.exists(full_path):
  545. # Found it. Load the golden file from the testdata directory.
  546. return open(full_path, 'rb')
  547. raise RuntimeError(
  548. 'Could not find golden files. This test must be run from within the '
  549. 'protobuf source package so that it can read test data files from the '
  550. 'C++ source tree.')
  551. def GoldenFileData(filename):
  552. """Finds the given golden file and returns its contents."""
  553. with GoldenFile(filename) as f:
  554. return f.read()
  555. def SetAllPackedFields(message):
  556. """Sets every field in the message to a unique value.
  557. Args:
  558. message: A TestPackedTypes instance.
  559. """
  560. message.packed_int32.extend([601, 701])
  561. message.packed_int64.extend([602, 702])
  562. message.packed_uint32.extend([603, 703])
  563. message.packed_uint64.extend([604, 704])
  564. message.packed_sint32.extend([605, 705])
  565. message.packed_sint64.extend([606, 706])
  566. message.packed_fixed32.extend([607, 707])
  567. message.packed_fixed64.extend([608, 708])
  568. message.packed_sfixed32.extend([609, 709])
  569. message.packed_sfixed64.extend([610, 710])
  570. message.packed_float.extend([611.0, 711.0])
  571. message.packed_double.extend([612.0, 712.0])
  572. message.packed_bool.extend([True, False])
  573. message.packed_enum.extend([unittest_pb2.FOREIGN_BAR,
  574. unittest_pb2.FOREIGN_BAZ])
  575. def SetAllPackedExtensions(message):
  576. """Sets every extension in the message to a unique value.
  577. Args:
  578. message: A unittest_pb2.TestPackedExtensions instance.
  579. """
  580. extensions = message.Extensions
  581. pb2 = unittest_pb2
  582. extensions[pb2.packed_int32_extension].extend([601, 701])
  583. extensions[pb2.packed_int64_extension].extend([602, 702])
  584. extensions[pb2.packed_uint32_extension].extend([603, 703])
  585. extensions[pb2.packed_uint64_extension].extend([604, 704])
  586. extensions[pb2.packed_sint32_extension].extend([605, 705])
  587. extensions[pb2.packed_sint64_extension].extend([606, 706])
  588. extensions[pb2.packed_fixed32_extension].extend([607, 707])
  589. extensions[pb2.packed_fixed64_extension].extend([608, 708])
  590. extensions[pb2.packed_sfixed32_extension].extend([609, 709])
  591. extensions[pb2.packed_sfixed64_extension].extend([610, 710])
  592. extensions[pb2.packed_float_extension].extend([611.0, 711.0])
  593. extensions[pb2.packed_double_extension].extend([612.0, 712.0])
  594. extensions[pb2.packed_bool_extension].extend([True, False])
  595. extensions[pb2.packed_enum_extension].extend([unittest_pb2.FOREIGN_BAR,
  596. unittest_pb2.FOREIGN_BAZ])
  597. def SetAllUnpackedFields(message):
  598. """Sets every field in the message to a unique value.
  599. Args:
  600. message: A unittest_pb2.TestUnpackedTypes instance.
  601. """
  602. message.unpacked_int32.extend([601, 701])
  603. message.unpacked_int64.extend([602, 702])
  604. message.unpacked_uint32.extend([603, 703])
  605. message.unpacked_uint64.extend([604, 704])
  606. message.unpacked_sint32.extend([605, 705])
  607. message.unpacked_sint64.extend([606, 706])
  608. message.unpacked_fixed32.extend([607, 707])
  609. message.unpacked_fixed64.extend([608, 708])
  610. message.unpacked_sfixed32.extend([609, 709])
  611. message.unpacked_sfixed64.extend([610, 710])
  612. message.unpacked_float.extend([611.0, 711.0])
  613. message.unpacked_double.extend([612.0, 712.0])
  614. message.unpacked_bool.extend([True, False])
  615. message.unpacked_enum.extend([unittest_pb2.FOREIGN_BAR,
  616. unittest_pb2.FOREIGN_BAZ])
  617. class NonStandardInteger(numbers.Integral):
  618. """An integer object that does not subclass int.
  619. This is used to verify that both C++ and regular proto systems can handle
  620. integer others than int and long and that they handle them in predictable
  621. ways.
  622. NonStandardInteger is the minimal legal specification for a custom Integral.
  623. As such, it does not support 0 < x < 5 and it is not hashable.
  624. Note: This is added here instead of relying on numpy or a similar library
  625. with custom integers to limit dependencies.
  626. """
  627. def __init__(self, val, error_string_on_conversion=None):
  628. assert isinstance(val, numbers.Integral)
  629. if isinstance(val, NonStandardInteger):
  630. val = val.val
  631. self.val = val
  632. self.error_string_on_conversion = error_string_on_conversion
  633. def __long__(self):
  634. if self.error_string_on_conversion:
  635. raise RuntimeError(self.error_string_on_conversion)
  636. return long(self.val)
  637. def __abs__(self):
  638. return NonStandardInteger(operator.abs(self.val))
  639. def __add__(self, y):
  640. return NonStandardInteger(operator.add(self.val, y))
  641. def __div__(self, y):
  642. return NonStandardInteger(operator.div(self.val, y))
  643. def __eq__(self, y):
  644. return operator.eq(self.val, y)
  645. def __floordiv__(self, y):
  646. return NonStandardInteger(operator.floordiv(self.val, y))
  647. def __truediv__(self, y):
  648. return NonStandardInteger(operator.truediv(self.val, y))
  649. def __invert__(self):
  650. return NonStandardInteger(operator.invert(self.val))
  651. def __mod__(self, y):
  652. return NonStandardInteger(operator.mod(self.val, y))
  653. def __mul__(self, y):
  654. return NonStandardInteger(operator.mul(self.val, y))
  655. def __neg__(self):
  656. return NonStandardInteger(operator.neg(self.val))
  657. def __pos__(self):
  658. return NonStandardInteger(operator.pos(self.val))
  659. def __pow__(self, y):
  660. return NonStandardInteger(operator.pow(self.val, y))
  661. def __trunc__(self):
  662. return int(self.val)
  663. def __radd__(self, y):
  664. return NonStandardInteger(operator.add(y, self.val))
  665. def __rdiv__(self, y):
  666. return NonStandardInteger(operator.div(y, self.val))
  667. def __rmod__(self, y):
  668. return NonStandardInteger(operator.mod(y, self.val))
  669. def __rmul__(self, y):
  670. return NonStandardInteger(operator.mul(y, self.val))
  671. def __rpow__(self, y):
  672. return NonStandardInteger(operator.pow(y, self.val))
  673. def __rfloordiv__(self, y):
  674. return NonStandardInteger(operator.floordiv(y, self.val))
  675. def __rtruediv__(self, y):
  676. return NonStandardInteger(operator.truediv(y, self.val))
  677. def __lshift__(self, y):
  678. return NonStandardInteger(operator.lshift(self.val, y))
  679. def __rshift__(self, y):
  680. return NonStandardInteger(operator.rshift(self.val, y))
  681. def __rlshift__(self, y):
  682. return NonStandardInteger(operator.lshift(y, self.val))
  683. def __rrshift__(self, y):
  684. return NonStandardInteger(operator.rshift(y, self.val))
  685. def __le__(self, y):
  686. if isinstance(y, NonStandardInteger):
  687. y = y.val
  688. return operator.le(self.val, y)
  689. def __lt__(self, y):
  690. if isinstance(y, NonStandardInteger):
  691. y = y.val
  692. return operator.lt(self.val, y)
  693. def __and__(self, y):
  694. return NonStandardInteger(operator.and_(self.val, y))
  695. def __or__(self, y):
  696. return NonStandardInteger(operator.or_(self.val, y))
  697. def __xor__(self, y):
  698. return NonStandardInteger(operator.xor(self.val, y))
  699. def __rand__(self, y):
  700. return NonStandardInteger(operator.and_(y, self.val))
  701. def __ror__(self, y):
  702. return NonStandardInteger(operator.or_(y, self.val))
  703. def __rxor__(self, y):
  704. return NonStandardInteger(operator.xor(y, self.val))
  705. def __bool__(self):
  706. return self.val
  707. def __nonzero__(self):
  708. return self.val
  709. def __ceil__(self):
  710. return self
  711. def __floor__(self):
  712. return self
  713. def __int__(self):
  714. if self.error_string_on_conversion:
  715. raise RuntimeError(self.error_string_on_conversion)
  716. return int(self.val)
  717. def __round__(self):
  718. return self
  719. def __repr__(self):
  720. return 'NonStandardInteger(%s)' % self.val