kuende-fakeredis 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.travis.yml +19 -0
  4. data/Gemfile +13 -0
  5. data/Guardfile +8 -0
  6. data/LICENSE +21 -0
  7. data/README.md +102 -0
  8. data/Rakefile +27 -0
  9. data/fakeredis.gemspec +24 -0
  10. data/gemfiles/redisrb-master.gemfile +14 -0
  11. data/lib/fake_redis.rb +1 -0
  12. data/lib/fakeredis.rb +6 -0
  13. data/lib/fakeredis/bitop_command.rb +56 -0
  14. data/lib/fakeredis/command_executor.rb +25 -0
  15. data/lib/fakeredis/expiring_hash.rb +70 -0
  16. data/lib/fakeredis/minitest.rb +24 -0
  17. data/lib/fakeredis/rspec.rb +24 -0
  18. data/lib/fakeredis/sort_method.rb +117 -0
  19. data/lib/fakeredis/sorted_set_argument_handler.rb +74 -0
  20. data/lib/fakeredis/sorted_set_store.rb +80 -0
  21. data/lib/fakeredis/transaction_commands.rb +83 -0
  22. data/lib/fakeredis/version.rb +3 -0
  23. data/lib/fakeredis/zset.rb +39 -0
  24. data/lib/redis/connection/memory.rb +1375 -0
  25. data/spec/bitop_command_spec.rb +209 -0
  26. data/spec/compatibility_spec.rb +9 -0
  27. data/spec/connection_spec.rb +85 -0
  28. data/spec/hashes_spec.rb +261 -0
  29. data/spec/keys_spec.rb +488 -0
  30. data/spec/lists_spec.rb +229 -0
  31. data/spec/memory_spec.rb +28 -0
  32. data/spec/server_spec.rb +100 -0
  33. data/spec/sets_spec.rb +280 -0
  34. data/spec/sort_method_spec.rb +74 -0
  35. data/spec/sorted_sets_spec.rb +578 -0
  36. data/spec/spec_helper.rb +29 -0
  37. data/spec/spec_helper_live_redis.rb +14 -0
  38. data/spec/strings_spec.rb +289 -0
  39. data/spec/subscription_spec.rb +107 -0
  40. data/spec/support/shared_examples/bitwise_operation.rb +59 -0
  41. data/spec/support/shared_examples/sortable.rb +69 -0
  42. data/spec/transactions_spec.rb +92 -0
  43. data/spec/upcase_method_name_spec.rb +18 -0
  44. metadata +148 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 76c8f5456b0498e73104bfa6827a7d25a96d1b1c
