Skip to content

Commit f20ca84

Browse files
committed
1. Patch for request tests
2. Patch for update - support lond int's 3. Patch for select to support set/frozenset Patch by Eduard Iskandarov
1 parent 9e5b778 commit f20ca84

File tree

4 files changed

+68
-37
lines changed

4 files changed

+68
-37
lines changed

src/tarantool/connection.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def update(self, space_name, key, op_list, return_tuple=None):
346346
347347
:rtype: `Response` instance
348348
'''
349-
assert isinstance(key, (int, basestring))
349+
assert isinstance(key, (int, long, basestring))
350350

351351
if return_tuple is None:
352352
return_tuple = self.return_tuple
@@ -382,8 +382,8 @@ def _select(self, space_name, index_name, values, offset=0, limit=0xffffffff):
382382
:type space_name: int or str
383383
:param index_name: index id to use
384384
:type index_name: int or str
385-
:param values: list of values to search over the index
386-
:type values: list of tuples
385+
:param values: values to search over the index
386+
:type values: list, tuple, set, frozenset of tuples
387387
:param offset: offset in the resulting tuple set
388388
:type offset: int
389389
:param limit: limits the total number of returned tuples
@@ -392,8 +392,8 @@ def _select(self, space_name, index_name, values, offset=0, limit=0xffffffff):
392392
:rtype: `Response` instance
393393
'''
394394

395-
# 'values' argument must be a list of tuples
396-
assert isinstance(values, (list, tuple))
395+
# 'values' argument must be a collection with tuples
396+
assert isinstance(values, (list, tuple, set, frozenset))
397397

398398
request = RequestSelect(self, space_name, index_name, values, offset, limit)
399399
response = self._send_request(request, space_name)
@@ -406,8 +406,8 @@ def select(self, space_name, values=None, **kwargs):
406406
407407
:param space_name: specifies which space to query
408408
:type space_name: int or str
409-
:param values: list of values to search over the index
410-
:type values: list of tuples
409+
:param values: values to search over the index
410+
:type values: list, tuple, set, frozenset of tuples
411411
:param index: specifies which index to use (default is **0** which means that the **primary index** will be used)
412412
:type index: int
413413
:param offset: offset in the resulting tuple set
@@ -439,18 +439,19 @@ def select(self, space_name, values=None, **kwargs):
439439
index = kwargs.get("index", 0)
440440

441441
# Perform smart type cheching (scalar / list of scalars / list of tuples)
442-
if values == None:
442+
if values is None:
443443
values = [[]]
444444
elif isinstance(values, (int, long, basestring)): # scalar
445445
# This request is looking for one single record
446446
values = [(values, )]
447447
elif isinstance(values, (list, tuple, set, frozenset)):
448-
if isinstance(values[0], (int, long, basestring)): # list of scalars
448+
any_item = next(iter(values))
449+
if isinstance(any_item, (int, long, basestring)): # list of scalars
449450
# This request is looking for several records using single-valued index
450451
# Ex: select(space_no, index_no, [1, 2, 3])
451452
# Transform a list of scalar values to a list of tuples
452453
values = [(v, ) for v in values]
453-
elif isinstance(values[0], (list, tuple)): # list of tuples
454+
elif isinstance(any_item, (list, tuple)): # list of tuples
454455
# This request is looking for serveral records using composite index
455456
pass
456457
else:

src/tarantool/request.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class RequestSelect(Request):
218218

219219
def __init__(self, conn, space_name, index_name, tuple_list, offset, limit): # pylint: disable=W0231
220220
super(RequestSelect, self).__init__(conn)
221-
assert isinstance(tuple_list, (list, tuple))
221+
assert isinstance(tuple_list, (list, tuple, set, frozenset))
222222

223223
space_no = self.conn.schema.space_no(space_name)
224224
index_no = self.conn.schema.index_no(space_no, index_name)
@@ -246,7 +246,7 @@ class RequestUpdate(Request):
246246
def __init__(self, conn, space_name, key, op_list, return_tuple): # pylint: disable=W0231
247247
super(RequestUpdate, self).__init__(conn)
248248
flags = 1 if return_tuple else 0
249-
assert isinstance(key, (int, basestring))
249+
assert isinstance(key, (int, long, basestring))
250250

