mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
extract database_cleaner-redis adapter.
This commit is contained in:
parent
c4e41967cd
commit
9100251d67
25 changed files with 390 additions and 0 deletions
1
Gemfile
1
Gemfile
|
@ -10,4 +10,5 @@ path "./adapters" do
|
|||
gem "database_cleaner-moped"
|
||||
gem "database_cleaner-neo4j"
|
||||
gem "database_cleaner-ohm"
|
||||
gem "database_cleaner-redis"
|
||||
end
|
||||
|
|
|
@ -35,6 +35,9 @@ PATH
|
|||
database_cleaner (~> 1.8.0)
|
||||
database_cleaner-redis (~> 1.8.0)
|
||||
ohm
|
||||
database_cleaner-redis (1.8.0)
|
||||
database_cleaner (~> 1.8.0)
|
||||
redis
|
||||
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
|
@ -289,6 +292,7 @@ DEPENDENCIES
|
|||
database_cleaner-moped!
|
||||
database_cleaner-neo4j!
|
||||
database_cleaner-ohm!
|
||||
database_cleaner-redis!
|
||||
datamapper
|
||||
dm-migrations
|
||||
dm-sqlite-adapter
|
||||
|
|
11
adapters/database_cleaner-redis/.gitignore
vendored
Normal file
11
adapters/database_cleaner-redis/.gitignore
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
/.bundle/
|
||||
/.yardoc
|
||||
/_yardoc/
|
||||
/coverage/
|
||||
/doc/
|
||||
/pkg/
|
||||
/spec/reports/
|
||||
/tmp/
|
||||
|
||||
# rspec failure tracking
|
||||
.rspec_status
|
3
adapters/database_cleaner-redis/.rspec
Normal file
3
adapters/database_cleaner-redis/.rspec
Normal file
|
@ -0,0 +1,3 @@
|
|||
--format documentation
|
||||
--color
|
||||
--require spec_helper
|
5
adapters/database_cleaner-redis/.travis.yml
Normal file
5
adapters/database_cleaner-redis/.travis.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
sudo: false
|
||||
language: ruby
|
||||
rvm:
|
||||
- 2.2.9
|
||||
before_install: gem install bundler -v 1.16.1
|
8
adapters/database_cleaner-redis/Gemfile
Normal file
8
adapters/database_cleaner-redis/Gemfile
Normal file
|
@ -0,0 +1,8 @@
|
|||
source "https://rubygems.org"
|
||||
|
||||
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
||||
|
||||
# Specify your gem's dependencies in database_cleaner-redis.gemspec
|
||||
gemspec
|
||||
|
||||
gem "database_cleaner", path: "../.."
|
44
adapters/database_cleaner-redis/Gemfile.lock
Normal file
44
adapters/database_cleaner-redis/Gemfile.lock
Normal file
|
@ -0,0 +1,44 @@
|
|||
PATH
|
||||
remote: ../..
|
||||
specs:
|
||||
database_cleaner (1.8.0)
|
||||
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
database_cleaner-redis (1.8.0)
|
||||
database_cleaner (~> 1.8.0)
|
||||
redis
|
||||
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
diff-lcs (1.3)
|
||||
rake (10.4.2)
|
||||
redis (3.1.0)
|
||||
rspec (3.7.0)
|
||||
rspec-core (~> 3.7.0)
|
||||
rspec-expectations (~> 3.7.0)
|
||||
rspec-mocks (~> 3.7.0)
|
||||
rspec-core (3.7.1)
|
||||
rspec-support (~> 3.7.0)
|
||||
rspec-expectations (3.7.0)
|
||||
diff-lcs (>= 1.2.0, < 2.0)
|
||||
rspec-support (~> 3.7.0)
|
||||
rspec-mocks (3.7.0)
|
||||
diff-lcs (>= 1.2.0, < 2.0)
|
||||
rspec-support (~> 3.7.0)
|
||||
rspec-support (3.7.1)
|
||||
|
||||
PLATFORMS
|
||||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
bundler (~> 1.16)
|
||||
database_cleaner!
|
||||
database_cleaner-redis!
|
||||
rake (~> 10.0)
|
||||
rspec (~> 3.0)
|
||||
|
||||
BUNDLED WITH
|
||||
1.16.1
|
21
adapters/database_cleaner-redis/LICENSE.txt
Normal file
21
adapters/database_cleaner-redis/LICENSE.txt
Normal file
|
@ -0,0 +1,21 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2009 Ben Mabey
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
39
adapters/database_cleaner-redis/README.md
Normal file
39
adapters/database_cleaner-redis/README.md
Normal file
|
@ -0,0 +1,39 @@
|
|||
# DatabaseCleaner::Redis
|
||||
|
||||
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/database_cleaner/redis`. To experiment with that code, run `bin/console` for an interactive prompt.
|
||||
|
||||
TODO: Delete this and the text above, and describe your gem
|
||||
|
||||
## Installation
|
||||
|
||||
Add this line to your application's Gemfile:
|
||||
|
||||
```ruby
|
||||
gem 'database_cleaner-redis'
|
||||
```
|
||||
|
||||
And then execute:
|
||||
|
||||
$ bundle
|
||||
|
||||
Or install it yourself as:
|
||||
|
||||
$ gem install database_cleaner-redis
|
||||
|
||||
## Usage
|
||||
|
||||
TODO: Write usage instructions here
|
||||
|
||||
## Development
|
||||
|
||||
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
||||
|
||||
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
||||
|
||||
## Contributing
|
||||
|
||||
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/database_cleaner-redis.
|
||||
|
||||
## License
|
||||
|
||||
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
6
adapters/database_cleaner-redis/Rakefile
Normal file
6
adapters/database_cleaner-redis/Rakefile
Normal file
|
@ -0,0 +1,6 @@
|
|||
require "bundler/gem_tasks"
|
||||
require "rspec/core/rake_task"
|
||||
|
||||
RSpec::Core::RakeTask.new(:spec)
|
||||
|
||||
task :default => :spec
|
14
adapters/database_cleaner-redis/bin/console
Executable file
14
adapters/database_cleaner-redis/bin/console
Executable file
|
@ -0,0 +1,14 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require "bundler/setup"
|
||||
require "database_cleaner/redis"
|
||||
|
||||
# You can add fixtures and/or initialization code here to make experimenting
|
||||
# with your gem easier. You can also use a different console, if you like.
|
||||
|
||||
# (If you use this, don't forget to add pry to your Gemfile!)
|
||||
# require "pry"
|
||||
# Pry.start
|
||||
|
||||
require "irb"
|
||||
IRB.start(__FILE__)
|
8
adapters/database_cleaner-redis/bin/setup
Executable file
8
adapters/database_cleaner-redis/bin/setup
Executable file
|
@ -0,0 +1,8 @@
|
|||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
set -vx
|
||||
|
||||
bundle install
|
||||
|
||||
# Do any other automated setup that you need to do here
|
|
@ -0,0 +1,30 @@
|
|||
|
||||
lib = File.expand_path("../lib", __FILE__)
|
||||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
||||
require "database_cleaner/redis/version"
|
||||
|
||||
Gem::Specification.new do |spec|
|
||||
spec.name = "database_cleaner-redis"
|
||||
spec.version = DatabaseCleaner::Redis::VERSION
|
||||
spec.authors = ["Ernesto Tagwerker"]
|
||||
spec.email = ["ernesto@ombulabs.com"]
|
||||
|
||||
spec.summary = "Strategies for cleaning databases using Redis. Can be used to ensure a clean state for testing."
|
||||
spec.description = "Strategies for cleaning databases using Redis. Can be used to ensure a clean state for testing."
|
||||
spec.homepage = "https://github.com/DatabaseCleaner/database_cleaner-redis"
|
||||
spec.license = "MIT"
|
||||
|
||||
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
||||
f.match(%r{^(test|spec|features)/})
|
||||
end
|
||||
spec.bindir = "exe"
|
||||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
||||
spec.require_paths = ["lib"]
|
||||
|
||||
spec.add_dependency "database_cleaner", "~> 1.8.0"
|
||||
spec.add_dependency "redis"
|
||||
|
||||
spec.add_development_dependency "bundler", "~> 1.16"
|
||||
spec.add_development_dependency "rake", "~> 10.0"
|
||||
spec.add_development_dependency "rspec", "~> 3.0"
|
||||
end
|
|
@ -0,0 +1 @@
|
|||
require "database_cleaner/redis"
|
|
@ -0,0 +1,4 @@
|
|||
require "database_cleaner/redis/version"
|
||||
require "database_cleaner"
|
||||
require "database_cleaner/redis/truncation"
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
require 'database_cleaner/generic/base'
|
||||
|
||||
module DatabaseCleaner
|
||||
module Redis
|
||||
def self.available_strategies
|
||||
%w{truncation}
|
||||
end
|
||||
|
||||
module Base
|
||||
include ::DatabaseCleaner::Generic::Base
|
||||
|
||||
def db=(desired_db)
|
||||
@db = desired_db
|
||||
end
|
||||
|
||||
def db
|
||||
@db ||= :default
|
||||
end
|
||||
|
||||
alias url db
|
||||
|
||||
private
|
||||
|
||||
def connection
|
||||
@connection ||= begin
|
||||
if url == :default
|
||||
::Redis.new
|
||||
elsif db.is_a?(::Redis) # pass directly the connection
|
||||
db
|
||||
else
|
||||
::Redis.new(:url => url)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,26 @@
|
|||
require 'database_cleaner/redis/base'
|
||||
require 'database_cleaner/generic/truncation'
|
||||
|
||||
module DatabaseCleaner
|
||||
module Redis
|
||||
class Truncation
|
||||
include ::DatabaseCleaner::Redis::Base
|
||||
include ::DatabaseCleaner::Generic::Truncation
|
||||
|
||||
def clean
|
||||
if @only
|
||||
@only.each do |term|
|
||||
connection.keys(term).each { |k| connection.del k }
|
||||
end
|
||||
elsif @tables_to_exclude
|
||||
keys_except = []
|
||||
@tables_to_exclude.each { |term| keys_except += connection.keys(term) }
|
||||
connection.keys.each { |k| connection.del(k) unless keys_except.include?(k) }
|
||||
else
|
||||
connection.flushdb
|
||||
end
|
||||
connection.quit unless url == :default
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,5 @@
|
|||
module DatabaseCleaner
|
||||
module Redis
|
||||
VERSION = "1.8.0"
|
||||
end
|
||||
end
|
|
@ -0,0 +1,42 @@
|
|||
require 'redis'
|
||||
require 'database_cleaner/redis/base'
|
||||
require 'database_cleaner/spec'
|
||||
|
||||
module DatabaseCleaner
|
||||
RSpec.describe Redis do
|
||||
it { is_expected.to respond_to(:available_strategies) }
|
||||
end
|
||||
|
||||
module Redis
|
||||
class ExampleStrategy
|
||||
include ::DatabaseCleaner::Redis::Base
|
||||
end
|
||||
|
||||
RSpec.describe ExampleStrategy do
|
||||
|
||||
it_should_behave_like "a generic strategy"
|
||||
it { is_expected.to respond_to(:db) }
|
||||
it { is_expected.to respond_to(:db=) }
|
||||
|
||||
context "when passing url" do
|
||||
it "should store my describe db" do
|
||||
url = 'redis://localhost:6379/2'
|
||||
subject.db = 'redis://localhost:6379/2'
|
||||
expect(subject.db).to eq url
|
||||
end
|
||||
end
|
||||
|
||||
context "when passing connection" do
|
||||
it "should store my describe db" do
|
||||
connection = ::Redis.new :url => 'redis://localhost:6379/2'
|
||||
subject.db = connection
|
||||
expect(subject.db).to eq connection
|
||||
end
|
||||
end
|
||||
|
||||
it "should default to :default" do
|
||||
expect(subject.db).to eq :default
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,43 @@
|
|||
require 'redis'
|
||||
require 'database_cleaner/redis/truncation'
|
||||
|
||||
RSpec.describe DatabaseCleaner::Redis::Truncation do
|
||||
around do |example|
|
||||
config = YAML::load(File.open("spec/support/redis.yml"))
|
||||
@redis = ::Redis.new :url => config['test']['url']
|
||||
|
||||
example.run
|
||||
|
||||
@redis.flushdb
|
||||
end
|
||||
|
||||
before do
|
||||
@redis.set 'Widget', 1
|
||||
@redis.set 'Gadget', 1
|
||||
end
|
||||
|
||||
context "by default" do
|
||||
it "truncates all keys" do
|
||||
expect { subject.clean }.to change { @redis.keys.size }.from(2).to(0)
|
||||
end
|
||||
end
|
||||
|
||||
context "when keys are provided to the :only option" do
|
||||
subject { described_class.new(only: ['Widge*']) }
|
||||
|
||||
it "only truncates the specified keys" do
|
||||
expect { subject.clean }.to change { @redis.keys.size }.from(2).to(1)
|
||||
expect(@redis.get('Gadget')).to eq '1'
|
||||
end
|
||||
end
|
||||
|
||||
context "when keys are provided to the :except option" do
|
||||
subject { described_class.new(except: ['Widg*']) }
|
||||
|
||||
it "truncates all but the specified keys" do
|
||||
expect { subject.clean }.to change { @redis.keys.size }.from(2).to(1)
|
||||
expect(@redis.get('Widget')).to eq '1'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
14
adapters/database_cleaner-redis/spec/spec_helper.rb
Normal file
14
adapters/database_cleaner-redis/spec/spec_helper.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
require "bundler/setup"
|
||||
require "database_cleaner/redis"
|
||||
|
||||
RSpec.configure do |config|
|
||||
# Enable flags like --only-failures and --next-failure
|
||||
config.example_status_persistence_file_path = ".rspec_status"
|
||||
|
||||
# Disable RSpec exposing methods globally on `Module` and `main`
|
||||
config.disable_monkey_patching!
|
||||
|
||||
config.expect_with :rspec do |c|
|
||||
c.syntax = :expect
|
||||
end
|
||||
end
|
8
adapters/database_cleaner-redis/spec/support/redis.yml
Normal file
8
adapters/database_cleaner-redis/spec/support/redis.yml
Normal file
|
@ -0,0 +1,8 @@
|
|||
test:
|
||||
url: 'redis://localhost:6379/0'
|
||||
|
||||
one:
|
||||
url: 'redis://localhost:6379/1'
|
||||
|
||||
two:
|
||||
url: 'redis://localhost:6379/2'
|
|
@ -24,6 +24,7 @@ Feature: database cleaning
|
|||
| Neo4j | truncation |
|
||||
| Neo4j | transaction |
|
||||
| Ohm | truncation |
|
||||
| Redis | truncation |
|
||||
|
||||
Scenario Outline: ruby app
|
||||
Given I am using <ORM>
|
||||
|
|
|
@ -19,6 +19,7 @@ Feature: database cleaning
|
|||
| MongoMapper |
|
||||
| Neo4j |
|
||||
| Ohm |
|
||||
| Redis |
|
||||
|
||||
Scenario Outline: ruby app
|
||||
Given I am using <ORM>
|
||||
|
|
|
@ -17,42 +17,56 @@ Feature: database cleaning using multiple ORMs
|
|||
| ActiveRecord | MongoMapper |
|
||||
| ActiveRecord | Neo4j |
|
||||
| ActiveRecord | Ohm |
|
||||
| ActiveRecord | Redis |
|
||||
| CouchPotato | ActiveRecord |
|
||||
| CouchPotato | DataMapper |
|
||||
| CouchPotato | Mongoid |
|
||||
| CouchPotato | MongoMapper |
|
||||
| CouchPotato | Neo4j |
|
||||
| CouchPotato | Ohm |
|
||||
| CouchPotato | Redis |
|
||||
| DataMapper | ActiveRecord |
|
||||
| DataMapper | CouchPotato |
|
||||
| DataMapper | Mongoid |
|
||||
| DataMapper | MongoMapper |
|
||||
| DataMapper | Neo4j |
|
||||
| DataMapper | Ohm |
|
||||
| DataMapper | Redis |
|
||||
| Mongoid | ActiveRecord |
|
||||
| Mongoid | CouchPotato |
|
||||
| Mongoid | DataMapper |
|
||||
| Mongoid | MongoMapper |
|
||||
| Mongoid | Neo4j |
|
||||
| Mongoid | Ohm |
|
||||
| Mongoid | Redis |
|
||||
| MongoMapper | ActiveRecord |
|
||||
| MongoMapper | CouchPotato |
|
||||
| MongoMapper | DataMapper |
|
||||
| MongoMapper | Mongoid |
|
||||
| MongoMapper | Neo4j |
|
||||
| MongoMapper | Ohm |
|
||||
| MongoMapper | Redis |
|
||||
| Neo4j | ActiveRecord |
|
||||
| Neo4j | CouchPotato |
|
||||
| Neo4j | DataMapper |
|
||||
| Neo4j | Mongoid |
|
||||
| Neo4j | MongoMapper |
|
||||
| Neo4j | Ohm |
|
||||
| Neo4j | Redis |
|
||||
| Ohm | ActiveRecord |
|
||||
| Ohm | CouchPotato |
|
||||
| Ohm | DataMapper |
|
||||
| Ohm | Mongoid |
|
||||
| Ohm | MongoMapper |
|
||||
| Ohm | Neo4j |
|
||||
| Ohm | Redis |
|
||||
| Redis | ActiveRecord |
|
||||
| Redis | DataMapper |
|
||||
| Redis | CouchPotato |
|
||||
| Redis | Mongoid |
|
||||
| Redis | MongoMapper |
|
||||
| Redis | Neo4j |
|
||||
| Redis | Ohm |
|
||||
|
||||
Scenario Outline: ruby app
|
||||
Given I am using <ORM1> and <ORM2>
|
||||
|
|
Loading…
Reference in a new issue