mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
extract database_cleaner-mongo adapter.
This commit is contained in:
parent
95619b2e74
commit
9d92ed033b
22 changed files with 399 additions and 0 deletions
1
Gemfile
1
Gemfile
|
@ -4,4 +4,5 @@ path "./adapters" do
|
|||
gem "database_cleaner-active_record"
|
||||
gem "database_cleaner-couch_potato"
|
||||
gem "database_cleaner-data_mapper"
|
||||
gem "database_cleaner-mongo"
|
||||
end
|
||||
|
|
|
@ -15,6 +15,9 @@ PATH
|
|||
database_cleaner-data_mapper (1.8.0)
|
||||
database_cleaner (~> 1.8.0)
|
||||
datamapper
|
||||
database_cleaner-mongo (1.8.0)
|
||||
database_cleaner (~> 1.8.0)
|
||||
mongo
|
||||
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
|
@ -263,6 +266,7 @@ DEPENDENCIES
|
|||
database_cleaner-active_record!
|
||||
database_cleaner-couch_potato!
|
||||
database_cleaner-data_mapper!
|
||||
database_cleaner-mongo!
|
||||
datamapper
|
||||
dm-migrations
|
||||
dm-sqlite-adapter
|
||||
|
|
11
adapters/database_cleaner-mongo/.gitignore
vendored
Normal file
11
adapters/database_cleaner-mongo/.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-mongo/.rspec
Normal file
3
adapters/database_cleaner-mongo/.rspec
Normal file
|
@ -0,0 +1,3 @@
|
|||
--format documentation
|
||||
--color
|
||||
--require spec_helper
|
5
adapters/database_cleaner-mongo/.travis.yml
Normal file
5
adapters/database_cleaner-mongo/.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-mongo/Gemfile
Normal file
8
adapters/database_cleaner-mongo/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-mongo.gemspec
|
||||
gemspec
|
||||
|
||||
gem "database_cleaner", path: "../.."
|
46
adapters/database_cleaner-mongo/Gemfile.lock
Normal file
46
adapters/database_cleaner-mongo/Gemfile.lock
Normal file
|
@ -0,0 +1,46 @@
|
|||
PATH
|
||||
remote: ../..
|
||||
specs:
|
||||
database_cleaner (1.8.0)
|
||||
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
database_cleaner-mongo (1.8.0)
|
||||
database_cleaner (~> 1.8.0)
|
||||
mongo
|
||||
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
bson (1.12.5)
|
||||
diff-lcs (1.3)
|
||||
mongo (1.12.5)
|
||||
bson (= 1.12.5)
|
||||
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-mongo!
|
||||
rake (~> 10.0)
|
||||
rspec (~> 3.0)
|
||||
|
||||
BUNDLED WITH
|
||||
1.16.1
|
21
adapters/database_cleaner-mongo/LICENSE.txt
Normal file
21
adapters/database_cleaner-mongo/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-mongo/README.md
Normal file
39
adapters/database_cleaner-mongo/README.md
Normal file
|
@ -0,0 +1,39 @@
|
|||
# DatabaseCleaner::Mongo
|
||||
|
||||
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/mongo`. 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-mongo'
|
||||
```
|
||||
|
||||
And then execute:
|
||||
|
||||
$ bundle
|
||||
|
||||
Or install it yourself as:
|
||||
|
||||
$ gem install database_cleaner-mongo
|
||||
|
||||
## 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-mongo.
|
||||
|
||||
## License
|
||||
|
||||
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
6
adapters/database_cleaner-mongo/Rakefile
Normal file
6
adapters/database_cleaner-mongo/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-mongo/bin/console
Executable file
14
adapters/database_cleaner-mongo/bin/console
Executable file
|
@ -0,0 +1,14 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require "bundler/setup"
|
||||
require "database_cleaner/mongo"
|
||||
|
||||
# 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-mongo/bin/setup
Executable file
8
adapters/database_cleaner-mongo/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/mongo/version"
|
||||
|
||||
Gem::Specification.new do |spec|
|
||||
spec.name = "database_cleaner-mongo"
|
||||
spec.version = DatabaseCleaner::Mongo::VERSION
|
||||
spec.authors = ["Ernesto Tagwerker"]
|
||||
spec.email = ["ernesto@ombulabs.com"]
|
||||
|
||||
spec.summary = "Strategies for cleaning databases using Mongo. Can be used to ensure a clean state for testing."
|
||||
spec.description = "Strategies for cleaning databases using Mongo. Can be used to ensure a clean state for testing."
|
||||
spec.homepage = "https://github.com/DatabaseCleaner/database_cleaner-mongo"
|
||||
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 "mongo"
|
||||
|
||||
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/mongo"
|
|
@ -0,0 +1,3 @@
|
|||
require 'database_cleaner/mongo/version'
|
||||
require 'database_cleaner'
|
||||
require 'database_cleaner/mongo/truncation'
|
|
@ -0,0 +1,16 @@
|
|||
module DatabaseCleaner
|
||||
module Mongo
|
||||
def self.available_strategies
|
||||
%w[truncation]
|
||||
end
|
||||
module Base
|
||||
def db=(desired_db)
|
||||
@db = desired_db
|
||||
end
|
||||
|
||||
def db
|
||||
@db || raise("You have not specified a database. (see Mongo::Database)")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,62 @@
|
|||
require 'database_cleaner/mongo/base'
|
||||
require 'database_cleaner/generic/truncation'
|
||||
require 'database_cleaner/mongo/truncation_mixin'
|
||||
module DatabaseCleaner
|
||||
module Mongo
|
||||
class Truncation
|
||||
include ::DatabaseCleaner::Generic::Truncation
|
||||
include TruncationMixin
|
||||
include Base
|
||||
private
|
||||
|
||||
def database
|
||||
db
|
||||
end
|
||||
|
||||
def collections_cache
|
||||
@@collections_cache ||= {}
|
||||
end
|
||||
|
||||
def mongoid_collection_names
|
||||
@@mongoid_collection_names ||= Hash.new{|h,k| h[k]=[]}.tap do |names|
|
||||
ObjectSpace.each_object(Class) do |klass|
|
||||
(names[klass.db.name] << klass.collection_name) if valid_collection_name?(klass)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def not_caching(db_name, list)
|
||||
@@not_caching ||= {}
|
||||
|
||||
unless @@not_caching.has_key?(db_name)
|
||||
@@not_caching[db_name] = true
|
||||
|
||||
puts "Not caching collection names for db #{db_name}. Missing these from models: #{list}"
|
||||
end
|
||||
end
|
||||
|
||||
def collections
|
||||
return collections_cache[database.name] if collections_cache.has_key?(database.name)
|
||||
db_collections = database.collections.select { |c| c.name !~ /^system\./ }
|
||||
|
||||
missing_collections = mongoid_collection_names[database.name] - db_collections.map(&:name)
|
||||
|
||||
if missing_collections.empty?
|
||||
collections_cache[database.name] = db_collections
|
||||
else
|
||||
not_caching(database.name, missing_collections)
|
||||
end
|
||||
|
||||
db_collections
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_collection_name?(klass)
|
||||
klass.ancestors.map(&:to_s).include?('Mongoid::Document') &&
|
||||
!klass.embedded &&
|
||||
!klass.collection_name.empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,26 @@
|
|||
module DatabaseCleaner
|
||||
module Mongo
|
||||
module TruncationMixin
|
||||
|
||||
def clean
|
||||
if @only
|
||||
collections.each { |c| c.send(truncate_method_name) if @only.include?(c.name) }
|
||||
else
|
||||
collections.each { |c| c.send(truncate_method_name) unless @tables_to_exclude.include?(c.name) }
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def collections
|
||||
database.collections.select { |c| c.name !~ /^system\./ }
|
||||
end
|
||||
|
||||
def truncate_method_name
|
||||
# This constant only exists in the 2.x series.
|
||||
defined?(::Mongo::VERSION) ? :delete_many : :remove
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,5 @@
|
|||
module DatabaseCleaner
|
||||
module Mongo
|
||||
VERSION = "1.8.0"
|
||||
end
|
||||
end
|
|
@ -0,0 +1,26 @@
|
|||
module MongoTest
|
||||
class Base
|
||||
def self.collection
|
||||
@connection ||= Mongo::Connection.new('127.0.0.1')
|
||||
@db ||= @connection.db('database_cleaner_specs')
|
||||
@collection ||= @db.collection(name) || @db.create_collection(name)
|
||||
end
|
||||
|
||||
def self.count
|
||||
@collection.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
|
||||
end
|
|
@ -0,0 +1,50 @@
|
|||
require 'mongo'
|
||||
require 'database_cleaner/mongo/truncation'
|
||||
require File.dirname(__FILE__) + '/mongo_examples'
|
||||
|
||||
RSpec.describe DatabaseCleaner::Mongo::Truncation do
|
||||
around do |example|
|
||||
connection = Mongo::Connection.new('127.0.0.1')
|
||||
db_name = 'database_cleaner_specs'
|
||||
db = connection.db(db_name)
|
||||
subject.db = db
|
||||
|
||||
example.run
|
||||
|
||||
connection.drop_database(db_name)
|
||||
end
|
||||
|
||||
before do
|
||||
MongoTest::Widget.new(name: 'some widget').save!
|
||||
MongoTest::Gadget.new(name: 'some gadget').save!
|
||||
end
|
||||
|
||||
context "by default" do
|
||||
it "truncates all collections" do
|
||||
expect { subject.clean }.to change {
|
||||
[MongoTest::Widget.count, MongoTest::Gadget.count]
|
||||
}.from([1,1]).to([0,0])
|
||||
end
|
||||
end
|
||||
|
||||
context "when collections are provided to the :only option" do
|
||||
subject { described_class.new(only: ['MongoTest::Widget']) }
|
||||
|
||||
it "only truncates the specified collections" do
|
||||
expect { subject.clean }.to change {
|
||||
[MongoTest::Widget.count, MongoTest::Gadget.count]
|
||||
}.from([1,1]).to([0,1])
|
||||
end
|
||||
end
|
||||
|
||||
context "when collections are provided to the :except option" do
|
||||
subject { described_class.new(except: ['MongoTest::Widget']) }
|
||||
|
||||
it "truncates all but the specified collections" do
|
||||
expect { subject.clean }.to change {
|
||||
[MongoTest::Widget.count, MongoTest::Gadget.count]
|
||||
}.from([1,1]).to([1,0])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
14
adapters/database_cleaner-mongo/spec/spec_helper.rb
Normal file
14
adapters/database_cleaner-mongo/spec/spec_helper.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
require "bundler/setup"
|
||||
require "database_cleaner/mongo"
|
||||
|
||||
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 a new issue