Skip to content

Commit

Permalink
Added ability to pass either list or tuple as params
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcoding committed Feb 16, 2017
1 parent 302ae79 commit dc287a7
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 66 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# asynctnt

[![Build Status](https://travis-ci.org/igorcoding/asynctnt.svg?branch=master)](https://travis-ci.org/igorcoding/asynctnt)
[![PyPI](https://img.shields.io/pypi/v/asynctnt.svg)](https://pypi.python.org/pypi/asynctnt)


asynctnt is a high-performance [Tarantool](https://tarantool.org/) database
Expand Down
2 changes: 1 addition & 1 deletion asynctnt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .connection import Connection, connect
from .iproto.protocol import Iterator, Response

__version__ = '0.0.10'
__version__ = '0.0.11'
7 changes: 0 additions & 7 deletions asynctnt/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,6 @@ def schema_id(self):
return None
return self._protocol.schema_id

@property
def encoding(self):
"""
Connection encoding
"""
return self._encoding

@property
def initial_read_buffer_size(self):
"""
Expand Down
1 change: 0 additions & 1 deletion asynctnt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class TarantoolDatabaseError(TarantoolError):
def __init__(self, code, message):
super(TarantoolDatabaseError, self).__init__(code, message)
self.code = code
self.error_code = ErrorCode(code)
self.message = message


Expand Down
15 changes: 8 additions & 7 deletions asynctnt/iproto/buffer.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,27 @@ cdef class WriteBuffer:
cdef char *_encode_list(self, char *p, list arr) except NULL
cdef char *_encode_tuple(self, char *p, tuple t) except NULL
cdef char *_encode_dict(self, char *p, dict d) except NULL
cdef char *_encode_key_sequence(self, char *p, t) except NULL
cdef char *_encode_obj(self, char *p, object o) except NULL

cdef tnt.tnt_update_op_kind _op_type_to_kind(self, char *str, ssize_t len)
cdef char *_encode_update_ops(self, char *p, list operations) except NULL

cdef void encode_request_call(self, str func_name, list args) except *
cdef void encode_request_eval(self, str expression, list args) except *
cdef void encode_request_call(self, str func_name, args) except *
cdef void encode_request_eval(self, str expression, args) except *
cdef void encode_request_select(self, uint32_t space, uint32_t index,
list key, uint64_t offset, uint64_t limit,
key, uint64_t offset, uint64_t limit,
uint32_t iterator) except *
cdef void encode_request_insert(self, uint32_t space, list t) except *
cdef void encode_request_insert(self, uint32_t space, t) except *
cdef void encode_request_delete(self, uint32_t space, uint32_t index,
list key) except *
key) except *
cdef void encode_request_update(self, uint32_t space, uint32_t index,
list key_tuple, list operations,
key_tuple, list operations,
uint32_t key_of_tuple=*,
uint32_t key_of_operations=*
) except *
cdef void encode_request_upsert(self, uint32_t space,
list t, list operations) except *
t, list operations) except *
cdef void encode_request_auth(self,
bytes username,
bytes scramble) except *
38 changes: 25 additions & 13 deletions asynctnt/iproto/buffer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,18 @@ cdef class WriteBuffer:

return p


cdef char *_encode_key_sequence(self, char *p, t) except NULL:
if isinstance(t, list) or t is None:
return self._encode_list(p, <list>t)
elif isinstance(t, tuple):
return self._encode_tuple(p, <tuple>t)
else:
raise TypeError(
'sequence must be either list or tuple, '
'got: {}'.format(type(t))
)

cdef char *_encode_obj(self, char *p, object o) except NULL:
cdef:
bytes o_string_temp
Expand Down Expand Up @@ -499,7 +511,7 @@ cdef class WriteBuffer:
'Unknown update operation type `{}`'.format(op_type_str))
return p

cdef void encode_request_call(self, str func_name, list args) except *:
cdef void encode_request_call(self, str func_name, args) except *:
cdef:
char *begin
char *p
Expand Down Expand Up @@ -537,9 +549,9 @@ cdef class WriteBuffer:

p = mp_encode_uint(p, tnt.TP_TUPLE)
self._length += (p - begin)
p = self._encode_list(p, args)
p = self._encode_key_sequence(p, args)

cdef void encode_request_eval(self, str expression, list args) except *:
cdef void encode_request_eval(self, str expression, args) except *:
cdef:
char *begin
char *p
Expand Down Expand Up @@ -577,10 +589,10 @@ cdef class WriteBuffer:

p = mp_encode_uint(p, tnt.TP_TUPLE)
self._length += (p - begin)
p = self._encode_list(p, args)
p = self._encode_key_sequence(p, args)

cdef void encode_request_select(self, uint32_t space, uint32_t index,
list key, uint64_t offset, uint64_t limit,
key, uint64_t offset, uint64_t limit,
uint32_t iterator) except *:
cdef:
char *begin
Expand Down Expand Up @@ -637,9 +649,9 @@ cdef class WriteBuffer:

p = mp_encode_uint(p, tnt.TP_KEY)
self._length += (p - begin)
p = self._encode_list(p, key)
p = self._encode_key_sequence(p, key)

cdef void encode_request_insert(self, uint32_t space, list t) except *:
cdef void encode_request_insert(self, uint32_t space, t) except *:
cdef:
char *begin
char *p
Expand All @@ -666,10 +678,10 @@ cdef class WriteBuffer:

p = mp_encode_uint(p, tnt.TP_TUPLE)
self._length += (p - begin)
p = self._encode_list(p, t)
p = self._encode_key_sequence(p, t)

cdef void encode_request_delete(self, uint32_t space, uint32_t index,
list key) except *:
key) except *:
cdef:
char *p
uint32_t body_map_sz
Expand Down Expand Up @@ -704,10 +716,10 @@ cdef class WriteBuffer:

p = mp_encode_uint(p, tnt.TP_KEY)
self._length += (p - begin)
p = self._encode_list(p, key)
p = self._encode_key_sequence(p, key)

cdef void encode_request_update(self, uint32_t space, uint32_t index,
list key_tuple, list operations,
key_tuple, list operations,
uint32_t key_of_tuple=tnt.TP_KEY,
uint32_t key_of_operations=tnt.TP_TUPLE
) except *:
Expand Down Expand Up @@ -747,13 +759,13 @@ cdef class WriteBuffer:
self._length += (p - begin)

p = self._encode_uint(p, key_of_tuple)
p = self._encode_list(p, key_tuple)
p = self._encode_key_sequence(p, key_tuple)

p = self._encode_uint(p, key_of_operations)
p = self._encode_update_ops(p, operations)

cdef void encode_request_upsert(self, uint32_t space,
list t, list operations) except *:
t, list operations) except *:
self.encode_request_update(space, 0, t, operations,
tnt.TP_TUPLE, tnt.TP_OPERATIONS)

Expand Down
16 changes: 8 additions & 8 deletions asynctnt/iproto/db.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ cdef class Db:
# cdef execute(self, Request req, float timeout)

cdef Request _ping(self)
cdef Request _call16(self, str func_name, list args)
cdef Request _call(self, str func_name, list args)
cdef Request _eval(self, str expression, list args)
cdef Request _select(self, uint32_t space, uint32_t index, list key,
cdef Request _call16(self, str func_name, args)
cdef Request _call(self, str func_name, args)
cdef Request _eval(self, str expression, args)
cdef Request _select(self, uint32_t space, uint32_t index, key,
uint64_t offset, uint64_t limit, uint32_t iterator)
cdef Request _insert(self, uint32_t space, list t, bint replace)
cdef Request _delete(self, uint32_t space, uint32_t index, list key)
cdef Request _insert(self, uint32_t space, t, bint replace)
cdef Request _delete(self, uint32_t space, uint32_t index, key)
cdef Request _update(self, uint32_t space, uint32_t index,
list key, list operations)
cdef Request _upsert(self, uint32_t space, list t, list operations)
key, list operations)
cdef Request _upsert(self, uint32_t space, t, list operations)
cdef Request _auth(self, bytes salt, str username, str password)

@staticmethod
Expand Down
16 changes: 8 additions & 8 deletions asynctnt/iproto/db.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ cdef class Db:
buf.write_length()
return Request.new(op, sync, schema_id, buf)

cdef Request _call16(self, str func_name, list args):
cdef Request _call16(self, str func_name, args):
cdef:
tnt.tp_request_type op
uint64_t sync
Expand All @@ -67,7 +67,7 @@ cdef class Db:
buf.write_length()
return Request.new(op, sync, schema_id, buf)

cdef Request _call(self, str func_name, list args):
cdef Request _call(self, str func_name, args):
cdef:
tnt.tp_request_type op
uint64_t sync
Expand All @@ -83,7 +83,7 @@ cdef class Db:
buf.write_length()
return Request.new(op, sync, schema_id, buf)

cdef Request _eval(self, str expression, list args):
cdef Request _eval(self, str expression, args):
cdef:
tnt.tp_request_type op
uint64_t sync
Expand All @@ -99,7 +99,7 @@ cdef class Db:
buf.write_length()
return Request.new(op, sync, schema_id, buf)

cdef Request _select(self, uint32_t space, uint32_t index, list key,
cdef Request _select(self, uint32_t space, uint32_t index, key,
uint64_t offset, uint64_t limit, uint32_t iterator):
cdef:
tnt.tp_request_type op
Expand All @@ -117,7 +117,7 @@ cdef class Db:
buf.write_length()
return Request.new(op, sync, schema_id, buf)

cdef Request _insert(self, uint32_t space, list t, bint replace):
cdef Request _insert(self, uint32_t space, t, bint replace):
cdef:
tnt.tp_request_type op
uint64_t sync
Expand All @@ -133,7 +133,7 @@ cdef class Db:
buf.write_length()
return Request.new(op, sync, schema_id, buf)

cdef Request _delete(self, uint32_t space, uint32_t index, list key):
cdef Request _delete(self, uint32_t space, uint32_t index, key):
cdef:
tnt.tp_request_type op
uint64_t sync
Expand All @@ -150,7 +150,7 @@ cdef class Db:
return Request.new(op, sync, schema_id, buf)

cdef Request _update(self, uint32_t space, uint32_t index,
list key, list operations):
key, list operations):
cdef:
tnt.tp_request_type op
uint64_t sync
Expand All @@ -166,7 +166,7 @@ cdef class Db:
buf.write_length()
return Request.new(op, sync, schema_id, buf)

cdef Request _upsert(self, uint32_t space, list t, list operations):
cdef Request _upsert(self, uint32_t space, t, list operations):
cdef:
tnt.tp_request_type op
uint64_t sync
Expand Down
18 changes: 12 additions & 6 deletions tests/test_op_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,18 @@ async def test__call_params_invalid_type(self):
with self.assertRaises(TypeError):
await self.conn.call('func_param', 220349)

with self.assertRaises(TypeError):
await self.conn.call('func_param', (1, 2))

with self.assertRaises(TypeError):
await self.conn.call('func_param', 'hey')

with self.assertRaises(TypeError):
await self.conn.call('func_param', {1: 1, 2: 2})

async def test__call_args_tuple(self):
try:
await self.conn.call('func_param', (1, 2))
except Exception as e:
self.fail(e)

async def test__call_complex_param(self):
p, cmp = get_complex_param(encoding=self.conn.encoding)
res = await self.conn.call('func_param', [p])
Expand Down Expand Up @@ -158,15 +161,18 @@ async def test__call16_params_invalid_type(self):
with self.assertRaises(TypeError):
await self.conn.call16('func_param', 220349)

with self.assertRaises(TypeError):
await self.conn.call16('func_param', (1, 2))

with self.assertRaises(TypeError):
await self.conn.call16('func_param', 'hey')

with self.assertRaises(TypeError):
await self.conn.call16('func_param', {1: 1, 2: 2})

async def test__call16_args_tuple(self):
try:
await self.conn.call16('func_param', (1, 2))
except Exception as e:
self.fail(e)

async def test__call16_complex_param(self):
p, cmp = get_complex_param(encoding=self.conn.encoding)
res = await self.conn.call('func_param', [p])
Expand Down
14 changes: 8 additions & 6 deletions tests/test_op_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,16 @@ async def test__delete_by_index_name_no_schema(self):

async def test__delete_invalid_types(self):
with self.assertRaisesRegex(
TypeError, "missing 2 required positional arguments: 'space' and 'key'"):
TypeError,
"missing 2 required positional arguments: 'space' and 'key'"):
await self.conn.delete()

with self.assertRaisesRegex(
TypeError, r'Expected list, got '):
await self.conn.delete(self.TESTER_SPACE_ID, (1,))

with self.assertRaisesRegex(
TypeError, r'Expected list, got '):
TypeError, r'sequence must be either list or tuple'):
await self.conn.delete(self.TESTER_SPACE_ID, {})

async def test__delete_key_tuple(self):
try:
await self.conn.delete(self.TESTER_SPACE_ID, (1,))
except Exception as e:
self.fail(e)
9 changes: 6 additions & 3 deletions tests/test_op_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,18 @@ async def test__eval_params_invalid_type(self):
with self.assertRaises(TypeError):
await self.conn.eval('return {...}', 220349)

with self.assertRaises(TypeError):
await self.conn.eval('return {...}', (1, 2))

with self.assertRaises(TypeError):
await self.conn.eval('return {...}', 'hey')

with self.assertRaises(TypeError):
await self.conn.eval('return {...}', {1: 1, 2: 2})

async def test__eval_args_tuple(self):
try:
await self.conn.eval('return {...}', (1, 2))
except Exception as e:
self.fail(e)

async def test__eval_complex_param(self):
p, cmp = get_complex_param(encoding=self.conn.encoding)
res = await self.conn.eval('return {...}', [p])
Expand Down
16 changes: 10 additions & 6 deletions tests/test_op_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,21 @@ async def test__select_all_params(self):
iterator=Iterator.LE)
self.assertListEqual(res.body, list(reversed(data))[1:3], 'Body ok')

async def test__select_key_tuple(self):
try:
await self.conn.select(self.TESTER_SPACE_ID, (1,))
except Exception as e:
self.fail(e)

async def test__select_invalid_types(self):
with self.assertRaisesRegex(
TypeError, r'missing 1 required positional argument: \'space\''):
TypeError,
r'missing 1 required positional argument: \'space\''):
await self.conn.select()

with self.assertRaisesRegex(
TypeError, r'Expected list, got tuple'):
await self.conn.select(self.TESTER_SPACE_ID, (1,))

with self.assertRaisesRegex(
TypeError, r'Expected list, got int'):
TypeError,
r'sequence must be either list or tuple, got: <class \'int\'>'):
await self.conn.select(self.TESTER_SPACE_ID, 1)

with self.assertRaisesRegex(
Expand Down

0 comments on commit dc287a7

Please sign in to comment.