mock_redis 0.16.1 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 07dd0bf6f710e9b04f28aa4f3c52756fd98d0286
4
- data.tar.gz: 10ed67b75a2ef384933f3a0eb53fd80130545bd9
3
+ metadata.gz: bb438752840e4549b0797ba546fe482c36bbfed7
4
+ data.tar.gz: af6544eeeabb9e54989b39aebf64bad53e2a4ac0
5
5
  SHA512:
6
- metadata.gz: f32e91e4c52602c4f0873b076cdf382a0fbc875e803d593dd87841d42eb2bf3a0264b902e5bf8b97e4ccf80e6944a028f54561b62872abaa49337a676fdae13d
7
- data.tar.gz: 2b2dc33938b162fb550cae6a38b83d68fea745bc3220f38d183c9168bbc7c2bf60cb1629ea18fc48a0614e68359cd5353649ca91ed2111a6fe3f01c1c4614eb7
6
+ metadata.gz: e548a84b1df2535ddde32358e88801383560e3cedb65e9892e07c4f1812b291e37d3e255cd5777463fa96ad11fa9c47c047b520d77fb101e3e430a5703159a69
7
+ data.tar.gz: e3c469733b2de7d7793ea892b651cc3b9d0005f97c73e8b71894d4d6e10a4e95ae12de0faf97bfc0110d60ea7352abd40272ac5cba8df0bbc05984646819afe7
@@ -51,9 +51,16 @@ Style/IfUnlessModifier:
51
51
  Style/Lambda:
52
52
  Enabled: false
53
53
 
54
+ # TODO: Address these at some point
55
+ Style/MethodMissing:
56
+ Enabled: false
57
+
54
58
  Style/MultilineBlockChain:
55
59
  Enabled: false
56
60
 
61
+ Style/NumericPredicate:
62
+ Enabled: false
63
+
57
64
  # Prefer curly braces except for %i/%w/%W, since those return arrays.
58
65
  Style/PercentLiteralDelimiters:
59
66
  PreferredDelimiters:
@@ -4,22 +4,26 @@ sudo: false
4
4
 
5
5
  cache: bundler
6
6
 
7
+ addons:
8
+ apt:
9
+ packages:
10
+ - redis-server
11
+
7
12
  services:
8
13
  - redis-server
9
14
 
10
15
  rvm:
11
- - 1.9.3
12
16
  - 2.0
13
17
  - 2.1
14
18
  - 2.2
15
- - 2.3.0
16
- - jruby-19mode
19
+ - 2.3.1
17
20
 
18
21
  before_script:
19
22
  - git config --local user.email "travis@travis.ci"
20
23
  - git config --local user.name "Travis CI"
21
24
 
22
25
  script:
26
+ - redis-cli --version
23
27
  - bundle exec rspec
24
28
  - bundle exec overcommit --sign
25
29
  - bundle exec overcommit --run
@@ -1,5 +1,12 @@
1
1
  # MockRedis Changelog
2
2
 
3
+ ### 0.17.0
4
+
5
+ * Upgrade minimum `redis` gem version to 3.3.0+
6
+ * Add support for XX, NX and INCR parameters of `ZADD`
7
+ * Drop support for Ruby 1.9.3/JRuby 1.7.x
8
+ * Fix ZREM to raise error when argument is an empty array
9
+
3
10
  ### 0.16.1
4
11
 
5
12
  * Relax `rake` gem dependency to allow 11.x.x versions
data/Gemfile CHANGED
@@ -4,10 +4,10 @@ source 'http://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  # Run all pre-commit hooks via Overcommit during CI runs
7
- gem 'overcommit', '0.32.0'
7
+ gem 'overcommit', '0.35.0'
8
8
 
9
9
  # Pin tool versions (which are executed by Overcommit) for Travis builds
10
- gem 'rubocop', '0.38.0'
10
+ gem 'rubocop', '0.42.0'
11
11
  gem 'travis', '~> 1.7'
12
12
 
13
13
  gem 'coveralls', require: false
data/README.md CHANGED
@@ -9,7 +9,7 @@ MockRedis provides the same interface as `redis-rb`, but it stores its
9
9
  data in memory instead of talking to a Redis server. It is intended
10
10
  for use in tests.
11
11
 
12
- The current implementation is tested against *Redis 2.8.17*. Older versions
12
+ The current implementation is tested against *Redis 3*. Older versions
13
13
  of Redis may return different results or not support some commands.
