binary.yaml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. desc: Tests of converstion to and from the RQL binary type
  2. tests:
  3. # Short binary data from 0 to 12 characters
  4. # Not fully implemented for JS as comparing Buffer objects is non-trivial
  5. - def:
  6. rb: s = "".force_encoding('BINARY')
  7. py: s = b''
  8. js: s = Buffer("", 'binary')
  9. - cd: r.binary(s)
  10. ot: s
  11. - cd: r.binary(s).count()
  12. ot: 0
  13. - def:
  14. rb: s = "\x00".force_encoding('BINARY')
  15. py: s = b'\x00'
  16. js: s = Buffer("\x00", 'binary')
  17. - cd: r.binary(s)
  18. ot: s
  19. - cd: r.binary(s).count()
  20. ot: 1
  21. - def:
  22. rb: s = "\x00\x42".force_encoding('BINARY')
  23. py: s = b'\x00\x42'
  24. js: s = Buffer("\x00\x42", 'binary')
  25. - cd: r.binary(s)
  26. ot: s
  27. - cd: r.binary(s).count()
  28. ot: 2
  29. - def:
  30. rb: s = "\x00\xfe\x7a".force_encoding('BINARY')
  31. py: s = b'\x00\xfe\x7a'
  32. js: s = Buffer("\x00\xfe\x7a", 'binary')
  33. - cd: r.binary(s)
  34. ot: s
  35. - cd: r.binary(s).count()
  36. ot: 3
  37. - def:
  38. rb: s = "\xed\xfe\x00\xba".force_encoding('BINARY')
  39. py: s = b'\xed\xfe\x00\xba'
  40. js: s = Buffer("\xed\xfe\x00\xba", 'binary')
  41. - cd: r.binary(s)
  42. ot: s
  43. - cd: r.binary(s).count()
  44. ot: 4
  45. - def:
  46. rb: s = "\x50\xf9\x00\x77\xf9".force_encoding('BINARY')
  47. py: s = b'\x50\xf9\x00\x77\xf9'
  48. js: s = Buffer("\x50\xf9\x00\x77\xf9", 'binary')
  49. - cd: r.binary(s)
  50. ot: s
  51. - cd: r.binary(s).count()
  52. ot: 5
  53. - def:
  54. rb: s = "\x2f\xe3\xb5\x57\x00\x92".force_encoding('BINARY')
  55. py: s = b'\x2f\xe3\xb5\x57\x00\x92'
  56. js: s = Buffer("\x2f\xe3\xb5\x57\x00\x92", 'binary')
  57. - cd: r.binary(s)
  58. ot: s
  59. - cd: r.binary(s).count()
  60. ot: 6
  61. - def:
  62. rb: s = "\xa9\x43\x54\xe9\x00\xf8\xfb".force_encoding('BINARY')
  63. py: s = b'\xa9\x43\x54\xe9\x00\xf8\xfb'
  64. js: s = Buffer("\xa9\x43\x54\xe9\x00\xf8\xfb", 'binary')
  65. - cd: r.binary(s)
  66. ot: s
  67. - cd: r.binary(s).count()
  68. ot: 7
  69. - def:
  70. rb: s = "\x57\xbb\xe5\x82\x8b\xd3\x00\xf9".force_encoding('BINARY')
  71. py: s = b'\x57\xbb\xe5\x82\x8b\xd3\x00\xf9'
  72. js: s = Buffer("\x57\xbb\xe5\x82\x8b\xd3\x00\xf9", 'binary')
  73. - cd: r.binary(s)
  74. ot: s
  75. - cd: r.binary(s).count()
  76. ot: 8
  77. - def:
  78. rb: s = "\x44\x1b\x3e\x00\x13\x19\x29\x2a\xbf".force_encoding('BINARY')
  79. py: s = b'\x44\x1b\x3e\x00\x13\x19\x29\x2a\xbf'
  80. js: s = Buffer("\x44\x1b\x3e\x00\x13\x19\x29\x2a\xbf", 'binary')
  81. - cd: r.binary(s)
  82. ot: s
  83. - cd: r.binary(s).count()
  84. ot: 9
  85. - def:
  86. rb: s = "\x8a\x1d\x09\x00\x5d\x60\x6b\x2e\x70\xd9".force_encoding('BINARY')
  87. py: s = b'\x8a\x1d\x09\x00\x5d\x60\x6b\x2e\x70\xd9'
  88. js: s = Buffer("\x8a\x1d\x09\x00\x5d\x60\x6b\x2e\x70\xd9", 'binary')
  89. - cd: r.binary(s)
  90. ot: s
  91. - cd: r.binary(s).count()
  92. ot: 10
  93. - def:
  94. rb: s = "\x00\xaf\x47\x4b\x38\x99\x14\x8d\x8f\x10\x51".force_encoding('BINARY')
  95. py: s = b'\x00\xaf\x47\x4b\x38\x99\x14\x8d\x8f\x10\x51'
  96. js: s = Buffer("\x00\xaf\x47\x4b\x38\x99\x14\x8d\x8f\x10\x51", 'binary')
  97. - cd: r.binary(s)
  98. ot: s
  99. - cd: r.binary(s).count()
  100. ot: 11
  101. - def:
  102. cd: s = "\x45\x39\x00\xf7\xc2\x37\xfd\xe0\x38\x82\x40\xa9".force_encoding('BINARY')
  103. py: s = b'\x45\x39\x00\xf7\xc2\x37\xfd\xe0\x38\x82\x40\xa9'
  104. js: s = Buffer("\x45\x39\x00\xf7\xc2\x37\xfd\xe0\x38\x82\x40\xa9", 'binary')
  105. - cd: r.binary(s)
  106. ot: s
  107. - cd: r.binary(s).count()
  108. ot: 12
  109. # Test comparisons
  110. # Binary objects to use, in order of increasing value
  111. - def:
  112. js: a = Buffer("\x00", 'binary')
  113. rb: a = "\x00".force_encoding('BINARY')
  114. py: a = b'\x00'
  115. - def:
  116. js: b = Buffer("\x00\x01", 'binary')
  117. rb: b = "\x00\x01".force_encoding('BINARY')
  118. py: b = b'\x00\x01'
  119. - def:
  120. js: c = Buffer("\x01", 'binary')
  121. rb: c = "\x01".force_encoding('BINARY')
  122. py: c = b'\x01'
  123. - def:
  124. js: d = Buffer("\x70\x22", 'binary')
  125. rb: d = "\x70\x22".force_encoding('BINARY')
  126. py: d = b'\x70\x22'
  127. - def:
  128. js: e = Buffer("\x80", 'binary')
  129. rb: e = "\x80".force_encoding('BINARY')
  130. py: e = b'\x80'
  131. - def:
  132. js: f = Buffer("\xFE", 'binary')
  133. rb: f = "\xFE".force_encoding('BINARY')
  134. py: f = b'\xFE'
  135. # a -> a
  136. - cd: r.binary(a).eq(r.binary(a))
  137. ot: true
  138. - cd: r.binary(a).le(r.binary(a))
  139. ot: true
  140. - cd: r.binary(a).ge(r.binary(a))
  141. ot: true
  142. - cd: r.binary(a).ne(r.binary(a))
  143. ot: false
  144. - cd: r.binary(a).lt(r.binary(a))
  145. ot: false
  146. - cd: r.binary(a).gt(r.binary(a))
  147. ot: false
  148. # a -> b
  149. - cd: r.binary(a).ne(r.binary(b))
  150. ot: true
  151. - cd: r.binary(a).lt(r.binary(b))
  152. ot: true
  153. - cd: r.binary(a).le(r.binary(b))
  154. ot: true
  155. - cd: r.binary(a).ge(r.binary(b))
  156. ot: false
  157. - cd: r.binary(a).gt(r.binary(b))
  158. ot: false
  159. - cd: r.binary(a).eq(r.binary(b))
  160. ot: false
  161. # b -> c
  162. - cd: r.binary(b).ne(r.binary(c))
  163. ot: true
  164. - cd: r.binary(b).lt(r.binary(c))
  165. ot: true
  166. - cd: r.binary(b).le(r.binary(c))
  167. ot: true
  168. - cd: r.binary(b).ge(r.binary(c))
  169. ot: false
  170. - cd: r.binary(b).gt(r.binary(c))
  171. ot: false
  172. - cd: r.binary(b).eq(r.binary(c))
  173. ot: false
  174. # c -> d
  175. - cd: r.binary(c).ne(r.binary(d))
  176. ot: true
  177. - cd: r.binary(c).lt(r.binary(d))
  178. ot: true
  179. - cd: r.binary(c).le(r.binary(d))
  180. ot: true
  181. - cd: r.binary(c).ge(r.binary(d))
  182. ot: false
  183. - cd: r.binary(c).gt(r.binary(d))
  184. ot: false
  185. - cd: r.binary(c).eq(r.binary(d))
  186. ot: false
  187. # d -> e
  188. - cd: r.binary(d).ne(r.binary(e))
  189. ot: true
  190. - cd: r.binary(d).lt(r.binary(e))
  191. ot: true
  192. - cd: r.binary(d).le(r.binary(e))
  193. ot: true
  194. - cd: r.binary(d).ge(r.binary(e))
  195. ot: false
  196. - cd: r.binary(d).gt(r.binary(e))
  197. ot: false
  198. - cd: r.binary(d).eq(r.binary(e))
  199. ot: false
  200. # e -> f
  201. - cd: r.binary(e).ne(r.binary(f))
  202. ot: true
  203. - cd: r.binary(e).lt(r.binary(f))
  204. ot: true
  205. - cd: r.binary(e).le(r.binary(f))
  206. ot: true
  207. - cd: r.binary(e).ge(r.binary(f))
  208. ot: false
  209. - cd: r.binary(e).gt(r.binary(f))
  210. ot: false
  211. - cd: r.binary(e).eq(r.binary(f))
  212. ot: false
  213. # f -> f
  214. - cd: r.binary(f).eq(r.binary(f))
  215. ot: true
  216. - cd: r.binary(f).le(r.binary(f))
  217. ot: true
  218. - cd: r.binary(f).ge(r.binary(f))
  219. ot: true
  220. - cd: r.binary(f).ne(r.binary(f))
  221. ot: false
  222. - cd: r.binary(f).lt(r.binary(f))
  223. ot: false
  224. - cd: r.binary(f).gt(r.binary(f))
  225. ot: false
  226. # Test encodings
  227. - py:
  228. cd: r.binary(u'イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム'.encode('utf-8'))
  229. ot: u'イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム'.encode('utf-8')
  230. py3:
  231. cd: r.binary(str('イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム').encode('utf-8'))
  232. ot: str('イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム').encode('utf-8')
  233. - py:
  234. cd: r.binary(u'ƀƁƂƃƄƅƆƇƈƉƊƋƌƍƎƏ'.encode('utf-16'))
  235. ot: u'ƀƁƂƃƄƅƆƇƈƉƊƋƌƍƎƏ'.encode('utf-16')
  236. py3:
  237. cd: r.binary(str('ƀƁƂƃƄƅƆƇƈƉƊƋƌƍƎƏ').encode('utf-16'))
  238. ot: str('ƀƁƂƃƄƅƆƇƈƉƊƋƌƍƎƏ').encode('utf-16')
  239. - py:
  240. cd: r.binary(u'lorem ipsum'.encode('ascii'))
  241. ot: u'lorem ipsum'.encode('ascii')
  242. py3:
  243. cd: r.binary(str('lorem ipsum').encode('ascii'))
  244. ot: str('lorem ipsum').encode('ascii')
  245. # Test coercions
  246. - py: r.binary(b'foo').coerce_to('string')
  247. ot: 'foo'
  248. - py:
  249. cd: r.binary(u'イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム'.encode('utf-8')).coerce_to('string')
  250. ot: u'イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム'
  251. py3:
  252. cd: r.binary(str('イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム').encode('utf-8')).coerce_to('string')
  253. ot: str('イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム')
  254. - py:
  255. cd: r.binary(u'lorem ipsum'.encode('ascii')).coerce_to('string')
  256. ot: u'lorem ipsum'
  257. py3:
  258. cd: r.binary(str('lorem ipsum').encode('ascii')).coerce_to('string')
  259. ot: str('lorem ipsum')
  260. - py: r.expr('foo').coerce_to('binary')
  261. ot: b'foo'
  262. - cd: r.binary(a).coerce_to('bool')
  263. ot: True
  264. - py: r.binary(b'foo').coerce_to('binary')
  265. ot: b'foo'
  266. # Test slice
  267. - py: r.binary(b'abcdefg').slice(-3,-1)
  268. ot: b'ef'
  269. - py: r.binary(b'abcdefg').slice(0, 2)
  270. ot: b'ab'
  271. - py: r.binary(b'abcdefg').slice(3, -1)
  272. ot: b'def'
  273. - py: r.binary(b'abcdefg').slice(-5, 5)
  274. ot: b'cde'
  275. - py: r.binary(b'abcdefg').slice(-8, 2)
  276. ot: b'ab'
  277. - py: r.binary(b'abcdefg').slice(5, 7)
  278. ot: b'fg'
  279. # Left side out-of-bound should clamp to index 0
  280. - py: r.binary(b'abcdefg').slice(-9, 2)
  281. ot: b'ab'
  282. # Right side out-of-bound should return the valid subset of the range
  283. - py: r.binary(b'abcdefg').slice(5, 9)
  284. ot: b'fg'
  285. # Test binary_format optarg
  286. - cd: r.binary(b)
  287. runopts:
  288. binary_format: "native"
  289. ot: b
  290. - cd: r.binary(b)
  291. runopts:
  292. binary_format: "raw"
  293. ot: {'$reql_type$':'BINARY','data':'AAE='}
  294. # Test r.binary of nested terms
  295. - cd: r.binary(r.expr("data"))
  296. ot:
  297. js: Buffer("data", "binary")
  298. rb: "data"
  299. py: b"data"
  300. - cd: r.binary(r.expr({}))
  301. ot: err('ReqlQueryLogicError', 'Expected type STRING but found OBJECT.', [])
  302. - cd: r.binary(r.expr([]))
  303. ot: err('ReqlQueryLogicError', 'Expected type STRING but found ARRAY.', [])
  304. # Test errors
  305. # Missing 'data' field
  306. - py: r.expr({'$reql_type$':'BINARY'})
  307. rb: r.expr({'$reql_type$':'BINARY'})
  308. ot: err('ReqlQueryLogicError','Invalid binary pseudotype:'+' lacking `data` key.',[])
  309. # Invalid base64 format
  310. - py: r.expr({'$reql_type$':'BINARY','data':'ABCDEFGH==AA'})
  311. ot: err('ReqlQueryLogicError','Invalid base64 format, data found after padding character \'=\'.',[])
  312. - py: r.expr({'$reql_type$':'BINARY','data':'ABCDEF==$'})
  313. ot: err('ReqlQueryLogicError','Invalid base64 format, data found after padding character \'=\'.',[])
  314. - py: r.expr({'$reql_type$':'BINARY','data':'A^CDEFGH'})
  315. ot: err('ReqlQueryLogicError','Invalid base64 character found:'+' \'^\'.',[])
  316. - py: r.expr({'$reql_type$':'BINARY','data':'ABCDE'})
  317. ot: err('ReqlQueryLogicError','Invalid base64 length:'+' 1 character remaining, cannot decode a full byte.',[])
  318. # Invalid coercions
  319. - cd: r.binary(a).coerce_to('array')
  320. ot: err('ReqlQueryLogicError','Cannot coerce BINARY to ARRAY.',[])
  321. - cd: r.binary(a).coerce_to('object')
  322. ot: err('ReqlQueryLogicError','Cannot coerce BINARY to OBJECT.',[])
  323. - cd: r.binary(a).coerce_to('number')
  324. ot: err('ReqlQueryLogicError','Cannot coerce BINARY to NUMBER.',[])
  325. - cd: r.binary(a).coerce_to('nu'+'ll')
  326. ot: err('ReqlQueryLogicError','Cannot coerce BINARY to NULL.',[])