mock_redis 0.20.0 → 0.21.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
  SHA256:
3
- metadata.gz: 0e3dccfea232f8d47d28dc16362a79232cd69a2a12e6d8127a43c83c03287ef4
4
- data.tar.gz: a2918811810246e6ddf92643e88121550310a7628f396aba12ed163b61c979a5
3
+ metadata.gz: 4443d90955b31a149afd0c66972a06993bf9b01f6bd826276c2d60edb49b3141
4
+ data.tar.gz: 2d096c0b7438b81720adcd50ecb36a8fa19df7a05c822ed4beee6d06d459f1b4
5
5
  SHA512:
6
- metadata.gz: e8233e6e66afbaa2f7801e33f7b16a7bbbb2eb4cb3caab73a0c19e94857542fe49a96a9c45e8e995b54bf71d50c90ef78cfb5e293e85d91a3ade26f0e3a3e4d8
7
- data.tar.gz: e30d5a2656ef3fb793cb9a0a2df9e1e24cd78aaa83a6cf5a3483353075f2a475379e294fc6b9c5905229db1a453d16cc7fa02095aed3ad59799312ff5d76ced5
6
+ metadata.gz: bdcb30c9756e77fcc4f857f0bdaa01d73faa28b8ea7cc37aa669c0f5cf2e2967d3e216a32ff50c7c78f6945b313f8525073060daf9f980187d566202f3dbcda2
7
+ data.tar.gz: 17502b628f1a5a62fa2046b529ef0178d8b84453d3a8b83441e3f2eef72282d45a79e60bee34e9a871ea553deb08c1c286b915d4b2c6ffe52fab3e6e32a6cc56
@@ -1,5 +1,13 @@
1
1
  # MockRedis Changelog
2
2
 
