From 3daddc18f03109cd6525bcec55b91b0ec86744bc Mon Sep 17 00:00:00 2001 From: Jeff Luckett Date: Mon, 7 Oct 2019 14:00:35 -0400 Subject: [PATCH 1/3] Converts keys to strings --- lib/mock_redis/hash_methods.rb | 2 +- spec/hash_methods_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 spec/hash_methods_spec.rb diff --git a/lib/mock_redis/hash_methods.rb b/lib/mock_redis/hash_methods.rb index 0320f46e..4c207611 100644 --- a/lib/mock_redis/hash_methods.rb +++ b/lib/mock_redis/hash_methods.rb @@ -143,7 +143,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/hash_methods_spec.rb b/spec/hash_methods_spec.rb new file mode 100644 index 00000000..629b966b --- /dev/null +++ b/spec/hash_methods_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' + +describe MockRedis::HashMethods do + subject { MockRedis.new } + + context 'all keys treated as strings' do + before do + subject.hmset(:foo, [:bar, :bas]) + subject.hmset(1, [:free, :bird]) + end + + it { expect(subject.hgetall('foo')).to eq 'bar' => 'bas' } + it { expect(subject.hgetall(:foo)).to eq 'bar' => 'bas' } + it { expect(subject.hgetall(1)).to eq 'free' => 'bird' } + it { expect(subject.hgetall('1')).to eq 'free' => 'bird' } + + context 'keys are deletable' do + it { expect(subject.del(1)).to eq 1 } + it { expect(subject.del('1')).to eq 1 } + it { expect(subject.del(:foo)).to eq 1 } + it { expect(subject.del('foo')).to eq 1 } + end + end +end From 429afe4ac1d35a82386ffedb9ceadf0ed0efcb99 Mon Sep 17 00:00:00 2001 From: Jeff Luckett Date: Tue, 8 Oct 2019 11:28:53 -0400 Subject: [PATCH 2/3] Allows hmset to take an array as its only arg. --- lib/mock_redis/hash_methods.rb | 8 +++++++- spec/commands/hmset_spec.rb | 26 ++++++++++++++++++++++++++ spec/hash_methods_spec.rb | 24 ------------------------ 3 files changed, 33 insertions(+), 25 deletions(-) delete mode 100644 spec/hash_methods_spec.rb diff --git a/lib/mock_redis/hash_methods.rb b/lib/mock_redis/hash_methods.rb index 4c207611..0f757c25 100644 --- a/lib/mock_redis/hash_methods.rb +++ b/lib/mock_redis/hash_methods.rb @@ -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)| 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 diff --git a/spec/hash_methods_spec.rb b/spec/hash_methods_spec.rb deleted file mode 100644 index 629b966b..00000000 --- a/spec/hash_methods_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -require 'spec_helper' - -describe MockRedis::HashMethods do - subject { MockRedis.new } - - context 'all keys treated as strings' do - before do - subject.hmset(:foo, [:bar, :bas]) - subject.hmset(1, [:free, :bird]) - end - - it { expect(subject.hgetall('foo')).to eq 'bar' => 'bas' } - it { expect(subject.hgetall(:foo)).to eq 'bar' => 'bas' } - it { expect(subject.hgetall(1)).to eq 'free' => 'bird' } - it { expect(subject.hgetall('1')).to eq 'free' => 'bird' } - - context 'keys are deletable' do - it { expect(subject.del(1)).to eq 1 } - it { expect(subject.del('1')).to eq 1 } - it { expect(subject.del(:foo)).to eq 1 } - it { expect(subject.del('foo')).to eq 1 } - end - end -end From c4f8a8e2c3b671115301efa7341ea3f27e9d1d57 Mon Sep 17 00:00:00 2001 From: Jeff Luckett Date: Wed, 9 Oct 2019 12:33:01 -0400 Subject: [PATCH 3/3] Redis error message is slightly different for the same error using different argument patterns. --- lib/mock_redis/hash_methods.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/mock_redis/hash_methods.rb b/lib/mock_redis/hash_methods.rb index 0f757c25..4eefe757 100644 --- a/lib/mock_redis/hash_methods.rb +++ b/lib/mock_redis/hash_methods.rb @@ -85,6 +85,7 @@ def mapped_hmget(key, *fields) 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 @@ -93,7 +94,7 @@ def hmset(key, *kvpairs) assert_has_args(kvpairs, 'hmset') if kvpairs.length.odd? - raise Redis::CommandError, 'ERR wrong number of arguments for \'hmset\' command' + raise Redis::CommandError, err_msg || 'ERR wrong number of arguments for HMSET' end kvpairs.each_slice(2) do |(k, v)|