251251
space_no = self.conn.schema.space_no(space_name)
252252
request_body = \

src/tarantool/schema.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(self, schema):
6161

6262
for (space_no, space_descr) in schema.iteritems():
6363
if not isinstance(space_no, (int, long)):
64-
ValueError('Invalid space_no: %s' % space_no)
64+
raise ValueError('Invalid space_no: %s' % space_no)
6565

6666
# Space name
6767
space_name = space_descr.get('name', None)
@@ -75,7 +75,7 @@ def __init__(self, schema):
7575
max_fieldno = 0
7676
for field_no in field_descrs.iterkeys():
7777
if not isinstance(field_no, (int, long)):
78-
ValueError('Invalid field_no: %s'%field_no)
78+
raise ValueError('Invalid field_no: %s'%field_no)
7979
max_fieldno = max(max_fieldno, field_no)
8080

8181
field_defs = [None] * (max_fieldno + 1)
@@ -98,7 +98,7 @@ def __init__(self, schema):
9898
max_indexno = 0
9999
for index_no in index_descrs.iterkeys():
100100
if not isinstance(index_no, (int, long)):
101-
ValueError('Invalid index_no: %s'%index_no)
101+
raise ValueError('Invalid index_no: %s'%index_no)
102102
max_indexno = max(max_indexno, index_no)
103103

104104
index_defs = [None] * (max_indexno + 1)
@@ -132,7 +132,7 @@ def __init__(self, schema):
132132
def _check_datatype(self, dtype):
133133
if dtype in (NUM, NUM64, RAW, STR):
134134
return dtype
135-
elif dtype == None:
135+
elif dtype is None:
136136
return RAW
137137
elif dtype == int:
138138
return NUM

tests/tarantool/request.py

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,63 @@
99

1010

1111
import tarantool.request
12+
from tarantool.schema import Schema
13+
14+
15+
class ConnectionMock(object):
16+
def __init__(self, default_type):
17+
self.schema = Schema({
18+
1: {
19+
'name': 'users',
20+
'default_type': default_type,
21+
'fields': {
22+
0: ('f1', default_type),
23+
1: ('f2', default_type),
24+
2: ('f3', default_type),
25+
},
26+
'indexes': {
27+
0: ('pk', [0, 0]),
28+
},
29+
}
30+
})
1231

1332

1433
class RequestInsert(unittest.TestCase):
34+
def setUp(self):
35+
self.conn1 = ConnectionMock(tarantool.NUM)
36+
self.conn2 = ConnectionMock(tarantool.STR)
1537

1638
def test__cast_to_bytes(self):
1739
'''
1840
Test binary INSERT request representation
1941
'''
2042
self.assertEqual(
21-
bytes(tarantool.request.RequestInsert(1, (1, 2000, 30000), False)),
43+
bytes(tarantool.request.RequestInsert(self.conn1, 1, (1, 2000, 30000), False)),
2244
binascii.unhexlify("0d0000001b00000000000000010000000000000003000000040100000004d00700000430750000")
2345
)
2446

2547
self.assertEqual(
26-
bytes(tarantool.request.RequestInsert(1, (b"AAA", b"BBBB", b"CCCCCC"), False)),
48+
bytes(tarantool.request.RequestInsert(self.conn2, 1, (b"AAA", b"BBBB", b"CCCCCC"), False)),
2749
binascii.unhexlify("0d0000001c0000000000000001000000000000000300000003414141044242424206434343434343")
2850
)
2951

3052

3153
class RequestDelete(unittest.TestCase):
54+
def setUp(self):
55+
self.conn1 = ConnectionMock(tarantool.NUM)
56+
self.conn2 = ConnectionMock(tarantool.STR)
3257

