mock_redis 0.13.2 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/.travis.yml +3 -0
- data/CHANGELOG.md +10 -0
- data/Gemfile +0 -5
- data/README.md +2 -2
- data/lib/mock_redis.rb +1 -0
- data/lib/mock_redis/database.rb +11 -2
- data/lib/mock_redis/future.rb +22 -0
- data/lib/mock_redis/pipelined_wrapper.rb +0 -21
- data/lib/mock_redis/set_methods.rb +1 -0
- data/lib/mock_redis/string_methods.rb +13 -5
- data/lib/mock_redis/transaction_wrapper.rb +22 -8
- data/lib/mock_redis/version.rb +1 -1
- data/lib/mock_redis/zset_methods.rb +12 -9
- data/mock_redis.gemspec +2 -1
- data/spec/commands/connected_spec.rb +1 -1
- data/spec/commands/exists_spec.rb +2 -2
- data/spec/commands/expire_spec.rb +18 -2
- data/spec/commands/expireat_spec.rb +2 -2
- data/spec/commands/hexists_spec.rb +4 -4
- data/spec/commands/hsetnx_spec.rb +2 -2
- data/spec/commands/move_spec.rb +8 -8
- data/spec/commands/msetnx_spec.rb +2 -2
- data/spec/commands/persist_spec.rb +4 -4
- data/spec/commands/pexpire_spec.rb +2 -2
- data/spec/commands/pexpireat_spec.rb +2 -2
- data/spec/commands/renamenx_spec.rb +2 -2
- data/spec/commands/sadd_spec.rb +2 -2
- data/spec/commands/scan_spec.rb +51 -0
- data/spec/commands/set_spec.rb +6 -6
- data/spec/commands/setbit_spec.rb +7 -0
- data/spec/commands/setnx_spec.rb +2 -2
- data/spec/commands/sismember_spec.rb +5 -5
- data/spec/commands/smove_spec.rb +6 -6
- data/spec/commands/srem_spec.rb +3 -3
- data/spec/commands/sunion_spec.rb +4 -0
- data/spec/commands/zadd_spec.rb +2 -2
- data/spec/commands/zrange_spec.rb +1 -1
- data/spec/commands/zrangebyscore_spec.rb +1 -1
- data/spec/commands/zrem_spec.rb +3 -3
- data/spec/commands/zremrangebyscore_spec.rb +1 -0
- data/spec/commands/zrevrange_spec.rb +1 -1
- data/spec/commands/zrevrangebyscore_spec.rb +1 -1
- data/spec/commands/zscore_spec.rb +2 -2
- data/spec/spec_helper.rb +11 -2
- data/spec/support/redis_multiplexer.rb +6 -6
- data/spec/support/shared_examples/only_operates_on_hashes.rb +2 -2
- data/spec/support/shared_examples/only_operates_on_lists.rb +2 -2
- data/spec/support/shared_examples/only_operates_on_sets.rb +2 -2
- data/spec/support/shared_examples/only_operates_on_strings.rb +2 -2
- data/spec/support/shared_examples/only_operates_on_zsets.rb +4 -4
- data/spec/transactions_spec.rb +23 -0
- metadata +36 -18
@@ -6,12 +6,12 @@ describe "#hsetnx(key, field)" do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
it "returns true if the field is absent" do
|
9
|
-
@redises.hsetnx(@key, 'field', 'val').should
|
9
|
+
@redises.hsetnx(@key, 'field', 'val').should == true
|
10
10
|
end
|
11
11
|
|
12
12
|
it "returns 0 if the field is present" do
|
13
13
|
@redises.hset(@key, 'field', 'val')
|
14
|
-
@redises.hsetnx(@key, 'field', 'val').should
|
14
|
+
@redises.hsetnx(@key, 'field', 'val').should == false
|
15
15
|
end
|
16
16
|
|
17
17
|
it "leaves the field unchanged if the field is present" do
|
data/spec/commands/move_spec.rb
CHANGED
@@ -17,7 +17,7 @@ describe '#move(key, db)' do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it "returns false" do
|
20
|
-
@redises.move(@key, @destdb).should
|
20
|
+
@redises.move(@key, @destdb).should == false
|
21
21
|
end
|
22
22
|
|
23
23
|
it "leaves destdb/key alone" do
|
@@ -38,7 +38,7 @@ describe '#move(key, db)' do
|
|
38
38
|
end
|
39
39
|
|
40
40
|
it "returns false" do
|
41
|
-
@redises.move(@key, @destdb).should
|
41
|
+
@redises.move(@key, @destdb).should == false
|
42
42
|
end
|
43
43
|
|
44
44
|
it "leaves destdb/key alone" do
|
@@ -53,7 +53,7 @@ describe '#move(key, db)' do
|
|
53
53
|
end
|
54
54
|
|
55
55
|
it "returns true" do
|
56
|
-
@redises.move(@key, @destdb).should
|
56
|
+
@redises.move(@key, @destdb).should == true
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
@@ -64,7 +64,7 @@ describe '#move(key, db)' do
|
|
64
64
|
end
|
65
65
|
|
66
66
|
it "removes key from srcdb" do
|
67
|
-
@redises.exists(@key).should
|
67
|
+
@redises.exists(@key).should == false
|
68
68
|
end
|
69
69
|
|
70
70
|
it "copies key to destdb" do
|
@@ -81,7 +81,7 @@ describe '#move(key, db)' do
|
|
81
81
|
end
|
82
82
|
|
83
83
|
it "removes key from srcdb" do
|
84
|
-
@redises.exists(@key).should
|
84
|
+
@redises.exists(@key).should == false
|
85
85
|
end
|
86
86
|
|
87
87
|
it "copies key to destdb" do
|
@@ -99,7 +99,7 @@ describe '#move(key, db)' do
|
|
99
99
|
end
|
100
100
|
|
101
101
|
it "removes key from srcdb" do
|
102
|
-
@redises.exists(@key).should
|
102
|
+
@redises.exists(@key).should == false
|
103
103
|
end
|
104
104
|
|
105
105
|
it "copies key to destdb" do
|
@@ -117,7 +117,7 @@ describe '#move(key, db)' do
|
|
117
117
|
end
|
118
118
|
|
119
119
|
it "removes key from srcdb" do
|
120
|
-
@redises.exists(@key).should
|
120
|
+
@redises.exists(@key).should == false
|
121
121
|
end
|
122
122
|
|
123
123
|
it "copies key to destdb" do
|
@@ -135,7 +135,7 @@ describe '#move(key, db)' do
|
|
135
135
|
end
|
136
136
|
|
137
137
|
it "removes key from srcdb" do
|
138
|
-
@redises.exists(@key).should
|
138
|
+
@redises.exists(@key).should == false
|
139
139
|
end
|
140
140
|
|
141
141
|
it "copies key to destdb" do
|
@@ -7,7 +7,7 @@ describe '#msetnx(key, value [, key, value, ...])' do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
it "responds with 1 if any keys were set" do
|
10
|
-
@redises.msetnx(@key1, 1).should
|
10
|
+
@redises.msetnx(@key1, 1).should == true
|
11
11
|
end
|
12
12
|
|
13
13
|
it "sets the values" do
|
@@ -23,7 +23,7 @@ describe '#msetnx(key, value [, key, value, ...])' do
|
|
23
23
|
|
24
24
|
it "responds with 0 if any value is already set" do
|
25
25
|
@redises.set(@key1, "old1")
|
26
|
-
@redises.msetnx(@key1, 'value1', @key2, 'value2').should
|
26
|
+
@redises.msetnx(@key1, 'value1', @key2, 'value2').should == false
|
27
27
|
end
|
28
28
|
|
29
29
|
it "raises an error if given an odd number of arguments" do
|
@@ -8,21 +8,21 @@ describe "#persist(key)" do
|
|
8
8
|
|
9
9
|
it "returns true for a key with a timeout" do
|
10
10
|
@redises.expire(@key, 10000)
|
11
|
-
@redises.persist(@key).should
|
11
|
+
@redises.persist(@key).should == true
|
12
12
|
end
|
13
13
|
|
14
14
|
it "returns false for a key with no timeout" do
|
15
|
-
@redises.persist(@key).should
|
15
|
+
@redises.persist(@key).should == false
|
16
16
|
end
|
17
17
|
|
18
18
|
it "returns false for a key that does not exist" do
|
19
|
-
@redises.persist('mock-redis-test:nonesuch').should
|
19
|
+
@redises.persist('mock-redis-test:nonesuch').should == false
|
20
20
|
end
|
21
21
|
|
22
22
|
it "removes the timeout" do
|
23
23
|
@redises.expire(@key, 10000)
|
24
24
|
@redises.persist(@key)
|
25
|
-
@redises.persist(@key).should
|
25
|
+
@redises.persist(@key).should == false
|
26
26
|
end
|
27
27
|
|
28
28
|
context "[mock only]" do
|
@@ -7,11 +7,11 @@ describe "#pexpire(key, ms)" do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
it "returns true for a key that exists" do
|
10
|
-
@redises.pexpire(@key, 1).should
|
10
|
+
@redises.pexpire(@key, 1).should == true
|
11
11
|
end
|
12
12
|
|
13
13
|
it "returns false for a key that does not exist" do
|
14
|
-
@redises.pexpire('mock-redis-test:nonesuch', 1).should
|
14
|
+
@redises.pexpire('mock-redis-test:nonesuch', 1).should == false
|
15
15
|
end
|
16
16
|
|
17
17
|
it "removes a key immediately when ms==0" do
|
@@ -7,12 +7,12 @@ describe "#pexpireat(key, timestamp_ms)" do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
it "returns true for a key that exists" do
|
10
|
-
@redises.pexpireat(@key, (Time.now.to_f * 1000).to_i + 1).should
|
10
|
+
@redises.pexpireat(@key, (Time.now.to_f * 1000).to_i + 1).should == true
|
11
11
|
end
|
12
12
|
|
13
13
|
it "returns false for a key that does not exist" do
|
14
14
|
@redises.pexpireat('mock-redis-test:nonesuch',
|
15
|
-
(Time.now.to_f * 1000).to_i + 1).should
|
15
|
+
(Time.now.to_f * 1000).to_i + 1).should == false
|
16
16
|
end
|
17
17
|
|
18
18
|
it "removes a key immediately when timestamp is now" do
|
@@ -9,12 +9,12 @@ describe '#renamenx(key, newkey)' do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it "responds with true when newkey does not exist" do
|
12
|
-
@redises.renamenx(@key, @newkey).should
|
12
|
+
@redises.renamenx(@key, @newkey).should == true
|
13
13
|
end
|
14
14
|
|
15
15
|
it "responds with false when newkey exists" do
|
16
16
|
@redises.set(@newkey, 'monkey')
|
17
|
-
@redises.renamenx(@key, @newkey).should
|
17
|
+
@redises.renamenx(@key, @newkey).should == false
|
18
18
|
end
|
19
19
|
|
20
20
|
it "moves the data" do
|
data/spec/commands/sadd_spec.rb
CHANGED
@@ -4,12 +4,12 @@ describe '#sadd(key, member)' do
|
|
4
4
|
before { @key = 'mock-redis-test:sadd' }
|
5
5
|
|
6
6
|
it "returns true if the set did not already contain member" do
|
7
|
-
@redises.sadd(@key, 1).should
|
7
|
+
@redises.sadd(@key, 1).should == true
|
8
8
|
end
|
9
9
|
|
10
10
|
it "returns false if the set did already contain member" do
|
11
11
|
@redises.sadd(@key, 1)
|
12
|
-
@redises.sadd(@key, 1).should
|
12
|
+
@redises.sadd(@key, 1).should == false
|
13
13
|
end
|
14
14
|
|
15
15
|
it "adds member to the set" do
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe '#scan' do
|
4
|
+
subject { MockRedis::Database.new(self) }
|
5
|
+
|
6
|
+
let(:count) { 10 }
|
7
|
+
let(:match) { '*' }
|
8
|
+
|
9
|
+
before do
|
10
|
+
expect(subject).to receive_message_chain(:data, :keys).and_return(collection)
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'when no keys are found' do
|
14
|
+
let(:collection) { [] }
|
15
|
+
|
16
|
+
it 'returns a 0 cursor and an empty collection' do
|
17
|
+
expect(subject.scan(0, count: count, match: match)).to eq(['0', []])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when keys are found' do
|
22
|
+
context 'when count is lower than collection size' do
|
23
|
+
let(:collection) { (count+1).times.map {|i| "mock:key#{1}"} }
|
24
|
+
let(:expected) { [count.to_s, collection]}
|
25
|
+
|
26
|
+
it 'returns a the next cursor and the collection' do
|
27
|
+
expect(subject.scan(0, count: count, match: match)).to eq(expected)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when count is greater or equal than collection size' do
|
32
|
+
let(:collection) { count.times.map {|i| "mock:key#{1}"} }
|
33
|
+
let(:expected) { ['0', collection]}
|
34
|
+
|
35
|
+
it 'returns a 0 cursor and the collection' do
|
36
|
+
expect(subject.scan(0, count: count, match: match)).to eq(expected)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when giving a custom match filter' do
|
41
|
+
let(:match) { 'mock:key*' }
|
42
|
+
let(:collection) { %w(mock:key mock:key2 mock:otherkey) }
|
43
|
+
let(:expected) { ['0', %w(mock:key mock:key2)]}
|
44
|
+
|
45
|
+
it 'returns a 0 cursor and the filtered collection' do
|
46
|
+
expect(subject.scan(0, count: count, match: match)).to eq(expected)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
data/spec/commands/set_spec.rb
CHANGED
@@ -9,28 +9,28 @@ describe '#set(key, value)' do
|
|
9
9
|
it 'raises an error for EX seconds = 0' do
|
10
10
|
expect do
|
11
11
|
@redises.set('mock-redis-test', 1, ex: 0)
|
12
|
-
end.to raise_error(Redis::CommandError, 'ERR invalid expire time in
|
12
|
+
end.to raise_error(Redis::CommandError, 'ERR invalid expire time in set')
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'raises an error for PX milliseconds = 0' do
|
16
16
|
expect do
|
17
17
|
@redises.set('mock-redis-test', 1, px: 0)
|
18
|
-
end.to raise_error(Redis::CommandError, 'ERR invalid expire time in
|
18
|
+
end.to raise_error(Redis::CommandError, 'ERR invalid expire time in set')
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'accepts NX' do
|
22
22
|
key = 'mock-redis-test'
|
23
23
|
@redises.del(key)
|
24
|
-
@redises.set(key, 1, nx: true).should
|
25
|
-
@redises.set(key, 1, nx: true).should
|
24
|
+
@redises.set(key, 1, nx: true).should == true
|
25
|
+
@redises.set(key, 1, nx: true).should == false
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'accepts XX' do
|
29
29
|
key = 'mock-redis-test'
|
30
30
|
@redises.del(key)
|
31
|
-
@redises.set(key, 1, xx: true).should
|
31
|
+
@redises.set(key, 1, xx: true).should == false
|
32
32
|
@redises.set(key, 1).should == 'OK'
|
33
|
-
@redises.set(key, 1, xx: true).should
|
33
|
+
@redises.set(key, 1, xx: true).should == true
|
34
34
|
end
|
35
35
|
|
36
36
|
context '[mock only]' do
|
@@ -44,5 +44,12 @@ describe "#setbit(key, offset)" do
|
|
44
44
|
@redises.get(@key).should == "@"
|
45
45
|
end
|
46
46
|
|
47
|
+
it "sets and retrieves bits" do
|
48
|
+
@redises.setbit(@key, 22, 1)
|
49
|
+
@redises.getbit(@key, 22).should == 1
|
50
|
+
@redises.setbit(@key, 23, 0)
|
51
|
+
@redises.getbit(@key, 23).should == 0
|
52
|
+
end
|
53
|
+
|
47
54
|
it_should_behave_like "a string-only command"
|
48
55
|
end
|
data/spec/commands/setnx_spec.rb
CHANGED
@@ -4,12 +4,12 @@ describe '#setnx(key, value)' do
|
|
4
4
|
before { @key = 'mock-redis-test:setnx' }
|
5
5
|
|
6
6
|
it "returns true if the key was absent" do
|
7
|
-
@redises.setnx(@key, 1).should
|
7
|
+
@redises.setnx(@key, 1).should == true
|
8
8
|
end
|
9
9
|
|
10
10
|
it "returns false if the key was present" do
|
11
11
|
@redises.set(@key, 2)
|
12
|
-
@redises.setnx(@key, 1).should
|
12
|
+
@redises.setnx(@key, 1).should == false
|
13
13
|
end
|
14
14
|
|
15
15
|
it "sets the value if missing" do
|
@@ -8,21 +8,21 @@ describe '#sismember(key, member)' do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
it "returns true if member is in set" do
|
11
|
-
@redises.sismember(@key, 'whiskey').should
|
12
|
-
@redises.sismember(@key, 'beer').should
|
11
|
+
@redises.sismember(@key, 'whiskey').should == true
|
12
|
+
@redises.sismember(@key, 'beer').should == true
|
13
13
|
end
|
14
14
|
|
15
15
|
it "returns false if member is not in set" do
|
16
|
-
@redises.sismember(@key, 'cola').should
|
16
|
+
@redises.sismember(@key, 'cola').should == false
|
17
17
|
end
|
18
18
|
|
19
19
|
it "stringifies member" do
|
20
20
|
@redises.sadd(@key, '1')
|
21
|
-
@redises.sismember(@key, 1).should
|
21
|
+
@redises.sismember(@key, 1).should == true
|
22
22
|
end
|
23
23
|
|
24
24
|
it "treats a nonexistent value as an empty set" do
|
25
|
-
@redises.sismember('mock-redis-test:nonesuch', 'beer').should
|
25
|
+
@redises.sismember('mock-redis-test:nonesuch', 'beer').should == false
|
26
26
|
end
|
27
27
|
|
28
28
|
it_should_behave_like "a set-only command"
|
data/spec/commands/smove_spec.rb
CHANGED
@@ -10,22 +10,22 @@ describe '#smove(source, destination, member)' do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
it "returns true if the member exists in src" do
|
13
|
-
@redises.smove(@src, @dest, 1).should
|
13
|
+
@redises.smove(@src, @dest, 1).should == true
|
14
14
|
end
|
15
15
|
|
16
16
|
it "returns false if the member exists in src" do
|
17
|
-
@redises.smove(@src, @dest, 'nope').should
|
17
|
+
@redises.smove(@src, @dest, 'nope').should == false
|
18
18
|
end
|
19
19
|
|
20
20
|
it "returns true if the member exists in src and dest" do
|
21
21
|
@redises.sadd(@dest, 1)
|
22
|
-
@redises.smove(@src, @dest, 1).should
|
22
|
+
@redises.smove(@src, @dest, 1).should == true
|
23
23
|
end
|
24
24
|
|
25
25
|
it "moves member from source to destination" do
|
26
26
|
@redises.smove(@src, @dest, 1)
|
27
|
-
@redises.sismember(@dest, 1).should
|
28
|
-
@redises.sismember(@src, 1).should
|
27
|
+
@redises.sismember(@dest, 1).should == true
|
28
|
+
@redises.sismember(@src, 1).should == false
|
29
29
|
end
|
30
30
|
|
31
31
|
it "cleans up empty sets" do
|
@@ -34,7 +34,7 @@ describe '#smove(source, destination, member)' do
|
|
34
34
|
end
|
35
35
|
|
36
36
|
it "treats a nonexistent value as an empty set" do
|
37
|
-
@redises.smove('mock-redis-test:nonesuch', @dest, 1).should
|
37
|
+
@redises.smove('mock-redis-test:nonesuch', @dest, 1).should == false
|
38
38
|
end
|
39
39
|
|
40
40
|
it_should_behave_like "a set-only command"
|
data/spec/commands/srem_spec.rb
CHANGED
@@ -9,11 +9,11 @@ describe '#srem(key, member)' do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it "returns true if member is in the set" do
|
12
|
-
@redises.srem(@key, 'bert').should
|
12
|
+
@redises.srem(@key, 'bert').should == true
|
13
13
|
end
|
14
14
|
|
15
15
|
it "returns false if member is not in the set" do
|
16
|
-
@redises.srem(@key, 'cookiemonster').should
|
16
|
+
@redises.srem(@key, 'cookiemonster').should == false
|
17
17
|
end
|
18
18
|
|
19
19
|
it "removes member from the set" do
|
@@ -23,7 +23,7 @@ describe '#srem(key, member)' do
|
|
23
23
|
|
24
24
|
it "stringifies member" do
|
25
25
|
@redises.sadd(@key, '1')
|
26
|
-
@redises.srem(@key, 1).should
|
26
|
+
@redises.srem(@key, 1).should == true
|
27
27
|
end
|
28
28
|
|
29
29
|
it "cleans up empty sets" do
|
@@ -18,6 +18,10 @@ describe '#sunion(key [, key, ...])' do
|
|
18
18
|
should == %w[2 3 5 7]
|
19
19
|
end
|
20
20
|
|
21
|
+
it "allows Array as argument" do
|
22
|
+
@redises.sunion([@evens, @primes]).should == %w[2 4 6 8 10 3 5 7]
|
23
|
+
end
|
24
|
+
|
21
25
|
it "raises an error if given 0 sets" do
|
22
26
|
lambda do
|
23
27
|
@redises.sunion
|
data/spec/commands/zadd_spec.rb
CHANGED
@@ -4,12 +4,12 @@ describe "#zadd(key, score, member)" do
|
|
4
4
|
before { @key = 'mock-redis-test:zadd' }
|
5
5
|
|
6
6
|
it "returns true if member wasn't present in the set" do
|
7
|
-
@redises.zadd(@key, 1, 'foo').should
|
7
|
+
@redises.zadd(@key, 1, 'foo').should == true
|
8
8
|
end
|
9
9
|
|
10
10
|
it "returns false if member was present in the set" do
|
11
11
|
@redises.zadd(@key, 1, 'foo')
|
12
|
-
@redises.zadd(@key, 1, 'foo').should
|
12
|
+
@redises.zadd(@key, 1, 'foo').should == false
|
13
13
|
end
|
14
14
|
|
15
15
|
it "adds member to the set" do
|
@@ -15,7 +15,7 @@ describe "#zrangebyscore(key, start, stop [:with_scores => true] [:limit => [off
|
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'should return an empty array' do
|
18
|
-
@redises.exists(@key).should
|
18
|
+
@redises.exists(@key).should == false
|
19
19
|
@redises.zrangebyscore(@key, 0, 4).should == []
|
20
20
|
end
|
21
21
|
end
|