mock_redis 0.22.0 → 0.27.0

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.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +26 -5
  3. data/.rubocop_todo.yml +1 -1
  4. data/.travis.yml +1 -0
  5. data/CHANGELOG.md +31 -0
  6. data/Gemfile +4 -2
  7. data/lib/mock_redis.rb +1 -8
  8. data/lib/mock_redis/connection_method.rb +13 -0
  9. data/lib/mock_redis/database.rb +44 -14
  10. data/lib/mock_redis/expire_wrapper.rb +1 -1
  11. data/lib/mock_redis/future.rb +1 -1
  12. data/lib/mock_redis/geospatial_methods.rb +5 -5
  13. data/lib/mock_redis/hash_methods.rb +9 -4
  14. data/lib/mock_redis/info_method.rb +2 -2
  15. data/lib/mock_redis/multi_db_wrapper.rb +3 -3
  16. data/lib/mock_redis/pipelined_wrapper.rb +1 -1
  17. data/lib/mock_redis/stream.rb +22 -2
  18. data/lib/mock_redis/stream/id.rb +1 -1
  19. data/lib/mock_redis/stream_methods.rb +16 -1
  20. data/lib/mock_redis/string_methods.rb +27 -20
  21. data/lib/mock_redis/transaction_wrapper.rb +3 -3
  22. data/lib/mock_redis/utility_methods.rb +1 -1
  23. data/lib/mock_redis/version.rb +1 -1
  24. data/lib/mock_redis/zset_methods.rb +34 -9
  25. data/mock_redis.gemspec +1 -1
  26. data/spec/commands/blpop_spec.rb +0 -6
  27. data/spec/commands/brpop_spec.rb +6 -5
  28. data/spec/commands/connection_spec.rb +15 -0
  29. data/spec/commands/del_spec.rb +17 -0
  30. data/spec/commands/dump_spec.rb +19 -0
  31. data/spec/commands/exists_spec.rb +34 -5
  32. data/spec/commands/future_spec.rb +11 -1
  33. data/spec/commands/geoadd_spec.rb +1 -1
  34. data/spec/commands/hset_spec.rb +6 -6
  35. data/spec/commands/keys_spec.rb +17 -0
  36. data/spec/commands/mget_spec.rb +6 -0
  37. data/spec/commands/move_spec.rb +5 -5
  38. data/spec/commands/pipelined_spec.rb +20 -0
  39. data/spec/commands/restore_spec.rb +47 -0
  40. data/spec/commands/set_spec.rb +59 -9
  41. data/spec/commands/setbit_spec.rb +1 -0
  42. data/spec/commands/setex_spec.rb +16 -0
  43. data/spec/commands/srandmember_spec.rb +1 -1
  44. data/spec/commands/xadd_spec.rb +23 -3
  45. data/spec/commands/xlen_spec.rb +3 -1
  46. data/spec/commands/xrange_spec.rb +13 -0
  47. data/spec/commands/xread_spec.rb +66 -0
  48. data/spec/commands/xtrim_spec.rb +6 -0
  49. data/spec/commands/zinterstore_spec.rb +34 -0
  50. data/spec/commands/zrange_spec.rb +1 -1
  51. data/spec/commands/zrangebyscore_spec.rb +1 -1
  52. data/spec/commands/zrevrange_spec.rb +1 -1
  53. data/spec/commands/zrevrangebyscore_spec.rb +1 -1
  54. data/spec/commands/zunionstore_spec.rb +33 -0
  55. data/spec/spec_helper.rb +2 -1
  56. data/spec/support/redis_multiplexer.rb +2 -1
  57. data/spec/transactions_spec.rb +16 -0
  58. metadata +14 -5
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#xread(keys, ids)' do
4
+ before :all do
5
+ sleep 1 - (Time.now.to_f % 1)
6
+ @key = 'mock-redis-test:xread'
7
+ @key1 = 'mock-redis-test:xread1'
8
+ end
9
+
10
+ it 'reads a single entry' do
11
+ @redises.xadd(@key, { key: 'value' }, id: '1234567891234-0')
12
+ expect(@redises.xread(@key, '0-0'))
13
+ .to eq({ @key => [['1234567891234-0', { 'key' => 'value' }]] })
14
+ end
15
+
16
+ it 'reads multiple entries from the beginning of the stream' do
17
+ @redises.xadd(@key, { key0: 'value0' }, id: '1234567891234-0')
18
+ @redises.xadd(@key, { key1: 'value1' }, id: '1234567891234-1')
19
+ expect(@redises.xread(@key, '0-0'))
20
+ .to eq({ @key => [['1234567891234-0', { 'key0' => 'value0' }],
21
+ ['1234567891234-1', { 'key1' => 'value1' }]] })
22
+ end
23
+
24
+ it 'reads entries greater than the ID passed' do
25
+ @redises.xadd(@key, { key0: 'value0' }, id: '1234567891234-0')
26
+ @redises.xadd(@key, { key1: 'value1' }, id: '1234567891234-1')
27
+ expect(@redises.xread(@key, '1234567891234-0'))
28
+ .to eq({ @key => [['1234567891234-1', { 'key1' => 'value1' }]] })
29
+ end
30
+
31
+ it 'reads from multiple streams' do
32
+ @redises.xadd(@key, { key: 'value' }, id: '1234567891234-0')
33
+ @redises.xadd(@key1, { key1: 'value1' }, id: '1234567891234-0')
34
+ expect(@redises.xread([@key, @key1], %w[0-0 0-0]))
35
+ .to eq({ @key => [['1234567891234-0', { 'key' => 'value' }]],
36
+ @key1 => [['1234567891234-0', { 'key1' => 'value1' }]] })
37
+ end
38
+
39
+ it 'reads from multiple streams at the given IDs' do
40
+ @redises.xadd(@key, { key: 'value0' }, id: '1234567891234-0')
41
+ @redises.xadd(@key, { key: 'value1' }, id: '1234567891234-1')
42
+ @redises.xadd(@key, { key: 'value2' }, id: '1234567891234-2')
43
+ @redises.xadd(@key1, { key1: 'value0' }, id: '1234567891234-0')
44
+ @redises.xadd(@key1, { key1: 'value1' }, id: '1234567891234-1')
45
+ @redises.xadd(@key1, { key1: 'value2' }, id: '1234567891234-2')
46
+ # The first stream won't return anything since we specify the last ID
47
+ expect(@redises.xread([@key, @key1], %w[1234567891234-2 1234567891234-1]))
48
+ .to eq({ @key1 => [['1234567891234-2', { 'key1' => 'value2' }]] })
49
+ end
50
+
51
+ it 'supports the block parameter' do
52
+ @redises.xadd(@key, { key: 'value' }, id: '1234567891234-0')
53
+ expect(@redises.xread(@key, '0-0', block: 1000))
54
+ .to eq({ @key => [['1234567891234-0', { 'key' => 'value' }]] })
55
+ end
56
+
57
+ it 'limits results with count' do
58
+ @redises.xadd(@key, { key: 'value' }, id: '1234567891234-0')
59
+ @redises.xadd(@key, { key: 'value' }, id: '1234567891234-1')
60
+ @redises.xadd(@key, { key: 'value' }, id: '1234567891234-2')
61
+ expect(@redises.xread(@key, '0-0', count: 1))
62
+ .to eq({ @key => [['1234567891234-0', { 'key' => 'value' }]] })
63
+ expect(@redises.xread(@key, '1234567891234-0', count: 1))
64
+ .to eq({ @key => [['1234567891234-1', { 'key' => 'value' }]] })
65
+ end
66
+ end
@@ -16,6 +16,12 @@ describe '#xtrim("mystream", 1000, approximate: true)' do
16
16
  expect(@redises.xtrim(@key, 4)).to eq 2
