mock_redis 0.21.0 → 0.26.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +26 -5
  3. data/.rubocop_todo.yml +1 -1
  4. data/.travis.yml +3 -3
  5. data/CHANGELOG.md +33 -0
  6. data/Gemfile +2 -2
  7. data/LICENSE.md +21 -0
  8. data/README.md +37 -13
  9. data/lib/mock_redis.rb +0 -7
  10. data/lib/mock_redis/database.rb +42 -14
  11. data/lib/mock_redis/future.rb +1 -1
  12. data/lib/mock_redis/geospatial_methods.rb +4 -4
  13. data/lib/mock_redis/hash_methods.rb +18 -6
  14. data/lib/mock_redis/info_method.rb +2 -2
  15. data/lib/mock_redis/multi_db_wrapper.rb +2 -2
  16. data/lib/mock_redis/stream.rb +25 -2
  17. data/lib/mock_redis/stream/id.rb +1 -1
  18. data/lib/mock_redis/stream_methods.rb +16 -1
  19. data/lib/mock_redis/string_methods.rb +17 -9
  20. data/lib/mock_redis/transaction_wrapper.rb +2 -2
  21. data/lib/mock_redis/utility_methods.rb +6 -3
  22. data/lib/mock_redis/version.rb +1 -1
  23. data/lib/mock_redis/zset_methods.rb +52 -9
  24. data/mock_redis.gemspec +1 -2
  25. data/spec/commands/blpop_spec.rb +0 -6
  26. data/spec/commands/brpop_spec.rb +6 -5
  27. data/spec/commands/del_spec.rb +15 -0
  28. data/spec/commands/dump_spec.rb +19 -0
  29. data/spec/commands/exists_spec.rb +34 -5
  30. data/spec/commands/future_spec.rb +11 -1
  31. data/spec/commands/geoadd_spec.rb +1 -1
  32. data/spec/commands/hmset_spec.rb +26 -0
  33. data/spec/commands/hset_spec.rb +6 -6
  34. data/spec/commands/keys_spec.rb +17 -0
  35. data/spec/commands/mget_spec.rb +6 -0
  36. data/spec/commands/move_spec.rb +5 -5
  37. data/spec/commands/pipelined_spec.rb +20 -0
  38. data/spec/commands/restore_spec.rb +47 -0
  39. data/spec/commands/scan_spec.rb +9 -0
  40. data/spec/commands/set_spec.rb +8 -4
  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 +20 -0
  45. data/spec/commands/xrange_spec.rb +13 -0
  46. data/spec/commands/xread_spec.rb +66 -0
  47. data/spec/commands/xtrim_spec.rb +6 -0
  48. data/spec/commands/zinterstore_spec.rb +34 -0
  49. data/spec/commands/zpopmax_spec.rb +60 -0
  50. data/spec/commands/zpopmin_spec.rb +60 -0
  51. data/spec/commands/zrange_spec.rb +1 -1
  52. data/spec/commands/zrangebyscore_spec.rb +1 -1
  53. data/spec/commands/zrevrange_spec.rb +1 -1
  54. data/spec/commands/zrevrangebyscore_spec.rb +1 -1
  55. data/spec/commands/zunionstore_spec.rb +33 -0
  56. data/spec/spec_helper.rb +4 -2
  57. data/spec/support/redis_multiplexer.rb +1 -0
  58. data/spec/transactions_spec.rb +16 -0
  59. metadata +16 -26
  60. data/LICENSE +0 -19
