mirror of
				https://github.com/DatabaseCleaner/database_cleaner
				synced 2023-03-27 23:22:03 -04:00 
			
		
		
		
	Merge commit '253247bd08' into multi_orms_connections
				
					
				
			Conflicts:
	VERSION.yml
	features/step_definitions/database_cleaner_steps.rb
	lib/database_cleaner/configuration.rb
	spec/database_cleaner/active_record/transaction_spec.rb
	spec/database_cleaner/configuration_spec.rb
	spec/database_cleaner/mongo_mapper/truncation_spec.rb
 This is merge is not yet complete.  The Mongoid strategy/specs is not completely updated
 to the new structure of the project.  Additionaly, I see quite a bit of
 duplication between Mongoid and MongoMapper.  Since they both rely on
 the same underlying mongo lib I am thinking of just having a single
 Mongo strategy.  How the default connection is inferred can use
 autodetect magic of MongoMapper and Mongoid. (Meaning, for the
     Truncation strategy the #connection method would get the connection
     from either MongoMapper or Mongoid depending on what is defined...
     or it could use what was set by the user.)
			
			
This commit is contained in:
		
						commit
						6e554cb2d7
					
				
					 17 changed files with 219 additions and 27 deletions
				
			
		
							
								
								
									
										1
									
								
								.vagrant
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.vagrant
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
fc7d78f3-6c37-416b-8f1c-e7ab2399877c
 | 
			
		||||
							
								
								
									
										6
									
								
								Gemfile
									
										
									
									
									
								
							
							
						
						
									
										6
									
								
								Gemfile
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -10,14 +10,16 @@ group :development do
 | 
			
		|||
  gem "datamapper", "0.10.2"
 | 
			
		||||
    gem "data_objects"
 | 
			
		||||
    gem "do_sqlite3"
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  gem "mongoid"
 | 
			
		||||
   gem "tzinfo"
 | 
			
		||||
  gem "mongo_mapper"
 | 
			
		||||
    gem "mongo"
 | 
			
		||||
    gem "mongo_ext"
 | 
			
		||||
    gem "bson_ext"
 | 
			
		||||
 | 
			
		||||
  gem "couch_potato"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
  gem "rake"
 | 
			
		||||
  gem "ruby-debug"
 | 
			
		||||
  gem 'sqlite3-ruby'
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										20
									
								
								Gemfile.lock
									
										
									
									
									
								
							
							
						
						
									
										20
									
								
								Gemfile.lock
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -8,6 +8,10 @@ dependencies:
 | 
			
		|||
    group: 
 | 
			
		||||
    - :development
 | 
			
		||||
    version: = 0.10.2
 | 
			
		||||
  tzinfo: 
 | 
			
		||||
    group: 
 | 
			
		||||
    - :development
 | 
			
		||||
    version: ">= 0"
 | 
			
		||||
  mongo: 
 | 
			
		||||
    group: 
 | 
			
		||||
    - :development
 | 
			
		||||
| 
						 | 
				
			
			@ -64,6 +68,10 @@ dependencies:
 | 
			
		|||
    group: 
 | 
			
		||||
    - :development
 | 
			
		||||
    version: ">= 0"
 | 
			
		||||
  mongoid: 
 | 
			
		||||
    group: 
 | 
			
		||||
    - :development
 | 
			
		||||
    version: ">= 0"
 | 
			
		||||
  json_pure: 
 | 
			
		||||
    group: 
 | 
			
		||||
    - :development
 | 
			
		||||
| 
						 | 
				
			
			@ -80,7 +88,7 @@ specs:
 | 
			
		|||
- rake: 
 | 
			
		||||
    version: 0.8.7
 | 
			
		||||
- ZenTest: 
 | 
			
		||||
    version: 4.3.1
 | 
			
		||||
    version: 4.3.2
 | 
			
		||||
- activesupport: 
 | 
			
		||||
    version: 2.3.8
 | 
			
		||||
- activerecord: 
 | 
			
		||||