14
14
 
15
15
  ## Getting Started
@@ -93,9 +93,8 @@ please submit a pull request with your (tested!) implementation.
93
93
 
94
94
  ## Compatibility
95
95
 
96
- As of version `0.8.2`, Ruby 1.9.3 and above are supported. For
97
- older versions of Ruby, use `0.8.1` or older. JRuby 1.7.9 is also
98
- supported.
96
+ As of version `0.8.2`, Ruby 2.0.0 and above are supported. For
97
+ older versions of Ruby, use `0.8.1` or older.
99
98
 
100
99
  ## Running the Tests
101
100
 
@@ -36,7 +36,11 @@ class MockRedis
36
36
  TransactionWrapper.new(
37
37
  ExpireWrapper.new(
38
38
  MultiDbWrapper.new(
39
- Database.new(self, *args)))))
39
+ Database.new(self, *args)
40
+ )
41
+ )
42
+ )
43
+ )
40
44
  end
41
45
 
42
46
  def id
@@ -291,7 +291,8 @@ class MockRedis
291
291
  "^#{pattern}$".
292
292
  gsub(/([+|()])/, '\\\\\1').
293
293
  gsub(/([^\\])\?/, '\\1.').
294
- gsub(/([^\\])\*/, '\\1.*'))
294
+ gsub(/([^\\])\*/, '\\1.*')
295
+ )
295
296
  end
296
297
 
297
298
  def remove_expiration(key)
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the gem version.
4
4
  class MockRedis
5
- VERSION = '0.16.1'.freeze
5
+ VERSION = '0.17.0'.freeze
6
6
  end
@@ -8,27 +8,84 @@ class MockRedis
8
8
  include UtilityMethods
9
9
 
10
10
  def zadd(key, *args)
11
- if args.size == 1 && args[0].is_a?(Array)
12
- args = args.first
13
- assert_has_args(args, 'zadd')
11
+ zadd_options = {}
12
+ zadd_options = args.pop if args.last.is_a?(Hash)
14
13
 
15
- args = args.each_slice(2).to_a unless args.first.is_a?(Array)
16
- retval = args.map(&:last).map { |member| !!zscore(key, member.to_s) }.count(false)
17
- with_zset_at(key) do |z|
18
- args.each { |score, member| z.add(score, member.to_s) }
19
- end
14
+ if zadd_options && zadd_options.include?(:nx) && zadd_options.include?(:xx)
15
+ raise Redis::CommandError, 'ERR XX and NX options at the same time are not compatible'
16
+ end
17
+
18
+ if args.size == 1 && args[0].is_a?(Array)
19
+ zadd_multiple_members(key, args.first, zadd_options)
20
20
  elsif args.size == 2
21
21
  score, member = args
22
- assert_scorey(score) unless score =~ /(\+|\-)inf/
23
- retval = !zscore(key, member)
24
- with_zset_at(key) { |z| z.add(score, member.to_s) }
22
+ zadd_one_member(key, score, member, zadd_options)
25
23
  else
26
24
  raise Redis::CommandError, 'ERR wrong number of arguments'
27
25
  end
26
+ end
28
27
 
29
- retval
28
+ def zadd_one_member(key, score, member, zadd_options = {})
29
+ assert_scorey(score) unless score =~ /(\+|\-)inf/
30
+
31
+ with_zset_at(key) do |zset|
32
+ if zadd_options[:incr]
33
+ if zadd_options[:xx]
34
+ member_present = zset.include?(member)
35
+ return member_present ? zincrby(key, score, member) : nil
36
+ end
37
+
38
+ if zadd_options[:nx]
39
+ member_present = zset.include?(member)
40
+ return member_present ? nil : zincrby(key, score, member)
41
+ end
42
+
43
+ zincrby(key, score, member)
44
+ elsif zadd_options[:xx]
45
+ zset.add(score, member.to_s) if zset.include?(member)
46
+ false
47
+ elsif zadd_options[:nx]
48
+ !zset.include?(member) && !!zset.add(score, member.to_s)
49
+ else
50
+ retval = !zscore(key, member)
51
+ zset.add(score, member.to_s)
52
+ retval
53
+ end
54
+ end
30
55
  end
31
56
 
