Skip to content

Commit

Permalink
Allows hmset to take an array as its only arg.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Luckett committed Oct 9, 2019
1 parent 3daddc1 commit 429afe4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
8 changes: 7 additions & 1 deletion lib/mock_redis/hash_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,16 @@ def mapped_hmget(key, *fields)
end

def hmset(key, *kvpairs)
if key.is_a? Array
kvpairs = key[1..-1]
key = key[0]
end

kvpairs.flatten!
assert_has_args(kvpairs, 'hmset')

if kvpairs.length.odd?
raise Redis::CommandError, 'ERR wrong number of arguments for HMSET'
raise Redis::CommandError, 'ERR wrong number of arguments for \'hmset\' command'
end

kvpairs.each_slice(2) do |(k, v)|
Expand Down
26 changes: 26 additions & 0 deletions spec/commands/hmset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,31 @@
end.should raise_error(Redis::CommandError)
end

# The following tests address https://github.com/sds/mock_redis/issues/170
context 'keys are stored as strings' do
before do
@redises.hmset(1, :foo, :bar)
@redises.hmset(:a_sym, :boo, :bas)
end

it { expect(@redises.hgetall('1')).to eq @redises.hgetall(1) }
it { expect(@redises.hgetall('a_sym')).to eq @redises.hgetall(:a_sym) }
it { expect(@redises.del('1')).to eq 1 }
it { expect(@redises.del(1)).to eq 1 }
it { expect(@redises.del('a_sym')).to eq 1 }
it { expect(@redises.del(:a_sym)).to eq 1 }

after do
@redises.del(1)
@redises.del(:a_sym)
end
end

# The following tests address https://github.com/sds/mock_redis/issues/134
context 'hmset accepts an array as its only argument' do
it { expect(@redises.hmset([@key, :bar, :bas])).to eq 'OK' }
it { lambda { @redises.hmset([:foo, :bar]) }.should raise_error(Redis::CommandError) }
end

it_should_behave_like 'a hash-only command'
end
24 changes: 0 additions & 24 deletions spec/hash_methods_spec.rb

This file was deleted.

0 comments on commit 429afe4

Please sign in to comment.