17
17
  end
18
18
 
19
+ it 'returns 0 if count is greater than size' do
20
+ initial = @redises.xrange(@key, '-', '+')
21
+ expect(@redises.xtrim(@key, 1000)).to eq 0
22
+ expect(@redises.xrange(@key, '-', '+')).to eql(initial)
23
+ end
24
+
19
25
  it 'deletes the oldes elements' do
20
26
  @redises.xtrim(@key, 4)
21
27
  expect(@redises.xrange(@key, '-', '+')).to eq(
@@ -42,6 +42,40 @@ describe '#zinterstore(destination, keys, [:weights => [w,w,], [:aggregate => :s
42
42
  end.should raise_error(Redis::CommandError)
43
43
  end
44
44
 
45
+ context 'when used with a set' do
46
+ before do
47
+ @primes_text = 'mock-redis-test:zinterstore:primes-text'
48
+
49
+ @redises.sadd(@primes_text, 'two')
50
+ @redises.sadd(@primes_text, 'three')
51
+ @redises.sadd(@primes_text, 'five')
52
+ @redises.sadd(@primes_text, 'seven')
53
+ end
54
+
55
+ it 'returns the number of elements in the new set' do
56
+ @redises.zinterstore(@dest, [@odds, @primes_text]).should == 3
57
+ end
58
+
59
+ it 'sums the scores, substituting 1.0 for set values' do
60
+ @redises.zinterstore(@dest, [@odds, @primes_text])
61
+ @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
62
+ [['three', 4.0], ['five', 6.0], ['seven', 8.0]]
63
+ end
64
+ end
65
+
66
+ context 'when used with a non-coercible structure' do
67
+ before do
68
+ @non_set = 'mock-redis-test:zinterstore:non-set'
69
+
70
+ @redises.set(@non_set, 'one')
71
+ end
72
+ it 'raises an error for wrong value type' do
73
+ lambda do
74
+ @redises.zinterstore(@dest, [@odds, @non_set])
75
+ end.should raise_error(Redis::CommandError)
76
+ end
77
+ end
78
+
45
79
  context 'the :weights argument' do
46
80
  it 'multiplies the scores by the weights while aggregating' do
47
81
  @redises.zinterstore(@dest, [@odds, @primes], :weights => [2, 3])
@@ -15,7 +15,7 @@ describe '#zrange(key, start, stop [, :with_scores => true])' do
15
15
  end
16
16
 
17
17
  it 'should return an empty array' do
18
- @redises.exists(@key).should == false
18
+ @redises.exists?(@key).should == false
19
19
  @redises.zrange(@key, 0, 4).should == []
20
20
  end
21
21
  end
@@ -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 == false
18
+ @redises.exists?(@key).should == false
19
19
  @redises.zrangebyscore(@key, 0, 4).should == []
20
20
  end
21
21
  end
@@ -15,7 +15,7 @@ describe '#zrevrange(key, start, stop [, :with_scores => true])' do
15
15
  end
16
16
 
17
17
  it 'should return an empty array' do
18
- @redises.exists(@key).should == false
18
+ @redises.exists?(@key).should == false
19
19
  @redises.zrevrange(@key, 0, 4).should == []
20
20
  end
21
21
  end
@@ -15,7 +15,7 @@ describe '#zrevrangebyscore(key, start, stop [:with_scores => true] [:limit => [
15
15
  end
16
16
 
17
17
  it 'should return an empty array' do
18
- @redises.exists(@key).should == false
18
+ @redises.exists?(@key).should == false
19
19
  @redises.zrevrangebyscore(@key, 0, 4).should == []
20
20
  end
21
21
  end
@@ -41,6 +41,39 @@ describe '#zunionstore(destination, keys, [:weights => [w,w,], [:aggregate => :s
41
41
  end.should raise_error(Redis::CommandError)
42
42
  end
43
43
 
44
+ context 'when used with a set' do
45
+ before do
46
+ @set4 = 'mock-redis-test:zunionstore4'
47
+
48
+ @redises.sadd(@set4, 'two')
49
+ @redises.sadd(@set4, 'three')
50
+ @redises.sadd(@set4, 'four')
51
+ end
52
+
53
+ it 'returns the number of elements in the new set' do
54
+ @redises.zunionstore(@dest, [@set3, @set4]).should == 4
55
+ end
56
+
57
+ it 'sums the scores, substituting 1.0 for set values' do
58
+ @redises.zunionstore(@dest, [@set3, @set4])
59
+ @redises.zrange(@dest, 0, -1, :with_scores => true).should ==
60
+ [['four', 1.0], ['one', 1.0], ['two', 3.0], ['three', 4.0]]
61
+ end
62
+ end
63
+
64
+ context 'when used with a non-coercible structure' do
65
+ before do
66
+ @non_set = 'mock-redis-test:zunionstore4'
67
+
68
+ @redises.set(@non_set, 'one')
69
+ end
70
+ it 'raises an error for wrong value type' do
71
+ lambda do
72
+ @redises.zunionstore(@dest, [@set1, @non_set])
73
+ end.should raise_error(Redis::CommandError)
74
+ end
75
+ end
76
+
44
77
  context 'the :weights argument' do
45
78
  it 'multiplies the scores by the weights while aggregating' do
46
79
  @redises.zunionstore(@dest, [@set1, @set2, @set3], :weights => [2, 3, 5])
@@ -11,11 +11,12 @@ end
11
11
  require 'rspec/its'
12
12
  require 'redis'
13
13
  $LOAD_PATH.unshift(File.expand_path(File.join(__FILE__, '..', '..', 'lib')))
14
+ require 'ruby2_keywords'
14
15
  require 'mock_redis'
15
16
  require 'timecop'
16
17
 
17
18
  $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..')))
18
- Dir['spec/support/**/*.rb'].each { |x| require x }
19
+ Dir['spec/support/**/*.rb'].sort.each { |x| require x }
19
20
 
20
21
  module TypeCheckingHelper
21
22
  def method_from_description(example)
@@ -9,6 +9,7 @@ class RedisMultiplexer < BlankSlate
9
9
 
10
10
  def initialize(*a)
11
11
  @mock_redis = MockRedis.new(*a)
12
+ Redis.exists_returns_integer = true
12
13
  @real_redis = Redis.new(*a)
13
14
  _gsub_clear
14
15
  end
@@ -22,7 +23,7 @@ class RedisMultiplexer < BlankSlate
22
23
  @gsub_from = @gsub_to = ''
23
24
  end
24
25
 
25
- def method_missing(method, *args, &blk)
26
+ ruby2_keywords def method_missing(method, *args, &blk)
26
27
  # If we're in a Redis command that accepts a block, and we execute more
27
28
  # redis commands, ONLY execute them on the Redis implementation that the
28
29
  # block came from.
@@ -67,6 +67,22 @@ describe 'transactions (multi/exec/discard)' do
67
67
  @redises.get('counter').should eq '6'
68
68
  @redises.get('test').should eq '1'
69
69
  end
70
+
71
+ it 'allows blocks within multi blocks' do
72
+ @redises.set('foo', 'bar')
73
+ @redises.set('fuu', 'baz')
74
+
75
+ result = nil
76
+
77
+ @redises.multi do |r|
78
+ result = r.mget('foo', 'fuu') { |reply| reply.map(&:upcase) }
79
+ r.del('foo', 'fuu')
80
+ end
81
+
82
+ result.value.should eq %w[BAR BAZ]
83
+ @redises.get('foo').should eq nil
84
+ @redises.get('fuu').should eq nil
85
+ end
70
86
  end
71
87
 
72
88
  context '#discard' do
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.22.0
4
+ version: 0.27.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane da Silva
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-10-10 00:00:00.000000000 Z
12
+ date: 2021-01-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: 4.1.0
20
+ version: 4.2.0
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: 4.1.0
27
+ version: 4.2.0
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rspec
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -90,6 +90,7 @@ files:
90
90
  - Rakefile
91
91
  - lib/mock_redis.rb
92
92
  - lib/mock_redis/assertions.rb
93
+ - lib/mock_redis/connection_method.rb
93
94
  - lib/mock_redis/database.rb
94
95
  - lib/mock_redis/exceptions.rb
95
96
  - lib/mock_redis/expire_wrapper.rb
@@ -126,11 +127,13 @@ files:
126
127
  - spec/commands/brpop_spec.rb
127
128
  - spec/commands/brpoplpush_spec.rb
128
129
  - spec/commands/connected_spec.rb
130
+ - spec/commands/connection_spec.rb
129
131
  - spec/commands/dbsize_spec.rb
130
132
  - spec/commands/decr_spec.rb
131
133
  - spec/commands/decrby_spec.rb
132
134
  - spec/commands/del_spec.rb
133
135
  - spec/commands/disconnect_spec.rb
136
+ - spec/commands/dump_spec.rb
134
137
  - spec/commands/echo_spec.rb
135
138
  - spec/commands/eval_spec.rb
136
139
  - spec/commands/evalsha_spec.rb
@@ -198,6 +201,7 @@ files:
198
201
  - spec/commands/randomkey_spec.rb
199
202
  - spec/commands/rename_spec.rb
200
203
  - spec/commands/renamenx_spec.rb
204
+ - spec/commands/restore_spec.rb
201
205
  - spec/commands/rpop_spec.rb
202
206
  - spec/commands/rpoplpush_spec.rb
203
207
  - spec/commands/rpush_spec.rb
@@ -239,6 +243,7 @@ files:
239
243
  - spec/commands/xadd_spec.rb
240
244
  - spec/commands/xlen_spec.rb
241
245
  - spec/commands/xrange_spec.rb
246
+ - spec/commands/xread_spec.rb
242
247
  - spec/commands/xrevrange_spec.rb
243
248
  - spec/commands/xtrim_spec.rb
244
249
  - spec/commands/zadd_spec.rb
@@ -291,7 +296,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
291
296
  - !ruby/object:Gem::Version
292
297
  version: '0'
293
298
  requirements: []
294
- rubygems_version: 3.0.3
299
+ rubygems_version: 3.1.4
295
300
  signing_key:
296
301
  specification_version: 4
297
302
  summary: Redis mock that just lives in memory; useful for testing.
@@ -308,11 +313,13 @@ test_files:
308
313
  - spec/commands/brpop_spec.rb
309
314
  - spec/commands/brpoplpush_spec.rb
310
315
  - spec/commands/connected_spec.rb
316
+ - spec/commands/connection_spec.rb
311
317
  - spec/commands/dbsize_spec.rb
312
318
  - spec/commands/decr_spec.rb
313
319
  - spec/commands/decrby_spec.rb
314
320
  - spec/commands/del_spec.rb
315
321
  - spec/commands/disconnect_spec.rb
322
+ - spec/commands/dump_spec.rb
316
323
  - spec/commands/echo_spec.rb
317
324
  - spec/commands/eval_spec.rb
318
325
  - spec/commands/evalsha_spec.rb
@@ -380,6 +387,7 @@ test_files:
380
387
  - spec/commands/randomkey_spec.rb
381
388
  - spec/commands/rename_spec.rb
382
389
  - spec/commands/renamenx_spec.rb
390
+ - spec/commands/restore_spec.rb
383
391
  - spec/commands/rpop_spec.rb
384
392
  - spec/commands/rpoplpush_spec.rb
385
393
  - spec/commands/rpush_spec.rb
@@ -421,6 +429,7 @@ test_files:
421
429
  - spec/commands/xadd_spec.rb
422
430
  - spec/commands/xlen_spec.rb
423
431
  - spec/commands/xrange_spec.rb
432
+ - spec/commands/xread_spec.rb
424
433
  - spec/commands/xrevrange_spec.rb
425
434
  - spec/commands/xtrim_spec.rb
426
435
  - spec/commands/zadd_spec.rb