@@ -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])
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#zpopmax(key, count)' do
4
+ before(:each) do
5
+ @key = 'mock-redis-test:zpopmax'
6
+ @redises.del(@key)
7
+ @redises.zadd(@key, 1, 'one')
8
+ @redises.zadd(@key, 2, 'two')
9
+ @redises.zadd(@key, 3, 'three')
10
+ end
11
+
12
+ context 'when count is unspecified' do
13
+ it 'returns nil if the set does not exist' do
14
+ @redises.zpopmax('does-not-exist').should nil
15
+ end
16
+
17
+ it 'returns the highest ranked element' do
18
+ @redises.zpopmax(@key).should == ['three', 3]
19
+ @redises.zcard(@key).should == 2
20
+ end
21
+ end
22
+
23
+ context 'when count is 1' do
24
+ let(:count) { 1 }
25
+
26
+ it 'returns nil if the set does not exist' do
27
+ @redises.zpopmax('does-not-exist', count).should nil
28
+ end
29
+
30
+ it 'returns the highest ranked element' do
31
+ @redises.zpopmax(@key, count).should == ['three', 3]
32
+ @redises.zcard(@key).should == 2
33
+ end
34
+ end
35
+
36
+ context 'when count is greater than 1' do
37
+ let(:count) { 2 }
38
+
39
+ it 'returns empty array if the set does not exist' do
40
+ @redises.zpopmax('does-not-exist', count).should == []
41
+ end
42
+
43
+ it 'returns the highest ranked elements' do
44
+ @redises.zpopmax(@key, count).should == [['three', 3], ['two', 2]]
45
+ @redises.zcard(@key).should == 1
46
+ end
47
+ end
48
+
49
+ context 'when count is greater than the size of the set' do
50
+ let(:count) { 4 }
51
+
52
+ it 'returns the entire set' do
53
+ before = @redises.zrange(@key, 0, count, with_scores: true).reverse
54
+ @redises.zpopmax(@key, count).should == before
55
+ @redises.zcard(@key).should == 0
56
+ end
57
+ end
58
+
59
+ it_should_behave_like 'a zset-only command'
60
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#zpopmin(key, count)' do
4
+ before(:each) do
5
+ @key = 'mock-redis-test:zpopmin'
6
+ @redises.del(@key)
7
+ @redises.zadd(@key, 1, 'one')
8
+ @redises.zadd(@key, 2, 'two')
9
+ @redises.zadd(@key, 3, 'three')
10
+ end
11
+
12
+ context 'when count is unspecified' do
13
+ it 'returns nil if the set does not exist' do
14
+ @redises.zpopmin('does-not-exist').should nil
15
+ end
16
+
17
+ it 'returns the lowest ranked element' do
18
+ @redises.zpopmin(@key).should == ['one', 1]
19
+ @redises.zcard(@key).should == 2
20
+ end
21
+ end
22
+
23
+ context 'when count is 1' do
24
+ let(:count) { 1 }
25
+
26
+ it 'returns nil if the set does not exist' do
27
+ @redises.zpopmin('does-not-exist', count).should nil
28
+ end
29
+
30
+ it 'returns the lowest ranked element' do
31
+ @redises.zpopmin(@key, count).should == ['one', 1]
32
+ @redises.zcard(@key).should == 2
33
+ end
34
+ end
35
+
36
+ context 'when count is greater than 1' do
37
+ let(:count) { 2 }
38
+
39
+ it 'returns empty array if the set does not exist' do
40
+ @redises.zpopmin('does-not-exist', count).should == []
41
+ end
42
+
43
+ it 'returns the lowest ranked elements' do
44
+ @redises.zpopmin(@key, count).should == [['one', 1], ['two', 2]]
45
+ @redises.zcard(@key).should == 1
46
+ end
47
+ end
48
+
49
+ context 'when count is greater than the size of the set' do
50
+ let(:count) { 4 }
51
+
52
+ it 'returns the entire set' do
53
+ before = @redises.zrange(@key, 0, count, with_scores: true)
54
+ @redises.zpopmin(@key, count).should == before
55
+ @redises.zcard(@key).should == 0
56
+ end
57
+ end
58
+
59
+ it_should_behave_like 'a zset-only command'
60
+ end
@@ -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])
@@ -15,7 +15,7 @@ require 'mock_redis'
15
15
  require 'timecop'
16
16
 
17
17
  $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..')))
18
- Dir['spec/support/**/*.rb'].each { |x| require x }
18
+ Dir['spec/support/**/*.rb'].sort.each { |x| require x }
19
19
 
20
20
  module TypeCheckingHelper
21
21
  def method_from_description(example)
@@ -27,7 +27,7 @@ module TypeCheckingHelper
27
27
  end
28
28
 
29
29
  def args_for_method(method)
30
- return [] if method.to_s == 'spop'
30
+ return [] if %w[spop zpopmin zpopmax].include?(method.to_s)
31
31
  method_arity = @redises.real.method(method).arity
32
32
  if method_arity < 0 # -1 comes from def foo(*args)
33
33
  [1, 2] # probably good enough
@@ -40,6 +40,8 @@ end
40
40
  RSpec.configure do |config|
41
41
  config.expect_with :rspec do |c|
42
42
  c.syntax = [:expect, :should]
43
+ # Allow for a large output so we can debug error messages
44
+ c.max_formatted_output_length = 1_000_000
43
45
  end
44
46
 
45
47
  config.mock_with :rspec do |c|
@@ -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
@@ -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.21.0
4
+ version: 0.26.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane da Silva
@@ -9,42 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-06-26 00:00:00.000000000 Z
12
+ date: 2020-08-26 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: rake
16
- requirement: !ruby/object:Gem::Requirement
17
- requirements:
18
- - - ">="
19
- - !ruby/object:Gem::Version
20
- version: '10'
21
- - - "<"
22
- - !ruby/object:Gem::Version
23
- version: '12'
24
- type: :development
25
- prerelease: false
26
- version_requirements: !ruby/object:Gem::Requirement
27
- requirements:
28
- - - ">="
29
- - !ruby/object:Gem::Version
30
- version: '10'
31
- - - "<"
32
- - !ruby/object:Gem::Version
33
- version: '12'
34
14
  - !ruby/object:Gem::Dependency
35
15
  name: redis
36
16
  requirement: !ruby/object:Gem::Requirement
