|
| 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 | + def prepare_dml_test_spaces(self): |
| 14 | + self.space_created = self.adm("box.schema.create_space('space_1')") |
| 15 | + self.adm(""" |
| 16 | + box.space['space_1']:create_index('primary', { |
| 17 | + type = 'tree', |
| 18 | + parts = {1, 'num'}, |
| 19 | + unique = true}) |
| 20 | + """.replace('\n', ' ')) |
| 21 | + self.adm(""" |
| 22 | + box.space['space_1']:create_index('secondary', { |
| 23 | + type = 'tree', |
| 24 | + parts = {2, 'num', 3, 'str'}, |
| 25 | + unique = false}) |
| 26 | + """.replace('\n', ' ')) |
| 27 | + |
| 28 | + self.space_created = self.adm("box.schema.create_space('space_2')") |
| 29 | + self.adm(""" |
| 30 | + box.space['space_2']:create_index('primary', { |
| 31 | + type = 'hash', |
| 32 | + parts = {1, 'num'}, |
| 33 | + unique = true}) |
| 34 | + """.replace('\n', ' ')) |
| 35 | + |
| 36 | + def prepare_encoding_test_spaces(self): |
| 37 | + |
| 38 | + |
| 39 | + def require_modules(self): |
| 40 | + self.adm("json = require('json')") |
| 41 | + self.adm("fiber = require('fiber')") |
| 42 | + self.adm("uuid = require('uuid')") |
| 43 | + |
| 44 | + @classmethod |
| 45 | + def setUpClass(self): |
| 46 | + print(' DML '.center(70, '='), file=sys.stderr) |
| 47 | + print('-' * 70, file=sys.stderr) |
| 48 | + self.srv = TarantoolServer() |
| 49 | + self.srv.script = 'test/suites/box.lua' |
| 50 | + self.srv.start() |
| 51 | + |
| 52 | + self.srv.admin(""" |
| 53 | + box.schema.user.create('test', { password = 'test' }) |
| 54 | + box.schema.user.grant('test', 'execute,read,write', 'universe') |
| 55 | + """) |
| 56 | + |
| 57 | + args = [self.srv.host, self.srv.args['primary']] |
| 58 | + kwargs = { 'user': 'test', 'password': 'test' } |
| 59 | + self.con_encoding_utf8 = tarantool.Connection(*args, **kwargs, encoding='utf-8') |
| 60 | + self.con_encoding_none = tarantool.Connection(*args, **kwargs, encoding=None) |
| 61 | + self.conns = [self.con_encoding_utf8, self.con_encoding_none] |
| 62 | + |
| 63 | + self.srv.admin("box.schema.create_space('space_str')") |
| 64 | + self.srv.admin(""" |
| 65 | + box.space['space_str']:create_index('primary', { |
| 66 | + type = 'tree', |
| 67 | + parts = {1, 'str'}, |
| 68 | + unique = true}) |
| 69 | + """.replace('\n', ' ')) |
| 70 | + |
| 71 | + self.srv.admin("box.schema.create_space('space_varbin')") |
| 72 | + self.srv.admin(""" |
| 73 | + box.space['space_varbin']:create_index('primary', { |
| 74 | + type = 'tree', |
| 75 | + parts = {1, 'varbinary'}, |
| 76 | + unique = true}) |
| 77 | + """.replace('\n', ' ')) |
| 78 | + self.srv.admin(""" |
| 79 | + buffer = require('buffer') |
| 80 | + ffi = require('ffi') |
| 81 | +
|
| 82 | + function encode_bin(bytes) |
| 83 | + local tmpbuf = buffer.ibuf() |
| 84 | + local p = tmpbuf:alloc(3 + #bytes) |
| 85 | + p[0] = 0x91 |
| 86 | + p[1] = 0xC4 |
| 87 | + p[2] = #bytes |
| 88 | + for i, c in pairs(bytes) do |
| 89 | + p[i + 3 - 1] = c |
| 90 | + end |
| 91 | + return tmpbuf |
| 92 | + end |
| 93 | +
|
| 94 | + function bintuple_insert(space, bytes) |
| 95 | + local tmpbuf = encode_bin(bytes) |
| 96 | + ffi.cdef[[ |
| 97 | + int box_insert(uint32_t space_id, const char *tuple, const char *tuple_end, box_tuple_t **result); |
| 98 | + ]] |
| 99 | + ffi.C.box_insert(space.id, tmpbuf.rpos, tmpbuf.wpos, nil) |
| 100 | + end |
| 101 | + """) |
| 102 | + |
| 103 | + def assertNotRaises(self, func, *args, **kwargs): |
| 104 | + try: |
| 105 | + func(*args, **kwargs) |
| 106 | + except Exception as e: |
| 107 | + self.fail('Function raised Exception: %s' % repr(e)) |
| 108 | + |
| 109 | + def setUp(self): |
| 110 | + # prevent a remote tarantool from clean our session |
| 111 | + if self.srv.is_started(): |
| 112 | + self.srv.touch_lock() |
| 113 | + |
| 114 | + def test_01_01_string_insert_encoding_utf8_behavior(self): |
| 115 | + self.assertNotRaises( |
| 116 | + self.con_encoding_utf8.insert, |
| 117 | + 'space_str', [ 'test_01_01' ]) |
| 118 | + |
| 119 | + def test_01_02_string_select_encoding_utf8_behavior(self): |
| 120 | + self.adm(r"box.space['space_str']:insert{'test_01_02'}") |
| 121 | + |
| 122 | + strdata = 'test_01_01' |
| 123 | + resp = self.con_encoding_utf8.select('space_str', [strdata]) |
| 124 | + self.assertEquals(resp[0][0], strdata) |
| 125 | + |
| 126 | + @skip_or_run_mp_bin_test |
| 127 | + @skip_or_run_varbinary_test |
| 128 | + def test_01_03_varbinary_insert_encoding_utf8_behavior(self): |
| 129 | + self.assertNotRaises( |
| 130 | + self.con_encoding_utf8.insert, |
| 131 | + 'space_varbin', [ b'test_01_03' ]) |
| 132 | + |
| 133 | + @skip_or_run_mp_bin_test |
| 134 | + @skip_or_run_varbinary_test |
| 135 | + def test_01_04_varbinary_select_encoding_utf8_behavior(self): |
| 136 | + self.adm(r""" |
| 137 | + bintuple_insert( |
| 138 | + box.space['space_varbin'], |
| 139 | + {0xDE, 0xAD, 0xBE, 0xAF, 0x01, 0x04}) |
| 140 | + """) |
| 141 | + |
| 142 | + bindata = bytes(bytearray.fromhex('DEADBEAF0104')) |
| 143 | + resp = self.con_encoding_utf8.select('space_varbin', [bindata]) |
| 144 | + self.assertEquals(resp[0][0], bindata) |
| 145 | + |
| 146 | + def test_02_01_string_insert_encoding_none_behavior(self): |
| 147 | + self.assertNotRaises( |
| 148 | + self.con_encoding_none.insert, |
| 149 | + 'space_str', |
| 150 | + [ bytes(bytearray.fromhex('DEADBEAF0201')) ]) |
| 151 | + |
| 152 | + def test_02_02_string_select_encoding_none_behavior(self): |
| 153 | + self.adm(r"box.space['space_str']:insert{'\xDE\xAD\xBE\xAF\x02\x02'}") |
| 154 | + |
| 155 | + bindata = bytes(bytearray.fromhex('DEADBEAF0202')) |
| 156 | + resp = self.con_encoding_none.select('space_str', [bindata]) |
| 157 | + self.assertEquals(resp[0][0], bindata) |
| 158 | + |
| 159 | + @classmethod |
| 160 | + def tearDownClass(self): |
| 161 | + for con in self.conns: |
| 162 | + con.close() |
| 163 | + self.srv.stop() |
| 164 | + self.srv.clean() |
0 commit comments