class Hoodoo::TransientStore::Mocks::Redis
Mock known uses of Redis
with test implementations. Use explicitly, or as an RSpec implicit mock via something like this:
allow( Redis ).to( receive( :new ). and_return( Hoodoo::TransientStore::Mocks::Redis.new ) )
…whenever you need to stub out real Redis
. You will# probably want to add:
before :all do # (or ":each") Hoodoo::TransientStore::Mocks::Redis.reset() end
…to “clean out Redis” before or between tests. You can check the contents of mock Redis
by examining ::store
‘s hash of data.
The test coverage for Hoodoo::TransientStore
selects this backend in passing. Generally speaking you should favour Hoodoo::TransientStore
over hard-coding to a storage engine available by the Hoodoo
abstraction and, as a result, may never need this mock class at all.
Public Class Methods
Wipe out all saved data.
# File lib/hoodoo/transient_store/mocks/redis.rb, line 53 def self.reset @@store = {} end
For test analysis, return the hash of ‘Redis’ mock data.
Entries are referenced by the key you used to originally store them; values are hashes with “:expires_at” giving an expiry time or “nil” and “:value” giving your stored value.
# File lib/hoodoo/transient_store/mocks/redis.rb, line 47 def self.store @@store end
Public Instance Methods
Alias for get
.
# File lib/hoodoo/transient_store/mocks/redis.rb, line 74 def []( key ) get( key ) end
Alias for set
.
# File lib/hoodoo/transient_store/mocks/redis.rb, line 95 def []=( key, value ) set( key, value ) end
Remove data for the given key.
# File lib/hoodoo/transient_store/mocks/redis.rb, line 116 def del( key ) if @@store.has_key?( key ) @@store.delete( key ) 1 else 0 end end
Set expiry time for a given key, which must exist.
ttl
-
time-to-live (‘live’ as in living, not as in ‘live TV’). A value in seconds, after which the data is considered expired.
# File lib/hoodoo/transient_store/mocks/redis.rb, line 105 def expire( key, ttl ) unless @@store.has_key?( key ) raise "Hoodoo::TransientStore::Mocks::Redis\#expire: Cannot find key '#{ key }'" end @@store[ key ][ :expires_at ] = Time.now.utc + ttl true end
Get the data stored under the given key. Returns nil
if not found / expired.
key
-
Key to look up (see
set
).
# File lib/hoodoo/transient_store/mocks/redis.rb, line 62 def get( key ) data = @@store[ key ] return nil if data.nil? expires_at = data[ :expires_at ] return nil unless expires_at.nil? || Time.now < expires_at return data[ :value ] end
Mock ‘info’ health check.
# File lib/hoodoo/transient_store/mocks/redis.rb, line 131 def info( command ) { :alive => command } end
Stub for ‘closing’ a connection.
# File lib/hoodoo/transient_store/mocks/redis.rb, line 127 def quit; end
Set data for a given key.
key
-
Key under which to store data.
value
-
Data
to store. ttl
-
(Optional) time-to-live (‘live’ as in living, not as in ‘live TV’) - a value in seconds, after which the data is considered expired. If omitted, the data does not expire.
# File lib/hoodoo/transient_store/mocks/redis.rb, line 88 def set( key, value, ttl = nil ) @@store[ key ] = { :value => value } 'OK' end