Skip to content

Allow range arguments to come as string. #108

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions mockredis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,8 @@ def _translate_range(self, len_, start, end):
"""
Translate range to valid bounds.
"""
start = int(start)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test to cover this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tester for client.py translation functions.
See comment about general test failure below.

end = int(end)
if start < 0:
start += len_
start = max(0, min(start, len_))
Expand Down
51 changes: 51 additions & 0 deletions mockredis/tests/test_translate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Test redis command range translation.
"""
from nose.tools import eq_

from mockredis.client import MockRedis


def test_translate_range():

# Translation expected to truncate start indices into [0, len], and end indices into [-1, len-1].
# It also handle digit string input, and convert negative indices to positive by counting from len backwards.
# It is not expected to enforce start <= end.
cases = [
(10, 1, 7, (1, 7)),
(10, '1', '7', (1, 7)),
(10, 1, -7, (1, 3)),
(10, 1, '-7', (1, 3)),
(10, -1, 7, (9, 7)),
(10, '-1', -11, (9, -1)),
(10, -11, -10, (0, 0)),
]

def _test(len_, start, end, expected):
redis = MockRedis()
eq_(tuple(redis._translate_range(len_, start, end)), expected)

for length, start_index, end_index, required in cases:
yield _test, length, start_index, end_index, required


def test_translate_limit():

# Translation expected to verify start is not larger than length and num is not less than or equal zero.
# If condition does not verify, the tuple (0,0) is returned, else (start, num) is returned.
cases = [
(10, 3, 0, (0, 0)),
(10, 3, -4, (0, 0)),
(10, 11, 2, (0, 0)),
(10, 11, -4, (0, 0)),
(10, 3, 2, (3, 2)),
(10, 3, 11, (3, 11)),
(10, 3, 9, (3, 9)),
]

def _test(len_, start, num, expected):
redis = MockRedis()
eq_(tuple(redis._translate_limit(len_, start, num)), expected)

for length, start_index, offset, required in cases:
yield _test, length, start_index, offset, required