Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inconsistent behavior between Redis#hdel, MockRedis#hdel #168

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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