Skip to content

Commit d3b9130

Browse files
committed
LT and GT support for ZADD
Part of the new commands in redis#1434
1 parent fc621bd commit d3b9130

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

redis/client.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2747,7 +2747,8 @@ def xtrim(self, name, maxlen, approximate=True):
27472747
return self.execute_command('XTRIM', name, *pieces)
27482748

27492749
# SORTED SET COMMANDS
2750-
def zadd(self, name, mapping, nx=False, xx=False, ch=False, incr=False):
2750+
def zadd(self, name, mapping, nx=False, xx=False, ch=False, incr=False,
2751+
gt=None, lt=None):
27512752
"""
27522753
Set any number of element-name, score pairs to the key ``name``. Pairs
27532754
are specified as a dict of element-names keys to score values.
@@ -2789,6 +2790,10 @@ def zadd(self, name, mapping, nx=False, xx=False, ch=False, incr=False):
27892790
if incr:
27902791
pieces.append(b'INCR')
27912792
options['as_score'] = True
2793+
if gt:
2794+
pieces.append(b'GT')
2795+
if lt:
2796+
pieces.append(b'LT')
27922797
for pair in mapping.items():
27932798
pieces.append(pair[1])
27942799
pieces.append(pair[0])

tests/test_commands.py

+11
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,17 @@ def test_zadd_incr_with_xx(self, r):
13751375
# redis-py
13761376
assert r.zadd('a', {'a1': 1}, xx=True, incr=True) is None
13771377

1378+
@skip_if_server_version_lt('6.2.0')
1379+
def test_zadd_gt_lt(self, r):
1380+
1381+
for i in range(1, 20):
1382+
r.zadd('a', {'a%s' % i: i})
1383+
assert r.zadd('a', {'a20': 5}, gt=3) == 1
1384+
1385+
for i in range(1, 20):
1386+
r.zadd('a', {'a%s' % i: i})
1387+
assert r.zadd('a', {'a2': 5}, lt=1) == 0
1388+
13781389
def test_zcard(self, r):
13791390
r.zadd('a', {'a1': 1, 'a2': 2, 'a3': 3})
13801391
assert r.zcard('a') == 3

0 commit comments

Comments
 (0)