| 
						 | 
				
			
			@ -151,6 +159,8 @@ specs:
 | 
			
		|||
    version: 0.10.2
 | 
			
		||||
- do_sqlite3: 
 | 
			
		||||
    version: 0.10.2
 | 
			
		||||
- durran-validatable: 
 | 
			
		||||
    version: 2.0.1
 | 
			
		||||
- gemcutter: 
 | 
			
		||||
    version: 0.5.0
 | 
			
		||||
- git: 
 | 
			
		||||
| 
						 | 
				
			
			@ -161,6 +171,8 @@ specs:
 | 
			
		|||
    version: 1.4.0
 | 
			
		||||
- jnunemaker-validatable: 
 | 
			
		||||
    version: 1.8.4
 | 
			
		||||
- leshill-will_paginate: 
 | 
			
		||||
    version: 2.3.11
 | 
			
		||||
- linecache: 
 | 
			
		||||
    version: "0.43"
 | 
			
		||||
- mongo: 
 | 
			
		||||
| 
						 | 
				
			
			@ -171,6 +183,8 @@ specs:
 | 
			
		|||
    version: 0.1.1
 | 
			
		||||
- mongo_mapper: 
 | 
			
		||||
    version: 0.7.6
 | 
			
		||||
- mongoid: 
 | 
			
		||||
    version: 1.0.6
 | 
			
		||||
- mysql: 
 | 
			
		||||
    version: 2.8.1
 | 
			
		||||
- rspec: 
 | 
			
		||||
| 
						 | 
				
			
			@ -181,7 +195,9 @@ specs:
 | 
			
		|||
    version: 0.10.3
 | 
			
		||||
- sqlite3-ruby: 
 | 
			
		||||
    version: 1.2.5
 | 
			
		||||
hash: f234f5ef0a35bbcee0aaa71ec88a1952c8aa92b9
 | 
			
		||||
- tzinfo: 
 | 
			
		||||
    version: 0.3.22
 | 
			
		||||
hash: 0c418ad5e81b096047c67b8f40ae9420fec31f45
 | 
			
		||||
sources: 
 | 
			
		||||
- Rubygems: 
 | 
			
		||||
    uri: http://rubygems.org
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										15
									
								
								History.txt
									
										
									
									
									
								
							
							
						
						
									
										15
									
								
								History.txt
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,7 +1,22 @@
 | 
			
		|||
0.5.x (In Git)
 | 
			
		||||
 | 
			
		||||
 === Bugfixes
 | 
			
		||||
  * Updates the DataMapper truncation strategy to version 0.10.3. (Robert Rouse)
 | 
			
		||||
 | 
			
		||||
== 0.5.2
 | 
			
		||||
 | 
			
		||||
 === Bugfixes
 | 
			
		||||
  * Removes extraneous puts call from configuration.rb.  (Ben Mabey)
 | 
			
		||||
 | 
			
		||||
== 0.5.1 - The Mongoid Release
 | 
			
		||||
 | 
			
		||||
This release also attempts to fix AR for Rails 3 support.  I have seen mixed reviews on this.  Some people
 | 
			
		||||
claim the fixes allow for use in Rails3 while others have not had good luck with it.  I plan on reworking
 | 
			
		||||
the way AR support is added so that it is more friendly with how Rails 3 uses autoload.
 | 
			
		||||
 | 
			
		||||
 === New features
 | 
			
		||||
  * Clean and clean_with methods are now aliased to clean! and clean_with!. (Ben Mabey)
 | 
			
		||||
  * Mongoid Support! (Sidney Burks)
 | 
			
		||||
 | 
			
		||||
=== Bugfixes
 | 
			
		||||
  * Check PostgreSQL version >= 8.2 before using TRUNCATE CASCADE (James B. Byrne)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,7 +5,7 @@ The original use case was to ensure a clean state during tests.  Each strategy
 | 
			
		|||
is a small amount of code but is code that is usually needed in any ruby app
 | 
			
		||||
that is testing with a database.
 | 
			
		||||
 | 
			
		||||
