mock_redis 0.28.0 → 0.31.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: 9dc2b4dd866fe426a0ee5b767bc46668c1ccb515f0b927feeac129b47f28a4a6
4
- data.tar.gz: cccf8ea1f98f9376afadcc57738eef1578bd07166b08bb5a11eb6c3410412f23
3
+ metadata.gz: 2870385004370b0471f3cf495184d8e11f138e0cb13b9bef9bd43ed7ce2b35ff
4
+ data.tar.gz: 75475cf000d2075ad2565440aea2521fa1034248726b5070ff149d9518c00c3a
5
5
  SHA512:
6
- metadata.gz: f8827a5223d959f27d33223904f0cdf705dfa280da700733ea83cb581ddc89ca3e427af39ec247ea05ebb79ecb9fe58ba3e8f2f1b7fd62204aacc65025f010cf
7
- data.tar.gz: e1e3115c5577f1e22249f3c6b9fb3b82ac1be4b8a4967824cbea5f56ce35efec15154d1f9871de5ba3abc337cbe6d38c9d93787150c8e3262ba30b395bcb58b4
6
+ metadata.gz: 6e6f75b19c46fd81ef553dfe1af1f32b2dceb13e4b660835cd58b1760fa8445c09c2ef26bf67cf64adcf87e196de24798333350e0a07ca0fac39939e296659e4
7
+ data.tar.gz: 77c2c5fc3f67f654d402161541b1527c6779859a0ec6e61a5fb6048b7abfcab50728ba2c25617a2adf303b22a23e662a8f2ee7236085db02682e0e8ca690438a
@@ -0,0 +1,31 @@
1
+ name: Lint
2
+ on:
3
+ push:
4
+ branches: [main]
5
+ pull_request:
6
+ branches: [main]
7
+
8
+ jobs:
9
+ overcommit:
10
+ timeout-minutes: 10
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+
16
+ - name: Set up Ruby
17
+ uses: ruby/setup-ruby@v1.97.0
18
+ with:
19
+ ruby-version: 2.7
20
+
21
+ - name: Install dependencies
22
+ run: bundle install
23
+
24
+ - name: Prepare environment
25
+ run: |
26
+ git config --local user.email "gh-actions@example.com"
27
+ git config --local user.name "GitHub Actions"
28
+ bundle exec overcommit --sign
29
+
30
+ - name: Run pre-commit checks
31
+ run: bundle exec overcommit --run
@@ -0,0 +1,63 @@
1
+ name: Tests
2
+ on:
3
+ push:
4
+ branches: [main]
5
+ pull_request:
6
+ branches: [main]
7
+
8
+ jobs:
9
+ rspec:
10
+ timeout-minutes: 10
11
+ runs-on: ubuntu-latest
12
+
13
+ strategy:
14
+ matrix:
15
+ ruby-version:
16
+ - '2.6'
17
+ - '2.7'
18
+ - '3.0'
19
+ redis-version:
20
+ - '6.2'
21
+
22
+ services:
23
+ redis:
24
+ image: redis:${{ matrix.redis-version }}-alpine
25
+ options: >-
26
+ --health-cmd "redis-cli ping"
27
+ --health-interval 10s
28
+ --health-timeout 5s
29
+ --health-retries 5
30
+ ports:
31
+ - 6379:6379
32
+
33
+ steps:
34
+ - uses: actions/checkout@v2
35
+
36
+ - name: Set up Ruby ${{ matrix.ruby-version }}
37
+ uses: ruby/setup-ruby@v1.97.0
38
+ with:
39
+ ruby-version: ${{ matrix.ruby-version }}
40
+
41
+ - name: Install dependencies
42
+ run: bundle install
43
+
44
+ - name: Run tests
45
+ run: bundle exec rspec
46
+
47
+ - name: Code coverage reporting
48
+ uses: coverallsapp/github-action@master
49
+ with:
50
+ github-token: ${{ secrets.github_token }}
51
+ flag-name: ruby${{ matrix.ruby-version }}-${{ matrix.redis-version }}
52
+ parallel: true
53
+
54
+ finish:
55
+ needs: rspec
56
+ runs-on: ubuntu-latest
57
+
58
+ steps:
59
+ - name: Finalize code coverage report
60
+ uses: coverallsapp/github-action@master
61
+ with:
62
+ github-token: ${{ secrets.github_token }}
63
+ parallel-finished: true
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # MockRedis Changelog
2
2
 
