mock_redis 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/lib/mock_redis.rb +11 -2
- data/lib/mock_redis/database.rb +8 -7
- data/lib/mock_redis/version.rb +1 -1
- data/spec/mock_redis_spec.rb +44 -0
- metadata +2 -2
data/CHANGELOG.md
CHANGED
data/lib/mock_redis.rb
CHANGED
@@ -21,6 +21,7 @@ class MockRedis
|
|
21
21
|
:timeout => 5.0,
|
22
22
|
:password => nil,
|
23
23
|
:db => 0,
|
24
|
+
:time_class => Time,
|
24
25
|
}
|
25
26
|
|
26
27
|
def self.connect(*args)
|
@@ -34,7 +35,7 @@ class MockRedis
|
|
34
35
|
TransactionWrapper.new(
|
35
36
|
ExpireWrapper.new(
|
36
37
|
MultiDbWrapper.new(
|
37
|
-
Database.new(*args)))))
|
38
|
+
Database.new(self, *args)))))
|
38
39
|
end
|
39
40
|
|
40
41
|
def id
|
@@ -58,6 +59,14 @@ class MockRedis
|
|
58
59
|
self.options[:db]
|
59
60
|
end
|
60
61
|
|
62
|
+
def now
|
63
|
+
self.options[:time_class].now
|
64
|
+
end
|
65
|
+
|
66
|
+
def time_at(timestamp)
|
67
|
+
self.options[:time_class].at(timestamp)
|
68
|
+
end
|
69
|
+
|
61
70
|
def client
|
62
71
|
self
|
63
72
|
end
|
@@ -79,7 +88,7 @@ class MockRedis
|
|
79
88
|
protected
|
80
89
|
|
81
90
|
def _parse_options(options)
|
82
|
-
return
|
91
|
+
return DEFAULTS.dup if options.nil?
|
83
92
|
|
84
93
|
defaults = DEFAULTS.dup
|
85
94
|
|
data/lib/mock_redis/database.rb
CHANGED
@@ -18,7 +18,8 @@ class MockRedis
|
|
18
18
|
|
19
19
|
attr_reader :data, :expire_times
|
20
20
|
|
21
|
-
def initialize(*args)
|
21
|
+
def initialize(base, *args)
|
22
|
+
@base = base
|
22
23
|
@data = {}
|
23
24
|
@expire_times = []
|
24
25
|
end
|
@@ -54,7 +55,7 @@ class MockRedis
|
|
54
55
|
end
|
55
56
|
|
56
57
|
def expire(key, seconds)
|
57
|
-
expireat(key,
|
58
|
+
expireat(key, @base.now.to_i + seconds.to_i)
|
58
59
|
end
|
59
60
|
|
60
61
|
def expireat(key, timestamp)
|
@@ -63,7 +64,7 @@ class MockRedis
|
|
63
64
|
end
|
64
65
|
|
65
66
|
if exists(key)
|
66
|
-
set_expiration(key,
|
67
|
+
set_expiration(key, @base.time_at(timestamp.to_i))
|
67
68
|
true
|
68
69
|
else
|
69
70
|
false
|
@@ -276,12 +277,12 @@ class MockRedis
|
|
276
277
|
}
|
277
278
|
end
|
278
279
|
|
279
|
-
def keys(format)
|
280
|
+
def keys(format = '*')
|
280
281
|
data.keys.grep(redis_pattern_to_ruby_regex(format))
|
281
282
|
end
|
282
283
|
|
283
284
|
def lastsave
|
284
|
-
|
285
|
+
@base.now.to_i
|
285
286
|
end
|
286
287
|
|
287
288
|
def persist(key)
|
@@ -335,7 +336,7 @@ class MockRedis
|
|
335
336
|
|
336
337
|
def ttl(key)
|
337
338
|
if has_expiration?(key)
|
338
|
-
expiration(key).to_i -
|
339
|
+
expiration(key).to_i - @base.now.to_i
|
339
340
|
else
|
340
341
|
-1
|
341
342
|
end
|
@@ -430,7 +431,7 @@ class MockRedis
|
|
430
431
|
# This method isn't private, but it also isn't a Redis command, so
|
431
432
|
# it doesn't belong up above with all the Redis commands.
|
432
433
|
def expire_keys
|
433
|
-
now =
|
434
|
+
now = @base.now
|
434
435
|
|
435
436
|
to_delete = expire_times.take_while do |(time, key)|
|
436
437
|
time <= now
|
data/lib/mock_redis/version.rb
CHANGED
data/spec/mock_redis_spec.rb
CHANGED
@@ -29,4 +29,48 @@ describe MockRedis do
|
|
29
29
|
|
30
30
|
its(:id) { should == url }
|
31
31
|
end
|
32
|
+
|
33
|
+
describe 'Injecting a time class' do
|
34
|
+
describe '.options' do
|
35
|
+
let(:time_stub) { double 'Time' }
|
36
|
+
let(:options) { { :time_class => time_stub } }
|
37
|
+
|
38
|
+
it 'defaults to Time' do
|
39
|
+
mock_redis = MockRedis.new
|
40
|
+
|
41
|
+
mock_redis.options[:time_class].should == Time
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'has a configurable Time class' do
|
45
|
+
mock_redis = MockRedis.new(options)
|
46
|
+
|
47
|
+
mock_redis.options[:time_class].should == time_stub
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '.now' do
|
52
|
+
let(:now) { 'Now' }
|
53
|
+
let(:time_stub) { double 'Time', :now => now }
|
54
|
+
let(:options) { { :time_class => time_stub } }
|
55
|
+
|
56
|
+
subject { MockRedis.new(options) }
|
57
|
+
|
58
|
+
its(:now) { should == now }
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '.expireat' do
|
62
|
+
let(:time_at) { 'expireat' }
|
63
|
+
let(:time_stub) { double 'Time' }
|
64
|
+
let(:options) { { :time_class => time_stub } }
|
65
|
+
let(:timestamp) { 123456 }
|
66
|
+
|
67
|
+
subject { MockRedis.new(options) }
|
68
|
+
|
69
|
+
it 'Forwards time_at to the time_class' do
|
70
|
+
time_stub.should_receive(:at).with(timestamp).and_return(time_at)
|
71
|
+
|
72
|
+
subject.time_at(timestamp).should == time_at
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
32
76
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mock_redis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-11-14 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|