Skip to content

Commit

Permalink
Merge pull request locationlabs#87 from charleswhchan/issue-85
Browse files Browse the repository at this point in the history
CC - Fix issue locationlabs#85. Return length of list after LPUSH/RPUSH command.
  • Loading branch information
srikalyan authored Nov 9, 2016
2 parents fa0d1b0 + 5e519b9 commit aee12a8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
9 changes: 8 additions & 1 deletion mockredis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,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."""
Expand All @@ -716,6 +720,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)
Expand Down
12 changes: 6 additions & 6 deletions mockredis/tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit aee12a8

Please sign in to comment.