57
+ private :zadd_one_member
58
+
59
+ def zadd_multiple_members(key, args, zadd_options = {})
60
+ assert_has_args(args, 'zadd')
61
+
62
+ args = args.each_slice(2).to_a unless args.first.is_a?(Array)
63
+ with_zset_at(key) do |zset|
64
+ if zadd_options[:incr]
65
+ raise Redis::CommandError, 'ERR INCR option supports a single increment-element pair'
66
+ elsif zadd_options[:xx]
67
+ args.each { |score, member| zset.include?(member) && zset.add(score, member.to_s) }
68
+ 0
69
+ elsif zadd_options[:nx]
70
+ args.reduce(0) do |retval, (score, member)|
71
+ unless zset.include?(member)
72
+ zset.add(score, member.to_s)
73
+ retval += 1
74
+ end
75
+ retval
76
+ end
77
+ else
78
+ args.reduce(0) do |retval, (score, member)|
79
+ retval += 1 unless zset.include?(member)
80
+ zset.add(score, member.to_s)
81
+ retval
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ private :zadd_multiple_members
88
+
32
89
  def zcard(key)
33
90
  with_zset_at(key, &:size)
34
91
  end
@@ -83,9 +140,13 @@ class MockRedis
83
140
  retval = with_zset_at(key) { |z| !!z.delete?(args.first.to_s) }
84
141
  else
85
142
  args = args.first
86
- retval = args.map { |member| !!zscore(key, member.to_s) }.count(true)
87
- with_zset_at(key) do |z|
88
- args.each { |member| z.delete?(member) }
143
+ if args.empty?
144
+ raise Redis::CommandError, "ERR wrong number of arguments for 'zrem' command"
145
+ else
146
+ retval = args.map { |member| !!zscore(key, member.to_s) }.count(true)
147
+ with_zset_at(key) do |z|
148
+ args.each { |member| z.delete?(member) }
149
+ end
89
150
  end
90
151
  end
91
152
 
@@ -119,8 +180,10 @@ class MockRedis
119
180
  to_response(
120
181
  apply_limit(
121
182
  zset.in_range(min, max).reverse,
122
- options[:limit]),
123
- options)
183
+ options[:limit]
184
+ ),
185
+ options
186
+ )
124
187
  end
125
188
  end
126
189
 
@@ -233,7 +296,7 @@ class MockRedis
233
296
  def assert_zsety(key)
234
297
  unless zsety?(key)
235
298
  raise Redis::CommandError,
236
- 'WRONGTYPE Operation against a key holding the wrong kind of value'
299
+ 'WRONGTYPE Operation against a key holding the wrong kind of value'
237
300
  end
238
301
  end
239
302
 
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
22
22
  s.require_paths = ['lib']
23
23
 
24
24
  s.add_development_dependency 'rake', '>= 10', '< 12'
25
- s.add_development_dependency 'redis', '~> 3.2.0'
25
+ s.add_development_dependency 'redis', '~> 3.3.0'
26
26
  s.add_development_dependency 'rspec', '~> 3.0'
27
27
  s.add_development_dependency 'rspec-its', '~> 1.0'
28
28
  end
@@ -8,7 +8,8 @@ describe '#del(key [, key, ...])' do
8
8
  @redises.del(
9
9
  'mock-redis-test:1',
10
10
  'mock-redis-test:2',
11
- 'mock-redis-test:other').should == 2
11
+ 'mock-redis-test:other'
12
+ ).should == 2
12
13
  end
13
14
 
14
15
  it 'actually removes the key' do
@@ -30,6 +30,82 @@ describe '#zadd(key, score, member)' do
30
30
  @redises.zscore(@key, 'foo').should == 2.0
31
31
  end
32
32
 