ActiveRecord, DataMapper, MongoMapper and CouchPotato are supported.
 | 
			
		||||
ActiveRecord, DataMapper, MongoMapper, Mongoid, and CouchPotato are supported.
 | 
			
		||||
 | 
			
		||||
h2. How to use
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,6 @@
 | 
			
		|||
--- 
 | 
			
		||||
:patch: 1
 | 
			
		||||
:minor: 5
 | 
			
		||||
:patch: 2
 | 
			
		||||
:build: 
 | 
			
		||||
:major: 0
 | 
			
		||||
:minor: 5
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,11 +5,11 @@
 | 
			
		|||
 | 
			
		||||
Gem::Specification.new do |s|
 | 
			
		||||
  s.name = %q{database_cleaner}
 | 
			
		||||
  s.version = "0.5.0"
 | 
			
		||||
  s.version = "0.5.2"
 | 
			
		||||
 | 
			
		||||
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
 | 
			
		||||
  s.authors = ["Ben Mabey"]
 | 
			
		||||
  s.date = %q{2010-02-22}
 | 
			
		||||
  s.date = %q{2010-04-15}
 | 
			
		||||
  s.description = %q{Strategies for cleaning databases.  Can be used to ensure a clean state for testing.}
 | 
			
		||||
  s.email = %q{ben@benmabey.com}
 | 
			
		||||
  s.extra_rdoc_files = [
 | 
			
		||||
| 
						 | 
				
			
			@ -29,6 +29,7 @@ Gem::Specification.new do |s|
 | 
			
		|||
     "examples/lib/activerecord_models.rb",
 | 
			
		||||
     "examples/lib/couchpotato_models.rb",
 | 
			
		||||
     "examples/lib/datamapper_models.rb",
 | 
			
		||||
     "examples/lib/mongoid_models.rb",
 | 
			
		||||
     "examples/lib/mongomapper_models.rb",
 | 
			
		||||
     "features/cleaning.feature",
 | 
			
		||||
     "features/step_definitions/database_cleaner_steps.rb",
 | 
			
		||||
| 
						 | 
				
			
			@ -42,11 +43,14 @@ Gem::Specification.new do |s|
 | 
			
		|||
     "lib/database_cleaner/data_mapper/transaction.rb",
 | 
			
		||||
     "lib/database_cleaner/data_mapper/truncation.rb",
 | 
			
		||||
     "lib/database_cleaner/mongo_mapper/truncation.rb",
 | 
			
		||||
     "lib/database_cleaner/mongoid/truncation.rb",
 | 
			
		||||
     "lib/database_cleaner/truncation_base.rb",
 | 
			
		||||
     "spec/database_cleaner/active_record/transaction_spec.rb",
 | 
			
		||||
     "spec/database_cleaner/active_record/truncation_spec.rb",
 | 
			
		||||
     "spec/database_cleaner/configuration_spec.rb",
 | 
			
		||||
     "spec/database_cleaner/couch_potato/truncation_spec.rb",
 | 
			
		||||
     "spec/database_cleaner/mongo_mapper/truncation_spec.rb",
 | 
			
		||||
     "spec/database_cleaner/mongoid/truncation_spec.rb",
 | 
			
		||||
     "spec/spec.opts",
 | 
			
		||||
     "spec/spec_helper.rb"
 | 
			
		||||
  ]
 | 
			
		||||
