From 4c46dcdce0375eb80bafaf3382ea33d2e98a5b69 Mon Sep 17 00:00:00 2001 From: Andrey Subbota Date: Mon, 24 Jun 2019 15:56:53 +0300 Subject: [PATCH] Fix inconsistent behavior between `Redis#hdel`, `MockRedis#hdel` --- lib/mock_redis/hash_methods.rb | 12 ++++-------- spec/commands/hdel_spec.rb | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/mock_redis/hash_methods.rb b/lib/mock_redis/hash_methods.rb index c071967a..0320f46e 100644 --- a/lib/mock_redis/hash_methods.rb +++ b/lib/mock_redis/hash_methods.rb @@ -8,14 +8,10 @@ module HashMethods def hdel(key, *fields) with_hash_at(key) do |hash| - if fields.is_a?(Array) - orig_size = hash.size - fields = fields.map(&:to_s) - hash.delete_if { |k, _v| fields.include?(k) } - orig_size - hash.size - else - hash.delete(fields[0].to_s) ? 1 : 0 - end + orig_size = hash.size + fields = Array(fields).flatten.map(&:to_s) + hash.delete_if { |k, _v| fields.include?(k) } + orig_size - hash.size end end diff --git a/spec/commands/hdel_spec.rb b/spec/commands/hdel_spec.rb index 5699c3df..d015569d 100644 --- a/spec/commands/hdel_spec.rb +++ b/spec/commands/hdel_spec.rb @@ -50,5 +50,21 @@ @redises.hget(@key, field).should be_nil end + it 'supports a variable number of fields as array' do + @redises.hdel(@key, %w[k1 k2]).should == 2 + + @redises.hget(@key, 'k1').should be_nil + @redises.hget(@key, 'k2').should be_nil + @redises.get(@key).should be_nil + end + + it 'supports a list of fields in various way' do + @redises.hdel(@key, ['k1'], 'k2').should == 2 + + @redises.hget(@key, 'k1').should be_nil + @redises.hget(@key, 'k2').should be_nil + @redises.get(@key).should be_nil + end + it_should_behave_like 'a hash-only command' end