summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--redis/commands/core.py8
-rw-r--r--tests/test_commands.py25
2 files changed, 18 insertions, 15 deletions
diff --git a/redis/commands/core.py b/redis/commands/core.py
index 073161f..3569d20 100644
--- a/redis/commands/core.py
+++ b/redis/commands/core.py
@@ -3943,8 +3943,8 @@ class SortedSetCommands(CommandsProtocol):
xx: bool = False,
ch: bool = False,
incr: bool = False,
- gt: bool = None,
- lt: bool = None,
+ gt: bool = False,
+ lt: bool = False,
) -> ResponseT:
"""
Set any number of element-name, score pairs to the key ``name``. Pairs
@@ -3983,12 +3983,14 @@ class SortedSetCommands(CommandsProtocol):
raise DataError("ZADD requires at least one element/score pair")
if nx and xx:
raise DataError("ZADD allows either 'nx' or 'xx', not both")
+ if gt and lt:
+ raise DataError("ZADD allows either 'gt' or 'lt', not both")
if incr and len(mapping) != 1:
raise DataError(
"ZADD option 'incr' only works when passing a "
"single element/score pair"
)
- if nx is True and (gt is not None or lt is not None):
+ if nx and (gt or lt):
raise DataError("Only one of 'nx', 'lt', or 'gr' may be defined.")
pieces: list[EncodableT] = []
diff --git a/tests/test_commands.py b/tests/test_commands.py
index d5ab4e3..a3972a5 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -2215,20 +2215,21 @@ class TestRedisCommands:
@skip_if_server_version_lt("6.2.0")
def test_zadd_gt_lt(self, r):
+ r.zadd("a", {"a": 2})
+ assert r.zadd("a", {"a": 5}, gt=True, ch=True) == 1
+ assert r.zadd("a", {"a": 1}, gt=True, ch=True) == 0
+ assert r.zadd("a", {"a": 5}, lt=True, ch=True) == 0
+ assert r.zadd("a", {"a": 1}, lt=True, ch=True) == 1
- for i in range(1, 20):
- r.zadd("a", {f"a{i}": i})
- assert r.zadd("a", {"a20": 5}, gt=3) == 1
-
- for i in range(1, 20):
- r.zadd("a", {f"a{i}": i})
- assert r.zadd("a", {"a2": 5}, lt=1) == 0
-
- # cannot use both nx and xx options
+ # cannot combine both nx and xx options and gt and lt options
+ with pytest.raises(exceptions.DataError):
+ r.zadd("a", {"a15": 15}, nx=True, lt=True)
+ with pytest.raises(exceptions.DataError):
+ r.zadd("a", {"a15": 15}, nx=True, gt=True)
+ with pytest.raises(exceptions.DataError):
+ r.zadd("a", {"a15": 15}, lt=True, gt=True)
with pytest.raises(exceptions.DataError):
- r.zadd("a", {"a15": 155}, nx=True, lt=True)
- r.zadd("a", {"a15": 155}, nx=True, gt=True)
- r.zadd("a", {"a15": 155}, lt=True, gt=True)
+ r.zadd("a", {"a15": 15}, nx=True, xx=True)
def test_zcard(self, r):
r.zadd("a", {"a1": 1, "a2": 2, "a3": 3})