Skip to content

Commit 9328025

Browse files
committed
Add support for LMPOP
1 parent 231d402 commit 9328025

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

redis/commands/core.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,19 @@ def brpoplpush(self, src, dst, timeout=0):
19261926
timeout = 0
19271927
return self.execute_command("BRPOPLPUSH", src, dst, timeout)
19281928

1929+
def lmpop(self, num_keys, keys, *args, count=1):
1930+
"""
1931+
Pop ``count`` values (default 1) first non-empty list key from the list
1932+
of provided key names.
1933+
1934+
For more information check https://redis.io/commands/lmpop
1935+
"""
1936+
args = [num_keys] + list_or_args(keys, args)
1937+
if count != 1:
1938+
args.extend(["COUNT", count])
1939+
1940+
return self.execute_command("LMPOP", *args)
1941+
19291942
def lindex(self, name, index):
19301943
"""
19311944
Return the item from list ``name`` at position ``index``

tests/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
REDIS_INFO = {}
1616
default_redis_url = "redis://localhost:6379/9"
1717
default_redismod_url = "redis://localhost:36379"
18+
default_redis_unstable_url = "redis://localhost:6378"
1819

1920
# default ssl client ignores verification for the purpose of testing
2021
default_redis_ssl_url = "rediss://localhost:6666"
@@ -54,6 +55,14 @@ def pytest_addoption(parser):
5455
" defaults to `%(default)s`",
5556
)
5657

58+
parser.addoption(
59+
"--redis-unstable-url",
60+
default=default_redis_unstable_url,
61+
action="store",
62+
help="Redis unstable (latest version) connection string "
63+
"defaults to %(default)s`",
64+
)
65+
5766

5867
def _get_info(redis_url):
5968
client = redis.Redis.from_url(redis_url)
@@ -357,6 +366,13 @@ def master_host(request):
357366
yield parts.hostname, parts.port
358367

359368

369+
@pytest.fixture()
370+
def unstable_r(request):
371+
url = request.config.getoption("--redis-unstable-url")
372+
with _get_client(redis.Redis, request, from_url=url) as client:
373+
yield client
374+
375+
360376
def wait_for_command(client, monitor, command, key=None):
361377
# issue a command with a key name that's local to this process.
362378
# if we find a command with our key before the command we're waiting

tests/test_commands.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,17 @@ def test_brpoplpush_empty_string(self, r):
14731473
r.rpush("a", "")
14741474
assert r.brpoplpush("a", "b") == b""
14751475

1476+
@pytest.mark.onlynoncluster
1477+
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
1478+
def test_lmpop(self, unstable_r):
1479+
unstable_r.rpush("foo", "1", "2", "3", "4", "5")
1480+
result = [b"foo", [b"1", b"2"]]
1481+
assert unstable_r.lmpop("2", "bar", "foo", "LEFT", count=2) == result
1482+
with pytest.raises(redis.ResponseError):
1483+
unstable_r.lmpop("2", "bar", "foo", count=2)
1484+
unstable_r.rpush("bar", "a", "b", "c", "d")
1485+
assert unstable_r.lmpop("2", ["bar", "foo"], "RIGHT") == [b"bar", [b"d"]]
1486+
14761487
def test_lindex(self, r):
14771488
r.rpush("a", "1", "2", "3")
14781489
assert r.lindex("a", "0") == b"1"

0 commit comments

Comments
 (0)