mock_redis 0.5.3 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ### 0.5.4
2
+ * Support `incrbyfloat` (new in Redis 2.6)
3
+ * Fix specs to pass in Redis 2.6
4
+ * Deprecated spec suite on 2.4
5
+
1
6
  ### 0.5.3
2
7
  * Support `location` as an alias to `id` for `Sidekiq`'s benefit
3
8
 
@@ -372,6 +372,10 @@ class MockRedis
372
372
  value.nil? || looks_like_integer?(value)
373
373
  end
374
374
 
375
+ def can_incr_float?(value)
376
+ value.nil? || looks_like_float?(value)
377
+ end
378
+
375
379
  def extract_timeout(arglist)
376
380
  timeout = assert_valid_timeout(arglist.last)
377
381
  [arglist[0..-2], arglist.last]
@@ -97,6 +97,9 @@ class MockRedis
97
97
  end
98
98
 
99
99
  def lrem(key, count, value)
100
+ unless looks_like_integer?(count.to_s)
101
+ raise Redis::CommandError, "ERR value is not an integer or out of range"
102
+ end
100
103
  count = count.to_i
101
104
  value = value.to_s
102
105
 
@@ -75,6 +75,22 @@ class MockRedis
75
75
  new_value
76
76
  end
77
77
 
78
+ def incrbyfloat(key, n)
79
+ assert_stringy(key)
80
+ unless can_incr_float?(data[key])
81
+ raise Redis::CommandError, "ERR value is not a valid float"
82
+ end
83
+
84
+ unless looks_like_float?(n.to_s)
85
+ raise Redis::CommandError, "ERR value is not a valid float"
86
+ end
87
+
88
+ new_value = data[key].to_f + n.to_f
89
+ data[key] = new_value.to_s
90
+ # for some reason, redis-rb doesn't return this as a string.
91
+ new_value
92
+ end
93
+
78
94
  def mget(*keys)
79
95
  assert_has_args(keys, 'mget')
80
96
 
@@ -1,3 +1,3 @@
1
1
  class MockRedis
2
- VERSION = '0.5.3'
2
+ VERSION = '0.5.4'
3
3
  end
@@ -42,8 +42,8 @@ class MockRedis
42
42
  end
43
43
 
44
44
  def zcount(key, min, max)
45
- assert_scorey(min, 'min or max') unless min == '-inf'
46
- assert_scorey(max, 'min or max') unless max == '+inf'
45
+ assert_scorey(min, 'ERR min or max is not a float') unless min == '-inf'
46
+ assert_scorey(max, 'ERR min or max is not a float') unless max == '+inf'
47
47
 
48
48
  with_zset_at(key) do |zset|
49
49
  zset.in_range(min, max).size
@@ -230,9 +230,9 @@ class MockRedis
230
230
  !!Float(x) rescue false
231
231
  end
232
232
 
233
- def assert_scorey(value, what='value')
233
+ def assert_scorey(value, message = "ERR value is not a valid float")
234
234
  unless looks_like_float?(value)
235
- raise Redis::CommandError, "ERR #{what} is not a double"
235
+ raise Redis::CommandError, message
236
236
  end
237
237
  end
238
238
 
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#incrbyfloat(key, increment)' do
4
+ before { @key = 'mock-redis-test:65374' }
5
+
6
+ it "returns the value after the increment" do
7
+ @redises.set(@key, 2.0)
8
+ @redises.incrbyfloat(@key, 2.1).should be_within(0.0001).of(4.1)
9
+ end
10
+
11
+ it "treats a missing key like 0" do
12
+ @redises.incrbyfloat(@key, 1.2).should be_within(0.0001).of(1.2)
13
+ end
14
+
15
+ it "increments negative numbers" do
16
+ @redises.set(@key, -10.4)
17
+ @redises.incrbyfloat(@key, 2.3).should be_within(0.0001).of(-8.1)
18
+ end
19
+
20
+ it "works multiple times" do
21
+ @redises.incrbyfloat(@key, 2.1).should be_within(0.0001).of(2.1)
22
+ @redises.incrbyfloat(@key, 2.2).should be_within(0.0001).of(4.3)
23
+ @redises.incrbyfloat(@key, 2.3).should be_within(0.0001).of(6.6)
24
+ end
25
+
26
+ it "accepts an float-ish string" do
27
+ @redises.incrbyfloat(@key, "2.2").should be_within(0.0001).of(2.2)
28
+ end
29
+
30
+ it "raises an error if the value does not look like an float" do
31
+ @redises.set(@key, "one.two")
32
+ lambda do
33
+ @redises.incrbyfloat(@key, 1)
34
+ end.should raise_error(RuntimeError)
35
+ end
36
+
37
+ it "raises an error if the delta does not look like an float" do
38
+ lambda do
39
+ @redises.incrbyfloat(@key, "foobar.baz")
40
+ end.should raise_error(RuntimeError)
41
+ end
42
+
43
+ it_should_behave_like "a string-only command"
44
+ end
@@ -61,9 +61,10 @@ describe "#lrem(key, count, value)" do
61
61
  @redises.lrem(@key, 0, "beer").should == 0
62
62
  end
63
63
 
64
- it "treats non-integer count as 0" do
65
- @redises.lrem(@key, 'foo', 'bottles').should == 3
66
- @redises.lrange(@key, 0, -1).grep(/bottles/).should be_empty
64
+ it "raises an error if the value does not look like an integer" do
65
+ lambda do
66
+ @redises.lrem(@key, 'foo', 'bottles')
67
+ end.should raise_error(RuntimeError)
67
68
  end
68
69
 
69
70
  it "removes empty lists" do
@@ -69,6 +69,9 @@ class RedisMultiplexer < BlankSlate
69
69
  a.collect(&:to_s).sort == b.collect(&:to_s).sort
70
70
  elsif a.is_a?(Exception) && b.is_a?(Exception)
71
71
  a.class == b.class && a.message == b.message
72
+ elsif a.is_a?(Float) && b.is_a?(Float)
73
+ delta = [a.abs, b.abs].min / 1_000_000.0
74
+ (a - b).abs < delta
72
75
  else
73
76
  false
74
77
  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.5.3
4
+ version: 0.5.4
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: 2012-10-14 00:00:00.000000000 Z
13
+ date: 2012-10-24 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -131,6 +131,7 @@ files:
131
131
  - spec/commands/hvals_spec.rb
132
132
  - spec/commands/incr_spec.rb
133
133
  - spec/commands/incrby_spec.rb
134
+ - spec/commands/incrbyfloat_spec.rb
134
135
  - spec/commands/info_spec.rb
135
136
  - spec/commands/keys_spec.rb
136
137
  - spec/commands/lastsave_spec.rb
@@ -274,6 +275,7 @@ test_files:
274
275
  - spec/commands/hvals_spec.rb
275
276
  - spec/commands/incr_spec.rb
276
277
  - spec/commands/incrby_spec.rb
278
+ - spec/commands/incrbyfloat_spec.rb
277
279
  - spec/commands/info_spec.rb
278
280
  - spec/commands/keys_spec.rb
279
281
  - spec/commands/lastsave_spec.rb