From 07f84e9b7202b3136952abcbcfca2fcd50ee997f Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Fri, 25 May 2018 23:38:08 -0700 Subject: [PATCH] extract database_cleaner-mongoid adapter. --- Gemfile | 1 + Gemfile.lock | 5 ++ adapters/database_cleaner-mongoid/.gitignore | 11 +++ adapters/database_cleaner-mongoid/.rspec | 3 + adapters/database_cleaner-mongoid/.travis.yml | 5 ++ adapters/database_cleaner-mongoid/Gemfile | 9 +++ .../database_cleaner-mongoid/Gemfile.lock | 67 +++++++++++++++++++ adapters/database_cleaner-mongoid/LICENSE.txt | 21 ++++++ adapters/database_cleaner-mongoid/README.md | 39 +++++++++++ adapters/database_cleaner-mongoid/Rakefile | 6 ++ adapters/database_cleaner-mongoid/bin/console | 14 ++++ adapters/database_cleaner-mongoid/bin/setup | 8 +++ .../database_cleaner-mongoid.gemspec | 31 +++++++++ .../lib/database_cleaner-mongoid.rb | 1 + .../lib/database_cleaner/mongoid.rb | 3 + .../lib/database_cleaner/mongoid/base.rb | 20 ++++++ .../database_cleaner/mongoid/truncation.rb | 49 ++++++++++++++ .../lib/database_cleaner/mongoid/version.rb | 5 ++ .../spec/spec_helper.rb | 14 ++++ features/cleaning.feature | 1 + features/cleaning_default_strategy.feature | 1 + features/cleaning_multiple_orms.feature | 6 ++ 22 files changed, 320 insertions(+) create mode 100644 adapters/database_cleaner-mongoid/.gitignore create mode 100644 adapters/database_cleaner-mongoid/.rspec create mode 100644 adapters/database_cleaner-mongoid/.travis.yml create mode 100644 adapters/database_cleaner-mongoid/Gemfile create mode 100644 adapters/database_cleaner-mongoid/Gemfile.lock create mode 100644 adapters/database_cleaner-mongoid/LICENSE.txt create mode 100644 adapters/database_cleaner-mongoid/README.md create mode 100644 adapters/database_cleaner-mongoid/Rakefile create mode 100755 adapters/database_cleaner-mongoid/bin/console create mode 100755 adapters/database_cleaner-mongoid/bin/setup create mode 100644 adapters/database_cleaner-mongoid/database_cleaner-mongoid.gemspec create mode 100644 adapters/database_cleaner-mongoid/lib/database_cleaner-mongoid.rb create mode 100644 adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid.rb create mode 100644 adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/base.rb create mode 100644 adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/truncation.rb create mode 100644 adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/version.rb create mode 100644 adapters/database_cleaner-mongoid/spec/spec_helper.rb diff --git a/Gemfile b/Gemfile index d22c030..b55b5b7 100644 --- a/Gemfile +++ b/Gemfile @@ -5,4 +5,5 @@ path "./adapters" do gem "database_cleaner-couch_potato" gem "database_cleaner-data_mapper" gem "database_cleaner-mongo" + gem "database_cleaner-mongoid" end diff --git a/Gemfile.lock b/Gemfile.lock index b2fa28f..d071f2d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -18,6 +18,10 @@ PATH database_cleaner-mongo (1.8.0) database_cleaner (~> 1.8.0) mongo + database_cleaner-mongoid (1.8.0) + database_cleaner (~> 1.8.0) + database_cleaner-mongo (~> 1.8.0) + mongoid GEM remote: https://rubygems.org/ @@ -267,6 +271,7 @@ DEPENDENCIES database_cleaner-couch_potato! database_cleaner-data_mapper! database_cleaner-mongo! + database_cleaner-mongoid! datamapper dm-migrations dm-sqlite-adapter diff --git a/adapters/database_cleaner-mongoid/.gitignore b/adapters/database_cleaner-mongoid/.gitignore new file mode 100644 index 0000000..b04a8c8 --- /dev/null +++ b/adapters/database_cleaner-mongoid/.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-mongoid/.rspec b/adapters/database_cleaner-mongoid/.rspec new file mode 100644 index 0000000..34c5164 --- /dev/null +++ b/adapters/database_cleaner-mongoid/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/adapters/database_cleaner-mongoid/.travis.yml b/adapters/database_cleaner-mongoid/.travis.yml new file mode 100644 index 0000000..0dc6355 --- /dev/null +++ b/adapters/database_cleaner-mongoid/.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-mongoid/Gemfile b/adapters/database_cleaner-mongoid/Gemfile new file mode 100644 index 0000000..0e454ce --- /dev/null +++ b/adapters/database_cleaner-mongoid/Gemfile @@ -0,0 +1,9 @@ +source "https://rubygems.org" + +git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } + +# Specify your gem's dependencies in database_cleaner-mongoid.gemspec +gemspec + +gem "database_cleaner", path: "../.." +gem "database_cleaner-mongo", path: ".." diff --git a/adapters/database_cleaner-mongoid/Gemfile.lock b/adapters/database_cleaner-mongoid/Gemfile.lock new file mode 100644 index 0000000..080ac31 --- /dev/null +++ b/adapters/database_cleaner-mongoid/Gemfile.lock @@ -0,0 +1,67 @@ +PATH + remote: ../.. + specs: + database_cleaner (1.8.0) + +PATH + remote: .. + specs: + database_cleaner-mongo (1.8.0) + database_cleaner (~> 1.8.0) + mongo + +PATH + remote: . + specs: + database_cleaner-mongoid (1.8.0) + database_cleaner (~> 1.8.0) + database_cleaner-mongo (~> 1.8.0) + mongoid + +GEM + remote: https://rubygems.org/ + specs: + activemodel (3.0.0) + activesupport (= 3.0.0) + builder (~> 2.1.2) + i18n (~> 0.4.1) + activesupport (3.0.0) + bson (1.12.5) + builder (2.1.2) + diff-lcs (1.3) + i18n (0.4.2) + mongo (1.12.5) + bson (= 1.12.5) + mongoid (2.2.6) + activemodel (~> 3.0.0) + mongo (~> 1.3) + tzinfo (~> 0.3.22) + 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) + tzinfo (0.3.48) + +PLATFORMS + ruby + +DEPENDENCIES + bundler (~> 1.16) + database_cleaner! + database_cleaner-mongo! + database_cleaner-mongoid! + rake (~> 10.0) + rspec (~> 3.0) + +BUNDLED WITH + 1.16.1 diff --git a/adapters/database_cleaner-mongoid/LICENSE.txt b/adapters/database_cleaner-mongoid/LICENSE.txt new file mode 100644 index 0000000..392bc0d --- /dev/null +++ b/adapters/database_cleaner-mongoid/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-mongoid/README.md b/adapters/database_cleaner-mongoid/README.md new file mode 100644 index 0000000..a509bf3 --- /dev/null +++ b/adapters/database_cleaner-mongoid/README.md @@ -0,0 +1,39 @@ +# DatabaseCleaner::Mongoid + +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/mongoid`. 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-mongoid' +``` + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install database_cleaner-mongoid + +## 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-mongoid. + +## 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-mongoid/Rakefile b/adapters/database_cleaner-mongoid/Rakefile new file mode 100644 index 0000000..b7e9ed5 --- /dev/null +++ b/adapters/database_cleaner-mongoid/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-mongoid/bin/console b/adapters/database_cleaner-mongoid/bin/console new file mode 100755 index 0000000..ac55de4 --- /dev/null +++ b/adapters/database_cleaner-mongoid/bin/console @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby + +require "bundler/setup" +require "database_cleaner/mongoid" + +# 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-mongoid/bin/setup b/adapters/database_cleaner-mongoid/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/adapters/database_cleaner-mongoid/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-mongoid/database_cleaner-mongoid.gemspec b/adapters/database_cleaner-mongoid/database_cleaner-mongoid.gemspec new file mode 100644 index 0000000..ea6ace3 --- /dev/null +++ b/adapters/database_cleaner-mongoid/database_cleaner-mongoid.gemspec @@ -0,0 +1,31 @@ + +lib = File.expand_path("../lib", __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require "database_cleaner/mongoid/version" + +Gem::Specification.new do |spec| + spec.name = "database_cleaner-mongoid" + spec.version = DatabaseCleaner::Mongoid::VERSION + spec.authors = ["Ernesto Tagwerker"] + spec.email = ["ernesto@ombulabs.com"] + + spec.summary = "Strategies for cleaning databases using Mongoid. Can be used to ensure a clean state for testing." + spec.description = "Strategies for cleaning databases using Mongoid. Can be used to ensure a clean state for testing." + spec.homepage = "https://github.com/DatabaseCleaner/database_cleaner-mongoid" + 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 "database_cleaner-mongo", "~> 1.8.0" + spec.add_dependency "mongoid" + + 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-mongoid/lib/database_cleaner-mongoid.rb b/adapters/database_cleaner-mongoid/lib/database_cleaner-mongoid.rb new file mode 100644 index 0000000..536edc9 --- /dev/null +++ b/adapters/database_cleaner-mongoid/lib/database_cleaner-mongoid.rb @@ -0,0 +1 @@ +require "database_cleaner/mongoid" diff --git a/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid.rb b/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid.rb new file mode 100644 index 0000000..e80f0b6 --- /dev/null +++ b/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid.rb @@ -0,0 +1,3 @@ +require "database_cleaner/mongoid/version" +require "database_cleaner" +require "database_cleaner/mongoid/truncation" diff --git a/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/base.rb b/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/base.rb new file mode 100644 index 0000000..417d466 --- /dev/null +++ b/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/base.rb @@ -0,0 +1,20 @@ +require 'database_cleaner/generic/base' +module DatabaseCleaner + module Mongoid + 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 + end + end +end diff --git a/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/truncation.rb b/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/truncation.rb new file mode 100644 index 0000000..6a6debb --- /dev/null +++ b/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/truncation.rb @@ -0,0 +1,49 @@ +require 'database_cleaner/mongoid/base' +require 'database_cleaner/generic/truncation' +require 'database_cleaner/mongo/truncation_mixin' +require 'database_cleaner/mongo2/truncation_mixin' +require 'database_cleaner/moped/truncation_base' +require 'mongoid/version' + +module DatabaseCleaner + module Mongoid + class Truncation + include ::DatabaseCleaner::Mongoid::Base + include ::DatabaseCleaner::Generic::Truncation + + if ::Mongoid::VERSION < '3' + + include ::DatabaseCleaner::Mongo::TruncationMixin + + private + + def database + ::Mongoid.database + end + + elsif ::Mongoid::VERSION < '5' + + include ::DatabaseCleaner::Moped::TruncationBase + + private + + def session + ::Mongoid::VERSION > "5.0.0" ? ::Mongoid.default_client : ::Mongoid.default_session + end + + def database + if not(@db.nil? or @db == :default) + ::Mongoid.databases[@db] + else + ::Mongoid.database + end + end + + else + + include ::DatabaseCleaner::Mongo2::TruncationMixin + + end + end + end +end diff --git a/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/version.rb b/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/version.rb new file mode 100644 index 0000000..b332f1f --- /dev/null +++ b/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/version.rb @@ -0,0 +1,5 @@ +module DatabaseCleaner + module Mongoid + VERSION = "1.8.0" + end +end diff --git a/adapters/database_cleaner-mongoid/spec/spec_helper.rb b/adapters/database_cleaner-mongoid/spec/spec_helper.rb new file mode 100644 index 0000000..f582c40 --- /dev/null +++ b/adapters/database_cleaner-mongoid/spec/spec_helper.rb @@ -0,0 +1,14 @@ +require "bundler/setup" +require "database_cleaner/mongoid" + +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 diff --git a/features/cleaning.feature b/features/cleaning.feature index e04a839..f44d800 100644 --- a/features/cleaning.feature +++ b/features/cleaning.feature @@ -18,6 +18,7 @@ Feature: database cleaning | CouchPotato | truncation | | DataMapper | transaction | | DataMapper | truncation | + | Mongoid | truncation | Scenario Outline: ruby app Given I am using diff --git a/features/cleaning_default_strategy.feature b/features/cleaning_default_strategy.feature index 8eac054..ed57c81 100644 --- a/features/cleaning_default_strategy.feature +++ b/features/cleaning_default_strategy.feature @@ -15,6 +15,7 @@ Feature: database cleaning | ActiveRecord | | CouchPotato | | DataMapper | + | Mongoid | Scenario Outline: ruby app Given I am using diff --git a/features/cleaning_multiple_orms.feature b/features/cleaning_multiple_orms.feature index 6e11c21..2f4b33b 100644 --- a/features/cleaning_multiple_orms.feature +++ b/features/cleaning_multiple_orms.feature @@ -13,10 +13,16 @@ Feature: database cleaning using multiple ORMs | ORM1 | ORM2 | | ActiveRecord | CouchPotato | | ActiveRecord | DataMapper | + | ActiveRecord | Mongoid | | CouchPotato | ActiveRecord | | CouchPotato | DataMapper | + | CouchPotato | Mongoid | | DataMapper | ActiveRecord | | DataMapper | CouchPotato | + | DataMapper | Mongoid | + | Mongoid | ActiveRecord | + | Mongoid | CouchPotato | + | Mongoid | DataMapper | Scenario Outline: ruby app Given I am using and