Skip to content

api: any msgpack supported type as a request key #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Package build (#238).
- Allow any MessagePack supported type as a request key (#240).

## 0.9.0 - 2022-06-20

Expand Down
8 changes: 4 additions & 4 deletions tarantool/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@
)
from tarantool.schema import Schema
from tarantool.utils import (
check_key,
greeting_decode,
version_id,
wrap_key,
ENCODING_DEFAULT,
)

Expand Down Expand Up @@ -1220,7 +1220,7 @@ def delete(self, space_name, key, *, index=0):
.. _delete: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/delete/
"""

key = check_key(key)
key = wrap_key(key)
if isinstance(space_name, str):
space_name = self.schema.get_space(space_name).sid
if isinstance(index, str):
Expand Down Expand Up @@ -1349,7 +1349,7 @@ def update(self, space_name, key, op_list, *, index=0):
.. _update: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/update/
"""

key = check_key(key)
key = wrap_key(key)
if isinstance(space_name, str):
space_name = self.schema.get_space(space_name).sid
if isinstance(index, str):
Expand Down Expand Up @@ -1534,7 +1534,7 @@ def select(self, space_name, key=None, *, offset=0, limit=0xffffffff, index=0, i

# Perform smart type checking (scalar / list of scalars / list of
# tuples)
key = check_key(key, select=True)
key = wrap_key(key, select=True)

if isinstance(space_name, str):
space_name = self.schema.get_space(space_name).sid
Expand Down
29 changes: 12 additions & 17 deletions tarantool/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import sys
import uuid

supported_types = (int, str, bytes, float,)

ENCODING_DEFAULT = "utf-8"

from base64 import decodebytes as base64_decode
Expand All @@ -22,33 +20,30 @@ def strxor(rhs, lhs):

return bytes([x ^ y for x, y in zip(rhs, lhs)])

def check_key(*args, **kwargs):
def wrap_key(*args, first=True, select=False):
"""
Validate request key types and map.
Wrap request key in list, if needed.

:param args: Method args.
:type args: :obj:`tuple`

:param kwargs: Method kwargs.
:type kwargs: :obj:`dict`
:param first: ``True`` if this is the first recursion iteration.
:type first: :obj:`bool`

:param select: ``True`` if wrapping SELECT request key.
:type select: :obj:`bool`

:rtype: :obj:`list`
"""

if 'first' not in kwargs:
kwargs['first'] = True
if 'select' not in kwargs:
kwargs['select'] = False
if len(args) == 0 and kwargs['select']:
if len(args) == 0 and select:
return []
if len(args) == 1:
if isinstance(args[0], (list, tuple)) and kwargs['first']:
kwargs['first'] = False
return check_key(*args[0], **kwargs)
elif args[0] is None and kwargs['select']:
if isinstance(args[0], (list, tuple)) and first:
return wrap_key(*args[0], first=False, select=select)
elif args[0] is None and select:
return []
for key in args:
assert isinstance(key, supported_types)

return list(args)


Expand Down
15 changes: 15 additions & 0 deletions test/suites/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def setUpClass(self):
parts = {1, 'string'},
unique = true})

pcall(function()
box.schema.space.create('test_pk')
box.space['test_pk']:create_index('primary', {
type = 'tree',
parts = {1, 'datetime'},
unique = true})
end)

box.schema.user.create('test', {password = 'test', if_not_exists = true})
box.schema.user.grant('test', 'read,write,execute', 'universe')

Expand Down Expand Up @@ -528,6 +536,13 @@ def test_tarantool_datetime_addition_winter_time_switch(self):
[case['res']])


@skip_or_run_datetime_test
def test_primary_key(self):
data = [tarantool.Datetime(year=1970, month=1, day=1), 'content']

self.assertSequenceEqual(self.con.insert('test_pk', data), [data])
self.assertSequenceEqual(self.con.select('test_pk', data[0]), [data])

@classmethod
def tearDownClass(self):
self.con.close()
Expand Down
16 changes: 16 additions & 0 deletions test/suites/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def setUpClass(self):
parts = {1, 'string'},
unique = true})

pcall(function()
box.schema.space.create('test_pk')
box.space['test_pk']:create_index('primary', {
type = 'tree',
parts = {1, 'decimal'},
unique = true})
end)

box.schema.user.create('test', {password = 'test', if_not_exists = true})
box.schema.user.grant('test', 'read,write,execute', 'universe')
""")
Expand Down Expand Up @@ -421,6 +429,14 @@ def test_tarantool_encode_with_precision_loss(self):
self.assertSequenceEqual(self.con.eval(lua_eval), [True])


@skip_or_run_decimal_test
def test_primary_key(self):
data = [decimal.Decimal('0'), 'content']

self.assertSequenceEqual(self.con.insert('test_pk', data), [data])
self.assertSequenceEqual(self.con.select('test_pk', data[0]), [data])


@classmethod
def tearDownClass(self):
self.con.close()
Expand Down
16 changes: 16 additions & 0 deletions test/suites/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def setUpClass(self):
parts = {1, 'string'},
unique = true})

pcall(function()
box.schema.space.create('test_pk')
box.space['test_pk']:create_index('primary', {
type = 'tree',
parts = {1, 'uuid'},
unique = true})
end)

box.schema.user.create('test', {password = 'test', if_not_exists = true})
box.schema.user.grant('test', 'read,write,execute', 'universe')
""")
Expand Down Expand Up @@ -125,6 +133,14 @@ def test_tarantool_encode(self):
self.assertSequenceEqual(self.con.eval(lua_eval), [True])


@skip_or_run_UUID_test
def test_primary_key(self):
data = [uuid.UUID('ae28d4f6-076c-49dd-8227-7f9fae9592d0'), 'content']

self.assertSequenceEqual(self.con.insert('test_pk', data), [data])
self.assertSequenceEqual(self.con.select('test_pk', data[0]), [data])


@classmethod
def tearDownClass(self):
self.con.close()
Expand Down