From 04ab75d4b47e6d19fce0b719e55296e99c3e0d29 Mon Sep 17 00:00:00 2001 From: Matt Secoske Date: Sat, 1 May 2021 15:41:57 -0500 Subject: [PATCH] hset was not accepting hashes as a parameter (#208) --- lib/mock_redis/hash_methods.rb | 4 ++++ spec/commands/hset_spec.rb | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/lib/mock_redis/hash_methods.rb b/lib/mock_redis/hash_methods.rb index 688976d2..92398306 100644 --- a/lib/mock_redis/hash_methods.rb +++ b/lib/mock_redis/hash_methods.rb @@ -131,6 +131,10 @@ def hscan_each(key, opts = {}, &block) def hset(key, *args) added = 0 with_hash_at(key) do |hash| + if args.length == 1 && args[0].is_a?(Hash) + args = args[0].to_a.flatten + end + args.each_slice(2) do |field, value| added += 1 unless hash.key?(field.to_s) hash[field.to_s] = value.to_s diff --git a/spec/commands/hset_spec.rb b/spec/commands/hset_spec.rb index 7266ba9e..18eab77e 100644 --- a/spec/commands/hset_spec.rb +++ b/spec/commands/hset_spec.rb @@ -34,5 +34,9 @@ @redises.hget(@key, '1').should == 'one' end + it 'stores fields sent in a hash' do + @redises.hset(@key, {'k1' => 'v1', 'k2' => 'v2'}).should == 2 + end + it_should_behave_like 'a hash-only command' end