3
+ ### 0.31.0
4
+
5
+ * Allow `ping` to take argument
6
+ * Raise `CommandError` on `hmget` with empty list of fields
7
+
8
+ ### 0.30.0
9
+
10
+ * Drop support for Ruby 2.4 and Ruby 2.5 since they are EOL
11
+ * Fix `expire` to to raise error on invalid integer
12
+
13
+ ### 0.29.0
14
+
15
+ * Add support for `logger` option ([#211](https://github.com/sds/mock_redis/pull/211))
16
+ * Fix `zadd` to not perform conditional type conversion ([#214](https://github.com/sds/mock_redis/pull/214))
17
+ * Fix `hdel` to raise error when called with empty array ([#215](https://github.com/sds/mock_redis/pull/215))
18
+
3
19
  ### 0.28.0
4
20
 
5
21
  * Fix `hmset` exception ([#206](https://github.com/sds/mock_redis/pull/206))
data/Gemfile CHANGED
@@ -9,4 +9,5 @@ gem 'overcommit', '0.53.0'
9
9
  # Pin tool versions (which are executed by Overcommit) for Travis builds
10
10
  gem 'rubocop', '0.82.0'
11
11
 
12
- gem 'coveralls', require: false
12
+ gem 'simplecov', '~> 0.21.0'
13
+ gem 'simplecov-lcov', '~> 0.8.0'
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # MockRedis
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/mock_redis.svg)](http://badge.fury.io/rb/mock_redis)
4
- [![Build Status](https://travis-ci.org/sds/mock_redis.svg)](https://travis-ci.org/sds/mock_redis)
4
+ ![Build Status](https://github.com/sds/mock_redis/actions/workflows/tests.yml/badge.svg)
5
5
  [![Coverage Status](https://coveralls.io/repos/sds/mock_redis/badge.svg)](https://coveralls.io/r/sds/mock_redis)
6
6
 
7
7
  MockRedis provides the same interface as `redis-rb`, but it stores its
@@ -10,9 +10,9 @@ for use in tests.
10
10
 
11
11
  ## Requirements
12
12
 
13
- * Ruby 2.4+
13
+ Ruby 2.6+
14
14
 
15
- The current implementation is tested against **Redis 5**. Older versions may work, but can also return different results or not support some commands.
15
+ The current implementation is tested against Redis 6.2. Older versions may work, but can also return different results or not support some commands.
16
16
 
17
17
  ## Getting Started
18
18
 
@@ -86,27 +86,27 @@ class MockRedis
86
86
  end
87
87
 
88
88
  def expire(key, seconds)
89
+ assert_valid_integer(seconds)
90
+
89
91
  pexpire(key, seconds.to_i * 1000)
90
92
  end
91
93
 
92
94
  def pexpire(key, ms)
95
+ assert_valid_integer(ms)
96
+
93
97
  now, miliseconds = @base.now
94
98
  now_ms = (now * 1000) + miliseconds
95
99
  pexpireat(key, now_ms + ms.to_i)
96
100
  end
97
101
 
98
102
  def expireat(key, timestamp)
99
- unless looks_like_integer?(timestamp.to_s)
100
- raise Redis::CommandError, 'ERR value is not an integer or out of range'
101
- end
103
+ assert_valid_integer(timestamp)
102
104
 
103
105
  pexpireat(key, timestamp.to_i * 1000)
104
106
  end
105
107
 
106
108
  def pexpireat(key, timestamp_ms)
107
- unless looks_like_integer?(timestamp_ms.to_s)
108
- raise Redis::CommandError, 'ERR value is not an integer or out of range'
109
- end
109
+ assert_valid_integer(timestamp_ms)
110
110
 
111
111
  if exists?(key)
112
112
  timestamp = Rational(timestamp_ms.to_i, 1000)
@@ -178,8 +178,8 @@ class MockRedis
178
178
  end
179
179
  end
180
180
 
181
- def ping
182
- 'PONG'
181
+ def ping(response = 'PONG')
182
+ response
183
183
  end
184
184
 
185
185
  def quit
@@ -280,6 +280,13 @@ class MockRedis
280
280
 
281
281
  private
282
282
 
283
+ def assert_valid_integer(integer)
284
+ unless looks_like_integer?(integer.to_s)
285
+ raise Redis::CommandError, 'ERR value is not an integer or out of range'
286
+ end
287
+ integer
288
+ end
289
+
283
290
  def assert_valid_timeout(timeout)
284
291
  if !looks_like_integer?(timeout.to_s)
285
292
  raise Redis::CommandError, 'ERR timeout is not an integer or out of range'
@@ -10,6 +10,11 @@ class MockRedis
10
10
  with_hash_at(key) do |hash|
11
11
  orig_size = hash.size
12
12
  fields = Array(fields).flatten.map(&:to_s)
13
+
14
+ if fields.empty?
15
+ raise Redis::CommandError, "ERR wrong number of arguments for 'hdel' command"
16
+ end
17
+
13
18
  hash.delete_if { |k, _v| fields.include?(k) }
14
19
  orig_size - hash.size
15
20
  end
@@ -70,8 +75,10 @@ class MockRedis
70
75
  end
71
76
 
72
77
  def hmget(key, *fields)
78
+ fields.flatten!
79
+
73
80
  assert_has_args(fields, 'hmget')
74
- fields.flatten.map { |f| hget(key, f) }
81
+ fields.map { |f| hget(key, f) }
75
82
  end
76
83
 
77
84
  def mapped_hmget(key, *fields)
@@ -49,15 +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
- 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
52
+ items = if start_id.exclusive
53
+ members
54
+ .select { |m| (start_id < m[0]) && (finish_id >= m[0]) }
55
+ .map { |m| [m[0].to_s, m[1]] }
56
+ else
57
+ members
58
+ .select { |m| (start_id <= m[0]) && (finish_id >= m[0]) }
59
+ .map { |m| [m[0].to_s, m[1]] }
60
+ end
61
61
  items.reverse! if reversed
62
62
  return items.first(opts['count'].to_i) if opts.key?('count')
63
63
  items
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the gem version.
4
4
  class MockRedis
5
- VERSION = '0.28.0'
5
+ VERSION = '0.31.0'
6
6
  end
@@ -23,12 +23,7 @@ class MockRedis
23
23
 
24
24
  def add(score, member)
25
25
  members.add(member)
26
- scores[member] =
27
- if score.to_f.to_i == score.to_f
28
- score.to_f.to_i
29
- else
30
- score.to_f
31
- end
26
+ scores[member] = score.to_f
32
27
  self
33
28
  end
34
29
 
data/lib/mock_redis.rb CHANGED
@@ -22,6 +22,7 @@ class MockRedis
22
22
  :path => nil,
23
23
  :timeout => 5.0,
24
24
  :password => nil,
25
+ :logger => nil,
25
26
  :db => 0,
26
27
  :time_class => Time,
27
28
  }.freeze
@@ -65,6 +66,10 @@ class MockRedis
65
66
  options[:db]
66
67
  end
67
68
 
69
+ def logger
70
+ options[:logger]
71
+ end
72
+
68
73
  def time_at(timestamp)
69
74
  options[:time_class].at(timestamp)
70
75
  end
@@ -86,7 +91,9 @@ class MockRedis
86
91
  end
87
92
 
88
93
  ruby2_keywords def method_missing(method, *args, &block)
89
- @db.send(method, *args, &block)
94
+ logging([[method, *args]]) do
95
+ @db.send(method, *args, &block)
96
+ end
90
97
  end
91
98
 
92
99
  def initialize_copy(source)
@@ -139,4 +146,30 @@ class MockRedis
139
146
 
140
147
  options
141
148
  end
149
+
150
+ def logging(commands)
151
+ return yield unless logger&.debug?
152
+
153
+ begin
154
+ commands.each do |name, *args|
155
+ logged_args = args.map do |a|
156
+ if a.respond_to?(:inspect) then a.inspect
157
+ elsif a.respond_to?(:to_s) then a.to_s
158
+ else
159
+ # handle poorly-behaved descendants of BasicObject
160
+ klass = a.instance_exec { (class << self; self end).superclass }
161
+ "\#<#{klass}:#{a.__id__}>"
162
+ end
163
+ end
164
+ logger.debug("[MockRedis] command=#{name.to_s.upcase} args=#{logged_args.join(' ')}")
165
+ end
166
+
167
+ t1 = Time.now
168
+ yield
169
+ ensure
170
+ if t1
171
+ logger.debug(format('[MockRedis] call_time=%<time>0.2f ms', time: ((Time.now - t1) * 1000)))
172
+ end
173
+ end
174
+ end
142
175
  end
@@ -21,7 +21,7 @@ describe '#expire(key, seconds)' do
21
21
 
22
22
  it 'raises an error if seconds is bogus' do
23
23
  lambda do
24
- @redises.expireat(@key, 'a couple minutes or so')
24
+ @redises.expire(@key, 'a couple minutes or so')
25
25
  end.should raise_error(Redis::CommandError)
26
26
  end
27
27
 
@@ -66,5 +66,12 @@ describe '#hdel(key, field)' do
66
66
  @redises.get(@key).should be_nil
67
67
  end
68
68
 
69
+ it 'raises error if an empty array is passed' do
70
+ expect { @redises.hdel(@key, []) }.to raise_error(
71
+ Redis::CommandError,
72
+ "ERR wrong number of arguments for 'hdel' command"
73
+ )
74
+ end
75
+
69
76
  it_should_behave_like 'a hash-only command'
70
77
  end
@@ -36,5 +36,11 @@ describe '#hmget(key, field [, field, ...])' do
36
36
  end.should raise_error(Redis::CommandError)
37
37
  end
38
38
 
39
+ it 'raises an error if given an empty list of fields' do
40
+ lambda do
41
+ @redises.hmget(@key, [])
42
+ end.should raise_error(Redis::CommandError)
43
+ end
44
+
39
45
  it_should_behave_like 'a hash-only command'
40
46
  end
@@ -35,7 +35,7 @@ describe '#hset(key, field)' do
35
35
  end
36
36
 
37
37
  it 'stores fields sent in a hash' do
38
- @redises.hset(@key, {'k1' => 'v1', 'k2' => 'v2'}).should == 2
38
+ @redises.hset(@key, { 'k1' => 'v1', 'k2' => 'v2' }).should == 2
39
39
  end
40
40
 
41
41
  it_should_behave_like 'a hash-only command'
@@ -21,7 +21,7 @@ describe '#pexpire(key, ms)' do
21
21
 
22
22
  it 'raises an error if ms is bogus' do
23
23
  lambda do
24
- @redises.pexpireat(@key, 'a couple minutes or so')
24
+ @redises.pexpire(@key, 'a couple minutes or so')
25
25
  end.should raise_error(Redis::CommandError)
26
26
  end
27
27
 
@@ -1,7 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe '#ping' do
4
- it "returns 'PONG'" do
4
+ it 'returns "PONG" with no arguments' do
5
5
  @redises.ping.should == 'PONG'
6
6
  end
7
+
8
+ it 'returns the argument' do
9
+ @redises.ping('HELLO').should == 'HELLO'
10
+ end
7
11
  end
@@ -15,7 +15,7 @@ describe '#xadd("mystream", { f1: "v1", f2: "v2" }, id: "0-0", maxlen: 1000, app
15
15
  it 'returns an id based on the timestamp' do
16
16
  t = Time.now.to_i
17
17
  id = @redises.xadd(@key, { key: 'value' })
18
- expect(@redises.xadd(@key, { key: 'value' })).to match(/#{t}\d{3}-0/)
18
+ expect(id).to match(/#{t}\d{3}-0/)
19
19
  end
20
20
 
21
21
  it 'adds data with symbols' do
@@ -23,6 +23,12 @@ describe '#zadd(key, score, member)' do
23
23
  @redises.zrange(@key, 0, -1).should == [member.to_s]
24
24
  end
25
25
 
26
+ it 'allows scores to be set to Float::INFINITY' do
27
+ member = '1'
28
+ @redises.zadd(@key, Float::INFINITY, member)
29
+ @redises.zrange(@key, 0, -1).should == [member]
30
+ end
31
+
26
32
  it 'updates the score' do
27
33
  @redises.zadd(@key, 1, 'foo')
28
34
  @redises.zadd(@key, 2, 'foo')
@@ -81,4 +81,13 @@ describe MockRedis do
81
81
  end
82
82
  end
83
83
  end
84
+
85
+ describe 'supplying a logger' do
86
+ it 'logs redis commands' do
87
+ logger = double('Logger', debug?: true, debug: nil)
88
+ mock_redis = MockRedis.new(logger: logger)
89
+ expect(logger).to receive(:debug).with(/command=HMGET args="hash" "key1" "key2"/)
90
+ mock_redis.hmget('hash', 'key1', 'key2')
91
+ end
92
+ end
84
93
  end
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,17 @@
1
- if ENV['TRAVIS']
2
- # When running in Travis, report coverage stats to Coveralls.
3
- require 'coveralls'
4
- Coveralls.wear!
5
- else
6
- # Otherwise render coverage information in coverage/index.html and display
7
- # coverage percentage in the console.
8
- require 'simplecov'
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter 'spec/'
4
+
5
+ if ENV['CI']
6
+ require 'simplecov-lcov'
7
+
8
+ SimpleCov::Formatter::LcovFormatter.config do |c|
9
+ c.report_with_single_file = true
10
+ c.single_report_path = 'coverage/lcov.info'
11
+ end
12
+
13
+ formatter SimpleCov::Formatter::LcovFormatter
14
+ end
9
15
  end
10
16
 
11
17
  require 'rspec/its'
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.28.0
4
+ version: 0.31.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-05-01 00:00:00.000000000 Z
12
+ date: 2022-04-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ruby2_keywords
@@ -89,6 +89,8 @@ executables: []
89
89
  extensions: []
90
90
  extra_rdoc_files: []
91
91
  files:
92
+ - ".github/workflows/lint.yml"
93
+ - ".github/workflows/tests.yml"
92
94
  - ".gitignore"
93
95
  - ".mailmap"
94
96
  - ".overcommit.yml"
@@ -96,7 +98,6 @@ files:
96
98
  - ".rubocop.yml"
97
99
  - ".rubocop_todo.yml"
98
100
  - ".simplecov"
99
- - ".travis.yml"
100
101
  - CHANGELOG.md
101
102
  - Gemfile
102
103
  - LICENSE.md
@@ -295,7 +296,7 @@ homepage: https://github.com/sds/mock_redis
295
296
  licenses:
296
297
  - MIT
297
298
  metadata: {}
298
- post_install_message:
299
+ post_install_message:
299
300
  rdoc_options: []
300
301
  require_paths:
301
302
  - lib
@@ -310,8 +311,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
310
311
  - !ruby/object:Gem::Version
311
312
  version: '0'
312
313
  requirements: []
313
- rubygems_version: 3.1.4
314
- signing_key:
314
+ rubygems_version: 3.1.6
315
+ signing_key:
315
316
  specification_version: 4
316
317
  summary: Redis mock that just lives in memory; useful for testing.
317
318
  test_files:
data/.travis.yml DELETED
@@ -1,33 +0,0 @@
1
- language: ruby
2
-
3
- cache: bundler
4
-
5
- addons:
6
- apt:
7
- packages:
8
- - redis-server
9
-
10
- services:
11
- - redis-server
12
-
13
- before_install:
14
- - sudo sed -e 's/^bind.*/bind 127.0.0.1/' /etc/redis/redis.conf > redis.conf
15
- - sudo mv redis.conf /etc/redis
16
- - sudo service redis-server start
17
- - echo PING | nc localhost 6379
18
-
19
- rvm:
20
- - 2.4
21
- - 2.5
22
- - 2.6
23
- - 2.7
24
-
25
- before_script:
26
- - git config --local user.email "travis@travis.ci"
27
- - git config --local user.name "Travis CI"
28
-
29
- script:
30
- - redis-cli --version
31
- - bundle exec rspec
32
- - bundle exec overcommit --sign
33
- - bundle exec overcommit --run