Sha256: 5585bea89ff24dbfafb87b9d0b959b1e049528c4e0be576abafecf5f92e12fff

Contents?: true

Size: 1.52 KB

Versions: 16

Compression:

Stored size: 1.52 KB

Contents

require 'spec_helper'

describe '#srandmember(key)' do
  before do
    @key = 'mock-redis-test:srandmember'

    @redises.sadd(@key, 'value')
  end

  it 'returns a member of the set' do
    @redises.srandmember(@key).should == 'value'
  end

  it 'does not modify the set' do
    @redises.srandmember(@key)
    @redises.smembers(@key).should == ['value']
  end

  it 'returns nil if the set is empty' do
    @redises.spop(@key)
    @redises.srandmember(@key).should be_nil
  end

  context 'when count argument is specified' do
    before do
      @redises.sadd(@key, 'value2')
      @redises.sadd(@key, 'value3')
    end

    # NOTE: We disable result checking since MockRedis and Redis will likely
    # return a different random set (since the selection is, well, random)
    it 'returns the whole set if count is greater than the set size' do
      @redises.send_without_checking(:srandmember, @key, 5).should =~ %w[value value2 value3]
    end

    it 'returns random members up to count from the set when count is smaller than the set size' do
      @redises.send_without_checking(:srandmember, @key, 2).size.should == 2
    end

    it 'returns random members up to count from the set when count is negative even if count.abs is greater than the set size' do # rubocop:disable Layout/LineLength
      @redises.send_without_checking(:srandmember, @key, -5).size.should == 5
    end

    it 'returns nil if the set is empty' do
      @redises.srem(@key, %w[value value2 value3])
      @redises.srandmember(@key, 2).should be_empty
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
mock_redis-0.36.0 spec/commands/srandmember_spec.rb
mock_redis-0.35.0 spec/commands/srandmember_spec.rb
mock_redis-0.34.0 spec/commands/srandmember_spec.rb
mock_redis-0.33.0 spec/commands/srandmember_spec.rb
mock_redis-0.32.0 spec/commands/srandmember_spec.rb
mock_redis-0.31.0 spec/commands/srandmember_spec.rb
mock_redis-0.30.0 spec/commands/srandmember_spec.rb
mock_redis-0.29.0 spec/commands/srandmember_spec.rb
mock_redis-0.28.0 spec/commands/srandmember_spec.rb
mock_redis-0.27.3 spec/commands/srandmember_spec.rb
mock_redis-0.27.2 spec/commands/srandmember_spec.rb
mock_redis-0.27.1 spec/commands/srandmember_spec.rb
mock_redis-0.27.0 spec/commands/srandmember_spec.rb
mock_redis-0.26.0 spec/commands/srandmember_spec.rb
mock_redis-0.25.0 spec/commands/srandmember_spec.rb
mock_redis-0.24.0 spec/commands/srandmember_spec.rb