mock_redis 0.6.5 → 0.6.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ ### 0.6.6
2
+ * Avoid mutation of @data from external reference
3
+ * Fix sorted set (e.g. `zadd`) with multiple score/member pairs
4
+
1
5
  ### 0.6.5
2
6
  * Fix `zrevrange` to return an empty array on invalid range
3
7
  * Fix `srandmember` spec on redis-rb 3.0.3
@@ -9,12 +9,16 @@ class MockRedis
9
9
  data_key_ref = data[key]
10
10
  ret = yield data[key]
11
11
  data[key] = data_key_ref if data[key].nil?
12
- ret
12
+ primitive?(ret) ? ret.dup : ret
13
13
  ensure
14
14
  clean_up_empties_at(key)
15
15
  end
16
16
  end
17
17
 
18
+ def primitive?(value)
19
+ value.kind_of?(::Array) || value.kind_of?(::Hash) || value.kind_of?(::String)
20
+ end
21
+
18
22
  def clean_up_empties_at(key)
19
23
  if data[key] && data[key].empty?
20
24
  del(key)
@@ -1,3 +1,3 @@
1
1
  class MockRedis
2
- VERSION = '0.6.5'
2
+ VERSION = '0.6.6'
3
3
  end
@@ -8,30 +8,20 @@ class MockRedis
8
8
  include UtilityMethods
9
9
 
10
10
  def zadd(key, *args)
11
- if !args.first.is_a?(Array)
12
- if args.size < 2
13
- raise Redis::CommandError, "ERR wrong number of arguments for 'zadd' command"
14
- elsif args.size.odd?
15
- raise Redis::CommandError, "ERR syntax error"
16
- end
17
- else
18
- unless args.all? {|pair| pair.size == 2 }
19
- raise(Redis::CommandError, "ERR syntax error")
20
- end
21
- end
22
-
23
- if args.size == 2
24
- score, member = args
25
- assert_scorey(score)
26
- retval = !zscore(key, member)
27
- with_zset_at(key) {|z| z.add(score, member.to_s)}
28
- else
11
+ if args.size == 1 && args[0].is_a?(Array)
29
12
  args = args.first
30
13
  args = args.each_slice(2).to_a unless args.first.is_a?(Array)
31
14
  retval = args.map(&:last).map { |member| !!zscore(key, member.to_s) }.count(false)
32
15
  with_zset_at(key) do |z|
33
16
  args.each { |score, member| z.add(score, member.to_s) }
34
17
  end
18
+ elsif args.size == 2
19
+ score, member = args
20
+ assert_scorey(score)
21
+ retval = !zscore(key, member)
22
+ with_zset_at(key) {|z| z.add(score, member.to_s)}
23
+ else
24
+ raise Redis::CommandError, "ERR wrong number of arguments"
35
25
  end
36
26
 
37
27
  retval
@@ -18,5 +18,15 @@ describe "#hgetall(key)" do
18
18
  @redises.hgetall('mock-redis-test:nonesuch').should == {}
19
19
  end
20
20
 
21
+ it "doesn't return a mutable reference to the returned data" do
22
+ mr = MockRedis.new
23
+ mr.hset(@key, 'k1', 'v1')
24
+ mr.hset(@key, 'k2', 'v2')
25
+ hash = mr.hgetall(@key)
26
+ hash['dont'] = 'mutate'
27
+ new_hash = mr.hgetall(@key)
28
+ new_hash.keys.sort.should == ['k1', 'k2']
29
+ end
30
+
21
31
  it_should_behave_like "a hash-only command"
22
32
  end
@@ -32,7 +32,8 @@ describe "#zadd(key, score, member)" do
32
32
 
33
33
  it "supports a variable number of arguments" do
34
34
  @redises.zadd(@key, [[1, 'one'], [2, 'two']])
35
- @redises.zrange(@key, 0, -1).should == ['one', 'two']
35
+ @redises.zadd(@key, [[3, 'three']])
36
+ @redises.zrange(@key, 0, -1).should == ['one', 'two', 'three']
36
37
  end
37
38
 
38
39
  it_should_behave_like "arg 1 is a score"
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.6.5
4
+ version: 0.6.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-03-13 00:00:00.000000000 Z
13
+ date: 2013-04-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake