diff --git a/lib/mock_redis/zset_methods.rb b/lib/mock_redis/zset_methods.rb index 1a3c19d..4b8e7af 100644 --- a/lib/mock_redis/zset_methods.rb +++ b/lib/mock_redis/zset_methods.rb @@ -233,6 +233,15 @@ def zscore(key, member) end end + def zmscore(key, *members) + with_zset_at(key) do |z| + members.map do |member| + score = z.score(member.to_s) + score&.to_f + end + end + end + def zunionstore(destination, keys, options = {}) assert_has_args(keys, 'zunionstore') diff --git a/spec/commands/zmscore_spec.rb b/spec/commands/zmscore_spec.rb new file mode 100644 index 0000000..ac9bc2d --- /dev/null +++ b/spec/commands/zmscore_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +RSpec.describe '#zmscore(key, member)' do + before { @key = 'mock-redis-test:zmscore' } + + it 'returns the score as a string' do + expect(@redises.zadd(@key, 0.25, 'foo')).to eq(true) + expect(@redises.zmscore(@key, 'foo')).to eq([0.25]) + end + + it 'handles integer members correctly' do + member = 11 + expect(@redises.zadd(@key, 0.25, member)).to eq(true) + expect(@redises.zmscore(@key, member)).to eq([0.25]) + end + + it 'returns nil if member is not present in the set' do + expect(@redises.zmscore(@key, 'foo')).to eq([nil]) + end + + it 'supports a variable number of arguments' do + @redises.zadd(@key, [[1, 'one'], [2, 'two']]) + expect(@redises.zmscore(@key, 'one', 'three', 'two')).to eq([1, nil, 2]) + end + + it_should_behave_like 'a zset-only command' +end