Sha256: aecac947c5c5994e57447fa3f1e0233bb640c70b0a40b594f24f132a32f4a845

Contents?: true

Size: 1.98 KB

Versions: 10

Compression:

Stored size: 1.98 KB

Contents

require 'test_helper'

class MockRedis
  def initialize
    @sets = []
    @setexes = []
    @setnxes = []
    @expires = []
  end

  def set(*a)
    @sets << a
  end

  def has_set?(*a)
    @sets.include?(a)
  end

  def setex(*a)
    @setexes << a
  end

  def has_setex?(*a)
    @setexes.include?(a)
  end

  def setnx(*a)
    @setnxes << a
  end

  def has_setnx?(*a)
    @setnxes.include?(a)
  end

  def multi(&block)
    instance_eval do
      def setnx(*a)
        @setnxes << a
      end

      block.call
    end
  end

  def expire(*a)
    @expires << a
  end

  def has_expire?(*a)
    @expires.include?(a)
  end

end

class MockTtlStore < MockRedis
  include Redis::Store::Ttl
end

describe MockTtlStore do
  let(:key) { 'hello' }
  let(:value) { 'value' }
  let(:options) { { :expire_after => 3600 } }
  let(:redis) { MockTtlStore.new }

  describe '#set' do
    describe 'without options' do
      it 'must call super with key and value' do
        redis.set(key, value)
        redis.has_set?(key, value).must_equal true
      end
    end

    describe 'with options' do
      it 'must call setex with proper expiry and set raw to true' do
        redis.set(key, value, options)
        redis.has_setex?(key, options[:expire_after], value, :raw => true).must_equal true
      end
    end
  end

  describe '#setnx' do
    describe 'without expiry' do
      it 'must call super with key and value' do
        redis.setnx(key, value)
        redis.has_setnx?(key, value)
      end

      it 'must not call expire' do
        MockTtlStore.any_instance.expects(:expire).never

        redis.setnx(key, value)
      end
    end

    describe 'with expiry' do
      it 'must call setnx with key and value and set raw to true' do
        redis.setnx(key, value, options)
        redis.has_setnx?(key, value, :raw => true).must_equal true
      end

      it 'must call expire' do
        redis.setnx(key, value, options)
        redis.has_expire?(key, options[:expire_after]).must_equal true
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 4 rubygems

Version Path
redis-store-1.1.5 test/redis/store/ttl_test.rb
redis-store-1.1.4 test/redis/store/ttl_test.rb
redis-store-json-3.0.0 redis-store-json/test/redis/store/ttl_test.rb
redis-rack-json-1.5.2 redis-store-json/test/redis/store/ttl_test.rb
redis-actionpack-json-4.0.0 redis-store-json/test/redis/store/ttl_test.rb
redis-store-1.1.3 test/redis/store/ttl_test.rb
redis-store-1.1.2 test/redis/store/ttl_test.rb
redis-store-1.1.1 test/redis/store/ttl_test.rb
redis-store-1.1.0 test/redis/store/ttl_test.rb
redis-store-1.1.0.rc2 test/redis/store/ttl_test.rb