mock_redis 0.27.3 → 0.28.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: f61e830490badf3dffbb7dff2ba426f7a359bbc9023243de838e4698318e8aaa
4
- data.tar.gz: 21bc925aad3d1b8b67e799da8ea8015810a79f40f0d8962a08d32ca9cee9c9bc
3
+ metadata.gz: 9dc2b4dd866fe426a0ee5b767bc46668c1ccb515f0b927feeac129b47f28a4a6
4
+ data.tar.gz: cccf8ea1f98f9376afadcc57738eef1578bd07166b08bb5a11eb6c3410412f23
5
5
  SHA512:
6
- metadata.gz: 01c57f3b8db4fb7835a6cf83cbe67010f3a501945751b7f2c7138b5488f62b72734903592e9bf189b2e41d6c9c6caad4d33799c0158bf5707990cec853860961
7
- data.tar.gz: 352e3efacf4b47935dcb20f4a427833858fe2229a33568251dc6f670b40ba5a0db22e484f92d5467ff26bd95b42696971211d86b97513efb9d6e65cd21d71740
6
+ metadata.gz: f8827a5223d959f27d33223904f0cdf705dfa280da700733ea83cb581ddc89ca3e427af39ec247ea05ebb79ecb9fe58ba3e8f2f1b7fd62204aacc65025f010cf
7
+ data.tar.gz: e1e3115c5577f1e22249f3c6b9fb3b82ac1be4b8a4967824cbea5f56ce35efec15154d1f9871de5ba3abc337cbe6d38c9d93787150c8e3262ba30b395bcb58b4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # MockRedis Changelog
2
2
 