| 
						 | 
				
			
			@ -56,16 +60,19 @@ Gem::Specification.new do |s|
 | 
			
		|||
  s.rubygems_version = %q{1.3.5}
 | 
			
		||||
  s.summary = %q{Strategies for cleaning databases.  Can be used to ensure a clean state for testing.}
 | 
			
		||||
  s.test_files = [
 | 
			
		||||
    "spec/database_cleaner/active_record/truncation_spec.rb",
 | 
			
		||||
    "spec/database_cleaner/active_record/transaction_spec.rb",
 | 
			
		||||
     "spec/database_cleaner/active_record/truncation_spec.rb",
 | 
			
		||||
     "spec/database_cleaner/configuration_spec.rb",
 | 
			
		||||
     "spec/database_cleaner/couch_potato/truncation_spec.rb",
 | 
			
		||||
     "spec/database_cleaner/mongo_mapper/truncation_spec.rb",
 | 
			
		||||
     "spec/database_cleaner/mongoid/truncation_spec.rb",
 | 
			
		||||
     "spec/spec_helper.rb",
 | 
			
		||||
     "examples/features/step_definitions/example_steps.rb",
 | 
			
		||||
     "examples/features/support/env.rb",
 | 
			
		||||
     "examples/lib/activerecord_models.rb",
 | 
			
		||||
     "examples/lib/couchpotato_models.rb",
 | 
			
		||||
     "examples/lib/datamapper_models.rb",
 | 
			
		||||
     "examples/lib/mongoid_models.rb",
 | 
			
		||||
     "examples/lib/mongomapper_models.rb"
 | 
			
		||||
  ]
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										23
									
								
								examples/lib/mongoid_models.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								examples/lib/mongoid_models.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,23 @@
 | 
			
		|||
require 'mongoid'
 | 
			
		||||
 | 
			
		||||
Mongoid.configure do |config|
 | 
			
		||||
  name = 'database_cleaner_test'
 | 
			
		||||
  config.master = Mongo::Connection.new.db(name)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#::MongoMapper.connection = Mongo::Connection.new('127.0.0.1')
 | 
			
		||||
#::MongoMapper.database = 'database_cleaner_test'
 | 
			
		||||
 | 
			
		||||
class Widget
 | 
			
		||||
  include Mongoid::Document
 | 
			
		||||
  field :id, :type => Integer
 | 
			
		||||
  field :name
 | 
			
		||||
 | 
			
		||||
  class << self
 | 
			
		||||
    #mongomapper doesn't seem to provide this...
 | 
			
		||||
    def create!(*args)
 | 
			
		||||
      new(*args).save!
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			@ -17,4 +17,5 @@ Feature: database cleaning
 | 
			
		|||
    | DataMapper   | transaction   |
 | 
			
		||||
    | DataMapper   | truncation    |
 | 
			
		||||
    | MongoMapper  | truncation    |
 | 
			
		||||
    | Mongoid      | truncation    |
 | 
			
		||||
    | CouchPotato  | truncation    |
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
 | 
			
		||||
Given /^I am using (ActiveRecord|DataMapper|MongoMapper|CouchPotato)$/ do |orm|
 | 
			
		||||
Given /^I am using (ActiveRecord|DataMapper|MongoMapper|Mongoid|CouchPotato)$/ do |orm|
 | 
			
		||||
  @feature_runner = FeatureRunner.new
 | 
			
		||||
  @feature_runner.orm = orm
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -102,10 +102,12 @@ module DatabaseCleaner
 | 
			
		|||
          :data_mapper
 | 
			
		||||
        elsif defined? ::MongoMapper
 | 
			
		||||
          :mongo_mapper
 | 
			
		||||
        elsif defined? ::Mongoid
 | 
			
		||||
          :mongoid
 | 
			
		||||
        elsif defined? ::CouchPotato
 | 
			
		||||
          :couch_potato
 | 
			
		||||
        else
 | 
			
		||||
          raise NoORMDetected, "No known ORM was detected!  Is ActiveRecord, DataMapper, MongoMapper or CouchPotato loaded?"
 | 
			
		||||
          raise NoORMDetected, "No known ORM was detected!  Is ActiveRecord, DataMapper, MongoMapper, Mongoid, or CouchPotato loaded?"
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
| 
						 | 
				
			
			@ -116,6 +118,8 @@ module DatabaseCleaner
 | 
			
		|||
          DatabaseCleaner::ActiveRecord
 | 
			
		||||
        when :data_mapper
 | 
			
		||||
          DatabaseCleaner::DataMapper
 | 
			
		||||
        when :mongoid
 | 
			
		||||
          DatabaseCleaner::Mongoid
 | 
			
		||||
        when :mongo_mapper
 | 
			
		||||
          DatabaseCleaner::MongoMapper
 | 
			
		||||
        when :couch_potato
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -30,7 +30,7 @@ module DataMapper
 | 
			
		|||
          execute("SET FOREIGN_KEY_CHECKS = 0;")
 | 
			
		||||
          yield
 | 
			
		||||
        ensure
 | 
			
		||||
          execute("SET FOREIGN_KEY_CHECKS = #{old};")
 | 
			
		||||
          execute("SET FOREIGN_KEY_CHECKS = #{old.first};")
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										20
									
								
								lib/database_cleaner/mongoid/base.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								lib/database_cleaner/mongoid/base.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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
 | 
			
		||||
							
								
								
									
										27
									
								
								lib/database_cleaner/mongoid/truncation.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								lib/database_cleaner/mongoid/truncation.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,27 @@
 | 
			
		|||
