mock_redis 0.23.0 → 0.27.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +26 -5
- data/.rubocop_todo.yml +1 -1
- data/.travis.yml +1 -0
- data/CHANGELOG.md +27 -0
- data/Gemfile +2 -2
- data/lib/mock_redis.rb +1 -1
- data/lib/mock_redis/connection_method.rb +13 -0
- data/lib/mock_redis/database.rb +21 -14
- data/lib/mock_redis/expire_wrapper.rb +1 -1
- data/lib/mock_redis/future.rb +1 -1
- data/lib/mock_redis/geospatial_methods.rb +5 -5
- data/lib/mock_redis/hash_methods.rb +9 -4
- data/lib/mock_redis/info_method.rb +2 -2
- data/lib/mock_redis/multi_db_wrapper.rb +3 -3
- data/lib/mock_redis/pipelined_wrapper.rb +1 -1
- data/lib/mock_redis/stream.rb +22 -2
- data/lib/mock_redis/stream/id.rb +1 -1
- data/lib/mock_redis/stream_methods.rb +16 -1
- data/lib/mock_redis/string_methods.rb +21 -17
- data/lib/mock_redis/transaction_wrapper.rb +3 -3
- data/lib/mock_redis/utility_methods.rb +1 -1
- data/lib/mock_redis/version.rb +1 -1
- data/mock_redis.gemspec +2 -1
- data/spec/commands/blpop_spec.rb +0 -6
- data/spec/commands/brpop_spec.rb +6 -5
- data/spec/commands/connection_spec.rb +15 -0
- data/spec/commands/del_spec.rb +17 -0
- data/spec/commands/exists_spec.rb +34 -5
- data/spec/commands/future_spec.rb +11 -1
- data/spec/commands/geoadd_spec.rb +1 -1
- data/spec/commands/hset_spec.rb +6 -6
- data/spec/commands/keys_spec.rb +17 -0
- data/spec/commands/mget_spec.rb +6 -0
- data/spec/commands/move_spec.rb +5 -5
- data/spec/commands/set_spec.rb +55 -7
- data/spec/commands/setbit_spec.rb +1 -0
- data/spec/commands/srandmember_spec.rb +1 -1
- data/spec/commands/xadd_spec.rb +23 -3
- data/spec/commands/xlen_spec.rb +3 -1
- data/spec/commands/xrange_spec.rb +13 -0
- data/spec/commands/xread_spec.rb +66 -0
- data/spec/commands/xtrim_spec.rb +6 -0
- data/spec/commands/zrange_spec.rb +1 -1
- data/spec/commands/zrangebyscore_spec.rb +1 -1
- data/spec/commands/zrevrange_spec.rb +1 -1
- data/spec/commands/zrevrangebyscore_spec.rb +1 -1
- data/spec/spec_helper.rb +2 -1
- data/spec/support/redis_multiplexer.rb +2 -1
- data/spec/transactions_spec.rb +16 -0
- metadata +24 -5
data/lib/mock_redis/stream/id.rb
CHANGED
@@ -31,7 +31,7 @@ class MockRedis
|
|
31
31
|
@timestamp = id
|
32
32
|
end
|
33
33
|
@sequence = @sequence.nil? ? sequence : @sequence.to_i
|
34
|
-
if
|
34
|
+
if self <= min
|
35
35
|
raise Redis::CommandError,
|
36
36
|
'ERR The ID specified in XADD is equal or smaller than ' \
|
37
37
|
'the target stream top item'
|
@@ -4,7 +4,6 @@ require 'mock_redis/stream'
|
|
4
4
|
|
5
5
|
# TODO: Implement the following commands
|
6
6
|
#
|
7
|
-
# * xread
|
8
7
|
# * xgroup
|
9
8
|
# * xreadgroup
|
10
9
|
# * xack
|
@@ -67,6 +66,22 @@ class MockRedis
|
|
67
66
|
end
|
68
67
|
end
|
69
68
|
|
69
|
+
def xread(keys, ids, count: nil, block: nil)
|
70
|
+
args = []
|
71
|
+
args += ['COUNT', count] if count
|
72
|
+
args += ['BLOCK', block.to_i] if block
|
73
|
+
result = {}
|
74
|
+
keys = keys.is_a?(Array) ? keys : [keys]
|
75
|
+
ids = ids.is_a?(Array) ? ids : [ids]
|
76
|
+
keys.each_with_index do |key, index|
|
77
|
+
with_stream_at(key) do |stream|
|
78
|
+
data = stream.read(ids[index], *args)
|
79
|
+
result[key] = data unless data.empty?
|
80
|
+
end
|
81
|
+
end
|
82
|
+
result
|
83
|
+
end
|
84
|
+
|
70
85
|
private
|
71
86
|
|
72
87
|
def with_stream_at(key, &blk)
|
@@ -93,6 +93,7 @@ class MockRedis
|
|
93
93
|
def getbit(key, offset)
|
94
94
|
assert_stringy(key)
|
95
95
|
|
96
|
+
offset = offset.to_i
|
96
97
|
offset_of_byte = offset / 8
|
97
98
|
offset_within_byte = offset % 8
|
98
99
|
|
@@ -154,9 +155,10 @@ class MockRedis
|
|
154
155
|
end
|
155
156
|
|
156
157
|
def mget(*keys)
|
158
|
+
keys.flatten!
|
159
|
+
|
157
160
|
assert_has_args(keys, 'mget')
|
158
161
|
|
159
|
-
keys.flatten!
|
160
162
|
keys.map do |key|
|
161
163
|
get(key) if stringy?(key)
|
162
164
|
end
|
@@ -188,7 +190,7 @@ class MockRedis
|
|
188
190
|
def msetnx(*kvpairs)
|
189
191
|
assert_has_args(kvpairs, 'msetnx')
|
190
192
|
|
191
|
-
if kvpairs.each_slice(2).any? { |(k, _)| exists(k) }
|
193
|
+
if kvpairs.each_slice(2).any? { |(k, _)| exists?(k) }
|
192
194
|
false
|
193
195
|
else
|
194
196
|
mset(*kvpairs)
|
@@ -200,19 +202,20 @@ class MockRedis
|
|
200
202
|
msetnx(*hash.to_a.flatten)
|
201
203
|
end
|
202
204
|
|
203
|
-
|
205
|
+
# Parameer list required to ensure the ArgumentError is returned correctly
|
206
|
+
# rubocop:disable Metrics/ParameterLists
|
207
|
+
def set(key, value, ex: nil, px: nil, nx: nil, xx: nil, keepttl: nil)
|
204
208
|
key = key.to_s
|
205
209
|
return_true = false
|
206
|
-
|
207
|
-
|
208
|
-
if exists(key)
|
210
|
+
if nx
|
211
|
+
if exists?(key)
|
209
212
|
return false
|
210
213
|
else
|
211
214
|
return_true = true
|
212
215
|
end
|
213
216
|
end
|
214
|
-
if
|
215
|
-
if exists(key)
|
217
|
+
if xx
|
218
|
+
if exists?(key)
|
216
219
|
return_true = true
|
217
220
|
else
|
218
221
|
return false
|
@@ -220,24 +223,24 @@ class MockRedis
|
|
220
223
|
end
|
221
224
|
data[key] = value.to_s
|
222
225
|
|
223
|
-
|
224
|
-
if
|
225
|
-
if
|
226
|
+
remove_expiration(key) unless keepttl
|
227
|
+
if ex
|
228
|
+
if ex == 0
|
226
229
|
raise Redis::CommandError, 'ERR invalid expire time in set'
|
227
230
|
end
|
228
|
-
expire(key,
|
231
|
+
expire(key, ex)
|
229
232
|
end
|
230
233
|
|
231
|
-
|
232
|
-
|
233
|
-
if duration == 0
|
234
|
+
if px
|
235
|
+
if px == 0
|
234
236
|
raise Redis::CommandError, 'ERR invalid expire time in set'
|
235
237
|
end
|
236
|
-
pexpire(key,
|
238
|
+
pexpire(key, px)
|
237
239
|
end
|
238
240
|
|
239
241
|
return_true ? true : 'OK'
|
240
242
|
end
|
243
|
+
# rubocop:enable Metrics/ParameterLists
|
241
244
|
|
242
245
|
def setbit(key, offset, value)
|
243
246
|
assert_stringy(key, 'ERR bit is not an integer or out of range')
|
@@ -245,6 +248,7 @@ class MockRedis
|
|
245
248
|
|
246
249
|
str = data[key] || ''
|
247
250
|
|
251
|
+
offset = offset.to_i
|
248
252
|
offset_of_byte = offset / 8
|
249
253
|
offset_within_byte = offset % 8
|
250
254
|
|
@@ -323,7 +327,7 @@ class MockRedis
|
|
323
327
|
end
|
324
328
|
|
325
329
|
def setnx(key, value)
|
326
|
-
if exists(key)
|
330
|
+
if exists?(key)
|
327
331
|
false
|
328
332
|
else
|
329
333
|
set(key, value)
|
@@ -15,9 +15,9 @@ class MockRedis
|
|
15
15
|
@multi_block_given = false
|
16
16
|
end
|
17
17
|
|
18
|
-
def method_missing(method, *args, &block)
|
18
|
+
ruby2_keywords def method_missing(method, *args, &block)
|
19
19
|
if in_multi?
|
20
|
-
future = MockRedis::Future.new([method, *args])
|
20
|
+
future = MockRedis::Future.new([method, *args], block)
|
21
21
|
@transaction_futures << future
|
22
22
|
|
23
23
|
if @multi_block_given
|
@@ -60,7 +60,7 @@ class MockRedis
|
|
60
60
|
begin
|
61
61
|
result = send(*future.command)
|
62
62
|
future.store_result(result)
|
63
|
-
|
63
|
+
future.value
|
64
64
|
rescue StandardError => e
|
65
65
|
e
|
66
66
|
end
|
data/lib/mock_redis/version.rb
CHANGED
data/mock_redis.gemspec
CHANGED
@@ -23,8 +23,9 @@ Gem::Specification.new do |s|
|
|
23
23
|
|
24
24
|
s.required_ruby_version = '>= 2.4'
|
25
25
|
|
26
|
-
s.add_development_dependency 'redis', '~> 4.
|
26
|
+
s.add_development_dependency 'redis', '~> 4.2.0'
|
27
27
|
s.add_development_dependency 'rspec', '~> 3.0'
|
28
28
|
s.add_development_dependency 'rspec-its', '~> 1.0'
|
29
|
+
s.add_development_dependency 'ruby2_keywords'
|
29
30
|
s.add_development_dependency 'timecop', '~> 0.9.1'
|
30
31
|
end
|
data/spec/commands/blpop_spec.rb
CHANGED
@@ -27,12 +27,6 @@ describe '#blpop(key [, key, ...,], timeout)' do
|
|
27
27
|
[@list1, 'one']
|
28
28
|
end
|
29
29
|
|
30
|
-
it 'raises an error on subsecond timeouts' do
|
31
|
-
lambda do
|
32
|
-
@redises.blpop(@list1, @list2, :timeout => 0.5)
|
33
|
-
end.should raise_error(Redis::CommandError)
|
34
|
-
end
|
35
|
-
|
36
30
|
it 'raises an error on negative timeout' do
|
37
31
|
lambda do
|
38
32
|
@redises.blpop(@list1, @list2, :timeout => -1)
|
data/spec/commands/brpop_spec.rb
CHANGED
@@ -26,11 +26,12 @@ describe '#brpop(key [, key, ...,], timeout)' do
|
|
26
26
|
[@list1, 'two']
|
27
27
|
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
29
|
+
# TODO: Not sure how redis-rb is handling this but they're not raising an error
|
30
|
+
# it 'raises an error on subsecond timeouts' do
|
31
|
+
# lambda do
|
32
|
+
# @redises.brpop(@list1, @list2, :timeout => 0.5)
|
33
|
+
# end.should raise_error(Redis::CommandError)
|
34
|
+
# end
|
34
35
|
|
35
36
|
it 'raises an error on negative timeout' do
|
36
37
|
lambda do
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe '#connection' do
|
4
|
+
let(:redis) { @redises.mock }
|
5
|
+
|
6
|
+
it 'returns the correct values' do
|
7
|
+
redis.connection.should == {
|
8
|
+
:host => '127.0.0.1',
|
9
|
+
:port => 6379,
|
10
|
+
:db => 0,
|
11
|
+
:id => 'redis://127.0.0.1:6379/0',
|
12
|
+
:location => '127.0.0.1:6379'
|
13
|
+
}
|
14
|
+
end
|
15
|
+
end
|
data/spec/commands/del_spec.rb
CHANGED
@@ -1,6 +1,16 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe '#del(key [, key, ...])' do
|
4
|
+
before :all do
|
5
|
+
sleep 1 - (Time.now.to_f % 1)
|
6
|
+
end
|
7
|
+
|
8
|
+
before :each do
|
9
|
+
# TODO: Redis appears to be returning a timestamp a few seconds in the future
|
10
|
+
# so we're ignoring the last 5 digits (time in milliseconds)
|
11
|
+
@redises._gsub(/\d{5}-\d/, '...-.')
|
12
|
+
end
|
13
|
+
|
4
14
|
it 'returns the number of keys deleted' do
|
5
15
|
@redises.set('mock-redis-test:1', 1)
|
6
16
|
@redises.set('mock-redis-test:2', 1)
|
@@ -32,4 +42,11 @@ describe '#del(key [, key, ...])' do
|
|
32
42
|
it 'raises an error if an empty array is given' do
|
33
43
|
expect { @redises.del [] }.to raise_error Redis::CommandError
|
34
44
|
end
|
45
|
+
|
46
|
+
it 'removes a stream key' do
|
47
|
+
@redises.xadd('mock-redis-stream', { key: 'value' }, maxlen: 0)
|
48
|
+
expect(@redises.exists?('mock-redis-stream')).to eq true
|
49
|
+
@redises.del('mock-redis-stream')
|
50
|
+
expect(@redises.exists?('mock-redis-stream')).to eq false
|
51
|
+
end
|
35
52
|
end
|
@@ -1,14 +1,43 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe '#exists(
|
4
|
-
before { @
|
3
|
+
describe '#exists(*keys)' do
|
4
|
+
before { @key1 = 'mock-redis-test:exists1' }
|
5
|
+
before { @key2 = 'mock-redis-test:exists2' }
|
6
|
+
|
7
|
+
it 'returns 0 for keys that do not exist' do
|
8
|
+
@redises.exists(@key1).should == 0
|
9
|
+
@redises.exists(@key1, @key2).should == 0
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'returns 1 for keys that do exist' do
|
13
|
+
@redises.set(@key1, 1)
|
14
|
+
@redises.exists(@key1).should == 1
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'returns the count of all keys that exist' do
|
18
|
+
@redises.set(@key1, 1)
|
19
|
+
@redises.set(@key2, 1)
|
20
|
+
@redises.exists(@key1, @key2).should == 2
|
21
|
+
@redises.exists(@key1, @key2, 'does-not-exist').should == 2
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#exists?(*keys)' do
|
26
|
+
before { @key1 = 'mock-redis-test:exists1' }
|
27
|
+
before { @key2 = 'mock-redis-test:exists2' }
|
5
28
|
|
6
29
|
it 'returns false for keys that do not exist' do
|
7
|
-
@redises.exists(@
|
30
|
+
@redises.exists?(@key1).should == false
|
31
|
+
@redises.exists?(@key1, @key2).should == false
|
8
32
|
end
|
9
33
|
|
10
34
|
it 'returns true for keys that do exist' do
|
11
|
-
@redises.set(@
|
12
|
-
@redises.exists(@
|
35
|
+
@redises.set(@key1, 1)
|
36
|
+
@redises.exists?(@key1).should == true
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'returns true if any keys exist' do
|
40
|
+
@redises.set(@key2, 1)
|
41
|
+
@redises.exists?(@key1, @key2).should == true
|
13
42
|
end
|
14
43
|
end
|
@@ -3,7 +3,12 @@ require 'spec_helper'
|
|
3
3
|
describe MockRedis::Future do
|
4
4
|
let(:command) { [:get, 'foo'] }
|
5
5
|
let(:result) { 'bar' }
|
6
|
-
|
6
|
+
let(:block) { ->(value) { value.upcase } }
|
7
|
+
|
8
|
+
before do
|
9
|
+
@future = MockRedis::Future.new(command)
|
10
|
+
@future2 = MockRedis::Future.new(command, block)
|
11
|
+
end
|
7
12
|
|
8
13
|
it 'remembers the command' do
|
9
14
|
@future.command.should eq(command)
|
@@ -17,4 +22,9 @@ describe MockRedis::Future do
|
|
17
22
|
@future.store_result(result)
|
18
23
|
@future.value.should eq(result)
|
19
24
|
end
|
25
|
+
|
26
|
+
it 'executes the block on the value if block is passed in' do
|
27
|
+
@future2.store_result(result)
|
28
|
+
@future2.value.should eq('BAR')
|
29
|
+
end
|
20
30
|
end
|
@@ -33,7 +33,7 @@ describe '#geoadd' do
|
|
33
33
|
context 'when coordinates are not in allowed range' do
|
34
34
|
let(:coords) { [181, 86] }
|
35
35
|
let(:message) do
|
36
|
-
formatted_coords = coords.map { |c| format('
|
36
|
+
formatted_coords = coords.map { |c| format('%<coords>.6f', coords: c) }
|
37
37
|
"ERR invalid longitude,latitude pair #{formatted_coords.join(',')}"
|
38
38
|
end
|
39
39
|
|
data/spec/commands/hset_spec.rb
CHANGED
@@ -5,18 +5,18 @@ describe '#hset(key, field)' do
|
|
5
5
|
@key = 'mock-redis-test:hset'
|
6
6
|
end
|
7
7
|
|
8
|
-
it 'returns
|
9
|
-
@redises.hset(@key, 'k1', 'v1').should ==
|
8
|
+
it 'returns 1 if the key does not exist' do
|
9
|
+
@redises.hset(@key, 'k1', 'v1').should == 1
|
10
10
|
end
|
11
11
|
|
12
|
-
it 'returns
|
12
|
+
it 'returns 1 if the key exists but the field does not' do
|
13
13
|
@redises.hset(@key, 'k1', 'v1')
|
14
|
-
@redises.hset(@key, 'k2', 'v2').should ==
|
14
|
+
@redises.hset(@key, 'k2', 'v2').should == 1
|
15
15
|
end
|
16
16
|
|
17
|
-
it 'returns
|
17
|
+
it 'returns 0 if the field already exists' do
|
18
18
|
@redises.hset(@key, 'k1', 'v1')
|
19
|
-
@redises.hset(@key, 'k1', 'v1').should ==
|
19
|
+
@redises.hset(@key, 'k1', 'v1').should == 0
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'creates a hash there is no such field' do
|
data/spec/commands/keys_spec.rb
CHANGED
@@ -29,6 +29,7 @@ describe '#keys()' do
|
|
29
29
|
|
30
30
|
@redises.set('mock-redis-test:special-key?', 'true')
|
31
31
|
@redises.set('mock-redis-test:special-key*', 'true')
|
32
|
+
@redises.set('mock-redis-test:special-key-!?*', 'true')
|
32
33
|
end
|
33
34
|
|
34
35
|
describe 'the ? character' do
|
@@ -53,6 +54,22 @@ describe '#keys()' do
|
|
53
54
|
'mock-redis-test:special-key?',
|
54
55
|
]
|
55
56
|
end
|
57
|
+
|
58
|
+
context 'multiple ? characters' do
|
59
|
+
it "properly handles multiple consequtive '?' characters" do
|
60
|
+
@redises.keys('mock-redis-test:special-key-???').sort.should == [
|
61
|
+
'mock-redis-test:special-key-!?*',
|
62
|
+
]
|
63
|
+
end
|
64
|
+
|
65
|
+
context '\\? as a literal ' do
|
66
|
+
it 'handles multiple ? as both literal and special character' do
|
67
|
+
@redises.keys('mock-redis-test:special-key-?\??').sort.should == [
|
68
|
+
'mock-redis-test:special-key-!?*',
|
69
|
+
]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
56
73
|
end
|
57
74
|
|
58
75
|
describe 'the * character' do
|
data/spec/commands/mget_spec.rb
CHANGED
@@ -49,5 +49,11 @@ describe '#mget(key [, key, ...])' do
|
|
49
49
|
@redises.mget
|
50
50
|
end.should raise_error(Redis::CommandError)
|
51
51
|
end
|
52
|
+
|
53
|
+
it 'raises an error if you pass it empty array' do
|
54
|
+
lambda do
|
55
|
+
@redises.mget([])
|
56
|
+
end.should raise_error(Redis::CommandError)
|
57
|
+
end
|
52
58
|
end
|
53
59
|
end
|
data/spec/commands/move_spec.rb
CHANGED
@@ -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 == false
|
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 == false
|
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 == false
|
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 == false
|
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 == false
|
138
|
+
@redises.exists?(@key).should == false
|
139
139
|
end
|
140
140
|
|
141
141
|
it 'copies key to destdb' do
|