6
6
import unittest
7
7
import tarantool
8
8
9
+ from .lib .skip import skip_or_run_mp_bin_test , skip_or_run_varbinary_test
9
10
from .lib .tarantool_server import TarantoolServer
10
11
11
12
class TestSuite_Request (unittest .TestCase ):
@@ -17,6 +18,12 @@ def setUpClass(self):
17
18
self .srv .script = 'test/suites/box.lua'
18
19
self .srv .start ()
19
20
self .con = tarantool .Connection (self .srv .host , self .srv .args ['primary' ])
21
+ self .con_encoding_utf8 = tarantool .Connection (self .srv .host ,
22
+ self .srv .args ['primary' ],
23
+ encoding = 'utf-8' )
24
+ self .con_encoding_none = tarantool .Connection (self .srv .host ,
25
+ self .srv .args ['primary' ],
26
+ encoding = None )
20
27
self .adm = self .srv .admin
21
28
self .space_created = self .adm ("box.schema.create_space('space_1')" )
22
29
self .adm ("""
@@ -31,17 +38,64 @@ def setUpClass(self):
31
38
parts = {2, 'num', 3, 'str'},
32
39
unique = false})
33
40
""" .replace ('\n ' , ' ' ))
41
+
34
42
self .space_created = self .adm ("box.schema.create_space('space_2')" )
35
43
self .adm ("""
36
44
box.space['space_2']:create_index('primary', {
37
45
type = 'hash',
38
46
parts = {1, 'num'},
39
47
unique = true})
40
48
""" .replace ('\n ' , ' ' ))
49
+
50
+ self .adm ("box.schema.create_space('space_str')" )
51
+ self .adm ("""
52
+ box.space['space_str']:create_index('primary', {
53
+ type = 'tree',
54
+ parts = {1, 'str'},
55
+ unique = true})
56
+ """ .replace ('\n ' , ' ' ))
57
+
58
+ self .adm ("box.schema.create_space('space_varbin')" )
59
+ self .adm ("""
60
+ box.space['space_varbin']:create_index('primary', {
61
+ type = 'tree',
62
+ parts = {1, 'varbinary'},
63
+ unique = true})
64
+ """ .replace ('\n ' , ' ' ))
65
+ self .adm ("""
66
+ buffer = require('buffer')
67
+ ffi = require('ffi')
68
+
69
+ function encode_bin(bytes)
70
+ local tmpbuf = buffer.ibuf()
71
+ local p = tmpbuf:alloc(3 + #bytes)
72
+ p[0] = 0x91
73
+ p[1] = 0xC4
74
+ p[2] = #bytes
75
+ for i, c in pairs(bytes) do
76
+ p[i + 3 - 1] = c
77
+ end
78
+ return tmpbuf
79
+ end
80
+
81
+ function bintuple_insert(space, bytes)
82
+ local tmpbuf = encode_bin(bytes)
83
+ ffi.cdef[[
84
+ int box_insert(uint32_t space_id, const char *tuple, const char *tuple_end, box_tuple_t **result);
85
+ ]]
86
+ ffi.C.box_insert(space.id, tmpbuf.rpos, tmpbuf.wpos, nil)
87
+ end
88
+ """ )
41
89
self .adm ("json = require('json')" )
42
90
self .adm ("fiber = require('fiber')" )
43
91
self .adm ("uuid = require('uuid')" )
44
92
93
+ def assertNotRaises (self , func , * args , ** kwargs ):
94
+ try :
95
+ func (* args , ** kwargs )
96
+ except Exception as e :
97
+ self .fail ('Function raised Exception: %s' % repr (e ))
98
+
45
99
def setUp (self ):
46
100
# prevent a remote tarantool from clean our session
47
101
if self .srv .is_started ():
@@ -55,6 +109,12 @@ def test_00_00_authenticate(self):
55
109
box.schema.user.grant('test', 'execute,read,write', 'universe')
56
110
""" ))
57
111
self .assertEqual (self .con .authenticate ('test' , 'test' )._data , None )
112
+ self .assertEqual (
113
+ self .con_encoding_utf8 .authenticate ('test' , 'test' )._data , None
114
+ )
115
+ self .assertEqual (
116
+ self .con_encoding_none .authenticate ('test' , 'test' )._data , None
117
+ )
58
118
59
119
def test_00_01_space_created (self ):
60
120
# Check that space is created in setUpClass
@@ -299,6 +359,50 @@ def test_12_update_fields(self):
299
359
[[2 , 'help' , 7 ]]
300
360
)
301
361
362
+ def test_13_00_string_insert_encoding_utf8_behavior (self ):
363
+ self .assertNotRaises (
364
+ self .con_encoding_utf8 .insert ,
365
+ 'space_str' , [ 'test_13_00' ])
366
+
367
+ def test_13_01_string_select_encoding_utf8_behavior (self ):
368
+ self .adm (r"box.space['space_str']:insert{'test_13_01'}" )
369
+
370
+ resp = self .con_encoding_utf8 .select ('space_str' , ['test_13_01' ])
371
+ self .assertIsInstance (resp [0 ][0 ], tarantool .utils .string_types )
372
+
373
+ @skip_or_run_mp_bin_test
374
+ @skip_or_run_varbinary_test
375
+ def test_13_02_varbinary_insert_encoding_utf8_behavior (self ):
376
+ self .assertNotRaises (
377
+ self .con_encoding_utf8 .insert ,
378
+ 'space_varbin' , [ b'test_13_02' ])
379
+
380
+ @skip_or_run_mp_bin_test
381
+ @skip_or_run_varbinary_test
382
+ def test_13_03_varbinary_select_encoding_utf8_behavior (self ):
383
+ self .adm (r"""
384
+ bintuple_insert(
385
+ box.space['space_varbin'],
386
+ {0xDE, 0xAD, 0xBE, 0xAF, 0x13, 0x03})
387
+ """ )
388
+
389
+ resp = self .con_encoding_utf8 .select (
390
+ 'space_varbin' , [bytes (bytearray .fromhex ('DEADBEAF1303' ))])
391
+ self .assertIsInstance (resp [0 ][0 ], tarantool .utils .binary_types )
392
+
393
+ def test_14_00_string_insert_encoding_none_behavior (self ):
394
+ self .assertNotRaises (
395
+ self .con_encoding_none .insert ,
396
+ 'space_str' ,
397
+ [ bytes (bytearray .fromhex ('DEADBEAF1400' )) ])
398
+
399
+ def test_14_01_string_select_encoding_none_behavior (self ):
400
+ self .adm (r"box.space['space_str']:insert{'\xDE\xAD\xBE\xAF\x14\x01'}" )
401
+
402
+ resp = self .con_encoding_none .select (
403
+ 'space_str' , [bytes (bytearray .fromhex ('DEADBEAF1401' ))])
404
+ self .assertIsInstance (resp [0 ][0 ], tarantool .utils .binary_types )
405
+
302
406
@classmethod
303
407
def tearDownClass (self ):
304
408
self .con .close ()
0 commit comments