require 'database_cleaner/mongoid/base'
 | 
			
		||||
require 'database_cleaner/generic/truncation'
 | 
			
		||||
 | 
			
		||||
module DatabaseCleaner
 | 
			
		||||
  module Mongoid
 | 
			
		||||
    class Truncation
 | 
			
		||||
      include ::DatabaseCleaner::Mongoid::Base
 | 
			
		||||
      include ::DatabaseCleaner::Generic::Truncation
 | 
			
		||||
 | 
			
		||||
      def clean
 | 
			
		||||
        if @only
 | 
			
		||||
          collections.each { |c| c.remove if @only.include?(c.name) }
 | 
			
		||||
        else
 | 
			
		||||
          collections.each { |c| c.remove unless @tables_to_exclude.include?(c.name) }
 | 
			
		||||
        end
 | 
			
		||||
        true
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      private
 | 
			
		||||
 | 
			
		||||
      def collections
 | 
			
		||||
        ::Mongoid.database.collections
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
  end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			@ -6,20 +6,12 @@ module DatabaseCleaner
 | 
			
		|||
  module ActiveRecord
 | 
			
		||||
 | 
			
		||||
    describe Transaction do
 | 
			
		||||
      let(:connection) { mock("connection") }
 | 
			
		||||
      let(:another_connection) { mock("a different connection") }
 | 
			
		||||
 | 
			
		||||
      let(:model_klass) do
 | 
			
		||||
        model_klass = mock("klass")
 | 
			
		||||
        model_klass.stub!(:connection).and_return(another_connection)
 | 
			
		||||
        model_klass
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      let (:connection) { mock("connection") }
 | 
			
		||||
      before(:each) do
 | 
			
		||||
        ::ActiveRecord::Base.stub!(:connection).and_return(connection)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      describe "start" do
 | 
			
		||||
      describe "#start" do
 | 
			
		||||
        it "should increment open transactions if possible" do
 | 
			
		||||
          connection.stub!(:respond_to?).with(:increment_open_transactions).and_return(true)
 | 
			
		||||
          connection.stub!(:begin_db_transaction)
 | 
			
		||||
| 
						 | 
				
			
			@ -44,14 +36,13 @@ module DatabaseCleaner
 | 
			
		|||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      describe "clean" do
 | 
			
		||||
        it "should finish a transaction" do
 | 
			
		||||
      describe "#clean" do
 | 
			
		||||
        it "should start a transaction" do
 | 
			
		||||
            connection.stub!(:decrement_open_transactions)
 | 
			
		||||
 | 
			
		||||
            connection.should_receive(:rollback_db_transaction)
 | 
			
		||||
            Transaction.new.clean
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        it "should decrement open transactions if possible" do
 | 
			
		||||
          connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(true)
 | 
			
		||||
          connection.stub!(:rollback_db_transaction)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
require File.dirname(__FILE__) + '/../../spec_helper'
 | 
			
		||||
require 'database_cleaner/mongo_mapper/truncation'
 | 
			
		||||
require 'mongo_mapper'
 | 
			
		||||
require 'database_cleaner/mongo_mapper/truncation'
 | 
			
		||||
