From 35074c95f365047eea2833fb66ff1ff804bdf5c4 Mon Sep 17 00:00:00 2001 From: Amotz Getzov Date: Thu, 23 Jun 2016 10:49:11 +0300 Subject: [PATCH 1/2] Allow range arguments to come as string to fix redis.call from Lua. --- mockredis/client.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mockredis/client.py b/mockredis/client.py index ab2a010..876fa0b 100644 --- a/mockredis/client.py +++ b/mockredis/client.py @@ -1444,6 +1444,8 @@ def _translate_range(self, len_, start, end): """ Translate range to valid bounds. """ + start = int(start) + end = int(end) if start < 0: start += len_ start = max(0, min(start, len_)) From 03a48e536d1c0404d6e69cbd110e9bc8c841b27a Mon Sep 17 00:00:00 2001 From: Amotz Getzov Date: Sun, 4 Dec 2016 14:11:13 +0200 Subject: [PATCH 2/2] Unit tess for client translation functions for range and limit. --- mockredis/tests/test_translate.py | 51 +++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 mockredis/tests/test_translate.py diff --git a/mockredis/tests/test_translate.py b/mockredis/tests/test_translate.py new file mode 100644 index 0000000..7b57a6b --- /dev/null +++ b/mockredis/tests/test_translate.py @@ -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