Skip to content

Commit

Permalink
Raise error when hdel is called with empty array (#215)
Browse files Browse the repository at this point in the history
Close #212
  • Loading branch information
hieuk09 committed Aug 12, 2021
1 parent a865df1 commit 8d1d36c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/mock_redis/hash_methods.rb
Expand Up @@ -10,6 +10,11 @@ def hdel(key, *fields)
with_hash_at(key) do |hash|
orig_size = hash.size
fields = Array(fields).flatten.map(&:to_s)

if fields.empty?
raise Redis::CommandError, "ERR wrong number of arguments for 'hdel' command"
end

hash.delete_if { |k, _v| fields.include?(k) }
orig_size - hash.size
end
Expand Down
7 changes: 7 additions & 0 deletions spec/commands/hdel_spec.rb
Expand Up @@ -66,5 +66,12 @@
@redises.get(@key).should be_nil
end

it 'raises error if an empty array is passed' do
expect { @redises.hdel(@key, []) }.to raise_error(
Redis::CommandError,
"ERR wrong number of arguments for 'hdel' command"
)
end

it_should_behave_like 'a hash-only command'
end

0 comments on commit 8d1d36c

Please sign in to comment.