3
+ ### 0.21.0
4
+
5
+ * Fix behavior of `time` to return array of two integers ([#161](https://github.com/sds/mock_redis/pull/161))
6
+ * Add support for `close` and `disconnect!` ([#163](https://github.com/sds/mock_redis/pull/163))
7
+ * Fix `set` to properly handle (and ignore) other options ([#164](https://github.com/sds/mock_redis/pull/163))
8
+ * Fix `srem` to allow array of integers as argument ([#166](https://github.com/sds/mock_redis/pull/166))
9
+ * Fix `hdel` to allow array as argument ([#168](https://github.com/sds/mock_redis/pull/168))
10
+
3
11
  ### 0.20.0
4
12
 
5
13
  * Add support for `count` parameter of `spop`
@@ -65,7 +65,9 @@ class MockRedis
65
65
  end
66
66
 
67
67
  def now
68
- options[:time_class].now
68
+ current_time = options[:time_class].now
69
+ miliseconds = (current_time.to_r - current_time.to_i) * 1_000
70
+ [current_time.to_i, miliseconds.to_i]
69
71
  end
70
72
  alias time now
71
73
 
@@ -56,6 +56,8 @@ class MockRedis
56
56
  def disconnect
57
57
  nil
58
58
  end
59
+ alias close disconnect
60
+ alias disconnect! close
59
61
 
60
62
  def connected?
61
63
  true
@@ -86,7 +88,8 @@ class MockRedis
86
88
  end
87
89
 
88
90
  def pexpire(key, ms)
89
- now_ms = (@base.now.to_r * 1000).to_i
91
+ now, miliseconds = @base.now
92
+ now_ms = (now * 1000) + miliseconds
90
93
  pexpireat(key, now_ms + ms.to_i)
91
94
  end
92
95
 
@@ -140,7 +143,7 @@ class MockRedis
140
143
  end
141
144
 
142
145
  def lastsave
143
- @base.now.to_i
146
+ @base.now.first
144
147
  end
145
148
 
146
149
  def persist(key)
@@ -201,17 +204,21 @@ class MockRedis
201
204
  if !exists(key)
202
205
  -2
203
206
  elsif has_expiration?(key)
204
- expiration(key).to_i - @base.now.to_i
207
+ now, = @base.now
208
+ expiration(key).to_i - now
205
209
  else
206
210
  -1
207
211
  end
208
212
  end
209
213
 
210
214
  def pttl(key)
215
+ now, miliseconds = @base.now
216
+ now_ms = now * 1000 + miliseconds
217
+
211
218
  if !exists(key)
212
219
  -2
213
220
  elsif has_expiration?(key)
214
- (expiration(key).to_r * 1000).to_i - (@base.now.to_r * 1000).to_i
221
+ (expiration(key).to_r * 1000).to_i - now_ms
215
222
  else
216
223
  -1
217
224
  end
@@ -321,10 +328,11 @@ class MockRedis
321
328
  # This method isn't private, but it also isn't a Redis command, so
322
329
  # it doesn't belong up above with all the Redis commands.
323
330
  def expire_keys
324
- now = @base.now
331
+ now, miliseconds = @base.now
332
+ now_ms = now * 1_000 + miliseconds
325
333
 
326
334
  to_delete = expire_times.take_while do |(time, _key)|
327
- (time.to_r * 1_000).to_i <= (now.to_r * 1_000).to_i
335
+ (time.to_r * 1_000).to_i <= now_ms
328
336
  end
329
337
 
330
338
  to_delete.each do |(_time, key)|
@@ -8,14 +8,10 @@ class MockRedis
8
8
 
9
9
  def hdel(key, *fields)
10
10
  with_hash_at(key) do |hash|
11
- if fields.is_a?(Array)
12
- orig_size = hash.size
13
- fields = fields.map(&:to_s)
14
- hash.delete_if { |k, _v| fields.include?(k) }
15
- orig_size - hash.size
16
- else
17
- hash.delete(fields[0].to_s) ? 1 : 0
18
- end
11
+ orig_size = hash.size
12
+ fields = Array(fields).flatten.map(&:to_s)
13
+ hash.delete_if { |k, _v| fields.include?(k) }
14
+ orig_size - hash.size
19
15
  end
20
16
  end
21
17
 
@@ -117,6 +117,7 @@ class MockRedis
117
117
  with_set_at(key) do |s|
118
118
  if members.is_a?(Array)
119
119
  orig_size = s.size
120
+ members = members.map(&:to_s)
120
121
  s.delete_if { |m| members.include?(m) }
121
122
  orig_size - s.size
122
123
  else
@@ -220,15 +220,22 @@ class MockRedis
220
220
  end
221
221
  data[key] = value.to_s
222
222
 
223
- # take latter
224
- expire_option = options.to_a.last
225
- if expire_option
226
- type, duration = expire_option
223
+ duration = options.delete(:ex)
224
+ if duration
227
225
  if duration == 0
228
226
  raise Redis::CommandError, 'ERR invalid expire time in set'
229
227
  end
230
- expire(key, type.to_sym == :ex ? duration : duration / 1000.0)
228
+ expire(key, duration)
231
229
  end
230
+
231
+ duration = options.delete(:px)
232
+ if duration
233
+ if duration == 0
234
+ raise Redis::CommandError, 'ERR invalid expire time in set'
235
+ end
236
+ expire(key, duration / 1000.0)
237
+ end
238
+
232
239
  return_true ? true : 'OK'
233
240
  end
234
241
 
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the gem version.
4
4
  class MockRedis
5
- VERSION = '0.20.0'
5
+ VERSION = '0.21.0'
6
6
  end
@@ -14,4 +14,16 @@ describe 'client' do
14
14
  redis.connect.should == redis
15
15
  end
16
16
  end
17
+
18
+ context '#disconnect!' do
19
+ it 'responds to disconnect!' do
20
+ expect(MockRedis.new).to respond_to(:disconnect!)
21
+ end
22
+ end
23
+
24
+ context '#close' do
25
+ it 'responds to close' do
26
+ expect(MockRedis.new).to respond_to(:close)
27
+ end
28
+ end
17
29
  end
@@ -50,5 +50,21 @@ describe '#hdel(key, field)' do
50
50
  @redises.hget(@key, field).should be_nil
51
51
  end
52
52
 
53
+ it 'supports a variable number of fields as array' do
54
+ @redises.hdel(@key, %w[k1 k2]).should == 2
55
+
56
+ @redises.hget(@key, 'k1').should be_nil
57
+ @redises.hget(@key, 'k2').should be_nil
58
+ @redises.get(@key).should be_nil
59
+ end
60
+
61
+ it 'supports a list of fields in various way' do
62
+ @redises.hdel(@key, ['k1'], 'k2').should == 2
63
+
64
+ @redises.hget(@key, 'k1').should be_nil
65
+ @redises.hget(@key, 'k2').should be_nil
66
+ @redises.get(@key).should be_nil
67
+ end
68
+
53
69
  it_should_behave_like 'a hash-only command'
54
70
  end
@@ -33,6 +33,12 @@ describe '#set(key, value)' do
33
33
  @redises.set(key, 1, xx: true).should == true
34
34
  end
35
35
 
36
+ it 'ignores other options' do
37
+ key = 'mock-redis-test'
38
+ @redises.del(key)
39
+ @redises.set(key, 1, logger: :something).should == 'OK'
40
+ end
41
+
36
42
  context '[mock only]' do
37
43
  before(:all) do
38
44
  @mock = @redises.mock
@@ -36,5 +36,10 @@ describe '#srem(key, member)' do
36
36
  @redises.get(@key).should be_nil
37
37
  end
38
38
 
39
+ it 'allow passing an array of integers as argument' do
40
+ @redises.sadd(@key, %w[1 2])
41
+ @redises.srem(@key, [1, 2]).should == 2
42
+ end
43
+
39
44
  it_should_behave_like 'a set-only command'
40
45
  end
@@ -49,23 +49,21 @@ describe MockRedis do
49
49
  end
50
50
 
51
51
  describe '.now' do
52
- let(:now) { 'Now' }
53
- let(:time_stub) { double 'Time', :now => now }
52
+ let(:time_stub) { double 'Time', :now => Time.new(2019, 1, 2, 3, 4, 6, '+00:00') }
54
53
  let(:options) { { :time_class => time_stub } }
55
54
 
56
55
  subject { MockRedis.new(options) }
57
56
 
58
- its(:now) { should == now }
57
+ its(:now) { should == [1_546_398_246, 0] }
59
58
  end
60
59
 
61
60
  describe '.time' do
62
- let(:now) { 'Now' }
63
- let(:time_stub) { double 'Time', :now => now }
61
+ let(:time_stub) { double 'Time', :now => Time.new(2019, 1, 2, 3, 4, 6, '+00:00') }
64
62
  let(:options) { { :time_class => time_stub } }
65
63
 
66
64
  subject { MockRedis.new(options) }
67
65
 
68
- its(:time) { should == now }
66
+ its(:time) { should == [1_546_398_246, 0] }
69
67
  end
70
68
 
71
69
  describe '.expireat' 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.20.0
4
+ version: 0.21.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-05-03 00:00:00.000000000 Z
12
+ date: 2019-06-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake