From 5e519b9436f7eb879b270693f121adb0c23be16f Mon Sep 17 00:00:00 2001 From: Charles Chan Date: Sat, 23 May 2015 17:48:56 -0700 Subject: [PATCH] CC - Fix issue #85. Return length of list after LPUSH/RPUSH command. See: http://redis.io/commands/lpush http://redis.io/commands/rpush https://github.com/locationlabs/mockredis/issues/85 --- mockredis/client.py | 9 ++++++++- mockredis/tests/test_list.py | 12 ++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/mockredis/client.py b/mockredis/client.py index 998d3c5..994a31c 100644 --- a/mockredis/client.py +++ b/mockredis/client.py @@ -648,7 +648,11 @@ def lpush(self, key, *args): # Creates the list at this key if it doesn't exist, and appends args to its beginning args_reversed = [self._encode(arg) for arg in args] args_reversed.reverse() - self.redis[self._encode(key)] = args_reversed + redis_list + updated_list = args_reversed + redis_list + self.redis[self._encode(key)] = updated_list + + # Return the length of the list after the push operation + return len(updated_list) def rpop(self, key): """Emulate lpop.""" @@ -673,6 +677,9 @@ def rpush(self, key, *args): # Creates the list at this key if it doesn't exist, and appends args to it redis_list.extend(map(self._encode, args)) + # Return the length of the list after the push operation + return len(redis_list) + def lrem(self, key, value, count=0): """Emulate lrem.""" value = self._encode(value) diff --git a/mockredis/tests/test_list.py b/mockredis/tests/test_list.py index 5269d10..d9b85be 100644 --- a/mockredis/tests/test_list.py +++ b/mockredis/tests/test_list.py @@ -86,15 +86,15 @@ def test_lpush(self): Insertion maintains order but not uniqueness. """ # lpush two values - self.redis.lpush(LIST1, VAL1) - self.redis.lpush(LIST1, VAL2) + eq_(1, self.redis.lpush(LIST1, VAL1)) + eq_(2, self.redis.lpush(LIST1, VAL2)) # validate insertion eq_(b"list", self.redis.type(LIST1)) eq_([bVAL2, bVAL1], self.redis.lrange(LIST1, 0, -1)) # insert two more values with one repeated - self.redis.lpush(LIST1, VAL1, VAL3) + eq_(4, self.redis.lpush(LIST1, VAL1, VAL3)) # validate the update eq_(b"list", self.redis.type(LIST1)) @@ -128,15 +128,15 @@ def test_rpush(self): Insertion maintains order but not uniqueness. """ # rpush two values - self.redis.rpush(LIST1, VAL1) - self.redis.rpush(LIST1, VAL2) + eq_(1, self.redis.rpush(LIST1, VAL1)) + eq_(2, self.redis.rpush(LIST1, VAL2)) # validate insertion eq_(b"list", self.redis.type(LIST1)) eq_([bVAL1, bVAL2], self.redis.lrange(LIST1, 0, -1)) # insert two more values with one repeated - self.redis.rpush(LIST1, VAL1, VAL3) + eq_(4, self.redis.rpush(LIST1, VAL1, VAL3)) # validate the update eq_(b"list", self.redis.type(LIST1))