Skip to content

Commit

Permalink
Add zmscore command
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-groble committed Nov 12, 2023
1 parent ff02359 commit a02b4a5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/mock_redis/zset_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
27 changes: 27 additions & 0 deletions spec/commands/zmscore_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a02b4a5

Please sign in to comment.