fakeredis 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 30c50ee5b3390908b28564542cf9d6ad6c1dfb20
4
- data.tar.gz: 30c85e12c5689721c1ead41da72a91c47a010a0b
3
+ metadata.gz: 1be2134927d3b7838ee3e2fd4376791bea516561
4
+ data.tar.gz: 2b3e0c64b79df1358151bbd1405000395fa06e86
5
5
  SHA512:
6
- metadata.gz: 62ec40ecbbf36d198b0a8b95d5772f6d1e79a9709378a67a9e5aaba8cc15f6122fba3966524831ce5a8e7b6ebdbceaf620a4320f338497deff52c8361d8c41c7
7
- data.tar.gz: 7d7c9066f97f7accfc95e40819b726ffcbf318e5e6f1af9fda405ce3104c56e546e2a2c6b5e61cac58a283a4d56f868c8c1c6bb5ca811ca440cc0eb62adf599a
6
+ metadata.gz: cfb37be84fd30d941fadc44a177e2029cd2de063fc090bc3004166a72f5dba6e7378e9c918aa1d156d44b8277f830dfedbd34aad57142770512f420f4f12fae3
7
+ data.tar.gz: 2f841badb4c16e9ff6d3c35b697b9ee262022bc8286553e7187a47173af742114eacd5cccdb3d0477c111457fc9a7f50fdb7de970a25c04ba1be33318c84a1ac
@@ -1,19 +1,22 @@
1
1
  language: ruby
2
- before_install:
3
- - travis_retry gem install bundler
2
+ cache: bundler
3
+ # Use the faster container based infrastructure
4
+ # http://blog.travis-ci.com/2014-12-17-faster-builds-with-container-based-infrastructure/
5
+ sudo: false
6
+
4
7
  rvm:
5
- - 2.1
6
8
  - 2.2
7
- - 2.3.1
9
+ - 2.3.6
10
+ - 2.4
11
+ - 2.5
8
12
  - ruby-head
9
13
  - jruby
10
14
  - rbx-2
15
+
11
16
  gemfile:
12
17
  - Gemfile
13
18
  - gemfiles/redisrb-master.gemfile
19
+
14
20
  matrix:
15
21
  allow_failures:
16
22
  - rvm: rbx-2
17
- # Use the faster container based infrastructure
18
- # http://blog.travis-ci.com/2014-12-17-faster-builds-with-container-based-infrastructure/
19
- sudo: false
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011-2016 Guillermo Iguaran
1
+ Copyright (c) 2011-2018 Guillermo Iguaran
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -15,8 +15,8 @@ Add it to your Gemfile:
15
15
 
16
16
  ## Versions
17
17
 
18
- FakeRedis currently supports redis-rb v3.x.y or later, if you are using
19
- redis-rb v2.2.x install the version 0.3.x:
18
+ FakeRedis currently supports redis-rb v3 or later, if you are using
19
+ redis-rb v2.2 install the version 0.3.x:
20
20
 
21
21
  gem install fakeredis -v "~> 0.3.0"
22
22
 
@@ -29,61 +29,63 @@ or use the branch 0-3-x on your Gemfile:
29
29
 
30
30
  You can use FakeRedis without any changes:
31
31
 
32
+ ```
32
33
  require "fakeredis"
33
-
34
+
34
35
  redis = Redis.new
35
-
36
+
36
37
  >> redis.set "foo", "bar"
37
38
  => "OK"
38
-
39
+
39
40
  >> redis.get "foo"
40
41
  => "bar"
42
+ ```
41
43
 
