Class: Hoodoo::TransientStore::Mocks::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/hoodoo/transient_store/mocks/redis.rb

Overview

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.

Constant Summary collapse

@@store =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resetObject

Wipe out all saved data.



53
54
55
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 53

def self.reset
  @@store = {}
end

.storeObject

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.



47
48
49
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 47

def self.store
  @@store
end

Instance Method Details

#[](key) ⇒ Object

Alias for #get.



74
75
76
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 74

def []( key )
  get( key )
end

#[]=(key, value) ⇒ Object

Alias for #set.



95
96
97
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 95

def []=( key, value )
  set( key, value )
end

#del(key) ⇒ Object

Remove data for the given key.



116
117
118
119
120
121
122
123
# 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

#expire(key, ttl) ⇒ Object

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.



105
106
107
108
109
110
111
112
# 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(key) ⇒ Object

Get the data stored under the given key. Returns nil if not found / expired.

key

Key to look up (see #set).



62
63
64
65
66
67
68
69
70
# 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

#info(command) ⇒ Object

Mock ‘info’ health check.



131
132
133
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 131

def info( command )
  { :alive => command }
end

#quitObject

Stub for ‘closing’ a connection.



127
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 127

def quit; end

#set(key, value, ttl = nil) ⇒ Object

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.



88
89
90
91
# File 'lib/hoodoo/transient_store/mocks/redis.rb', line 88

def set( key, value, ttl = nil )
  @@store[ key ] = { :value => value }
  'OK'
end