33
+ it 'with XX option command do nothing if element not exist' do
34
+ @redises.zadd(@key, 1, 'foo')
35
+ @redises.zadd(@key, 2, 'bar', xx: true)
36
+ @redises.zrange(@key, 0, -1).should_not include 'bar'
37
+ end
38
+
39
+ it 'with XX option command update index on exist element' do
40
+ @redises.zadd(@key, 1, 'foo')
41
+ @redises.zadd(@key, 2, 'foo', xx: true)
42
+ @redises.zscore(@key, 'foo').should == 2.0
43
+ end
44
+
45
+ it 'with XX option and multiple elements command update index on exist element' do
46
+ @redises.zadd(@key, 1, 'foo')
47
+ added_count = @redises.zadd(@key, [[2, 'foo'], [2, 'bar']], xx: true)
48
+ added_count.should == 0
49
+
50
+ @redises.zscore(@key, 'foo').should == 2.0
51
+ @redises.zrange(@key, 0, -1).should_not include 'bar'
52
+ end
53
+
54
+ it "with NX option don't update current element" do
55
+ @redises.zadd(@key, 1, 'foo')
56
+ @redises.zadd(@key, 2, 'foo', nx: true)
57
+ @redises.zscore(@key, 'foo').should == 1.0
58
+ end
59
+
60
+ it 'with NX option create new element' do
61
+ @redises.zadd(@key, 1, 'foo')
62
+ @redises.zadd(@key, 2, 'bar', nx: true)
63
+ @redises.zrange(@key, 0, -1).should include 'bar'
64
+ end
65
+
66
+ it 'with NX option and multiple elements command only create element' do
67
+ @redises.zadd(@key, 1, 'foo')
68
+ added_count = @redises.zadd(@key, [[2, 'foo'], [2, 'bar']], nx: true)
69
+ added_count.should == 1
70
+ @redises.zscore(@key, 'bar').should == 2.0
71
+ @redises.zrange(@key, 0, -1).should eq %w[foo bar]
72
+ end
73
+
74
+ it 'XX and NX options in same time raise error' do
75
+ lambda do
76
+ @redises.zadd(@key, 1, 'foo', nx: true, xx: true)
77
+ end.should raise_error(Redis::CommandError)
78
+ end
79
+
80
+ it 'with INCR is act like zincrby' do
81
+ @redises.zadd(@key, 10, 'bert', incr: true).should == 10.0
82
+ @redises.zadd(@key, 3, 'bert', incr: true).should == 13.0
83
+ end
84
+
85
+ it 'with INCR and XX not create element' do
86
+ @redises.zadd(@key, 10, 'bert', xx: true, incr: true).should be_nil
87
+ end
88
+
89
+ it 'with INCR and XX increase score for exist element' do
90
+ @redises.zadd(@key, 2, 'bert')
91
+ @redises.zadd(@key, 10, 'bert', xx: true, incr: true).should == 12.0
92
+ end
93
+
94
+ it 'with INCR and NX create element with score' do
95
+ @redises.zadd(@key, 11, 'bert', nx: true, incr: true).should == 11.0
96
+ end
97
+
98
+ it 'with INCR and NX not update element' do
99
+ @redises.zadd(@key, 1, 'bert')
100
+ @redises.zadd(@key, 10, 'bert', nx: true, incr: true).should be_nil
101
+ end
102
+
103
+ it 'with INCR with variable number of arguments raise error' do
104
+ lambda do
105
+ @redises.zadd(@key, [[1, 'one'], [2, 'two']], incr: true)
106
+ end.should raise_error(Redis::CommandError)
107
+ end
108
+
33
109
  it 'supports a variable number of arguments' do
34
110
  @redises.zadd(@key, [[1, 'one'], [2, 'two']])
35
111
  @redises.zadd(@key, [[3, 'three']])
@@ -33,5 +33,11 @@ describe '#zrem(key, member)' do
33
33
  @redises.zrange(@key, 0, -1).should be_empty
34
34
  end
35
35
 
36
+ it 'raises an error if member is an empty array' do
37
+ lambda do
38
+ @redises.zrem(@key, [])
39
+ end.should raise_error(Redis::CommandError)
40
+ end
41
+
36
42
  it_should_behave_like 'a zset-only command'
37
43
  end
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.16.1
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brigade Engineering
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-03-18 00:00:00.000000000 Z
12
+ date: 2016-08-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -37,14 +37,14 @@ dependencies:
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 3.2.0
40
+ version: 3.3.0
41
41
  type: :development
42
42
  prerelease: false
43
43
  version_requirements: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 3.2.0
47
+ version: 3.3.0
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rspec
50
50
  requirement: !ruby/object:Gem::Requirement
@@ -281,7 +281,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
281
281
  version: '0'
282
282
  requirements: []
283
283
  rubyforge_project:
284
- rubygems_version: 2.4.5.1
284
+ rubygems_version: 2.5.1
285
285
  signing_key:
286
286
  specification_version: 4
287
287
  summary: Redis mock that just lives in memory; useful for testing.