Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addresses issues with #hmset not string-ifying keys, and not accepting Array as only arg. #173

Merged
merged 4 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/mock_redis/hash_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)|
Expand Down Expand Up @@ -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)
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