3
+ ### 0.28.0
4
+
5
+ * Fix `hmset` exception ([#206](https://github.com/sds/mock_redis/pull/206))
6
+ * Fix `hmset` to accept hashes in addition to key/value pairs ([#208](https://github.com/sds/mock_redis/pull/208))
7
+ * Fix stream ID regex to support `(` ([#209](https://github.com/sds/mock_redis/pull/209))
8
+ * Allow `mget` to accept a block ([#210](https://github.com/sds/mock_redis/pull/210))
9
+
3
10
  ### 0.27.3
4
11
 
5
12
  * Ensure `ruby2_keywords` dependency is `require`d at runtime
@@ -94,7 +94,7 @@ class MockRedis
94
94
  assert_has_args(kvpairs, 'hmset')
95
95
 
96
96
  if kvpairs.length.odd?
97
- raise Redis::CommandError, err_msg || 'ERR wrong number of arguments for HMSET'
97
+ raise Redis::CommandError, err_msg || 'ERR wrong number of arguments for \'hmset\' command'
98
98
  end
99
99
 
100
100
  kvpairs.each_slice(2) do |(k, v)|
@@ -131,6 +131,10 @@ class MockRedis
131
131
  def hset(key, *args)
132
132
  added = 0
133
133
  with_hash_at(key) do |hash|
134
+ if args.length == 1 && args[0].is_a?(Hash)
135
+ args = args[0].to_a.flatten
136
+ end
137
+
134
138
  args.each_slice(2) do |field, value|
135
139
  added += 1 unless hash.key?(field.to_s)
136
140
  hash[field.to_s] = value.to_s
@@ -49,9 +49,15 @@ class MockRedis
49
49
  opts = options opts_in, ['count']
50
50
  start_id = MockRedis::Stream::Id.new(start)
51
51
  finish_id = MockRedis::Stream::Id.new(finish, sequence: Float::INFINITY)
52
- items = members
53
- .select { |m| (start_id <= m[0]) && (finish_id >= m[0]) }
54
- .map { |m| [m[0].to_s, m[1]] }
52
+ if start_id.exclusive
53
+ items = members
54
+ .select { |m| (start_id < m[0]) && (finish_id >= m[0]) }
55
+ .map { |m| [m[0].to_s, m[1]] }
56
+ else
57
+ items = members
58
+ .select { |m| (start_id <= m[0]) && (finish_id >= m[0]) }
59
+ .map { |m| [m[0].to_s, m[1]] }
60
+ end
55
61
  items.reverse! if reversed
56
62
  return items.first(opts['count'].to_i) if opts.key?('count')
57
63
  items
@@ -3,9 +3,10 @@ class MockRedis
3
3
  class Id
4
4
  include Comparable
5
5
 
6
- attr_accessor :timestamp, :sequence
6
+ attr_accessor :timestamp, :sequence, :exclusive
7
7
 
8
8
  def initialize(id, min: nil, sequence: 0)
9
+ @exclusive = false
9
10
  case id
10
11
  when '*'
11
12
  @timestamp = (Time.now.to_f * 1000).to_i
@@ -20,8 +21,12 @@ class MockRedis
20
21
  @timestamp = @sequence = Float::INFINITY
21
22
  else
22
23
  if id.is_a? String
23
- (_, @timestamp, @sequence) = id.match(/^(\d+)-?(\d+)?$/)
24
- .to_a
24
+ # See https://redis.io/topics/streams-intro
25
+ # Ids are a unix timestamp in milliseconds followed by an
26
+ # optional dash sequence number, e.g. -0. They can also optionally
27
+ # be prefixed with '(' to change the XRANGE to exclusive.
28
+ (_, @timestamp, @sequence) = id.match(/^\(?(\d+)-?(\d+)?$/).to_a
29
+ @exclusive = true if id[0] == '('
25
30
  if @timestamp.nil?
26
31
  raise Redis::CommandError,
27
32
  'ERR Invalid stream ID specified as stream command argument'
@@ -154,14 +154,16 @@ class MockRedis
154
154
  new_value
155
155
  end
156
156
 
157
- def mget(*keys)
157
+ def mget(*keys, &blk)
158
158
  keys.flatten!
159
159
 
160
160
  assert_has_args(keys, 'mget')
161
161
 
162
- keys.map do |key|
162
+ data = keys.map do |key|
163
163
  get(key) if stringy?(key)
164
164
  end
165
+
166
+ blk ? blk.call(data) : data
165
167
  end
166
168
 
167
169
  def mapped_mget(*keys)
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the gem version.
4
4
  class MockRedis
5
- VERSION = '0.27.3'
5
+ VERSION = '0.28.0'
6
6
  end
@@ -34,5 +34,9 @@ describe '#hset(key, field)' do
34
34
  @redises.hget(@key, '1').should == 'one'
35
35
  end
36
36
 
37
+ it 'stores fields sent in a hash' do
38
+ @redises.hset(@key, {'k1' => 'v1', 'k2' => 'v2'}).should == 2
39
+ end
40
+
37
41
  it_should_behave_like 'a hash-only command'
38
42
  end
@@ -56,4 +56,10 @@ describe '#mget(key [, key, ...])' do
56
56
  end.should raise_error(Redis::CommandError)
57
57
  end
58
58
  end
59
+
60
+ context 'emulate block' do
61
+ it 'returns an array of values' do
62
+ @redises.mget(@key1, @key2) { |values| values.map(&:to_i) }.should == [1, 2]
63
+ end
64
+ end
59
65
  end
@@ -14,20 +14,20 @@ describe '#xadd("mystream", { f1: "v1", f2: "v2" }, id: "0-0", maxlen: 1000, app
14
14
 
15
15
  it 'returns an id based on the timestamp' do
16
16
  t = Time.now.to_i
17
- id = @redises.xadd(@key, key: 'value')
18
- expect(@redises.xadd(@key, key: 'value')).to match(/#{t}\d{3}-0/)
17
+ id = @redises.xadd(@key, { key: 'value' })
18
+ expect(@redises.xadd(@key, { key: 'value' })).to match(/#{t}\d{3}-0/)
19
19
  end
20
20
 
21
21
  it 'adds data with symbols' do
22
- @redises.xadd(@key, symbol_key: :symbol_value)
22
+ @redises.xadd(@key, { symbol_key: :symbol_value })
23
23
  expect(@redises.xrange(@key, '-', '+').last[1])
24
24
  .to eq('symbol_key' => 'symbol_value')
25
25
  end
26
26
 
27
27
  it 'increments the sequence number with the same timestamp' do
28
28
  Timecop.freeze do
29
- @redises.xadd(@key, key: 'value')
30
- expect(@redises.xadd(@key, key: 'value')).to match(/\d+-1/)
29
+ @redises.xadd(@key, { key: 'value' })
30
+ expect(@redises.xadd(@key, { key: 'value' })).to match(/\d+-1/)
31
31
  end
32
32
  end
33
33
 
@@ -67,7 +67,7 @@ describe '#xadd("mystream", { f1: "v1", f2: "v2" }, id: "0-0", maxlen: 1000, app
67
67
  it 'caters for the current time being before the last time' do
68
68
  t = (Time.now.to_f * 1000).to_i + 2000
69
69
  @redises.xadd(@key, { key: 'value' }, id: "#{t}-0")
70
- expect(@redises.xadd(@key, key: 'value')).to match(/#{t}-1/)
70
+ expect(@redises.xadd(@key, { key: 'value' })).to match(/#{t}-1/)
71
71
  end
72
72
 
73
73
  it 'appends a sequence number if it is missing' do
@@ -14,9 +14,9 @@ describe '#xlen(key)' do
14
14
 
15
15
  it 'returns the number of items in the stream' do
16
16
  expect(@redises.xlen(@key)).to eq 0
17
- @redises.xadd(@key, key: 'value')
17
+ @redises.xadd(@key, { key: 'value' })
18
18
  expect(@redises.xlen(@key)).to eq 1
19
- 3.times { @redises.xadd(@key, key: 'value') }
19
+ 3.times { @redises.xadd(@key, { key: 'value' }) }
20
20
  expect(@redises.xlen(@key)).to eq 4
21
21
  end
22
22
  end
@@ -79,13 +79,23 @@ describe '#xrange("mystream", first: "0-1", last: "0-3", count: 10)' do
79
79
  )
80
80
  end
81
81
 
82
- it 'returns entries with both a lower and an upper limit' do
83
- expect(@redises.xrange(@key, '1234567891239-0', '1234567891285-0')).to eq(
82
+ it 'returns entries with both a lower and an upper limit inclusive' do
83
+ expect(@redises.xrange(@key, '1234567891245-0', '1234567891278-0')).to eq(
84
84
  [
85
85
  ['1234567891245-0', { 'key2' => 'value2' }],
86
86
  ['1234567891245-1', { 'key3' => 'value3' }],
87
+ ['1234567891278-0', { 'key4' => 'value4' }]
88
+ ]
89
+ )
90
+ end
91
+
92
+ it 'returns entries with both a lower and an upper limit exclusive' do
93
+ expect(@redises.xrange(@key, '(1234567891245-0', '1234567891285-1')).to eq(
94
+ [
95
+ # We no longer get '1234567891245-0'
96
+ ['1234567891245-1', { 'key3' => 'value3' }],
87
97
  ['1234567891278-0', { 'key4' => 'value4' }],
88
- ['1234567891278-1', { 'key5' => 'value5' }]
98
+ ['1234567891278-1', { 'key5' => 'value5' }] # Note sequence -1
89
99
  ]
90
100
  )
91
101
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mock_redis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.27.3
4
+ version: 0.28.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane da Silva
8
8
  - Samuel Merritt
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-01-10 00:00:00.000000000 Z
12
+ date: 2021-05-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ruby2_keywords
@@ -295,7 +295,7 @@ homepage: https://github.com/sds/mock_redis
295
295
  licenses:
296
296
  - MIT
297
297
  metadata: {}
298
- post_install_message:
298
+ post_install_message:
299
299
  rdoc_options: []
300
300
  require_paths:
301
301
  - lib
@@ -311,7 +311,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
311
311
  version: '0'
312
312
  requirements: []
313
313
  rubygems_version: 3.1.4
314
- signing_key:
314
+ signing_key:
315
315
  specification_version: 4
316
316
  summary: Redis mock that just lives in memory; useful for testing.
317
317
  test_files: