diff --git a/lib/mock_redis/hash_methods.rb b/lib/mock_redis/hash_methods.rb index 0320f46e..4eefe757 100644 --- a/lib/mock_redis/hash_methods.rb +++ b/lib/mock_redis/hash_methods.rb @@ -84,10 +84,17 @@ def mapped_hmget(key, *fields) end def hmset(key, *kvpairs) + if key.is_a? Array + err_msg = 'ERR wrong number of arguments for \'hmset\' command' + 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_msg || 'ERR wrong number of arguments for HMSET' end kvpairs.each_slice(2) do |(k, v)| @@ -143,7 +150,7 @@ def hvals(key) private def with_hash_at(key, &blk) - with_thing_at(key, :assert_hashy, proc { {} }, &blk) + with_thing_at(key.to_s, :assert_hashy, proc { {} }, &blk) end def hashy?(key) diff --git a/spec/commands/hmset_spec.rb b/spec/commands/hmset_spec.rb index fc7f260b..b5157f71 100644 --- a/spec/commands/hmset_spec.rb +++ b/spec/commands/hmset_spec.rb @@ -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