Skip to content

Commit

Permalink
Fix inconsistent behavior between Redis#hdel, MockRedis#hdel
Browse files Browse the repository at this point in the history
  • Loading branch information
numbata authored and sds committed Jun 26, 2019
1 parent 90f6a5a commit 942cf0f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
12 changes: 4 additions & 8 deletions lib/mock_redis/hash_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions spec/commands/hdel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 942cf0f

Please sign in to comment.