37
17
  requirements:
38
18
  - - "~>"
39
19
  - !ruby/object:Gem::Version
40
- version: 4.1.0
20
+ version: 4.2.0
41
21
  type: :development
42
22
  prerelease: false
43
23
  version_requirements: !ruby/object:Gem::Requirement
44
24
  requirements:
45
25
  - - "~>"
46
26
  - !ruby/object:Gem::Version
47
- version: 4.1.0
27
+ version: 4.2.0
48
28
  - !ruby/object:Gem::Dependency
49
29
  name: rspec
50
30
  requirement: !ruby/object:Gem::Requirement
@@ -105,7 +85,7 @@ files:
105
85
  - ".travis.yml"
106
86
  - CHANGELOG.md
107
87
  - Gemfile
108
- - LICENSE
88
+ - LICENSE.md
109
89
  - README.md
110
90
  - Rakefile
111
91
  - lib/mock_redis.rb
@@ -151,6 +131,7 @@ files:
151
131
  - spec/commands/decrby_spec.rb
152
132
  - spec/commands/del_spec.rb
153
133
  - spec/commands/disconnect_spec.rb
134
+ - spec/commands/dump_spec.rb
154
135
  - spec/commands/echo_spec.rb
155
136
  - spec/commands/eval_spec.rb
156
137
  - spec/commands/evalsha_spec.rb
@@ -218,6 +199,7 @@ files:
218
199
  - spec/commands/randomkey_spec.rb
219
200
  - spec/commands/rename_spec.rb
220
201
  - spec/commands/renamenx_spec.rb
202
+ - spec/commands/restore_spec.rb
221
203
  - spec/commands/rpop_spec.rb
222
204
  - spec/commands/rpoplpush_spec.rb
223
205
  - spec/commands/rpush_spec.rb
@@ -259,6 +241,7 @@ files:
259
241
  - spec/commands/xadd_spec.rb
260
242
  - spec/commands/xlen_spec.rb
261
243
  - spec/commands/xrange_spec.rb
244
+ - spec/commands/xread_spec.rb
262
245
  - spec/commands/xrevrange_spec.rb
263
246
  - spec/commands/xtrim_spec.rb
264
247
  - spec/commands/zadd_spec.rb
@@ -266,6 +249,8 @@ files:
266
249
  - spec/commands/zcount_spec.rb
267
250
  - spec/commands/zincrby_spec.rb
268
251
  - spec/commands/zinterstore_spec.rb
252
+ - spec/commands/zpopmax_spec.rb
253
+ - spec/commands/zpopmin_spec.rb
269
254
  - spec/commands/zrange_spec.rb
270
255
  - spec/commands/zrangebyscore_spec.rb
271
256
  - spec/commands/zrank_spec.rb
@@ -309,7 +294,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
309
294
  - !ruby/object:Gem::Version
310
295
  version: '0'
311
296
  requirements: []
312
- rubygems_version: 3.0.3
297
+ rubygems_version: 3.1.1
313
298
  signing_key:
314
299
  specification_version: 4
315
300
  summary: Redis mock that just lives in memory; useful for testing.
@@ -331,6 +316,7 @@ test_files:
331
316
  - spec/commands/decrby_spec.rb
332
317
  - spec/commands/del_spec.rb
333
318
  - spec/commands/disconnect_spec.rb
319
+ - spec/commands/dump_spec.rb
334
320
  - spec/commands/echo_spec.rb
335
321
  - spec/commands/eval_spec.rb
336
322
  - spec/commands/evalsha_spec.rb
@@ -398,6 +384,7 @@ test_files:
398
384
  - spec/commands/randomkey_spec.rb
399
385
  - spec/commands/rename_spec.rb
400
386
  - spec/commands/renamenx_spec.rb
387
+ - spec/commands/restore_spec.rb
401
388
  - spec/commands/rpop_spec.rb
402
389
  - spec/commands/rpoplpush_spec.rb
403
390
  - spec/commands/rpush_spec.rb
@@ -439,6 +426,7 @@ test_files:
439
426
  - spec/commands/xadd_spec.rb
440
427
  - spec/commands/xlen_spec.rb
441
428
  - spec/commands/xrange_spec.rb
429
+ - spec/commands/xread_spec.rb
442
430
  - spec/commands/xrevrange_spec.rb
443
431
  - spec/commands/xtrim_spec.rb
444
432
  - spec/commands/zadd_spec.rb
@@ -446,6 +434,8 @@ test_files:
446
434
  - spec/commands/zcount_spec.rb
447
435
  - spec/commands/zincrby_spec.rb
448
436
  - spec/commands/zinterstore_spec.rb
437
+ - spec/commands/zpopmax_spec.rb
438
+ - spec/commands/zpopmin_spec.rb
449
439
  - spec/commands/zrange_spec.rb
450
440
  - spec/commands/zrangebyscore_spec.rb
451
441
  - spec/commands/zrank_spec.rb