From 40398c83698cc42cc93e4064a3e3f09f1a722730 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Thu, 7 Oct 2021 22:50:11 +0300 Subject: [PATCH] [Redis 6.2] Add LT/GT options to ZADD --- lib/redis.rb | 8 ++++++- test/lint/sorted_sets.rb | 51 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/redis.rb b/lib/redis.rb index 52509569f..c7c38700b 100644 --- a/lib/redis.rb +++ b/lib/redis.rb @@ -1641,6 +1641,10 @@ def zcard(key) # add elements) # - `:nx => true`: Don't update already existing elements (always # add new elements) + # - `:lt => true`: Only update existing elements if the new score + # is less than the current score + # - `:gt => true`: Only update existing elements if the new score + # is greater than the current score # - `:ch => true`: Modify the return value from the number of new # elements added, to the total number of elements changed (CH is an # abbreviation of changed); changed elements are new elements added @@ -1655,10 +1659,12 @@ def zcard(key) # pairs that were **added** to the sorted set. # - `Float` when option :incr is specified, holding the score of the member # after incrementing it. - def zadd(key, *args, nx: nil, xx: nil, ch: nil, incr: nil) + def zadd(key, *args, nx: nil, xx: nil, lt: nil, gt: nil, ch: nil, incr: nil) command = [:zadd, key] command << "NX" if nx command << "XX" if xx + command << "LT" if lt + command << "GT" if gt command << "CH" if ch command << "INCR" if incr diff --git a/test/lint/sorted_sets.rb b/test/lint/sorted_sets.rb index 288155cd7..22bbebdcf 100644 --- a/test/lint/sorted_sets.rb +++ b/test/lint/sorted_sets.rb @@ -45,6 +45,35 @@ def test_zadd # Incompatible options combination assert_raises(Redis::CommandError) { r.zadd("foo", 1, "s1", xx: true, nx: true) } end + + target_version "6.2" do + # LT option + r.zadd("foo", 2, "s1") + + r.zadd("foo", 3, "s1", lt: true) + assert_equal 2.0, r.zscore("foo", "s1") + + r.zadd("foo", 1, "s1", lt: true) + assert_equal 1.0, r.zscore("foo", "s1") + + assert_equal true, r.zadd("foo", 3, "s2", lt: true) # adds new member + r.del "foo" + + # GT option + r.zadd("foo", 2, "s1") + + r.zadd("foo", 1, "s1", gt: true) + assert_equal 2.0, r.zscore("foo", "s1") + + r.zadd("foo", 3, "s1", gt: true) + assert_equal 3.0, r.zscore("foo", "s1") + + assert_equal true, r.zadd("foo", 1, "s2", gt: true) # adds new member + r.del "foo" + + # Incompatible options combination + assert_raises(Redis::CommandError) { r.zadd("foo", 1, "s1", nx: true, gt: true) } + end end def test_variadic_zadd @@ -109,6 +138,28 @@ def test_variadic_zadd # Incompatible options combination assert_raises(Redis::CommandError) { r.zadd("foo", [1, "s1"], xx: true, nx: true) } end + + target_version "6.2" do + # LT option + r.zadd("foo", 2, "s1") + + assert_equal 1, r.zadd("foo", [3, "s1", 2, "s2"], lt: true, ch: true) + assert_equal 2.0, r.zscore("foo", "s1") + + assert_equal 1, r.zadd("foo", [1, "s1"], lt: true, ch: true) + + r.del "foo" + + # GT option + r.zadd("foo", 2, "s1") + + assert_equal 1, r.zadd("foo", [1, "s1", 2, "s2"], gt: true, ch: true) + assert_equal 2.0, r.zscore("foo", "s1") + + assert_equal 1, r.zadd("foo", [3, "s1"], gt: true, ch: true) + + r.del "foo" + end end def test_zrem