|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import sys |
| 6 | +import unittest |
| 7 | +import tarantool |
| 8 | + |
| 9 | +from .lib.skip import skip_or_run_mp_bin_test, skip_or_run_varbinary_test |
| 10 | +from .lib.tarantool_server import TarantoolServer |
| 11 | + |
| 12 | +class TestSuite_Encoding(unittest.TestCase): |
| 13 | + @classmethod |
| 14 | + def setUpClass(self): |
| 15 | + print(' ENCODING '.center(70, '='), file=sys.stderr) |
| 16 | + print('-' * 70, file=sys.stderr) |
| 17 | + self.srv = TarantoolServer() |
| 18 | + self.srv.script = 'test/suites/box.lua' |
| 19 | + self.srv.start() |
| 20 | + |
| 21 | + self.srv.admin(""" |
| 22 | + box.schema.user.create('test', { password = 'test' }) |
| 23 | + box.schema.user.grant('test', 'execute,read,write', 'universe') |
| 24 | + """) |
| 25 | + |
| 26 | + args = [self.srv.host, self.srv.args['primary']] |
| 27 | + kwargs = { 'user': 'test', 'password': 'test' } |
| 28 | + self.con_encoding_utf8 = tarantool.Connection(*args, encoding='utf-8', **kwargs) |
| 29 | + self.con_encoding_none = tarantool.Connection(*args, encoding=None, **kwargs) |
| 30 | + self.conns = [self.con_encoding_utf8, self.con_encoding_none] |
| 31 | + |
| 32 | + self.srv.admin("box.schema.create_space('space_str')") |
| 33 | + self.srv.admin(""" |
| 34 | + box.space['space_str']:create_index('primary', { |
| 35 | + type = 'tree', |
| 36 | + parts = {1, 'str'}, |
| 37 | + unique = true}) |
| 38 | + """.replace('\n', ' ')) |
| 39 | + |
| 40 | + self.srv.admin("box.schema.create_space('space_varbin')") |
| 41 | + self.srv.admin(r""" |
| 42 | + box.space['space_varbin']:format({ |
| 43 | + { |
| 44 | + 'id', |
| 45 | + type = 'number', |
| 46 | + is_nullable = false |
| 47 | + }, |
| 48 | + { |
| 49 | + 'varbin', |
| 50 | + type = 'varbinary', |
| 51 | + is_nullable = false, |
| 52 | + } |
| 53 | + }) |
| 54 | + """.replace('\n', ' ')) |
| 55 | + self.srv.admin(""" |
| 56 | + box.space['space_varbin']:create_index('id', { |
| 57 | + type = 'tree', |
| 58 | + parts = {1, 'number'}, |
| 59 | + unique = true}) |
| 60 | + """.replace('\n', ' ')) |
| 61 | + self.srv.admin(""" |
| 62 | + box.space['space_varbin']:create_index('varbin', { |
| 63 | + type = 'tree', |
| 64 | + parts = {2, 'varbinary'}, |
| 65 | + unique = true}) |
| 66 | + """.replace('\n', ' ')) |
| 67 | + |
| 68 | + self.srv.admin(r""" |
| 69 | + local buffer = require('buffer') |
| 70 | + local ffi = require('ffi') |
| 71 | +
|
| 72 | + function encode_bin(bytes) |
| 73 | + local tmpbuf = buffer.ibuf() |
| 74 | + local p = tmpbuf:alloc(3 + #bytes) |
| 75 | + p[0] = 0x91 |
| 76 | + p[1] = 0xC4 |
| 77 | + p[2] = #bytes |
| 78 | + for i, c in pairs(bytes) do |
| 79 | + p[i + 3 - 1] = c |
| 80 | + end |
| 81 | + return tmpbuf |
| 82 | + end |
| 83 | +
|
| 84 | + function bintuple_insert(space, bytes) |
| 85 | + local tmpbuf = encode_bin(bytes) |
| 86 | + ffi.cdef[[ |
| 87 | + int box_insert(uint32_t space_id, const char *tuple, const char *tuple_end, box_tuple_t **result); |
| 88 | + ]] |
| 89 | + ffi.C.box_insert(space.id, tmpbuf.rpos, tmpbuf.wpos, nil) |
| 90 | + end |
| 91 | + """) |
| 92 | + |
| 93 | + self.srv.admin(""" |
| 94 | + function get_type(arg) |
| 95 | + return type(arg) |
| 96 | + end |
| 97 | + """) |
| 98 | + |
| 99 | + def assertNotRaises(self, func, *args, **kwargs): |
| 100 | + try: |
| 101 | + func(*args, **kwargs) |
| 102 | + except Exception as e: |
| 103 | + self.fail('Function raised Exception: %s' % repr(e)) |
| 104 | + |
| 105 | + def setUp(self): |
| 106 | + # prevent a remote tarantool from clean our session |
| 107 | + if self.srv.is_started(): |
| 108 | + self.srv.touch_lock() |
| 109 | + |
| 110 | + # encoding = 'utf-8' |
| 111 | + # |
| 112 | + # Python 3 -> Tarantool -> Python 3 |
| 113 | + # str -> mp_str (string) -> str |
| 114 | + # bytes -> mp_bin (varbinary) -> bytes |
| 115 | + def test_01_01_str_encode_for_encoding_utf8_behavior(self): |
| 116 | + self.assertNotRaises( |
| 117 | + self.con_encoding_utf8.insert, |
| 118 | + 'space_str', [ 'test_01_01' ]) |
| 119 | + |
| 120 | + def test_01_02_string_decode_for_encoding_utf8_behavior(self): |
| 121 | + self.srv.admin(r"box.space['space_str']:insert{'test_01_02'}") |
| 122 | + |
| 123 | + resp = self.con_encoding_utf8.eval("return box.space.space_str:get('test_01_02')") |
| 124 | + self.assertSequenceEqual(resp, [['test_01_02']]) |
| 125 | + |
| 126 | + @skip_or_run_mp_bin_test |
| 127 | + @skip_or_run_varbinary_test |
| 128 | + def test_01_03_bytes_encode_for_encoding_utf8_behavior(self): |
| 129 | + self.assertNotRaises( |
| 130 | + self.con_encoding_utf8.insert, |
| 131 | + 'space_varbin', [ 103, bytes(bytearray.fromhex('DEADBEAF0103')) ]) |
| 132 | + |
| 133 | + @skip_or_run_mp_bin_test |
| 134 | + @skip_or_run_varbinary_test |
| 135 | + def test_01_04_varbinary_decode_for_encoding_utf8_behavior(self): |
| 136 | + self.con_encoding_utf8.execute(r""" |
| 137 | + INSERT INTO "space_varbin" VALUES (104, x'DEADBEAF0104'); |
| 138 | + """) |
| 139 | + |
| 140 | + resp = self.con_encoding_utf8.execute(r""" |
| 141 | + SELECT * FROM "space_varbin" WHERE "varbin" == x'DEADBEAF0104'; |
| 142 | + """) |
| 143 | + self.assertSequenceEqual(resp, [[104, bytes(bytearray.fromhex('DEADBEAF0104'))]]) |
| 144 | + |
| 145 | + # encoding = None |
| 146 | + # |
| 147 | + # Python 3 -> Tarantool -> Python 3 |
| 148 | + # bytes -> mp_str (string) -> bytes |
| 149 | + # str -> mp_str (string) |
| 150 | + # mp_bin (string) -> bytes |
| 151 | + def test_02_01_str_encode_for_encoding_none_behavior(self): |
| 152 | + self.assertNotRaises( |
| 153 | + self.con_encoding_none.insert, |
| 154 | + 'space_str', [ 'test_02_01' ]) |
| 155 | + |
| 156 | + def test_02_02_string_decode_for_encoding_none_behavior(self): |
| 157 | + self.srv.admin(r"box.space['space_str']:insert{'test_02_02'}") |
| 158 | + |
| 159 | + resp = self.con_encoding_none.eval("return box.space.space_str:get('test_02_02')") |
| 160 | + self.assertSequenceEqual(resp, [[b'test_02_02']]) |
| 161 | + |
| 162 | + def test_02_03_bytes_decode_for_encoding_utf8_behavior(self): |
| 163 | + self.assertNotRaises( |
| 164 | + self.con_encoding_none.insert, |
| 165 | + 'space_str', [ b'test_02_03' ]) |
| 166 | + |
| 167 | + @skip_or_run_mp_bin_test |
| 168 | + @skip_or_run_varbinary_test |
| 169 | + def test_02_04_varbinary_decode_for_encoding_utf8_behavior(self): |
| 170 | + self.con_encoding_utf8.execute(r""" |
| 171 | + INSERT INTO "space_varbin" VALUES (204, x'DEADBEAF0204'); |
| 172 | + """) |
| 173 | + |
| 174 | + resp = self.con_encoding_utf8.execute(r""" |
| 175 | + SELECT * FROM "space_varbin" WHERE "varbin" == x'DEADBEAF0204'; |
| 176 | + """) |
| 177 | + self.assertSequenceEqual(resp, [[204, bytes(bytearray.fromhex('DEADBEAF0204'))]]) |
| 178 | + |
| 179 | + @classmethod |
| 180 | + def tearDownClass(self): |
| 181 | + for con in self.conns: |
| 182 | + con.close() |
| 183 | + self.srv.stop() |
| 184 | + self.srv.clean() |
0 commit comments