mock_redis 0.24.0 → 0.25.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: 0d399a835c1f41a8eb0e5fc42a366e64fc399b5d76ebc23f0d40f73c9db4888e
4
- data.tar.gz: 1442920d2b12e46d2d45ff9d8854e4ccf3665b5a5f9a4aef6d29669c9110d023
3
+ metadata.gz: e4a2d7e6a155059cb97120f7f95c1e992a87a3933be5fe337a443dc0a9ac30df
4
+ data.tar.gz: 135172a723e9d5bfb058eec85863387093d56e3a6d7729f28da425f2b239e4bb
5
5
  SHA512:
6
- metadata.gz: 62b218977c75b5c7ffc80e3762bf0f7791c8f17dfe4f875a8874118583124736ab782e1c985dc92363d62e4366170306d9d0211b19cead2982f16cbff2c13557
7
- data.tar.gz: 97b5eeb1c20e597d2ce35e9d109f62aaf3a2271f42c86515be1d7e0d627ad628173aee7d74062b119cf954538116f0b199d06eee5d1fe71b552dea543859a69e
6
+ metadata.gz: 1e1b5cba39d01b11b2c85442c5d9485285bf661316e5196448484bc78dd9d866c3829648cd547ac0996d454f113d054c3bc43728f917740870f5fe191c97b15c
7
+ data.tar.gz: bf5d20de1111be19c03cacb2639f303d0ced578656bfc5b4dae4e8cd6e28991be003e7a20702ab6825526fbee6f91ebcd676fbb27c9010792abd37a2e7eab218
@@ -1,5 +1,11 @@
1
1
  # MockRedis Changelog
2
2
 
