extract database_cleaner-moped adapter.
This commit is contained in:
parent
3c9b4c1a41
commit
7aca1791d0
1
Gemfile
1
Gemfile
|
@ -7,4 +7,5 @@ path "./adapters" do
|
|||
gem "database_cleaner-mongo"
|
||||
gem "database_cleaner-mongoid"
|
||||
gem "database_cleaner-mongo_mapper"
|
||||
gem "database_cleaner-moped"
|
||||
end
|
||||
|
|
|
@ -25,6 +25,9 @@ PATH
|
|||
database_cleaner (~> 1.8.0)
|
||||
database_cleaner-mongo (~> 1.8.0)
|
||||
mongoid
|
||||
database_cleaner-moped (1.8.0)
|
||||
database_cleaner (~> 1.8.0)
|
||||
moped
|
||||
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
|
@ -276,6 +279,7 @@ DEPENDENCIES
|
|||
database_cleaner-mongo!
|
||||
database_cleaner-mongo_mapper!
|
||||
database_cleaner-mongoid!
|
||||
database_cleaner-moped!
|
||||
datamapper
|
||||
dm-migrations
|
||||
dm-sqlite-adapter
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
/.bundle/
|
||||
/.yardoc
|
||||
/_yardoc/
|
||||
/coverage/
|
||||
/doc/
|
||||
/pkg/
|
||||
/spec/reports/
|
||||
/tmp/
|
||||
|
||||
# rspec failure tracking
|
||||
.rspec_status
|
|
@ -0,0 +1,3 @@
|
|||
--format documentation
|
||||
--color
|
||||
--require spec_helper
|
|
@ -0,0 +1,5 @@
|
|||
sudo: false
|
||||
language: ruby
|
||||
rvm:
|
||||
- 2.2.9
|
||||
before_install: gem install bundler -v 1.16.1
|
|
@ -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-moped.gemspec
|
||||
gemspec
|
||||
|
||||
gem "database_cleaner", path: "../.."
|
|
@ -0,0 +1,44 @@
|
|||
PATH
|
||||
remote: ../..
|
||||
specs:
|
||||
database_cleaner (1.8.0)
|
||||
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
database_cleaner-moped (1.8.0)
|
||||
database_cleaner (~> 1.8.0)
|
||||
moped
|
||||
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
diff-lcs (1.3)
|
||||
moped (1.5.2)
|
||||
rake (10.4.2)
|
||||
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-moped!
|
||||
rake (~> 10.0)
|
||||
rspec (~> 3.0)
|
||||
|
||||
BUNDLED WITH
|
||||
1.16.1
|
|
@ -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.
|
|
@ -0,0 +1,39 @@
|
|||
# DatabaseCleaner::Moped
|
||||
|
||||
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/moped`. 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-moped'
|
||||
```
|
||||
|
||||
And then execute:
|
||||
|
||||
$ bundle
|
||||
|
||||
Or install it yourself as:
|
||||
|
||||
$ gem install database_cleaner-moped
|
||||
|
||||
## 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-moped.
|
||||
|
||||
## License
|
||||
|
||||
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@ -0,0 +1,6 @@
|
|||
require "bundler/gem_tasks"
|
||||
require "rspec/core/rake_task"
|
||||
|
||||
RSpec::Core::RakeTask.new(:spec)
|
||||
|
||||
task :default => :spec
|
|
@ -0,0 +1,14 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require "bundler/setup"
|
||||
require "database_cleaner/moped"
|
||||
|
||||
# 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__)
|
|
@ -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/moped/version"
|
||||
|
||||
Gem::Specification.new do |spec|
|
||||
spec.name = "database_cleaner-moped"
|
||||
spec.version = DatabaseCleaner::Moped::VERSION
|
||||
spec.authors = ["Ernesto Tagwerker"]
|
||||
spec.email = ["ernesto@ombulabs.com"]
|
||||
|
||||
spec.summary = "Strategies for cleaning databases using Moped. Can be used to ensure a clean state for testing."
|
||||
spec.description = "Strategies for cleaning databases using Moped. Can be used to ensure a clean state for testing."
|
||||
spec.homepage = "https://github.com/DatabaseCleaner/database_cleaner-moped"
|
||||
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 "moped"
|
||||
|
||||
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/moped"
|
|
@ -0,0 +1,4 @@
|
|||
require "database_cleaner/moped/version"
|
||||
require "database_cleaner"
|
||||
require "database_cleaner/moped/truncation"
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
require 'database_cleaner/generic/base'
|
||||
|
||||
module DatabaseCleaner
|
||||
module Moped
|
||||
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
|
||||
|
||||
def host_port=(desired_host)
|
||||
@host = desired_host
|
||||
end
|
||||
|
||||
def host
|
||||
@host ||= '127.0.0.1:27017'
|
||||
end
|
||||
|
||||
def db_version
|
||||
@db_version ||= session.command('buildinfo' => 1)['version']
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def session
|
||||
@session ||= ::Moped::Session.new([host], database: db)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,9 @@
|
|||
require 'database_cleaner/moped/truncation_base'
|
||||
|
||||
module DatabaseCleaner
|
||||
module Moped
|
||||
class Truncation
|
||||
include ::DatabaseCleaner::Moped::TruncationBase
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,44 @@
|
|||
require 'database_cleaner/moped/base'
|
||||
require 'database_cleaner/generic/truncation'
|
||||
|
||||
module DatabaseCleaner
|
||||
module Moped
|
||||
module TruncationBase
|
||||
include ::DatabaseCleaner::Moped::Base
|
||||
include ::DatabaseCleaner::Generic::Truncation
|
||||
|
||||
def clean
|
||||
if @only
|
||||
collections.each { |c| session[c].find.remove_all if @only.include?(c) }
|
||||
else
|
||||
collections.each { |c| session[c].find.remove_all unless @tables_to_exclude.include?(c) }
|
||||
end
|
||||
wait_for_removals_to_finish
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def collections
|
||||
if db != :default
|
||||
session.use(db)
|
||||
end
|
||||
|
||||
if db_version.split('.').first.to_i >= 3
|
||||
session.command(listCollections: 1, filter: { 'name' => { '$not' => /.?system\.|\$/ } })['cursor']['firstBatch'].map do |collection|
|
||||
collection['name']
|
||||
end
|
||||
else
|
||||
session['system.namespaces'].find(name: { '$not' => /\.system\.|\$/ }).to_a.map do |collection|
|
||||
_, name = collection['name'].split('.', 2)
|
||||
name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def wait_for_removals_to_finish
|
||||
session.command(getlasterror: 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,5 @@
|
|||
module DatabaseCleaner
|
||||
module Moped
|
||||
VERSION = "1.8.0"
|
||||
end
|
||||
end
|
|
@ -0,0 +1,32 @@
|
|||
module MopedTest
|
||||
class Base
|
||||
def self.collection
|
||||
@db ||= 'database_cleaner_specs'
|
||||
@session ||= ::Moped::Session.new(['127.0.0.1:27017'], database: @db)
|
||||
@collection ||= @session[name]
|
||||
end
|
||||
|
||||
def self.count
|
||||
@collection.find.count
|
||||
end
|
||||
|
||||
def initialize(attrs={})
|
||||
@attrs = attrs
|
||||
end
|
||||
|
||||
def save!
|
||||
self.class.collection.insert(@attrs)
|
||||
end
|
||||
end
|
||||
|
||||
class Widget < Base
|
||||
end
|
||||
class Gadget < Base
|
||||
end
|
||||
class System < Base
|
||||
def self.collection
|
||||
super
|
||||
@collection = @session['system_logs']
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,51 @@
|
|||
require 'moped'
|
||||
require 'database_cleaner/moped/truncation'
|
||||
require File.dirname(__FILE__) + '/moped_examples'
|
||||
|
||||
RSpec.describe DatabaseCleaner::Moped::Truncation do
|
||||
around do |example|
|
||||
db_name = 'database_cleaner_specs'
|
||||
session = ::Moped::Session.new(['127.0.0.1:27017'], database: db_name)
|
||||
subject.db = db_name
|
||||
|
||||
example.run
|
||||
|
||||
session.drop
|
||||
session.command(getlasterror: 1)
|
||||
end
|
||||
|
||||
before do
|
||||
MopedTest::Widget.new(name: 'some widget').save!
|
||||
MopedTest::Gadget.new(name: 'some gadget').save!
|
||||
MopedTest::System.new(name: 'some system').save!
|
||||
end
|
||||
|
||||
context "by default" do
|
||||
it "truncates all collections" do
|
||||
expect { subject.clean }.to change {
|
||||
[MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count]
|
||||
}.from([1,1,1]).to([0,0,0])
|
||||
end
|
||||
end
|
||||
|
||||
context "when collections are provided to the :only option" do
|
||||
subject { described_class.new(only: ['MopedTest::Widget']) }
|
||||
|
||||
it "only truncates the specified collections" do
|
||||
expect { subject.clean }.to change {
|
||||
[MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count]
|
||||
}.from([1,1,1]).to([0,1,1])
|
||||
end
|
||||
end
|
||||
|
||||
context "when collections are provided to the :except option" do
|
||||
subject { described_class.new(except: ['MopedTest::Widget']) }
|
||||
|
||||
it "truncates all but the specified collections" do
|
||||
expect { subject.clean }.to change {
|
||||
[MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count]
|
||||
}.from([1,1,1]).to([1,0,0])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
require "bundler/setup"
|
||||
require "database_cleaner/moped"
|
||||
|
||||
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
|
Loading…
Reference in New Issue