3358
def test__cast_to_bytes(self):
3459
'''
3560
Test binary DELETE request representation
3661
'''
3762
self.assertEqual(
38-
bytes(tarantool.request.RequestDelete(1, 1, False)),
63+
bytes(tarantool.request.RequestDelete(self.conn1, 1, 1, False)),
3964
binascii.unhexlify("1500000011000000000000000100000000000000010000000401000000")
4065
)
4166

4267
self.assertEqual(
43-
bytes(tarantool.request.RequestDelete(1, b"AAA", False)),
68+
bytes(tarantool.request.RequestDelete(self.conn2, 1, b"AAA", False)),
4469
binascii.unhexlify("15000000100000000000000001000000000000000100000003414141")
4570
)
4671

@@ -50,48 +75,53 @@ def test__cast_to_bytes(self):
5075

5176

5277
class RequestSelect(unittest.TestCase):
78+
def setUp(self):
79+
self.conn1 = ConnectionMock(tarantool.NUM)
80+
self.conn2 = ConnectionMock(tarantool.STR)
5381

5482
def test__cast_to_bytes(self):
5583
'''
5684
Test binary SELECT request representation
5785
'''
5886
# select * from t1 where k0 = 1
5987
self.assertEqual(
60-
bytes(tarantool.request.RequestSelect(1, 0, [(1,)], 0, 0xffff)),
88+
bytes(tarantool.request.RequestSelect(self.conn1, 1, 0, [(1,)], 0, 0xffff)),
6189
binascii.unhexlify("110000001d00000000000000010000000000000000000000ffff000001000000010000000401000000"),
6290
"Select using integer key"
6391
)
6492

6593
# select * from t1 where k0 = "AAA"
6694
self.assertEqual(
67-
bytes(tarantool.request.RequestSelect(1, 0, [(b"AAA",)], 0, 0xffff)),
95+
bytes(tarantool.request.RequestSelect(self.conn2, 1, 0, [(b"AAA",)], 0, 0xffff)),
6896
binascii.unhexlify("110000001c00000000000000010000000000000000000000ffff0000010000000100000003414141"),
6997
"Select using string key"
7098
)
7199

72100
# select * from t1 where k0 in (1, 2, 3)
73101
self.assertEqual(
74-
bytes(tarantool.request.RequestSelect(1, 0, [(1,), (2,), (3,)], 0, 0xffff)),
102+
bytes(tarantool.request.RequestSelect(self.conn1, 1, 0, [(1,), (2,), (3,)], 0, 0xffff)),
75103
binascii.unhexlify("110000002f00000000000000010000000000000000000000ffff000003000000010000000401000000010000000402000000010000000403000000"),
76104
"Select multiple keys"
77105
)
78106

79107
# select * from t1 where k0 = (1, 2)
80108
self.assertEqual(
81-
bytes(tarantool.request.RequestSelect(1, 0, [(1, 2)], 0, 0xffff)),
109+
bytes(tarantool.request.RequestSelect(self.conn1, 1, 0, [(1, 2)], 0, 0xffff)),
82110
binascii.unhexlify("110000002200000000000000010000000000000000000000ffff0000010000000200000004010000000402000000"),
83111
"Select using composite index"
84112
)
85113

86114
# select * from t1 where k0 = (1, 2) or k0 = (3, 4)
87115
self.assertEqual(
88-
bytes(tarantool.request.RequestSelect(1, 0, [(1, 2), (3, 4)], 0, 0xffff)),
116+
bytes(tarantool.request.RequestSelect(self.conn1, 1, 0, [(1, 2), (3, 4)], 0, 0xffff)),
89117
binascii.unhexlify("110000003000000000000000010000000000000000000000ffff00000200000002000000040100000004020000000200000004030000000404000000"),
90118
"Select multiple keys using composite index"
91119
)
92120

93121

94122
class RequestUpdate(unittest.TestCase):
123+
def setUp(self):
124+
self.conn = ConnectionMock(tarantool.NUM)
95125