3
+ ### 0.25.0
4
+
5
+ * Add support for `xread` command ([#190](https://github.com/sds/mock_redis/pull/190))
6
+ * Fix `mget` to raise error when passing empty array ([#191](https://github.com/sds/mock_redis/pull/191))
7
+ * Fix `xadd` when `maxlen` is zero ([#192](https://github.com/sds/mock_redis/pull/192))
8
+
3
9
  ### 0.24.0
4
10
 
5
11
  * Fix handling of blocks within `multi` blocks ([#185](https://github.com/sds/mock_redis/pull/185))
@@ -23,14 +23,29 @@ class MockRedis
23
23
 
24
24
  def add(id, values)
25
25
  @last_id = MockRedis::Stream::Id.new(id, min: @last_id)
26
+ if @last_id.to_s == '0-0'
27
+ raise Redis::CommandError,
28
+ 'ERR The ID specified in XADD is equal or smaller than ' \
29
+ 'the target stream top item'
30
+ # TOOD: Redis version 6.0.4, w redis 4.2.1 generates the following error message:
31
+ # 'ERR The ID specified in XADD must be greater than 0-0'
32
+ end
26
33
  members.add [@last_id, Hash[values.map { |k, v| [k.to_s, v.to_s] }]]
27
34
  @last_id.to_s
28
35
  end
29
36
 
30
37
  def trim(count)
31
38
  deleted = @members.size - count
32
- @members = @members.to_a[-count..-1].to_set
33
- deleted
39
+ if deleted > 0
40
+ @members = if count == 0
41
+ Set.new
42
+ else
43
+ @members.to_a[-count..-1].to_set
44
+ end
45
+ deleted
46
+ else
47
+ 0
48
+ end
34
49
  end
35
50
 
36
51
  def range(start, finish, reversed, *opts_in)
@@ -45,6 +60,11 @@ class MockRedis
45
60
  items
46
61
  end
47
62
 
63
+ def read(id)
64
+ stream_id = MockRedis::Stream::Id.new(id)
65
+ members.select { |m| (stream_id < m[0]) }.map { |m| [m[0].to_s, m[1]] }
66
+ end
67
+
48
68
  def each
49
69
  members.each { |m| yield m }
50
70
  end
@@ -31,13 +31,6 @@ class MockRedis
31
31
  @timestamp = id
32
32
  end
33
33
  @sequence = @sequence.nil? ? sequence : @sequence.to_i
34
- if @timestamp == 0 && @sequence == 0
35
- raise Redis::CommandError,
36
- 'ERR The ID specified in XADD is equal or smaller than ' \
37
- 'the target stream top item'
38
- # TOOD: Redis version 6.0.4, w redis 4.2.1 generates the following error message:
39
- # 'ERR The ID specified in XADD must be greater than 0-0'
40
- end
41
34
  if self <= min
42
35
  raise Redis::CommandError,
43
36
  'ERR The ID specified in XADD is equal or smaller than ' \
@@ -4,7 +4,6 @@ require 'mock_redis/stream'
4
4
 
5
5
  # TODO: Implement the following commands
6
6
  #
7
- # * xread
8
7
  # * xgroup
9
8
  # * xreadgroup
10
9
  # * xack
@@ -67,6 +66,20 @@ class MockRedis
67
66
  end
68
67
  end
69
68
 
69
+ # TODO: Implement count and block parameters
70
+ def xread(keys, ids)
71
+ result = {}
72
+ keys = keys.is_a?(Array) ? keys : [keys]
73
+ ids = ids.is_a?(Array) ? ids : [ids]
74
+ keys.each_with_index do |key, index|
75
+ with_stream_at(key) do |stream|
76
+ data = stream.read(ids[index])
77
+ result[key] = data unless data.empty?
78
+ end
79
+ end
80
+ result
81
+ end
82
+
70
83
  private
71
84
 
72
85
  def with_stream_at(key, &blk)
@@ -154,9 +154,10 @@ class MockRedis
154
154
  end
155
155
 
156
156
  def mget(*keys)
157
+ keys.flatten!
158
+
157
159
  assert_has_args(keys, 'mget')
158
160
 
159
- keys.flatten!
160
161
  keys.map do |key|
161
162
  get(key) if stringy?(key)
162
163
  end
@@ -18,7 +18,7 @@ class MockRedis
18
18
  end
19
19
 
20
20
  def clean_up_empties_at(key)
21
- if data[key]&.empty? && data[key] != ''
21
+ if data[key]&.empty? && data[key] != '' && !data[key].is_a?(Stream)
22
22
  del(key)
23
23
  end
24
24
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the gem version.
4
4
  class MockRedis
5
- VERSION = '0.24.0'
5
+ VERSION = '0.25.0'
6
6
  end
@@ -1,6 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe '#del(key [, key, ...])' do
4
+ before :all do
5
+ sleep 1 - (Time.now.to_f % 1)
6
+ end
7
+
8
+ before :each do
9
+ @redises._gsub(/\d{3}-\d/, '...-.')
10
+ end
11
+
4
12
  it 'returns the number of keys deleted' do
5
13
  @redises.set('mock-redis-test:1', 1)
6
14
  @redises.set('mock-redis-test:2', 1)
@@ -32,4 +40,11 @@ describe '#del(key [, key, ...])' do
32
40
  it 'raises an error if an empty array is given' do
33
41
  expect { @redises.del [] }.to raise_error Redis::CommandError
34
42
  end
43
+
44
+ it 'removes a stream key' do
45
+ @redises.xadd('mock-redis-stream', { key: 'value' }, maxlen: 0)
46
+ expect(@redises.exists?('mock-redis-stream')).to eq true
47
+ @redises.del('mock-redis-stream')
48
+ expect(@redises.exists?('mock-redis-stream')).to eq false
49
+ end
35
50
  end
@@ -49,5 +49,11 @@ describe '#mget(key [, key, ...])' do
49
49
  @redises.mget
50
50
  end.should raise_error(Redis::CommandError)
51
51
  end
52
+
53
+ it 'raises an error if you pass it empty array' do
54
+ lambda do
55
+ @redises.mget([])
56
+ end.should raise_error(Redis::CommandError)
57
+ end
52
58
  end
53
59
  end
@@ -101,4 +101,22 @@ describe '#xadd("mystream", { f1: "v1", f2: "v2" }, id: "0-0", maxlen: 1000, app
101
101
  ]
102
102
  )
103
103
  end
104
+
105
+ it 'supports a maxlen greater than the current size' do
106
+ @redises.xadd(@key, { key1: 'value1' }, id: '1234567891234-0')
107
+ @redises.xadd(@key, { key2: 'value2' }, id: '1234567891245-0', maxlen: 1000)
108
+ expect(@redises.xrange(@key, '-', '+')).to eq(
109
+ [
110
+ ['1234567891234-0', { 'key1' => 'value1' }],
111
+ ['1234567891245-0', { 'key2' => 'value2' }],
112
+ ]
113
+ )
114
+ end
115
+
116
+ it 'creates an empty stream with maxlen of 0' do
117
+ @redises.xadd(@key, { key: 'value' }, maxlen: 0)
118
+ expect(@redises.xlen(@key)).to eq 0
119
+ expect(@redises.xrange(@key, '-', '+')).to eq([])
120
+ expect(@redises.exists?(@key)).to eq true
121
+ end
104
122
  end
@@ -54,6 +54,19 @@ describe '#xrange("mystream", first: "0-1", last: "0-3", count: 10)' do
54
54
  )
55
55
  end
56
56
 
57
+ it 'returns all entries with a lower limit of 0-0' do
58
+ expect(@redises.xrange(@key, '0-0', '+')).to eq(
59
+ [
60
+ ['1234567891234-0', { 'key1' => 'value1' }],
61
+ ['1234567891245-0', { 'key2' => 'value2' }],
62
+ ['1234567891245-1', { 'key3' => 'value3' }],
63
+ ['1234567891278-0', { 'key4' => 'value4' }],
64
+ ['1234567891278-1', { 'key5' => 'value5' }],
65
+ ['1234567891299-0', { 'key6' => 'value6' }]
66
+ ]
67
+ )
68
+ end
69
+
57
70
  it 'returns entries with an upper limit' do
58
71
  expect(@redises.xrange(@key, '-', '1234567891285-0')).to eq(
59
72
  [
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#xread(keys, ids)' do
4
+ before :all do
5
+ sleep 1 - (Time.now.to_f % 1)
6
+ @key = 'mock-redis-test:xread'
7
+ @key1 = 'mock-redis-test:xread1'
8
+ end
9
+
10
+ it 'reads a single entry' do
11
+ @redises.xadd(@key, { key: 'value' }, id: '1234567891234-0')
12
+ expect(@redises.xread(@key, '0-0'))
13
+ .to eq({ @key => [['1234567891234-0', { 'key' => 'value' }]] })
14
+ end
15
+
16
+ it 'reads multiple entries from the beginning of the stream' do
17
+ @redises.xadd(@key, { key0: 'value0' }, id: '1234567891234-0')
18
+ @redises.xadd(@key, { key1: 'value1' }, id: '1234567891234-1')
19
+ expect(@redises.xread(@key, '0-0'))
20
+ .to eq({ @key => [['1234567891234-0', { 'key0' => 'value0' }],
21
+ ['1234567891234-1', { 'key1' => 'value1' }]] })
22
+ end
23
+
24
+ it 'reads entries greater than the ID passed' do
25
+ @redises.xadd(@key, { key0: 'value0' }, id: '1234567891234-0')
26
+ @redises.xadd(@key, { key1: 'value1' }, id: '1234567891234-1')
27
+ expect(@redises.xread(@key, '1234567891234-0'))
28
+ .to eq({ @key => [['1234567891234-1', { 'key1' => 'value1' }]] })
29
+ end
30
+
31
+ it 'reads from multiple streams' do
32
+ @redises.xadd(@key, { key: 'value' }, id: '1234567891234-0')
33
+ @redises.xadd(@key1, { key1: 'value1' }, id: '1234567891234-0')
34
+ expect(@redises.xread([@key, @key1], %w[0-0 0-0]))
35
+ .to eq({ @key => [['1234567891234-0', { 'key' => 'value' }]],
36
+ @key1 => [['1234567891234-0', { 'key1' => 'value1' }]] })
37
+ end
38
+
39
+ it 'reads from multiple streams at the given IDs' do
40
+ @redises.xadd(@key, { key: 'value0' }, id: '1234567891234-0')
41
+ @redises.xadd(@key, { key: 'value1' }, id: '1234567891234-1')
42
+ @redises.xadd(@key, { key: 'value2' }, id: '1234567891234-2')
43
+ @redises.xadd(@key1, { key1: 'value0' }, id: '1234567891234-0')
44
+ @redises.xadd(@key1, { key1: 'value1' }, id: '1234567891234-1')
45
+ @redises.xadd(@key1, { key1: 'value2' }, id: '1234567891234-2')
46
+ # The first stream won't return anything since we specify the last ID
47
+ expect(@redises.xread([@key, @key1], %w[1234567891234-2 1234567891234-1]))
48
+ .to eq({ @key1 => [['1234567891234-2', { 'key1' => 'value2' }]] })
49
+ end
50
+ end
@@ -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(
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.24.0
4
+ version: 0.25.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: 2020-06-18 00:00:00.000000000 Z
12
+ date: 2020-06-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
@@ -241,6 +241,7 @@ files:
241
241
  - spec/commands/xadd_spec.rb
242
242
  - spec/commands/xlen_spec.rb
243
243
  - spec/commands/xrange_spec.rb
244
+ - spec/commands/xread_spec.rb
244
245
  - spec/commands/xrevrange_spec.rb
245
246
  - spec/commands/xtrim_spec.rb
246
247
  - spec/commands/zadd_spec.rb
@@ -425,6 +426,7 @@ test_files:
425
426
  - spec/commands/xadd_spec.rb
426
427
  - spec/commands/xlen_spec.rb
427
428
  - spec/commands/xrange_spec.rb
429
+ - spec/commands/xread_spec.rb
428
430
  - spec/commands/xrevrange_spec.rb
429
431
  - spec/commands/xtrim_spec.rb
430
432
  - spec/commands/zadd_spec.rb