Skip to content

Commit 4ec6748

Browse files
Rework tests
1 parent ea80949 commit 4ec6748

File tree

2 files changed

+112
-90
lines changed

2 files changed

+112
-90
lines changed

test/suites/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
from .test_mesh import TestSuite_Mesh
1313
from .test_execute import TestSuite_Execute
1414
from .test_dbapi import TestSuite_DBAPI
15+
from .test_encoding import TestSuite_Encoding
1516

1617
test_cases = (TestSuite_Schema_UnicodeConnection,
1718
TestSuite_Schema_BinaryConnection,
1819
TestSuite_Request, TestSuite_Protocol, TestSuite_Reconnect,
19-
TestSuite_Mesh, TestSuite_Execute, TestSuite_DBAPI)
20+
TestSuite_Mesh, TestSuite_Execute, TestSuite_DBAPI,
21+
TestSuite_Encoding)
2022

2123
def load_tests(loader, tests, pattern):
2224
suite = unittest.TestSuite()

test/suites/test_encoding.py

+109-89
Original file line numberDiff line numberDiff line change
@@ -10,94 +10,90 @@
1010
from .lib.tarantool_server import TarantoolServer
1111

1212
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-
4413
@classmethod
4514
def setUpClass(self):
46-
print(' DML '.center(70, '='), file=sys.stderr)
15+
print(' ENCODING '.center(70, '='), file=sys.stderr)
4716
print('-' * 70, file=sys.stderr)
4817
self.srv = TarantoolServer()
4918
self.srv.script = 'test/suites/box.lua'
5019
self.srv.start()
5120

5221
self.srv.admin("""
53-
box.schema.user.create('test', { password = 'test' })
54-
box.schema.user.grant('test', 'execute,read,write', 'universe')
22+
box.schema.user.create('test', { password = 'test' })
23+
box.schema.user.grant('test', 'execute,read,write', 'universe')
5524
""")
5625

5726
args = [self.srv.host, self.srv.args['primary']]
5827
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)
28+
self.con_encoding_utf8 = tarantool.Connection(*args, encoding='utf-8', **kwargs)
29+
self.con_encoding_none = tarantool.Connection(*args, encoding=None, **kwargs)
6130
self.conns = [self.con_encoding_utf8, self.con_encoding_none]
6231

6332
self.srv.admin("box.schema.create_space('space_str')")
6433
self.srv.admin("""
65-
box.space['space_str']:create_index('primary', {
66-
type = 'tree',
67-
parts = {1, 'str'},
68-
unique = true})
34+
box.space['space_str']:create_index('primary', {
35+
type = 'tree',
36+
parts = {1, 'str'},
37+
unique = true})
6938
""".replace('\n', ' '))
7039

7140
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', ' '))
7261
self.srv.admin("""
73-
box.space['space_varbin']:create_index('primary', {
74-
type = 'tree',
75-
parts = {1, 'varbinary'},
76-
unique = true})
62+
box.space['space_varbin']:create_index('varbin', {
63+
type = 'tree',
64+
parts = {2, 'varbinary'},
65+
unique = true})
7766
""".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+
7893
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
94+
function get_type(arg)
95+
return type(arg)
9096
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
10197
""")
10298

10399
def assertNotRaises(self, func, *args, **kwargs):
@@ -111,50 +107,74 @@ def setUp(self):
111107
if self.srv.is_started():
112108
self.srv.touch_lock()
113109

114-
def test_01_01_string_insert_encoding_utf8_behavior(self):
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):
115116
self.assertNotRaises(
116117
self.con_encoding_utf8.insert,
117118
'space_str', [ 'test_01_01' ])
118119

119-
def test_01_02_string_select_encoding_utf8_behavior(self):
120-
self.adm(r"box.space['space_str']:insert{'test_01_02'}")
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'}")
121122

122-
strdata = 'test_01_01'
123-
resp = self.con_encoding_utf8.select('space_str', [strdata])
124-
self.assertEquals(resp[0][0], strdata)
123+
resp = self.con_encoding_utf8.eval("return box.space.space_str:get('test_01_02')")
124+
self.assertSequenceEqual(resp, [['test_01_02']])
125125

126126
@skip_or_run_mp_bin_test
127127
@skip_or_run_varbinary_test
128-
def test_01_03_varbinary_insert_encoding_utf8_behavior(self):
128+
def test_01_03_bytes_encode_for_encoding_utf8_behavior(self):
129129
self.assertNotRaises(
130130
self.con_encoding_utf8.insert,
131-
'space_varbin', [ b'test_01_03' ])
131+
'space_varbin', [ 103, bytes(bytearray.fromhex('DEADBEAF0103')) ])
132132

133133
@skip_or_run_mp_bin_test
134134
@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})
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');
140138
""")
141139

142-
bindata = bytes(bytearray.fromhex('DEADBEAF0104'))
143-
resp = self.con_encoding_utf8.select('space_varbin', [bindata])
144-
self.assertEquals(resp[0][0], bindata)
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' ])
145155

146-
def test_02_01_string_insert_encoding_none_behavior(self):
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):
147163
self.assertNotRaises(
148164
self.con_encoding_none.insert,
149-
'space_str',
150-
[ bytes(bytearray.fromhex('DEADBEAF0201')) ])
165+
'space_str', [ b'test_02_03' ])
151166

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'}")
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+
""")
154173

155-
bindata = bytes(bytearray.fromhex('DEADBEAF0202'))
156-
resp = self.con_encoding_none.select('space_str', [bindata])
157-
self.assertEquals(resp[0][0], bindata)
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'))]])
158178

159179
@classmethod
160180
def tearDownClass(self):

0 commit comments

Comments
 (0)