96126
def test__cast_to_bytes(self):
97127
'''
@@ -103,56 +133,56 @@ def test__cast_to_bytes(self):
103133

104134
# update t17 set k51 = 0x11223344 where k0 = 0x22
105135
self.assertEqual(
106-
bytes(tarantool.request.RequestUpdate(0x11, 0x22, [(0x33, '=', 0x11223344)], False)),
136+
bytes(tarantool.request.RequestUpdate(self.conn, 0x11, 0x22, [(0x33, '=', 0x11223344)], False)),
107137
binascii.unhexlify("130000001f0000000000000011000000000000000100000004220000000100000033000000000444332211"),
108138
"Update: assign single integer value using an integer key"
109139
)
110140

111141
# update t17 set k51 = 0x11223344 where k0 = "ZZZZZZ"
112142
self.assertEqual(
113-
bytes(tarantool.request.RequestUpdate(0x11, b"ZZZZZZ", [(0x33, '=', 0x11223344)], False)),
143+
bytes(tarantool.request.RequestUpdate(self.conn, 0x11, b"ZZZZZZ", [(0x33, '=', 0x11223344)], False)),
114144
binascii.unhexlify("130000002100000000000000110000000000000001000000065a5a5a5a5a5a0100000033000000000444332211"),
115145
"Update: assign single integer value using a string key"
116146
)
117147

118148
# update t17 set k51 = "NNN" where k0 = 0x22
119149
self.assertEqual(
120-
bytes(tarantool.request.RequestUpdate(0x11, 0x22, [(0x33, '=', b"NNN")], False)),
150+
bytes(tarantool.request.RequestUpdate(self.conn, 0x11, 0x22, [(0x33, '=', b"NNN")], False)),
121151
binascii.unhexlify("130000001e000000000000001100000000000000010000000422000000010000003300000000034e4e4e"),
122152
"Update: assign single string value using an integer key"
123153
)
124154

125155
# update t17 set k51 = "NNN" where k0 = "ZZZZZZ"
126156
self.assertEqual(
127-
bytes(tarantool.request.RequestUpdate(0x11, b"ZZZZZZ", [(0x33, '=', b"NNN")], False)),
157+
bytes(tarantool.request.RequestUpdate(self.conn, 0x11, b"ZZZZZZ", [(0x33, '=', b"NNN")], False)),
128158
binascii.unhexlify("130000002000000000000000110000000000000001000000065a5a5a5a5a5a010000003300000000034e4e4e"),
129159
"Update: assign single string value using a string key"
130160
)
131161

132162
# update t17 set k51 = 0x3333, k68 = 0x4444, k85 = 0x5555 where k0 = 0x22
133163
self.assertEqual(
134-
bytes(tarantool.request.RequestUpdate(0x11, 0x22, [(0x33, '=', 0x3333), (0x44, '=', 0x4444), (0x55, '=', 0x5555)], False)),
164+
bytes(tarantool.request.RequestUpdate(self.conn, 0x11, 0x22, [(0x33, '=', 0x3333), (0x44, '=', 0x4444), (0x55, '=', 0x5555)], False)),
135165
binascii.unhexlify("130000003300000000000000110000000000000001000000042200000003000000330000000004333300004400000000044444000055000000000455550000"),
136166
"Update: assign multiple integer values using an integer key"
137167
)
138168

139169
# update t17 set k51 = 0x3333, k68 = 0x4444, k85 = 0x5555 where k0 = "ZZZZZZ"
140170
self.assertEqual(
141-
bytes(tarantool.request.RequestUpdate(0x11, "ZZZZZZ", [(0x33, '=', 0x3333), (0x44, '=', 0x4444), (0x55, '=', 0x5555)], False)),
171+
bytes(tarantool.request.RequestUpdate(self.conn, 0x11, "ZZZZZZ", [(0x33, '=', 0x3333), (0x44, '=', 0x4444), (0x55, '=', 0x5555)], False)),
142172
binascii.unhexlify("130000003500000000000000110000000000000001000000065a5a5a5a5a5a03000000330000000004333300004400000000044444000055000000000455550000"),
143173
"Update: assign multiple integer values using a string key"
144174
)
145175

146176
# update t17 set k51 = "KKK", k68 = "LLL", k85 = "MMM" where k0 = 0x22
147177
self.assertEqual(
148-
bytes(tarantool.request.RequestUpdate(0x11, 0x22, [(0x33,'=', b"KKK"), (0x44,'=', b"LLL"), (0x55,'=', b"MMM")], False)),
178+
bytes(tarantool.request.RequestUpdate(self.conn, 0x11, 0x22, [(0x33,'=', b"KKK"), (0x44,'=', b"LLL"), (0x55,'=', b"MMM")], False)),
149179
binascii.unhexlify("1300000030000000000000001100000000000000010000000422000000030000003300000000034b4b4b4400000000034c4c4c5500000000034d4d4d"),
150180
"Update: assign multiple string values using an integer key"
151181
)
152182

153183
# update t17 set k51 = "KKK", k68 = "LLL", k85 = "MMM" where k0 = "ZZZZZZ"
154184
self.assertEqual(
155-
bytes(tarantool.request.RequestUpdate(0x11, b"ZZZZZZ", [(0x33,'=', b"KKK"), (0x44,'=', b"LLL"), (0x55,'=', b"MMM")], False)),
185+
bytes(tarantool.request.RequestUpdate(self.conn, 0x11, b"ZZZZZZ", [(0x33,'=', b"KKK"), (0x44,'=', b"LLL"), (0x55,'=', b"MMM")], False)),
156186
binascii.unhexlify("130000003200000000000000110000000000000001000000065a5a5a5a5a5a030000003300000000034b4b4b4400000000034c4c4c5500000000034d4d4d"),
157187
"Update: assign multiple string values using a string key"
158188
)
@@ -162,7 +192,7 @@ def test__cast_to_bytes(self):
162192

163193
# update t17 set k51 = k51 + 0x55 where k0 = 0x22
164194
self.assertEqual(
165-
bytes(tarantool.request.RequestUpdate(0x11, 0x22, [(0x33, '+', 0x55)], False)),
195+
bytes(tarantool.request.RequestUpdate(self.conn, 0x11, 0x22, [(0x33, '+', 0x55)], False)),
166196
binascii.unhexlify("130000001f00000000000000"
167197
+"11000000" # space_no
168198
+"00000000" # flags
@@ -183,7 +213,7 @@ def test__cast_to_bytes(self):
183213

184214
# update t17 set k51 = k51 & 0x55 where k0 = 0x22
185215
self.assertEqual(
186-
bytes(tarantool.request.RequestUpdate(0x11, 0x22, [(0x33, '&', 0x55)], False)),
216+
bytes(tarantool.request.RequestUpdate(self.conn, 0x11, 0x22, [(0x33, '&', 0x55)], False)),
187217
binascii.unhexlify("130000001f00000000000000" # 12 byte header
188218
+ "11000000" # space_no
189219
+ "00000000" # flags
@@ -205,7 +235,7 @@ def test__cast_to_bytes(self):
205235

206236
# update t17 set k51 = k51 | 0x55 where k0 = 0x22
207237
self.assertEqual(
208-
bytes(tarantool.request.RequestUpdate(0x11, 0x22, [(0x33, '^', 0x55)], False)),
238+
bytes(tarantool.request.RequestUpdate(self.conn, 0x11, 0x22, [(0x33, '^', 0x55)], False)),
209239
binascii.unhexlify("130000001f00000000000000" # 12 byte header
210240
+ "11000000" # space_no
211241
+ "00000000" # flags
@@ -227,7 +257,7 @@ def test__cast_to_bytes(self):
227257

228258
# update t17 set k51 = k51 | 0x55 where k0 = 0x22
229259
self.assertEqual(
230-
bytes(tarantool.request.RequestUpdate(0x11, 0x22, [(0x33, '|', 0x55)], False)),
260+
bytes(tarantool.request.RequestUpdate(self.conn, 0x11, 0x22, [(0x33, '|', 0x55)], False)),
231261
binascii.unhexlify("130000001f00000000000000" # 12 byte header
232262
+ "11000000" # space_no
233263
+ "00000000" # flags

0 commit comments

Comments
 (0)