Skip to content

Commit

Permalink
Add support for LMPOP (#1843)
Browse files Browse the repository at this point in the history
* Add support for LMPOP

* add type hints

* fix linters
  • Loading branch information
dvora-h authored Feb 2, 2022
1 parent b541da6 commit aaddeb6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
19 changes: 19 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1917,6 +1917,25 @@ def brpoplpush(self, src, dst, timeout=0):
timeout = 0
return self.execute_command("BRPOPLPUSH", src, dst, timeout)

def lmpop(
self,
num_keys: int,
*args: List[str],
direction: str = None,
count: Optional[int] = 1,
) -> List:
"""
Pop ``count`` values (default 1) first non-empty list key from the list
of args provided key names.
For more information check https://redis.io/commands/lmpop
"""
args = [num_keys] + list(args) + [direction]
if count != 1:
args.extend(["COUNT", count])

return self.execute_command("LMPOP", *args)

def lindex(self, name, index):
"""
Return the item from list ``name`` at position ``index``
Expand Down
11 changes: 11 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,17 @@ def test_brpoplpush_empty_string(self, r):
r.rpush("a", "")
assert r.brpoplpush("a", "b") == b""

@pytest.mark.onlynoncluster
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
def test_lmpop(self, unstable_r):
unstable_r.rpush("foo", "1", "2", "3", "4", "5")
result = [b"foo", [b"1", b"2"]]
assert unstable_r.lmpop("2", "bar", "foo", direction="LEFT", count=2) == result
with pytest.raises(redis.ResponseError):
unstable_r.lmpop("2", "bar", "foo", direction="up", count=2)
unstable_r.rpush("bar", "a", "b", "c", "d")
assert unstable_r.lmpop("2", "bar", "foo", direction="LEFT") == [b"bar", [b"a"]]

def test_lindex(self, r):
r.rpush("a", "1", "2", "3")
assert r.lindex("a", "0") == b"1"
Expand Down

0 comments on commit aaddeb6

Please sign in to comment.