From 28f6dc3f5cd7c46522c4374e72e6f4c1fb4461d5 Mon Sep 17 00:00:00 2001 From: Joshua Van Deren Date: Thu, 14 Nov 2024 15:48:52 -0700 Subject: [PATCH] Fix hset for array of key + value --- lib/mock_redis/hash_methods.rb | 1 + spec/commands/hset_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/mock_redis/hash_methods.rb b/lib/mock_redis/hash_methods.rb index b400510..0d25267 100644 --- a/lib/mock_redis/hash_methods.rb +++ b/lib/mock_redis/hash_methods.rb @@ -137,6 +137,7 @@ def hscan_each(key, opts = {}, &block) def hset(key, *args) added = 0 + args = args.flatten with_hash_at(key) do |hash| if args.length == 1 && args[0].is_a?(Hash) args = args[0].to_a.flatten diff --git a/spec/commands/hset_spec.rb b/spec/commands/hset_spec.rb index bc6506c..3adbf79 100644 --- a/spec/commands/hset_spec.rb +++ b/spec/commands/hset_spec.rb @@ -38,5 +38,17 @@ expect(@redises.hset(@key, { 'k1' => 'v1', 'k2' => 'v2' })).to eq(2) end + it 'stores array values correctly' do + @redises.hset(@key, ['k1', 'v1', 'k2', 'v2']) + expect(@redises.hget(@key, 'k1')).to eq('v1') + expect(@redises.hget(@key, 'k2')).to eq('v2') + end + + it 'stores multiple arguments correctly' do + @redises.hset(@key, 'k1', 'v1', 'k2', 'v2') + expect(@redises.hget(@key, 'k1')).to eq('v1') + expect(@redises.hget(@key, 'k2')).to eq('v2') + end + it_should_behave_like 'a hash-only command' end