From 9d92ed033b3cba95a7f322e5925040d131ffb77a Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Fri, 25 May 2018 23:11:47 -0700 Subject: [PATCH] extract database_cleaner-mongo adapter. --- Gemfile | 1 + Gemfile.lock | 4 ++ adapters/database_cleaner-mongo/.gitignore | 11 ++++ adapters/database_cleaner-mongo/.rspec | 3 + adapters/database_cleaner-mongo/.travis.yml | 5 ++ adapters/database_cleaner-mongo/Gemfile | 8 +++ adapters/database_cleaner-mongo/Gemfile.lock | 46 ++++++++++++++ adapters/database_cleaner-mongo/LICENSE.txt | 21 +++++++ adapters/database_cleaner-mongo/README.md | 39 ++++++++++++ adapters/database_cleaner-mongo/Rakefile | 6 ++ adapters/database_cleaner-mongo/bin/console | 14 +++++ adapters/database_cleaner-mongo/bin/setup | 8 +++ .../database_cleaner-mongo.gemspec | 30 +++++++++ .../lib/database_cleaner-mongo.rb | 1 + .../lib/database_cleaner/mongo.rb | 3 + .../lib/database_cleaner/mongo/base.rb | 16 +++++ .../lib/database_cleaner/mongo/truncation.rb | 62 +++++++++++++++++++ .../mongo/truncation_mixin.rb | 26 ++++++++ .../lib/database_cleaner/mongo/version.rb | 5 ++ .../database_cleaner/mongo/mongo_examples.rb | 26 ++++++++ .../database_cleaner/mongo/truncation_spec.rb | 50 +++++++++++++++ .../spec/spec_helper.rb | 14 +++++ 22 files changed, 399 insertions(+) create mode 100644 adapters/database_cleaner-mongo/.gitignore create mode 100644 adapters/database_cleaner-mongo/.rspec create mode 100644 adapters/database_cleaner-mongo/.travis.yml create mode 100644 adapters/database_cleaner-mongo/Gemfile create mode 100644 adapters/database_cleaner-mongo/Gemfile.lock create mode 100644 adapters/database_cleaner-mongo/LICENSE.txt create mode 100644 adapters/database_cleaner-mongo/README.md create mode 100644 adapters/database_cleaner-mongo/Rakefile create mode 100755 adapters/database_cleaner-mongo/bin/console create mode 100755 adapters/database_cleaner-mongo/bin/setup create mode 100644 adapters/database_cleaner-mongo/database_cleaner-mongo.gemspec create mode 100644 adapters/database_cleaner-mongo/lib/database_cleaner-mongo.rb create mode 100644 adapters/database_cleaner-mongo/lib/database_cleaner/mongo.rb create mode 100644 adapters/database_cleaner-mongo/lib/database_cleaner/mongo/base.rb create mode 100644 adapters/database_cleaner-mongo/lib/database_cleaner/mongo/truncation.rb create mode 100644 adapters/database_cleaner-mongo/lib/database_cleaner/mongo/truncation_mixin.rb create mode 100644 adapters/database_cleaner-mongo/lib/database_cleaner/mongo/version.rb create mode 100644 adapters/database_cleaner-mongo/spec/database_cleaner/mongo/mongo_examples.rb create mode 100644 adapters/database_cleaner-mongo/spec/database_cleaner/mongo/truncation_spec.rb create mode 100644 adapters/database_cleaner-mongo/spec/spec_helper.rb diff --git a/Gemfile b/Gemfile index 2416d81..d22c030 100644 --- a/Gemfile +++ b/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 diff --git a/Gemfile.lock b/Gemfile.lock index 0da4202..b2fa28f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/adapters/database_cleaner-mongo/.gitignore b/adapters/database_cleaner-mongo/.gitignore new file mode 100644 index 0000000..b04a8c8 --- /dev/null +++ b/adapters/database_cleaner-mongo/.gitignore @@ -0,0 +1,11 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ + +# rspec failure tracking +.rspec_status diff --git a/adapters/database_cleaner-mongo/.rspec b/adapters/database_cleaner-mongo/.rspec new file mode 100644 index 0000000..34c5164 --- /dev/null +++ b/adapters/database_cleaner-mongo/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/adapters/database_cleaner-mongo/.travis.yml b/adapters/database_cleaner-mongo/.travis.yml new file mode 100644 index 0000000..0dc6355 --- /dev/null +++ b/adapters/database_cleaner-mongo/.travis.yml @@ -0,0 +1,5 @@ +sudo: false +language: ruby +rvm: + - 2.2.9 +before_install: gem install bundler -v 1.16.1 diff --git a/adapters/database_cleaner-mongo/Gemfile b/adapters/database_cleaner-mongo/Gemfile new file mode 100644 index 0000000..8d40bd0 --- /dev/null +++ b/adapters/database_cleaner-mongo/Gemfile @@ -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: "../.." diff --git a/adapters/database_cleaner-mongo/Gemfile.lock b/adapters/database_cleaner-mongo/Gemfile.lock new file mode 100644 index 0000000..daefd40 --- /dev/null +++ b/adapters/database_cleaner-mongo/Gemfile.lock @@ -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 diff --git a/adapters/database_cleaner-mongo/LICENSE.txt b/adapters/database_cleaner-mongo/LICENSE.txt new file mode 100644 index 0000000..392bc0d --- /dev/null +++ b/adapters/database_cleaner-mongo/LICENSE.txt @@ -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. diff --git a/adapters/database_cleaner-mongo/README.md b/adapters/database_cleaner-mongo/README.md new file mode 100644 index 0000000..8f9be5c --- /dev/null +++ b/adapters/database_cleaner-mongo/README.md @@ -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). diff --git a/adapters/database_cleaner-mongo/Rakefile b/adapters/database_cleaner-mongo/Rakefile new file mode 100644 index 0000000..b7e9ed5 --- /dev/null +++ b/adapters/database_cleaner-mongo/Rakefile @@ -0,0 +1,6 @@ +require "bundler/gem_tasks" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new(:spec) + +task :default => :spec diff --git a/adapters/database_cleaner-mongo/bin/console b/adapters/database_cleaner-mongo/bin/console new file mode 100755 index 0000000..e35b53f --- /dev/null +++ b/adapters/database_cleaner-mongo/bin/console @@ -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__) diff --git a/adapters/database_cleaner-mongo/bin/setup b/adapters/database_cleaner-mongo/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/adapters/database_cleaner-mongo/bin/setup @@ -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 diff --git a/adapters/database_cleaner-mongo/database_cleaner-mongo.gemspec b/adapters/database_cleaner-mongo/database_cleaner-mongo.gemspec new file mode 100644 index 0000000..a370c7b --- /dev/null +++ b/adapters/database_cleaner-mongo/database_cleaner-mongo.gemspec @@ -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 diff --git a/adapters/database_cleaner-mongo/lib/database_cleaner-mongo.rb b/adapters/database_cleaner-mongo/lib/database_cleaner-mongo.rb new file mode 100644 index 0000000..3dde9b2 --- /dev/null +++ b/adapters/database_cleaner-mongo/lib/database_cleaner-mongo.rb @@ -0,0 +1 @@ +require "database_cleaner/mongo" diff --git a/adapters/database_cleaner-mongo/lib/database_cleaner/mongo.rb b/adapters/database_cleaner-mongo/lib/database_cleaner/mongo.rb new file mode 100644 index 0000000..6fbfc7c --- /dev/null +++ b/adapters/database_cleaner-mongo/lib/database_cleaner/mongo.rb @@ -0,0 +1,3 @@ +require 'database_cleaner/mongo/version' +require 'database_cleaner' +require 'database_cleaner/mongo/truncation' diff --git a/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/base.rb b/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/base.rb new file mode 100644 index 0000000..809a6e2 --- /dev/null +++ b/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/base.rb @@ -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 diff --git a/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/truncation.rb b/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/truncation.rb new file mode 100644 index 0000000..a09d1ff --- /dev/null +++ b/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/truncation.rb @@ -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 diff --git a/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/truncation_mixin.rb b/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/truncation_mixin.rb new file mode 100644 index 0000000..755b3df --- /dev/null +++ b/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/truncation_mixin.rb @@ -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 diff --git a/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/version.rb b/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/version.rb new file mode 100644 index 0000000..3c7596e --- /dev/null +++ b/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/version.rb @@ -0,0 +1,5 @@ +module DatabaseCleaner + module Mongo + VERSION = "1.8.0" + end +end diff --git a/adapters/database_cleaner-mongo/spec/database_cleaner/mongo/mongo_examples.rb b/adapters/database_cleaner-mongo/spec/database_cleaner/mongo/mongo_examples.rb new file mode 100644 index 0000000..0360294 --- /dev/null +++ b/adapters/database_cleaner-mongo/spec/database_cleaner/mongo/mongo_examples.rb @@ -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 diff --git a/adapters/database_cleaner-mongo/spec/database_cleaner/mongo/truncation_spec.rb b/adapters/database_cleaner-mongo/spec/database_cleaner/mongo/truncation_spec.rb new file mode 100644 index 0000000..9cf1dff --- /dev/null +++ b/adapters/database_cleaner-mongo/spec/database_cleaner/mongo/truncation_spec.rb @@ -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 + diff --git a/adapters/database_cleaner-mongo/spec/spec_helper.rb b/adapters/database_cleaner-mongo/spec/spec_helper.rb new file mode 100644 index 0000000..5797f97 --- /dev/null +++ b/adapters/database_cleaner-mongo/spec/spec_helper.rb @@ -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