42
44
  Read [redis-rb](https://github.com/redis/redis-rb) documentation and
43
45
  [Redis](http://redis.io) homepage for more info about commands
44
46
 
45
47
  ## Usage with RSpec
46
48
 
47
- Require this either in your Gemfile or in RSpec's support scripts. So either:
49
+ Require this either in your Gemfile or in RSpec's support scripts. So either:
48
50
 
49
- # Gemfile
50
- group :test do
51
- gem "rspec"
52
- gem "fakeredis", :require => "fakeredis/rspec"
53
- end
51
+ ```ruby
52
+ # Gemfile
53
+ group :test do
54
+ gem "rspec"
55
+ gem "fakeredis", :require => "fakeredis/rspec"
56
+ end
57
+ ```
54
58
 
55
59
  Or:
56
60
 
57
- # spec/support/fakeredis.rb
58
- require 'fakeredis/rspec'
61
+ ```ruby
62
+ # spec/support/fakeredis.rb
63
+ require 'fakeredis/rspec'
64
+ ```
59
65
 
60
66
  ## Usage with Minitest
61
67
 
62
68
  Require this either in your Gemfile or in Minitest's support scripts. So
63
- either:
69
+ either:
64
70
 
65
- # Gemfile
66
- group :test do
67
- gem "minitest"
68
- gem "fakeredis", :require => "fakeredis/minitest"
69
- end
71
+ ```ruby
72
+ # Gemfile
73
+ group :test do
74
+ gem "minitest"
75
+ gem "fakeredis", :require => "fakeredis/minitest"
76
+ end
77
+ ```
70
78
 
71
79
  Or:
72
80
 
73
- # test/test_helper.rb (or test/minitest_config.rb)
74
- require 'fakeredis/minitest'
81
+ ```ruby
82
+ # test/test_helper.rb (or test/minitest_config.rb)
83
+ require 'fakeredis/minitest'
84
+ ```
75
85
 
76
86
  ## Acknowledgements
77
87
 
78
- * [dim](https://github.com/dim)
79
- * [czarneckid](https://github.com/czarneckid)
80
- * [obrie](https://github.com/obrie)
81
- * [jredville](https://github.com/jredville)
82
- * [redsquirrel](https://github.com/redsquirrel)
83
- * [dpick](https://github.com/dpick)
84
- * [caius](https://github.com/caius)
85
- * [Travis-CI](http://travis-ci.org/) (Travis-CI also uses Fakeredis in its tests!!!)
86
-
88
+ Thanks to [all contributors](https://github.com/guilleiguaran/fakeredis/graphs/contributors), specially to [Caius Durling](https://github.com/caius) the most active one.
87
89
 
88
90
  ## Contributing to FakeRedis
89
91
 
@@ -98,5 +100,5 @@ Or:
98
100
 
99
101
  ## Copyright
100
102
 
101
- Copyright (c) 2011-2016 Guillermo Iguaran. See LICENSE for
103
+ Copyright (c) 2011-2018 Guillermo Iguaran. See LICENSE for
102
104
  further details.
@@ -18,6 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- s.add_runtime_dependency(%q<redis>, ["~> 3.2"])
21
+ s.add_runtime_dependency(%q<redis>, [">= 3.2", "< 5.0"])
22
22
  s.add_development_dependency(%q<rspec>, ["~> 3.0"])
23
23
  end
@@ -3,4 +3,16 @@ require 'redis/connection/memory'
3
3
 
4
4
  module FakeRedis
5
5
  Redis = ::Redis
6
+
7
+ def self.enable
8
+ Redis::Connection.drivers << Redis::Connection::Memory unless enabled?
9
+ end
10
+
11
+ def self.enabled?
12
+ Redis::Connection.drivers.last == Redis::Connection::Memory
13
+ end
14
+
15
+ def self.disable
16
+ Redis::Connection.drivers.delete_if {|driver| Redis::Connection::Memory == driver }
17
+ end
6
18
  end
@@ -1,13 +1,16 @@
1
1
  module FakeRedis
2
2
  module CommandExecutor
3
3
  def write(command)
4
- meffod = command.shift.to_s.downcase.to_sym
4
+ meffod = command[0].to_s.downcase.to_sym
5
+ args = command[1..-1]
5
6
 
6
7
  if in_multi && !(TRANSACTION_COMMANDS.include? meffod) # queue commands
7
- queued_commands << [meffod, *command]
8
+ queued_commands << [meffod, *args]
8
9
  reply = 'QUEUED'
10
+ elsif respond_to?(meffod) && method(meffod).arity.zero?
11
+ reply = send(meffod)
9
12
  elsif respond_to?(meffod)
10
- reply = send(meffod, *command)
13
+ reply = send(meffod, *args)
11
14
  else
12
15
  raise Redis::CommandError, "ERR unknown command '#{meffod}'"
13
16
  end
@@ -68,7 +68,7 @@ module FakeRedis
68
68
 
69
69
  class SortedSetIntersectStore < SortedSetStore
70
70
  def selected_keys
71
- @values ||= hashes.inject([]) { |r, h| r.empty? ? h.keys : (r & h.keys) }
71
+ @values ||= hashes.map(&:keys).reduce(:&)
72
72
  end
73
73
  end
74
74
 
@@ -1,5 +1,5 @@
1
1
  module FakeRedis
2
- TRANSACTION_COMMANDS = [:discard, :exec, :multi, :watch, :unwatch]
2
+ TRANSACTION_COMMANDS = [:discard, :exec, :multi, :watch, :unwatch, :client]
3
3
 
4
4
  module TransactionCommands
5
5
  def self.included(klass)
@@ -1,3 +1,3 @@
1
1
  module FakeRedis
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -9,6 +9,7 @@ require "fakeredis/sorted_set_store"
9
9
  require "fakeredis/transaction_commands"
10
10
  require "fakeredis/zset"
11
11
  require "fakeredis/bitop_command"
12
+ require "fakeredis/version"
12
13
 
13
14
  class Redis
14
15
  module Connection
@@ -56,6 +57,7 @@ class Redis
56
57
  def database_id
57
58
  @database_id ||= 0
58
59
  end
60
+
59
61
  attr_writer :database_id
60
62
 
61
63
  def database_instance_key
@@ -89,6 +91,16 @@ class Redis
89
91
  def disconnect
90
92
  end
91
93
 
94
+ def client(command, _options = {})
95
+ case command
96
+ when :setname then true
97
+ when :getname then nil
98
+ when :client then true
99
+ else
100
+ raise Redis::CommandError, "ERR unknown command '#{command}'"
101
+ end
102
+ end
103
+
92
104
  def timeout=(usecs)
93
105
  end
94
106
 
@@ -386,9 +398,9 @@ class Redis
386
398
  index = data[key].index(pivot.to_s)
387
399
  return -1 if index.nil?
388
400
 
389
- case where
390
- when :before then data[key].insert(index, value)
391
- when :after then data[key].insert(index + 1, value)
401
+ case where.to_s
402
+ when /\Abefore\z/i then data[key].insert(index, value)
403
+ when /\Aafter\z/i then data[key].insert(index + 1, value)
392
404
  else raise_syntax_error
393
405
  end
394
406
  end
@@ -559,11 +571,14 @@ class Redis
559
571
  result
560
572
  end
561
573
 
562
- def spop(key)
574
+ def spop(key, count = nil)
563
575
  data_type_check(key, ::Set)
564
- elem = srandmember(key)
565
- srem(key, elem)
566
- elem
576
+ results = (count || 1).times.map do
577
+ elem = srandmember(key)
578
+ srem(key, elem)
579
+ elem
580
+ end.compact
581
+ count.nil? ? results.first : results
567
582
  end
568
583
 
569
584
  def scard(key)
@@ -779,6 +794,7 @@ class Redis
779
794
  data[key][field[0].to_s] = field[1].to_s
780
795
  end
781
796
  end
797
+ "OK"
782
798
  end
783
799
 
784
800
  def hmget(key, *fields)
@@ -967,7 +983,7 @@ class Redis
967
983
  end
968
984
 
969
985
  start_cursor = start_cursor.to_i
970
- data_type_check(start_cursor, Fixnum)
986
+ data_type_check(start_cursor, Integer)
971
987
 
972
988
  cursor = start_cursor
973
989
  returned_keys = []
@@ -1077,7 +1093,7 @@ class Redis
1077
1093
  results = sort_keys(data[key])
1078
1094
  # Select just the keys unless we want scores
1079
1095
  results = results.map(&:first) unless with_scores
1080
- results[start..stop].flatten.map(&:to_s)
1096
+ (results[start..stop] || []).flatten.map(&:to_s)
1081
1097
  end
1082
1098
 
1083
1099
  def zrangebylex(key, start, stop, *opts)
@@ -1286,7 +1302,7 @@ class Redis
1286
1302
  end
1287
1303
 
1288
1304
  start_cursor = start_cursor.to_i
1289
- data_type_check(start_cursor, Fixnum)
1305
+ data_type_check(start_cursor, Integer)
1290
1306
 
1291
1307
  cursor = start_cursor
1292
1308
  next_keys = []
@@ -1417,4 +1433,5 @@ class Redis
1417
1433
  end
1418
1434
  end
1419
1435
 
1436
+ # FIXME this line should be deleted as explicit enabling is better
1420
1437
  Redis::Connection.drivers << Redis::Connection::Memory
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe FakeRedis::CommandExecutor do
4
+
5
+ let(:redis) { Redis.new }
6
+
7
+ context '#write' do
8
+ it 'does not modify its argument' do
9
+ command = [:get, 'key']
10
+ redis.write(command)
11
+ expect(command).to eql([:get, 'key'])
12
+ end
13
+ end
14
+
15
+ end
@@ -70,7 +70,7 @@ module FakeRedis
70
70
 
71
71
  it "should not error with a disconnected client" do
72
72
  @client1 = Redis.new
73
- @client1.client.disconnect
73
+ @client1.close
74
74
  expect(@client1.get("key1")).to be_nil
75
75
  end
76
76
 
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe FakeRedis do
4
+ after { described_class.disable }
5
+
6
+ describe '.enable' do
7
+ it 'in memory connection' do
8
+ described_class.enable
9
+ expect(described_class.enabled?).to be_truthy
10
+ end
11
+ end
12
+
13
+ describe '.disable' do
14
+ before { described_class.enable }
15
+
16
+ it 'in memory connection' do
17
+ described_class.disable
18
+ expect(described_class.enabled?).to be_falsy
19
+ end
20
+ end
21
+ end
@@ -137,6 +137,10 @@ module FakeRedis
137
137
  expect { @client.hmset("hash", "foo1", "bar1", "foo2", "bar2", "foo3") }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for HMSET")
138
138
  end
139
139
 
140
+ it "should return OK on success" do
141
+ expect(@client.hmset("key", "k1", "value1")).to eq("OK")
142
+ end
143
+
140
144
  it "should set multiple hash fields to multiple values" do
141
145
  @client.hmset("key", "k1", "value1", "k2", "value2")
142
146
 
@@ -25,6 +25,18 @@ module FakeRedis
25
25
  expect(@client.lrange("key1", 0, -1)).to eq(["v1", "v2", "v3", "99", "100"])
26
26
  end
27
27
 
28
+ it "inserts with case-insensitive position token" do
29
+ @client.rpush("key1", "v1")
30
+ @client.rpush("key1", "v4")
31
+
32
+ @client.linsert("key1", :BEFORE, "v4", "v2")
33
+ @client.linsert("key1", "Before", "v4", "v3")
34
+ @client.linsert("key1", :AFTER, "v4", "v5")
35
+ @client.linsert("key1", "After", "v5", "v6")
36
+
37
+ expect(@client.lrange("key1", 0, -1)).to eq(%w(v1 v2 v3 v4 v5 v6))
38
+ end
39
+
28
40
  it "should not insert if after/before index not found" do
29
41
  @client.rpush("key", "v1")
30
42
  expect(@client.linsert("key", :before, "unknown", "v2")).to eq(-1)
@@ -9,6 +9,12 @@ RSpec.describe FakeRedis do
9
9
  end
10
10
  end
11
11
 
12
+ describe '#write' do
13
+ it 'should not send unexpected arguments' do
14
+ expect { redis.write(['info', 'server']) }.not_to raise_error
15
+ end
16
+ end
17
+
12
18
  describe '#scan' do
13
19
  def result
14
20
  returned_keys = []
@@ -78,4 +84,22 @@ RSpec.describe FakeRedis do
78
84
  end
79
85
  end
80
86
  end
87
+
88
+ describe '#client' do
89
+ it 'returns 1 when command is :setname' do
90
+ expect(redis.write([:client, :setname])).to eq 1
91
+ end
92
+
93
+ it 'returns nil when command is :getname' do
94
+ expect(redis.write([:client, :getname])).to eq nil
95
+ end
96
+
97
+ it 'returns true when the comment is :client with an argument' do
98
+ expect(redis.write([:client, :client, :list])).to eq 1
99
+ end
100
+
101
+ it 'raises error for other commands' do
102
+ expect { redis.write([:client, :wrong]) }.to raise_error(Redis::CommandError, "ERR unknown command 'wrong'")
103
+ end
104
+ end
81
105
  end
@@ -148,6 +148,22 @@ module FakeRedis
148
148
  expect(["a", "b"].include?(@client.spop("key1"))).to be true
149
149
  end
150
150
 
151
+ it "should pop multiple members from a set" do
152
+ @client.sadd("key1", "a")
153
+ @client.sadd("key1", "b")
154
+ @client.sadd("key1", "c")
155
+
156
+ vals = @client.spop("key1", 2)
157
+ expect(vals.count).to eq(2)
158
+ vals.each { |v| expect(["a", "b", "c"].include?(v)).to be true }
159
+
160
+ new_vals = @client.spop("key1", 2)
161
+ expect(new_vals.count).to eq(1)
162
+ expect(["a", "b", "c"].include?(new_vals.first)).to be true
163
+
164
+ expect(["a", "b", "c"]).to eq((vals + new_vals).sort)
165
+ end
166
+
151
167
  it "should remove a member from a set" do
152
168
  @client.sadd("key1", "a")
153
169
  @client.sadd("key1", "b")
@@ -306,6 +306,22 @@ module FakeRedis
306
306
  it "should error with an invalid aggregate" do
307
307
  expect { @client.zinterstore("out", %w(key1 key2), :aggregate => :invalid) }.to raise_error(Redis::CommandError, "ERR syntax error")
308
308
  end
309
+
310
+ it 'stores nothing when there are no members in common' do
311
+ @client.zadd("k1", 1, "1")
312
+ @client.zadd("k1", 1, "2")
313
+ @client.sadd("k2", "a")
314
+ @client.sadd("k3", "b")
315
+
316
+ expect(@client.zinterstore("out", %w(k1 k2 k3))).to eq(0)
317
+ expect(@client.zrange("out", 0, -1)).to eq([])
318
+ end
319
+
320
+ it 'handles range start being higher than number of members' do
321
+ @client.zadd("key", 1, "1")
322
+
323
+ expect(@client.zrange("key", 10, 10)).to eq([])
324
+ end
309
325
  end
310
326
 
311
327
  context "zremrangebyscore" do
@@ -7,7 +7,11 @@ require "fakeredis/rspec"
7
7
  require "support/shared_examples/sortable"
8
8
  require "support/shared_examples/bitwise_operation"
9
9
 
10
+
10
11
  RSpec.configure do |config|
12
+ # Enable memory adapter
13
+ config.before(:each) { FakeRedis.enable }
14
+
11
15
  # replaces -b -fdoc --color in .rspec
12
16
  config.color = true
13
17
  config.default_formatter = "doc"
@@ -25,5 +29,5 @@ RSpec.configure do |config|
25
29
  end
26
30
 
27
31
  def fakeredis?
28
- true
32
+ FakeRedis.enabled?
29
33
  end
@@ -1,14 +1,14 @@
1
1
  require "spec_helper"
2
2
 
3
- # Remove memory so we test against actual redis
4
- Redis::Connection.drivers.pop
5
-
6
3
  RSpec.configure do |config|
7
4
  config.before(:each) do
5
+ # Disable so we test against actual redis
6
+ FakeRedis.disable
7
+
8
8
  Redis.new.flushall
9
9
  end
10
10
  end
11
11
 
12
12
  def fakeredis?
13
- false
13
+ FakeRedis.enabled?
14
14
  end
@@ -33,7 +33,7 @@ module FakeRedis
33
33
  end
34
34
 
35
35
  context '#discard' do
36
- it "should responde with 'OK' after #multi" do
36
+ it "should respond with 'OK' after #multi" do
37
37
  @client.multi
38
38
  expect(@client.discard).to eq('OK')
39
39
  end
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakeredis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guillermo Iguaran
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-24 00:00:00.000000000 Z
11
+ date: 2018-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: '3.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: rspec
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -69,8 +75,10 @@ files:
69
75
  - lib/fakeredis/zset.rb
70
76
  - lib/redis/connection/memory.rb
71
77
  - spec/bitop_command_spec.rb
78
+ - spec/command_executor_spec.rb
72
79
  - spec/compatibility_spec.rb
73
80
  - spec/connection_spec.rb
81
+ - spec/fakeredis_spec.rb
74
82
  - spec/hashes_spec.rb
75
83
  - spec/keys_spec.rb
76
84
  - spec/lists_spec.rb
@@ -107,14 +115,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
115
  version: '0'
108
116
  requirements: []
109
117
  rubyforge_project:
110
- rubygems_version: 2.5.1
118
+ rubygems_version: 2.6.11
111
119
  signing_key:
112
120
  specification_version: 4
113
121
  summary: Fake (In-memory) driver for redis-rb.
114
122
  test_files:
115
123
  - spec/bitop_command_spec.rb
124
+ - spec/command_executor_spec.rb
116
125
  - spec/compatibility_spec.rb
117
126
  - spec/connection_spec.rb
127
+ - spec/fakeredis_spec.rb
118
128
  - spec/hashes_spec.rb
119
129
  - spec/keys_spec.rb
120
130
  - spec/lists_spec.rb