4
+ data.tar.gz: a5443bdf8ae83d89a21cd5d5859e4a71c60327f0
5
+ SHA512:
6
+ metadata.gz: 6c1160fc425e5d4c597e97c4b35229e31908ecfd1e43f569c1fb523af6acb29e2e9e52636cf7554212946f83f0c89044c37bb11d21d56dd749731e9c25b94c71
7
+ data.tar.gz: ae72a230bd9cecadd6511a60a2090f88465b07b7f34eb35fa536e6d99e74e404dfa8342d4ad3b278fa50f1fa72683836d4b5861ae97a9f4a0174dfbb036f2524
@@ -0,0 +1,9 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ gemfiles/*.gemfile.lock
5
+ pkg/*
6
+ .rvmrc
7
+ *.rbc
8
+ .ruby-version
9
+ .ruby-gemset
@@ -0,0 +1,19 @@
1
+ language: ruby
2
+ before_install:
3
+ - travis_retry gem install bundler
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - 2.1
8
+ - ruby-head
9
+ - jruby
10
+ - rbx-2
11
+ gemfile:
12
+ - Gemfile
13
+ - gemfiles/redisrb-master.gemfile
14
+ matrix:
15
+ allow_failures:
16
+ - 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/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'rake'
4
+ gem 'rdoc'
5
+
6
+ platforms :rbx do
7
+ gem 'racc'
8
+ gem 'rubysl', '~> 2.0'
9
+ gem 'psych'
10
+ end
11
+
12
+ # Specify your gem's dependencies in fakeredis.gemspec
13
+ gemspec
@@ -0,0 +1,8 @@
1
+ ignore([%r{^bin/*}, %r{^lib/*}, %r{^tmp/*}])
2
+
3
+ guard 'rspec', cmd: 'bundle exec rspec', all_after_pass: false, all_on_start: false do
4
+ watch(%r{^spec/.+_spec\.rb$})
5
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
6
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2011-2014 Guillermo Iguaran
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
@@ -0,0 +1,102 @@
1
+ # FakeRedis [![Build Status](https://secure.travis-ci.org/guilleiguaran/fakeredis.png)](http://travis-ci.org/guilleiguaran/fakeredis)
2
+ This a fake implementation of redis-rb for machines without Redis or test environments
3
+
4
+
5
+ ## Installation
6
+
7
+ Install the gem:
8
+
9
+ gem install fakeredis
10
+
11
+ Add it to your Gemfile:
12
+
13
+ gem "fakeredis"
14
+
15
+
16
+ ## Versions
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:
20
+
21
+ gem install fakeredis -v "~> 0.3.0"
22
+
23
+ or use the branch 0-3-x on your Gemfile:
24
+
25
+ gem "fakeredis", :git => "git://github.com/guilleiguaran/fakeredis.git", :branch => "0-3-x"
26
+
27
+
28
+ ## Usage
29
+
30
+ You can use FakeRedis without any changes:
31
+
32
+ require "fakeredis"
33
+
34
+ redis = Redis.new
35
+
36
+ >> redis.set "foo", "bar"
37
+ => "OK"
38
+
39
+ >> redis.get "foo"
40
+ => "bar"
41
+
42
+ Read [redis-rb](https://github.com/redis/redis-rb) documentation and
43
+ [Redis](http://redis.io) homepage for more info about commands
44
+
45
+ ## Usage with RSpec
46
+
47
+ Require this either in your Gemfile or in RSpec's support scripts. So either:
48
+
49
+ # Gemfile
50
+ group :test do
51
+ gem "rspec"
52
+ gem "fakeredis", :require => "fakeredis/rspec"
53
+ end
54
+
55
+ Or:
56
+
57
+ # spec/support/fakeredis.rb
58
+ require 'fakeredis/rspec'
59
+
60
+ ## Usage with Minitest
61
+
62
+ Require this either in your Gemfile or in Minitest's support scripts. So
63
+ either:
64
+
65
+ # Gemfile
66
+ group :test do
67
+ gem "minitest"
68
+ gem "fakeredis", :require => "fakeredis/minitest"
69
+ end
70
+
71
+ Or:
72
+
73
+ # test/test_helper.rb (or test/minitest_config.rb)
74
+ require 'fakeredis/minitest'
75
+
76
+ ## Acknowledgements
77
+
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
+
87
+
88
+ ## Contributing to FakeRedis
89
+
90
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
91
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
92
+ * Fork the project
93
+ * Start a feature/bugfix branch
94
+ * Commit and push until you are happy with your contribution
95
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
96
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
97
+
98
+
99
+ ## Copyright
100
+
101
+ Copyright (c) 2011-2014 Guillermo Iguaran. See LICENSE for
102
+ further details.
@@ -0,0 +1,27 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ $:.push File.expand_path("../lib", __FILE__)
5
+ require "fakeredis/version"
6
+
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ require 'rspec/core'
10
+ require 'rspec/core/rake_task'
11
+ RSpec::Core::RakeTask.new(:spec) do |spec|
12
+ spec.pattern = FileList['spec/**/*_spec.rb']
13
+ spec.ruby_opts="-w"
14
+ end
15
+
16
+ task :default => :spec
17
+
18
+ require 'rdoc/task'
19
+ Rake::RDocTask.new do |rdoc|
20
+ version = FakeRedis::VERSION
21
+
22
+ rdoc.rdoc_dir = 'rdoc'
23
+ rdoc.title = "fakeredis #{version}"
24
+ rdoc.rdoc_files.include('README*')
25
+ rdoc.rdoc_files.include('lib/**/*.rb')
26
+ end
27
+
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "fakeredis/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "kuende-fakeredis"
7
+ s.version = FakeRedis::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Guillermo Iguaran"]
10
+ s.email = ["guilleiguaran@gmail.com"]
11
+ s.homepage = "https://guilleiguaran.github.com/fakeredis"
12
+ s.license = "MIT"
13
+ s.summary = %q{Fake (In-memory) driver for redis-rb.}
14
+ s.description = %q{Fake (In-memory) driver for redis-rb. Useful for testing environment and machines without Redis.}
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency(%q<redis>, ["~> 3.2"])
22
+ s.add_development_dependency(%q<rspec>, ["~> 3.0"])
23
+ s.add_development_dependency "guard-rspec"
24
+ end
@@ -0,0 +1,14 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'rake'
4
+ gem 'rdoc'
5
+ gem "redis", github: "redis/redis-rb"
6
+
7
+ platforms :rbx do
8
+ gem 'racc'
9
+ gem 'rubysl', '~> 2.0'
10
+ gem 'psych'
11
+ end
12
+
13
+ # Specify your gem's dependencies in fakeredis.gemspec
14
+ gemspec :path => ".."
@@ -0,0 +1 @@
1
+ require "fakeredis"
@@ -0,0 +1,6 @@
1
+ require 'redis'
2
+ require 'redis/connection/memory'
3
+
4
+ module FakeRedis
5
+ Redis = ::Redis
6
+ end
@@ -0,0 +1,56 @@
1
+ module FakeRedis
2
+ module BitopCommand
3
+ BIT_OPERATORS = {
4
+ 'or' => :|,
5
+ 'and' => :&,
6
+ 'xor' => :'^',
7
+ 'not' => :~,
8
+ }
9
+
10
+ def bitop(operation, destkey, *keys)
11
+ if result = apply(operator(operation), keys)
12
+ set(destkey, result)
13
+ result.length
14
+ else
15
+ 0
16
+ end
17
+ rescue ArgumentError => _
18
+ raise_argument_error('bitop')
19
+ end
20
+
21
+ private
22
+
23
+ def operator(operation)
24
+ BIT_OPERATORS[operation.to_s.downcase]
25
+ end
26
+
27
+ def apply(operator, keys)
28
+ case operator
29
+ when :~
30
+ raise ArgumentError if keys.count != 1
31
+ bitwise_not(keys.first)
32
+ when :&, :|, :'^'
33
+ raise ArgumentError if keys.empty?
34
+ bitwise_operation(operator, keys)
35
+ else
36
+ raise ArgumentError
37
+ end
38
+ end
39
+
40
+ def bitwise_not(key)
41
+ if value = get(keys.first)
42
+ value.bytes.map { |byte| ~ byte }.pack('c*')
43
+ end
44
+ end
45
+
46
+ def bitwise_operation(operation, keys)
47
+ apply_onto, *values = keys.map { |key| get(key) }.reject(&:nil?)
48
+ values.reduce(apply_onto) do |memo, value|
49
+ shorter, longer = [memo, value].sort_by(&:length).map(&:bytes).map(&:to_a)
50
+ longer.each_with_index.map do |byte, index|
51
+ byte.send(operation, shorter[index] || 0)
52
+ end.pack('c*')
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,25 @@
1
+ module FakeRedis
2
+ module CommandExecutor
3
+ def write(command)
4
+ meffod = command.shift.to_s.downcase.to_sym
5
+
6
+ if in_multi && !(TRANSACTION_COMMANDS.include? meffod) # queue commands
7
+ queued_commands << [meffod, *command]
8
+ reply = 'QUEUED'
9
+ elsif respond_to?(meffod)
10
+ reply = send(meffod, *command)
11
+ else
12
+ raise Redis::CommandError, "ERR unknown command '#{meffod}'"
13
+ end
14
+
15
+ if reply == true
16
+ reply = 1
17
+ elsif reply == false
18
+ reply = 0
19
+ end
20
+
21
+ replies << reply
22
+ nil
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,70 @@
1
+ module FakeRedis
2
+ # Represents a normal hash with some additional expiration information
3
+ # associated with each key
4
+ class ExpiringHash < Hash
5
+ attr_reader :expires
6
+
7
+ def initialize(*)
8
+ super
9
+ @expires = {}
10
+ end
11
+
12
+ def [](key)
13
+ key = normalize key
14
+ delete(key) if expired?(key)
15
+ super
16
+ end
17
+
18
+ def []=(key, val)
19
+ key = normalize key
20
+ expire(key)
21
+ super
22
+ end
23
+
24
+ def delete(key)
25
+ key = normalize key
26
+ expire(key)
27
+ super
28
+ end
29
+
30
+ def expire(key)
31
+ key = normalize key
32
+ expires.delete(key)
33
+ end
34
+
35
+ def expired?(key)
36
+ key = normalize key
37
+ expires.include?(key) && expires[key] <= Time.now
38
+ end
39
+
40
+ def key?(key)
41
+ key = normalize key
42
+ delete(key) if expired?(key)
43
+ super
44
+ end
45
+
46
+ def values_at(*keys)
47
+ keys.each do |key|
48
+ key = normalize(key)
49
+ delete(key) if expired?(key)
50
+ end
51
+ super
52
+ end
53
+
54
+ def keys
55
+ super.select do |key|
56
+ key = normalize(key)
57
+ if expired?(key)
58
+ delete(key)
59
+ false
60
+ else
61
+ true
62
+ end
63
+ end
64
+ end
65
+
66
+ def normalize key
67
+ key.to_s
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,24 @@
1
+ # Require this either in your Gemfile or in your minitest configuration.
2
+ # Examples:
3
+ #
4
+ # # Gemfile
5
+ # group :test do
6
+ # gem 'minitest'
7
+ # gem 'fakeredis', :require => 'fakeredis/minitest'
8
+ # end
9
+ #
10
+ # # test/test_helper.rb (or test/minitest_config.rb)
11
+ # require 'fakeredis/minitest'
12
+
13
+ require 'fakeredis'
14
+
15
+ module FakeRedis
16
+ module Minitest
17
+ def setup
18
+ super
19
+ Redis::Connection::Memory.reset_all_databases
20
+ end
21
+
22
+ ::Minitest::Test.send(:include, self)
23
+ end
24
+ end