Skip to content

Commit aee12a8

Browse files
authored
Merge pull request #87 from charleswhchan/issue-85
CC - Fix issue #85. Return length of list after LPUSH/RPUSH command.
2 parents fa0d1b0 + 5e519b9 commit aee12a8

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

mockredis/client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,11 @@ def lpush(self, key, *args):
691691
# Creates the list at this key if it doesn't exist, and appends args to its beginning
692692
args_reversed = [self._encode(arg) for arg in args]
693693
args_reversed.reverse()
694-
self.redis[self._encode(key)] = args_reversed + redis_list
694+
updated_list = args_reversed + redis_list
695+
self.redis[self._encode(key)] = updated_list
696+
697+
# Return the length of the list after the push operation
698+
return len(updated_list)
695699

696700
def rpop(self, key):
697701
"""Emulate lpop."""
@@ -716,6 +720,9 @@ def rpush(self, key, *args):
716720
# Creates the list at this key if it doesn't exist, and appends args to it
717721
redis_list.extend(map(self._encode, args))
718722

723+
# Return the length of the list after the push operation
724+
return len(redis_list)
725+
719726
def lrem(self, key, value, count=0):
720727
"""Emulate lrem."""
721728
value = self._encode(value)

mockredis/tests/test_list.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ def test_lpush(self):
8686
Insertion maintains order but not uniqueness.
8787
"""
8888
# lpush two values
89-
self.redis.lpush(LIST1, VAL1)
90-
self.redis.lpush(LIST1, VAL2)
89+
eq_(1, self.redis.lpush(LIST1, VAL1))
90+
eq_(2, self.redis.lpush(LIST1, VAL2))
9191

9292
# validate insertion
9393
eq_(b"list", self.redis.type(LIST1))
9494
eq_([bVAL2, bVAL1], self.redis.lrange(LIST1, 0, -1))
9595

9696
# insert two more values with one repeated
97-
self.redis.lpush(LIST1, VAL1, VAL3)
97+
eq_(4, self.redis.lpush(LIST1, VAL1, VAL3))
9898

9999
# validate the update
100100
eq_(b"list", self.redis.type(LIST1))
@@ -128,15 +128,15 @@ def test_rpush(self):
128128
Insertion maintains order but not uniqueness.
129129
"""
130130
# rpush two values
131-
self.redis.rpush(LIST1, VAL1)
132-
self.redis.rpush(LIST1, VAL2)
131+
eq_(1, self.redis.rpush(LIST1, VAL1))
132+
eq_(2, self.redis.rpush(LIST1, VAL2))
133133

134134
# validate insertion
135135
eq_(b"list", self.redis.type(LIST1))
136136
eq_([bVAL1, bVAL2], self.redis.lrange(LIST1, 0, -1))
137137

138138
# insert two more values with one repeated
139-
self.redis.rpush(LIST1, VAL1, VAL3)
139+
eq_(4, self.redis.rpush(LIST1, VAL1, VAL3))
140140

141141
# validate the update
142142
eq_(b"list", self.redis.type(LIST1))

0 commit comments

Comments
 (0)