mock_redis 0.10.0 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +2 -0
- data/CHANGELOG.md +14 -0
- data/README.md +12 -7
- data/lib/mock_redis/database.rb +14 -1
- data/lib/mock_redis/hash_methods.rb +19 -2
- data/lib/mock_redis/list_methods.rb +13 -3
- data/lib/mock_redis/set_methods.rb +11 -2
- data/lib/mock_redis/string_methods.rb +1 -1
- data/lib/mock_redis/version.rb +1 -1
- data/lib/mock_redis/zset_methods.rb +3 -1
- data/spec/commands/del_spec.rb +10 -0
- data/spec/commands/hincrbyfloat_spec.rb +58 -0
- data/spec/commands/lpush_spec.rb +6 -0
- data/spec/commands/lpushx_spec.rb +12 -0
- data/spec/commands/lrange_spec.rb +9 -1
- data/spec/commands/ltrim_spec.rb +5 -0
- data/spec/commands/pexpire_spec.rb +3 -3
- data/spec/commands/pttl_spec.rb +41 -0
- data/spec/commands/rpush_spec.rb +6 -0
- data/spec/commands/rpushx_spec.rb +12 -0
- data/spec/commands/sadd_spec.rb +5 -0
- data/spec/commands/select_spec.rb +8 -0
- data/spec/commands/srandmember_spec.rb +26 -0
- data/spec/commands/ttl_spec.rb +2 -2
- data/spec/commands/zadd_spec.rb +6 -0
- data/spec/commands/zrange_spec.rb +11 -0
- data/spec/commands/zrangebyscore_spec.rb +11 -0
- data/spec/commands/zrevrange_spec.rb +11 -0
- data/spec/commands/zrevrangebyscore_spec.rb +11 -0
- metadata +12 -15
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fb3767ba43e249e21a60d9a6d98c9774252a9358
|
4
|
+
data.tar.gz: b6e646227a8ee1a21397a91d66855b5a35fd7c04
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0809de69122227d0ab9935b12b3f4fa65a4af1855ad33042a3cc8df59de5cb4ca4545cea3e6a693622661f5d20a2c9ab395dc3a87075ab5df318e5f2c819e38e
|
7
|
+
data.tar.gz: 7f245ec950344101749bd080025d3b164a935f3ba8862cd54d9aeb84a0be29a75de7e1b0240294eb1efe1facc94b34e6587ad4143ed707e28e1ed2284d524cb0
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
### 0.11.0
|
2
|
+
|
3
|
+
* Raise errors for empty arrays as arguments
|
4
|
+
* Update error messages to conform to Redis 2.8. This officially means
|
5
|
+
`mock_redis` no *longer supports Redis 2.6 or lower*. All testing in
|
6
|
+
TravisCI is now done against 2.8
|
7
|
+
* Update return value of TTL to return -2 if key doesn't exist
|
8
|
+
* Add support for the HINCRBYFLOAT command
|
9
|
+
* Add support for `count` parameter on SRANDMEMBER
|
10
|
+
* Add support for the PTTL command
|
11
|
+
* Improve support for negative start indices in LRANGE
|
12
|
+
* Improve support for negative start indices in LTRIM
|
13
|
+
* Allow `del` to accept arrays of keys
|
14
|
+
|
1
15
|
### 0.10.0
|
2
16
|
* Add support for :nx, :xx, :ex, :px options for SET command
|
3
17
|
|
data/README.md
CHANGED
@@ -1,13 +1,17 @@
|
|
1
1
|
# MockRedis
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/mock_redis.png)](http://badge.fury.io/rb/mock_redis)
|
3
4
|
[![Build Status](https://travis-ci.org/causes/mock_redis.png)](https://travis-ci.org/causes/mock_redis)
|
4
5
|
|
5
6
|
A mock redis gem.
|
6
7
|
|
7
|
-
MockRedis provides the same interface as redis-rb
|
8
|
+
MockRedis provides the same interface as `redis-rb`, but it stores its
|
8
9
|
data in memory instead of talking to a Redis server. It is intended
|
9
10
|
for use in tests.
|
10
11
|
|
12
|
+
The current implementation is tested against *Redis 2.8.4*. Older versions
|
13
|
+
of Redis may return different results or not support some commands.
|
14
|
+
|
11
15
|
## Getting Started
|
12
16
|
|
13
17
|
It's as easy as `require 'mock_redis'; mr = MockRedis.new`. Then you can
|
@@ -29,10 +33,10 @@ of supported methods:
|
|
29
33
|
|
30
34
|
* String methods: `get`, `set`, `append`, `incr`, etc.
|
31
35
|
* List methods: `lpush`, `lpop`, `lrange`, `rpoplpush`, etc.
|
32
|
-
* Set methods: `sadd`, `sinter`, `sismember`, etc.
|
33
|
-
* Hash methods: `hset`, `hget`, `hgetall`, `hmget`, etc.
|
36
|
+
* Set methods: `sadd`, `sinter`, `sismember`, `srandmember`, etc.
|
37
|
+
* Hash methods: `hset`, `hget`, `hgetall`, `hmget`, `hincrby`, `hincrbyfloat` etc.
|
34
38
|
* Sorted set methods: `zadd`, `zrank`, `zunionstore`, etc.
|
35
|
-
* Expirations: `expire`, `ttl`, etc.
|
39
|
+
* Expirations: `expire`, `pexpire`, `ttl`, `pttl`, etc.
|
36
40
|
* Transactions: `multi`, `exec`, `discard`
|
37
41
|
* Futures
|
38
42
|
|
@@ -87,14 +91,15 @@ please submit a pull request with your (tested!) implementation.
|
|
87
91
|
|
88
92
|
## Compatibility
|
89
93
|
|
90
|
-
As of version 0.8.2
|
91
|
-
older versions of Ruby, use 0.8.1 or older.
|
94
|
+
As of version `0.8.2`, Ruby 1.9.3 and above are supported. For
|
95
|
+
older versions of Ruby, use `0.8.1` or older. JRuby 1.7.9 is also
|
96
|
+
supported.
|
92
97
|
|
93
98
|
## Running the Tests
|
94
99
|
|
95
100
|
If you want to work on this, you'll probably want to run the
|
96
101
|
tests. (Just kidding! There's no probably about it.) These tests were
|
97
|
-
written with Redis 2.
|
102
|
+
written with Redis `2.8.4` running on localhost without any passwords
|
98
103
|
required. If you're using a different version of Redis, you may see
|
99
104
|
failures due to error message text being different. If you're running
|
100
105
|
a really old version of Redis, you'll definitely see failures due to
|
data/lib/mock_redis/database.rb
CHANGED
@@ -55,6 +55,7 @@ class MockRedis
|
|
55
55
|
|
56
56
|
def del(*keys)
|
57
57
|
keys.
|
58
|
+
flatten.
|
58
59
|
find_all{|key| data[key]}.
|
59
60
|
each {|k| persist(k)}.
|
60
61
|
each {|k| data.delete(k)}.
|
@@ -163,13 +164,25 @@ class MockRedis
|
|
163
164
|
end
|
164
165
|
|
165
166
|
def ttl(key)
|
166
|
-
if
|
167
|
+
if !exists(key)
|
168
|
+
-2
|
169
|
+
elsif has_expiration?(key)
|
167
170
|
expiration(key).to_i - @base.now.to_i
|
168
171
|
else
|
169
172
|
-1
|
170
173
|
end
|
171
174
|
end
|
172
175
|
|
176
|
+
def pttl(key)
|
177
|
+
if !exists(key)
|
178
|
+
-2
|
179
|
+
elsif has_expiration?(key)
|
180
|
+
(expiration(key).to_r * 1000).to_i - (@base.now.to_r * 1000).to_i
|
181
|
+
else
|
182
|
+
-1
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
173
186
|
def type(key)
|
174
187
|
if !exists(key)
|
175
188
|
'none'
|
@@ -46,6 +46,24 @@ class MockRedis
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
+
def hincrbyfloat(key, field, increment)
|
50
|
+
with_hash_at(key) do |hash|
|
51
|
+
field = field.to_s
|
52
|
+
unless can_incr_float?(data[key][field])
|
53
|
+
raise Redis::CommandError, "ERR hash value is not a valid float"
|
54
|
+
end
|
55
|
+
|
56
|
+
unless looks_like_float?(increment.to_s)
|
57
|
+
raise Redis::CommandError, "ERR value is not a valid float"
|
58
|
+
end
|
59
|
+
|
60
|
+
new_value = (hash[field] || "0").to_f + increment.to_f
|
61
|
+
new_value = new_value.to_i if new_value % 1 == 0
|
62
|
+
hash[field] = new_value.to_s
|
63
|
+
new_value
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
49
67
|
def hkeys(key)
|
50
68
|
with_hash_at(key, &:keys)
|
51
69
|
end
|
@@ -116,9 +134,8 @@ class MockRedis
|
|
116
134
|
|
117
135
|
def assert_hashy(key)
|
118
136
|
unless hashy?(key)
|
119
|
-
raise Redis::CommandError, "
|
137
|
+
raise Redis::CommandError, "WRONGTYPE Operation against a key holding the wrong kind of value"
|
120
138
|
end
|
121
139
|
end
|
122
|
-
|
123
140
|
end
|
124
141
|
end
|
@@ -82,11 +82,16 @@ class MockRedis
|
|
82
82
|
|
83
83
|
def lpush(key, values)
|
84
84
|
values = [values] unless values.is_a?(Array)
|
85
|
+
assert_has_args(values, 'lpush')
|
85
86
|
with_list_at(key) {|l| values.each {|v| l.unshift(v.to_s)}}
|
86
87
|
llen(key)
|
87
88
|
end
|
88
89
|
|
89
90
|
def lpushx(key, value)
|
91
|
+
value = [value] unless value.is_a?(Array)
|
92
|
+
if value.length != 1
|
93
|
+
raise Redis::CommandError, "ERR wrong number of arguments for 'lpushx' command"
|
94
|
+
end
|
90
95
|
assert_listy(key)
|
91
96
|
return 0 unless list_at?(key)
|
92
97
|
lpush(key, value)
|
@@ -94,7 +99,7 @@ class MockRedis
|
|
94
99
|
|
95
100
|
def lrange(key, start, stop)
|
96
101
|
start = start.to_i
|
97
|
-
with_list_at(key) {|l| start < l.size ? l[start..stop.to_i] : []}
|
102
|
+
with_list_at(key) {|l| start < l.size ? l[[start, -l.length].max..stop.to_i] : []}
|
98
103
|
end
|
99
104
|
|
100
105
|
def lrem(key, count, value)
|
@@ -139,7 +144,7 @@ class MockRedis
|
|
139
144
|
|
140
145
|
def ltrim(key, start, stop)
|
141
146
|
with_list_at(key) do |list|
|
142
|
-
list.replace(list[start.to_i..stop.to_i] || []) if list
|
147
|
+
list.replace(list[[start.to_i, -list.length].max..stop.to_i] || []) if list
|
143
148
|
'OK'
|
144
149
|
end
|
145
150
|
end
|
@@ -156,11 +161,16 @@ class MockRedis
|
|
156
161
|
|
157
162
|
def rpush(key, values)
|
158
163
|
values = [values] unless values.is_a?(Array)
|
164
|
+
assert_has_args(values, 'rpush')
|
159
165
|
with_list_at(key) {|l| values.each {|v| l.push(v.to_s)}}
|
160
166
|
llen(key)
|
161
167
|
end
|
162
168
|
|
163
169
|
def rpushx(key, value)
|
170
|
+
value = [value] unless value.is_a?(Array)
|
171
|
+
if value.length != 1
|
172
|
+
raise Redis::CommandError, "ERR wrong number of arguments for 'rpushx' command"
|
173
|
+
end
|
164
174
|
assert_listy(key)
|
165
175
|
return 0 unless list_at?(key)
|
166
176
|
rpush(key, value)
|
@@ -182,7 +192,7 @@ class MockRedis
|
|
182
192
|
def assert_listy(key)
|
183
193
|
unless listy?(key)
|
184
194
|
# Not the most helpful error, but it's what redis-rb barfs up
|
185
|
-
raise Redis::CommandError, "
|
195
|
+
raise Redis::CommandError, "WRONGTYPE Operation against a key holding the wrong kind of value"
|
186
196
|
end
|
187
197
|
end
|
188
198
|
|
@@ -8,6 +8,7 @@ class MockRedis
|
|
8
8
|
|
9
9
|
def sadd(key, members)
|
10
10
|
members = [members].flatten.map(&:to_s)
|
11
|
+
assert_has_args(members, 'sadd')
|
11
12
|
|
12
13
|
with_set_at(key) do |s|
|
13
14
|
if members.size > 1
|
@@ -84,7 +85,15 @@ class MockRedis
|
|
84
85
|
|
85
86
|
def srandmember(key, count = nil)
|
86
87
|
members = with_set_at(key, &:to_a)
|
87
|
-
|
88
|
+
if count
|
89
|
+
if count > 0
|
90
|
+
members.sample(count)
|
91
|
+
else
|
92
|
+
count.abs.times.map { members[rand(members.length)] }
|
93
|
+
end
|
94
|
+
else
|
95
|
+
members[rand(members.length)]
|
96
|
+
end
|
88
97
|
end
|
89
98
|
|
90
99
|
def srem(key, members)
|
@@ -136,7 +145,7 @@ class MockRedis
|
|
136
145
|
def assert_sety(key)
|
137
146
|
unless sety?(key)
|
138
147
|
# Not the most helpful error, but it's what redis-rb barfs up
|
139
|
-
raise Redis::CommandError, "
|
148
|
+
raise Redis::CommandError, "WRONGTYPE Operation against a key holding the wrong kind of value"
|
140
149
|
end
|
141
150
|
end
|
142
151
|
|
@@ -247,7 +247,7 @@ class MockRedis
|
|
247
247
|
end
|
248
248
|
|
249
249
|
def assert_stringy(key,
|
250
|
-
message="
|
250
|
+
message="WRONGTYPE Operation against a key holding the wrong kind of value")
|
251
251
|
unless stringy?(key)
|
252
252
|
raise Redis::CommandError, message
|
253
253
|
end
|
data/lib/mock_redis/version.rb
CHANGED
@@ -10,6 +10,8 @@ class MockRedis
|
|
10
10
|
def zadd(key, *args)
|
11
11
|
if args.size == 1 && args[0].is_a?(Array)
|
12
12
|
args = args.first
|
13
|
+
assert_has_args(args, 'zadd')
|
14
|
+
|
13
15
|
args = args.each_slice(2).to_a unless args.first.is_a?(Array)
|
14
16
|
retval = args.map(&:last).map { |member| !!zscore(key, member.to_s) }.count(false)
|
15
17
|
with_zset_at(key) do |z|
|
@@ -211,7 +213,7 @@ class MockRedis
|
|
211
213
|
def assert_zsety(key)
|
212
214
|
unless zsety?(key)
|
213
215
|
raise Redis::CommandError,
|
214
|
-
"
|
216
|
+
"WRONGTYPE Operation against a key holding the wrong kind of value"
|
215
217
|
end
|
216
218
|
end
|
217
219
|
|
data/spec/commands/del_spec.rb
CHANGED
@@ -17,4 +17,14 @@ describe '#del(key [, key, ...])' do
|
|
17
17
|
|
18
18
|
@redises.get('mock-redis-test:1').should be_nil
|
19
19
|
end
|
20
|
+
|
21
|
+
it "accepts an array of keys" do
|
22
|
+
@redises.set('mock-redis-test:1', 1)
|
23
|
+
@redises.set('mock-redis-test:2', 2)
|
24
|
+
|
25
|
+
@redises.del(%w[mock-redis-test:1 mock-redis-test:2])
|
26
|
+
|
27
|
+
@redises.get('mock-redis-test:1').should be_nil
|
28
|
+
@redises.get('mock-redis-test:2').should be_nil
|
29
|
+
end
|
20
30
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe '#hincrbyfloat(key, field, increment)' do
|
4
|
+
before do
|
5
|
+
@key = 'mock-redis-test:hincrbyfloat'
|
6
|
+
@field = 'count'
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns the value after the increment" do
|
10
|
+
@redises.hset(@key, @field, 2.0)
|
11
|
+
@redises.hincrbyfloat(@key, @field, 2.1).should be_within(0.0001).of(4.1)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "treats a missing key like 0" do
|
15
|
+
@redises.hincrbyfloat(@key, @field, 1.2).should be_within(0.0001).of(1.2)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "creates a hash if nothing is present" do
|
19
|
+
@redises.hincrbyfloat(@key, @field, 1.0)
|
20
|
+
@redises.hget(@key, @field).should == "1"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "increments negative numbers" do
|
24
|
+
@redises.hset(@key, @field, -10.4)
|
25
|
+
@redises.hincrbyfloat(@key, @field, 2.3).should be_within(0.0001).of(-8.1)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "works multiple times" do
|
29
|
+
@redises.hincrbyfloat(@key, @field, 2.1).should be_within(0.0001).of(2.1)
|
30
|
+
@redises.hincrbyfloat(@key, @field, 2.2).should be_within(0.0001).of(4.3)
|
31
|
+
@redises.hincrbyfloat(@key, @field, 2.3).should be_within(0.0001).of(6.6)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "accepts a float-ish string" do
|
35
|
+
@redises.hincrbyfloat(@key, @field, "2.2").should be_within(0.0001).of(2.2)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "treats the field as a string" do
|
39
|
+
field = 11
|
40
|
+
@redises.hset(@key, field, 2)
|
41
|
+
@redises.hincrbyfloat(@key, field, 2).should == 4.0
|
42
|
+
end
|
43
|
+
|
44
|
+
it "raises an error if the value does not look like a float" do
|
45
|
+
@redises.hset(@key, @field, "one.two")
|
46
|
+
lambda do
|
47
|
+
@redises.hincrbyfloat(@key, @field, 1)
|
48
|
+
end.should raise_error(RuntimeError)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "raises an error if the delta does not look like a float" do
|
52
|
+
lambda do
|
53
|
+
@redises.hincrbyfloat(@key, @field, "foobar.baz")
|
54
|
+
end.should raise_error(RuntimeError)
|
55
|
+
end
|
56
|
+
|
57
|
+
it_should_behave_like "a hash-only command"
|
58
|
+
end
|
data/spec/commands/lpush_spec.rb
CHANGED
@@ -33,5 +33,11 @@ describe "#lpush(key, value)" do
|
|
33
33
|
@redises.lindex(@key, 2).should == "1"
|
34
34
|
end
|
35
35
|
|
36
|
+
it "raises an error if an empty array is given" do
|
37
|
+
lambda do
|
38
|
+
@redises.lpush(@key, [])
|
39
|
+
end.should raise_error(Redis::CommandError)
|
40
|
+
end
|
41
|
+
|
36
42
|
it_should_behave_like "a list-only command"
|
37
43
|
end
|
@@ -29,5 +29,17 @@ describe "#lpushx(key, value)" do
|
|
29
29
|
@redises.lindex(@key, 0).should == "2"
|
30
30
|
end
|
31
31
|
|
32
|
+
it "raises an error if an empty array is given" do
|
33
|
+
lambda do
|
34
|
+
@redises.lpushx(@key, [])
|
35
|
+
end.should raise_error(Redis::CommandError)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "raises an error if an array of more than one item is given" do
|
39
|
+
lambda do
|
40
|
+
@redises.lpushx(@key, [1, 2])
|
41
|
+
end.should raise_error(Redis::CommandError)
|
42
|
+
end
|
43
|
+
|
32
44
|
it_should_behave_like "a list-only command"
|
33
45
|
end
|
@@ -23,10 +23,18 @@ describe "#lrange(key, start, stop)" do
|
|
23
23
|
@redises.lrange(@key, 3, 2).should == []
|
24
24
|
end
|
25
25
|
|
26
|
-
it "works with negative
|
26
|
+
it "works with a negative stop index" do
|
27
27
|
@redises.lrange(@key, 2, -1).should == %w[v2 v3 v4]
|
28
28
|
end
|
29
29
|
|
30
|
+
it "works with negative start and stop indices" do
|
31
|
+
@redises.lrange(@key, -2, -1).should == %w[v3 v4]
|
32
|
+
end
|
33
|
+
|
34
|
+
it "works with negative start indices less than list length" do
|
35
|
+
@redises.lrange(@key, -10, -2).should == %w[v0 v1 v2 v3]
|
36
|
+
end
|
37
|
+
|
30
38
|
it "returns [] when run against a nonexistent value" do
|
31
39
|
@redises.lrange("mock-redis-test:bogus-key", 0, 1).should == []
|
32
40
|
end
|
data/spec/commands/ltrim_spec.rb
CHANGED
@@ -26,6 +26,11 @@ describe "#ltrim(key, start, stop)" do
|
|
26
26
|
@redises.lrange(@key, 0, -1).should == %w[v3 v4]
|
27
27
|
end
|
28
28
|
|
29
|
+
it "trims the list to include only the specified elements (out of range negative indices)" do
|
30
|
+
@redises.ltrim(@key, -10, -2)
|
31
|
+
@redises.lrange(@key, 0, -1).should == %w[v0 v1 v2 v3]
|
32
|
+
end
|
33
|
+
|
29
34
|
it "does not crash on overly-large indices" do
|
30
35
|
@redises.ltrim(@key, 100, 200)
|
31
36
|
@redises.lrange(@key, 0, -1).should == %w[]
|
@@ -38,13 +38,13 @@ describe "#pexpire(key, ms)" do
|
|
38
38
|
end
|
39
39
|
|
40
40
|
before do
|
41
|
-
@now = Time.now
|
41
|
+
@now = Time.now.round
|
42
42
|
Time.stub!(:now).and_return(@now)
|
43
43
|
end
|
44
44
|
|
45
45
|
it "removes keys after enough time has passed" do
|
46
46
|
@mock.pexpire(@key, 5)
|
47
|
-
Time.stub!(:now).and_return(@now + Rational(6,1000))
|
47
|
+
Time.stub!(:now).and_return(@now + Rational(6, 1000))
|
48
48
|
@mock.get(@key).should be_nil
|
49
49
|
end
|
50
50
|
|
@@ -52,7 +52,7 @@ describe "#pexpire(key, ms)" do
|
|
52
52
|
@mock.pexpire(@key, 5)
|
53
53
|
@mock.pexpire(@key, 6)
|
54
54
|
|
55
|
-
Time.stub!(:now).and_return(@now +
|
55
|
+
Time.stub!(:now).and_return(@now + Rational(5, 1000))
|
56
56
|
@mock.get(@key).should_not be_nil
|
57
57
|
end
|
58
58
|
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "#pttl(key)" do
|
4
|
+
before do
|
5
|
+
@key = 'mock-redis-test:persist'
|
6
|
+
@redises.set(@key, 'spork')
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns -1 for a key with no expiration" do
|
10
|
+
@redises.pttl(@key).should == -1
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns -2 for a key that does not exist" do
|
14
|
+
@redises.pttl('mock-redis-test:nonesuch').should == -2
|
15
|
+
end
|
16
|
+
|
17
|
+
it "stringifies key" do
|
18
|
+
@redises.expire(@key, 8)
|
19
|
+
# Don't check against Redis since millisecond timing differences are likely
|
20
|
+
@redises.send_without_checking(:pttl, @key.to_sym).should > 0
|
21
|
+
end
|
22
|
+
|
23
|
+
context "[mock only]" do
|
24
|
+
# These are mock-only since we can't actually manipulate time in
|
25
|
+
# the real Redis.
|
26
|
+
|
27
|
+
before(:all) do
|
28
|
+
@mock = @redises.mock
|
29
|
+
end
|
30
|
+
|
31
|
+
before do
|
32
|
+
@now = Time.now.round
|
33
|
+
Time.stub!(:now).and_return(@now)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "gives you the key's remaining lifespan in milliseconds" do
|
37
|
+
@mock.expire(@key, 5)
|
38
|
+
@mock.pttl(@key).should == 5000
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/spec/commands/rpush_spec.rb
CHANGED
@@ -33,5 +33,11 @@ describe "#rpush(key)" do
|
|
33
33
|
@redises.lindex(@key, 2).should == "3"
|
34
34
|
end
|
35
35
|
|
36
|
+
it "raises an error if an empty array is given" do
|
37
|
+
lambda do
|
38
|
+
@redises.rpush(@key, [])
|
39
|
+
end.should raise_error(Redis::CommandError)
|
40
|
+
end
|
41
|
+
|
36
42
|
it_should_behave_like "a list-only command"
|
37
43
|
end
|
@@ -29,5 +29,17 @@ describe "#rpushx(key, value)" do
|
|
29
29
|
@redises.lindex(@key, 1).should == "2"
|
30
30
|
end
|
31
31
|
|
32
|
+
it "raises an error if an empty array is given" do
|
33
|
+
lambda do
|
34
|
+
@redises.rpushx(@key, [])
|
35
|
+
end.should raise_error(Redis::CommandError)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "raises an error if an array of more than one item is given" do
|
39
|
+
lambda do
|
40
|
+
@redises.rpushx(@key, [1, 2])
|
41
|
+
end.should raise_error(Redis::CommandError)
|
42
|
+
end
|
43
|
+
|
32
44
|
it_should_behave_like "a list-only command"
|
33
45
|
end
|
data/spec/commands/sadd_spec.rb
CHANGED
@@ -35,6 +35,11 @@ describe '#sadd(key, member)' do
|
|
35
35
|
@redises.smembers(@key).should == %w[1 2 3]
|
36
36
|
end
|
37
37
|
|
38
|
+
it "raises an error if an empty array is given" do
|
39
|
+
lambda do
|
40
|
+
@redises.sadd(@key, [])
|
41
|
+
end.should raise_error(Redis::CommandError)
|
42
|
+
end
|
38
43
|
end
|
39
44
|
|
40
45
|
it_should_behave_like "a set-only command"
|
@@ -49,5 +49,13 @@ describe "#select(db)" do
|
|
49
49
|
@mock.select(1)
|
50
50
|
@mock.ttl(@key).should == 200
|
51
51
|
end
|
52
|
+
|
53
|
+
it "keeps expire times in miliseconds per-db" do
|
54
|
+
@mock.select(0)
|
55
|
+
(100000 - 1000..100000 + 1000).should cover(@mock.pttl(@key))
|
56
|
+
|
57
|
+
@mock.select(1)
|
58
|
+
(200000 - 1000..200000 + 1000).should cover(@mock.pttl(@key))
|
59
|
+
end
|
52
60
|
end
|
53
61
|
end
|
@@ -20,4 +20,30 @@ describe '#srandmember(key)' do
|
|
20
20
|
@redises.spop(@key)
|
21
21
|
@redises.srandmember(@key).should be_nil
|
22
22
|
end
|
23
|
+
|
24
|
+
context "when count argument is specified" do
|
25
|
+
before do
|
26
|
+
@redises.sadd(@key, 'value2')
|
27
|
+
@redises.sadd(@key, 'value3')
|
28
|
+
end
|
29
|
+
|
30
|
+
# NOTE: We disable result checking since MockRedis and Redis will likely
|
31
|
+
# return a different random set (since the selection is, well, random)
|
32
|
+
it "returns the whole set if count is greater than the set size" do
|
33
|
+
@redises.send_without_checking(:srandmember, @key, 5).should =~ %w[value value2 value3]
|
34
|
+
end
|
35
|
+
|
36
|
+
it "returns random members up to count from the set when count is smaller than the set size" do
|
37
|
+
@redises.send_without_checking(:srandmember, @key, 2).size.should == 2
|
38
|
+
end
|
39
|
+
|
40
|
+
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
|
41
|
+
@redises.send_without_checking(:srandmember, @key, -5).size.should == 5
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns nil if the set is empty" do
|
45
|
+
@redises.srem(@key, %w[value value2 value3])
|
46
|
+
@redises.srandmember(@key, 2).should be_empty
|
47
|
+
end
|
48
|
+
end
|
23
49
|
end
|
data/spec/commands/ttl_spec.rb
CHANGED
@@ -10,8 +10,8 @@ describe "#ttl(key)" do
|
|
10
10
|
@redises.ttl(@key).should == -1
|
11
11
|
end
|
12
12
|
|
13
|
-
it "returns -
|
14
|
-
@redises.ttl('mock-redis-test:nonesuch').should == -
|
13
|
+
it "returns -2 for a key that does not exist" do
|
14
|
+
@redises.ttl('mock-redis-test:nonesuch').should == -2
|
15
15
|
end
|
16
16
|
|
17
17
|
it "stringifies key" do
|
data/spec/commands/zadd_spec.rb
CHANGED
@@ -36,6 +36,12 @@ describe "#zadd(key, score, member)" do
|
|
36
36
|
@redises.zrange(@key, 0, -1).should == ['one', 'two', 'three']
|
37
37
|
end
|
38
38
|
|
39
|
+
it "raises an error if an empty array is given" do
|
40
|
+
lambda do
|
41
|
+
@redises.zadd(@key, [])
|
42
|
+
end.should raise_error(Redis::CommandError)
|
43
|
+
end
|
44
|
+
|
39
45
|
it_should_behave_like "arg 1 is a score"
|
40
46
|
it_should_behave_like "a zset-only command"
|
41
47
|
end
|
@@ -9,6 +9,17 @@ describe "#zrange(key, start, stop [, :with_scores => true])" do
|
|
9
9
|
@redises.zadd(@key, 4, 'Madison')
|
10
10
|
end
|
11
11
|
|
12
|
+
context 'when the zset is empty' do
|
13
|
+
before do
|
14
|
+
@redises.del(@key)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should return an empty array' do
|
18
|
+
@redises.exists(@key).should be_false
|
19
|
+
@redises.zrange(@key, 0, 4).should == []
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
12
23
|
it "returns the elements when the range is given as strings" do
|
13
24
|
@redises.zrange(@key, "0", "1").should == ['Washington', 'Adams']
|
14
25
|
end
|
@@ -9,6 +9,17 @@ describe "#zrangebyscore(key, start, stop [:with_scores => true] [:limit => [off
|
|
9
9
|
@redises.zadd(@key, 4, 'Madison')
|
10
10
|
end
|
11
11
|
|
12
|
+
context 'when the zset is empty' do
|
13
|
+
before do
|
14
|
+
@redises.del(@key)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should return an empty array' do
|
18
|
+
@redises.exists(@key).should be_false
|
19
|
+
@redises.zrangebyscore(@key, 0, 4).should == []
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
12
23
|
it "returns the elements in order by score" do
|
13
24
|
@redises.zrangebyscore(@key, 1, 2).should == ['Washington', 'Adams']
|
14
25
|
end
|
@@ -9,6 +9,17 @@ describe "#zrevrange(key, start, stop [, :with_scores => true])" do
|
|
9
9
|
@redises.zadd(@key, 4, 'Madison')
|
10
10
|
end
|
11
11
|
|
12
|
+
context 'when the zset is empty' do
|
13
|
+
before do
|
14
|
+
@redises.del(@key)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should return an empty array' do
|
18
|
+
@redises.exists(@key).should be_false
|
19
|
+
@redises.zrevrange(@key, 0, 4).should == []
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
12
23
|
it "returns the elements in order by score" do
|
13
24
|
@redises.zrevrange(@key, 0, 1).should == ['Madison', 'Jefferson']
|
14
25
|
end
|
@@ -9,6 +9,17 @@ describe "#zrevrangebyscore(key, start, stop [:with_scores => true] [:limit => [
|
|
9
9
|
@redises.zadd(@key, 4, 'Madison')
|
10
10
|
end
|
11
11
|
|
12
|
+
context 'when the zset is empty' do
|
13
|
+
before do
|
14
|
+
@redises.del(@key)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should return an empty array' do
|
18
|
+
@redises.exists(@key).should be_false
|
19
|
+
@redises.zrevrangebyscore(@key, 0, 4).should == []
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
12
23
|
it "returns the elements in order by score" do
|
13
24
|
@redises.zrevrangebyscore(@key, 4, 3).should == ['Madison', 'Jefferson']
|
14
25
|
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mock_redis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.11.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Causes Engineering
|
@@ -10,12 +9,11 @@ authors:
|
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date:
|
12
|
+
date: 2014-01-22 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: rake
|
17
16
|
requirement: !ruby/object:Gem::Requirement
|
18
|
-
none: false
|
19
17
|
requirements:
|
20
18
|
- - ~>
|
21
19
|
- !ruby/object:Gem::Version
|
@@ -23,7 +21,6 @@ dependencies:
|
|
23
21
|
type: :development
|
24
22
|
prerelease: false
|
25
23
|
version_requirements: !ruby/object:Gem::Requirement
|
26
|
-
none: false
|
27
24
|
requirements:
|
28
25
|
- - ~>
|
29
26
|
- !ruby/object:Gem::Version
|
@@ -31,7 +28,6 @@ dependencies:
|
|
31
28
|
- !ruby/object:Gem::Dependency
|
32
29
|
name: redis
|
33
30
|
requirement: !ruby/object:Gem::Requirement
|
34
|
-
none: false
|
35
31
|
requirements:
|
36
32
|
- - ~>
|
37
33
|
- !ruby/object:Gem::Version
|
@@ -39,7 +35,6 @@ dependencies:
|
|
39
35
|
type: :development
|
40
36
|
prerelease: false
|
41
37
|
version_requirements: !ruby/object:Gem::Requirement
|
42
|
-
none: false
|
43
38
|
requirements:
|
44
39
|
- - ~>
|
45
40
|
- !ruby/object:Gem::Version
|
@@ -47,7 +42,6 @@ dependencies:
|
|
47
42
|
- !ruby/object:Gem::Dependency
|
48
43
|
name: rspec
|
49
44
|
requirement: !ruby/object:Gem::Requirement
|
50
|
-
none: false
|
51
45
|
requirements:
|
52
46
|
- - ~>
|
53
47
|
- !ruby/object:Gem::Version
|
@@ -55,7 +49,6 @@ dependencies:
|
|
55
49
|
type: :development
|
56
50
|
prerelease: false
|
57
51
|
version_requirements: !ruby/object:Gem::Requirement
|
58
|
-
none: false
|
59
52
|
requirements:
|
60
53
|
- - ~>
|
61
54
|
- !ruby/object:Gem::Version
|
@@ -130,6 +123,7 @@ files:
|
|
130
123
|
- spec/commands/hget_spec.rb
|
131
124
|
- spec/commands/hgetall_spec.rb
|
132
125
|
- spec/commands/hincrby_spec.rb
|
126
|
+
- spec/commands/hincrbyfloat_spec.rb
|
133
127
|
- spec/commands/hkeys_spec.rb
|
134
128
|
- spec/commands/hlen_spec.rb
|
135
129
|
- spec/commands/hmget_spec.rb
|
@@ -167,6 +161,7 @@ files:
|
|
167
161
|
- spec/commands/pexpireat_spec.rb
|
168
162
|
- spec/commands/ping_spec.rb
|
169
163
|
- spec/commands/pipelined_spec.rb
|
164
|
+
- spec/commands/pttl_spec.rb
|
170
165
|
- spec/commands/quit_spec.rb
|
171
166
|
- spec/commands/randomkey_spec.rb
|
172
167
|
- spec/commands/rename_spec.rb
|
@@ -233,27 +228,26 @@ files:
|
|
233
228
|
homepage: https://github.com/causes/mock_redis
|
234
229
|
licenses:
|
235
230
|
- MIT
|
231
|
+
metadata: {}
|
236
232
|
post_install_message:
|
237
233
|
rdoc_options: []
|
238
234
|
require_paths:
|
239
235
|
- lib
|
240
236
|
required_ruby_version: !ruby/object:Gem::Requirement
|
241
|
-
none: false
|
242
237
|
requirements:
|
243
|
-
- -
|
238
|
+
- - '>='
|
244
239
|
- !ruby/object:Gem::Version
|
245
240
|
version: '0'
|
246
241
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
247
|
-
none: false
|
248
242
|
requirements:
|
249
|
-
- -
|
243
|
+
- - '>='
|
250
244
|
- !ruby/object:Gem::Version
|
251
245
|
version: '0'
|
252
246
|
requirements: []
|
253
247
|
rubyforge_project:
|
254
|
-
rubygems_version:
|
248
|
+
rubygems_version: 2.0.14
|
255
249
|
signing_key:
|
256
|
-
specification_version:
|
250
|
+
specification_version: 4
|
257
251
|
summary: Redis mock that just lives in memory; useful for testing.
|
258
252
|
test_files:
|
259
253
|
- spec/client_spec.rb
|
@@ -288,6 +282,7 @@ test_files:
|
|
288
282
|
- spec/commands/hget_spec.rb
|
289
283
|
- spec/commands/hgetall_spec.rb
|
290
284
|
- spec/commands/hincrby_spec.rb
|
285
|
+
- spec/commands/hincrbyfloat_spec.rb
|
291
286
|
- spec/commands/hkeys_spec.rb
|
292
287
|
- spec/commands/hlen_spec.rb
|
293
288
|
- spec/commands/hmget_spec.rb
|
@@ -325,6 +320,7 @@ test_files:
|
|
325
320
|
- spec/commands/pexpireat_spec.rb
|
326
321
|
- spec/commands/ping_spec.rb
|
327
322
|
- spec/commands/pipelined_spec.rb
|
323
|
+
- spec/commands/pttl_spec.rb
|
328
324
|
- spec/commands/quit_spec.rb
|
329
325
|
- spec/commands/randomkey_spec.rb
|
330
326
|
- spec/commands/rename_spec.rb
|
@@ -388,3 +384,4 @@ test_files:
|
|
388
384
|
- spec/support/shared_examples/only_operates_on_zsets.rb
|
389
385
|
- spec/support/shared_examples/sorts_enumerables.rb
|
390
386
|
- spec/transactions_spec.rb
|
387
|
+
has_rdoc:
|