From c68fcb50f246d3b18fd625665b219f849612ced0 Mon Sep 17 00:00:00 2001 From: Steven Harman Date: Mon, 15 Jan 2024 13:00:25 -0500 Subject: [PATCH] Add srem? for redis-rb compatibility --- lib/mock_redis/set_methods.rb | 5 +++++ spec/commands/srem_spec.rb | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/mock_redis/set_methods.rb b/lib/mock_redis/set_methods.rb index bf355b6..138a3db 100644 --- a/lib/mock_redis/set_methods.rb +++ b/lib/mock_redis/set_methods.rb @@ -137,6 +137,11 @@ def srem(key, members) end end + def srem?(key, members) + res = srem(key, members) + res.is_a?(Numeric) ? res > 0 : res + end + def sscan(key, cursor, opts = {}) common_scan(smembers(key), cursor, opts) end diff --git a/spec/commands/srem_spec.rb b/spec/commands/srem_spec.rb index 452f704..2c68b97 100644 --- a/spec/commands/srem_spec.rb +++ b/spec/commands/srem_spec.rb @@ -41,5 +41,20 @@ expect(@redises.srem(@key, [1, 2])).to eq(2) end + context 'srem?' do + it 'returns true if member is in the set' do + expect(@redises.srem?(@key, 'bert')).to eq(true) + end + + it 'returns false if member is not in the set' do + expect(@redises.srem?(@key, 'cookiemonster')).to eq(false) + end + + it 'removes member from the set' do + @redises.srem?(@key, 'ernie') + expect(@redises.smembers(@key)).to eq(['bert']) + end + end + it_should_behave_like 'a set-only command' end