require File.dirname(__FILE__) + '/mongo_examples'
 | 
			
		||||
 | 
			
		||||
module DatabaseCleaner
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										84
									
								
								spec/database_cleaner/mongoid/truncation_spec.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								spec/database_cleaner/mongoid/truncation_spec.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,84 @@
 | 
			
		|||
require File.dirname(__FILE__) + '/../../spec_helper'
 | 
			
		||||
require 'mongoid'
 | 
			
		||||
require 'database_cleaner/mongoid/truncation'
 | 
			
		||||
 | 
			
		||||
TEST_DATABASE = 'database_cleaner_specs'
 | 
			
		||||
Mongoid.configure do |config|
 | 
			
		||||
  name = TEST_DATABASE
 | 
			
		||||
  config.master = Mongo::Connection.new.db(name)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
class TestClassA
 | 
			
		||||
  include Mongoid::Document
 | 
			
		||||
  field :name
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
class TestClassB
 | 
			
		||||
  include Mongoid::Document
 | 
			
		||||
  field :name
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
module DatabaseCleaner
 | 
			
		||||
  module Mongoid
 | 
			
		||||
 | 
			
		||||
    describe Truncation do
 | 
			
		||||
      before(:each) do
 | 
			
		||||
        ::Mongoid.database.connection.drop_database(TEST_DATABASE)
 | 
			
		||||
        #::MongoMapper.connection.db(TEST_DATABASE).collections.each {|c| c.remove }
 | 
			
		||||
        #::MongoMapper.database = TEST_DATABASE
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def ensure_counts(expected_counts)
 | 
			
		||||
        # I had to add this sanity_check garbage because I was getting non-determinisc results from mongomapper at times.. 
 | 
			
		||||
        # very odd and disconcerting...
 | 
			
		||||
        sanity_check = expected_counts.delete(:sanity_check)
 | 
			
		||||
        begin
 | 
			
		||||
          expected_counts.each do |model_class, expected_count|
 | 
			
		||||
            model_class.count.should equal(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{model_class.count}"
 | 
			
		||||
          end
 | 
			
		||||
        rescue Spec::Expectations::ExpectationNotMetError => e
 | 
			
		||||
          raise !sanity_check ? e : Spec::ExpectationNotMetError::ExpectationNotMetError.new("SANITY CHECK FAILURE! This should never happen here: #{e.message}")
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def create_testclassa(attrs={})
 | 
			
		||||
        TestClassA.new({:name => 'some testclassa'}.merge(attrs)).save!
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def create_testclassb(attrs={})
 | 
			
		||||
        TestClassB.new({:name => 'some testclassb'}.merge(attrs)).save!
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "truncates all collections by default" do
 | 
			
		||||
        create_testclassa
 | 
			
		||||
        create_testclassb
 | 
			
		||||
        ensure_counts(TestClassA => 1, TestClassB => 1, :sanity_check => true)
 | 
			
		||||
        Truncation.new.clean
 | 
			
		||||
        ensure_counts(TestClassA => 0, TestClassB => 0)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      context "when collections are provided to the :only option" do
 | 
			
		||||
        it "only truncates the specified collections" do
 | 
			
		||||
          create_testclassa
 | 
			
		||||
          create_testclassb
 | 
			
		||||
          ensure_counts(TestClassA => 1, TestClassB => 1, :sanity_check => true)
 | 
			
		||||
          Truncation.new(:only => ['test_class_as']).clean
 | 
			
		||||
          ensure_counts(TestClassA => 0, TestClassB => 1)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      context "when collections are provided to the :except option" do
 | 
			
		||||
        it "truncates all but the specified collections" do
 | 
			
		||||
          create_testclassa
 | 
			
		||||
          create_testclassb
 | 
			
		||||
          ensure_counts(TestClassA => 1, TestClassB => 1, :sanity_check => true)
 | 
			
		||||
          Truncation.new(:except => ['test_class_as']).clean
 | 
			
		||||
          ensure_counts(TestClassA => 1, TestClassB => 0)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue