diff --git a/examples/features/support/env.rb b/examples/features/support/env.rb index 2d6708c..065928a 100644 --- a/examples/features/support/env.rb +++ b/examples/features/support/env.rb @@ -8,6 +8,7 @@ DB_DIR = "#{File.dirname(__FILE__)}/../../db" orm = ENV['ORM'] another_orm = ENV['ANOTHER_ORM'] strategy = ENV['STRATEGY'] +multiple_db = ENV['MULTIPLE_DBS'] if orm && strategy $:.unshift(File.dirname(__FILE__) + '/../../../lib') @@ -26,18 +27,19 @@ if orm && strategy rescue LoadError => e raise "You don't have the #{another_orm} ORM installed" end - else - end - unless another_orm - DatabaseCleaner.strategy = strategy.to_sym - else + if multiple_db && another_orm + DatabaseCleaner[ orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym, {:connection => :one} ].strategy = strategy.to_sym + DatabaseCleaner[ another_orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym, {:connection => :two} ].strategy = strategy.to_sym + elsif another_orm DatabaseCleaner[ orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym ].strategy = strategy.to_sym DatabaseCleaner[ another_orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym ].strategy = strategy.to_sym + else + DatabaseCleaner.strategy = strategy.to_sym end else diff --git a/lib/database_cleaner/active_record/base.rb b/lib/database_cleaner/active_record/base.rb index 88bc90f..ea81633 100644 --- a/lib/database_cleaner/active_record/base.rb +++ b/lib/database_cleaner/active_record/base.rb @@ -1,5 +1,5 @@ require 'database_cleaner/generic/base' -require 'active_record/base' +require 'active_record' module DatabaseCleaner module ActiveRecord diff --git a/lib/database_cleaner/base.rb b/lib/database_cleaner/base.rb index dc87025..66e082c 100644 --- a/lib/database_cleaner/base.rb +++ b/lib/database_cleaner/base.rb @@ -36,6 +36,12 @@ module DatabaseCleaner else raise ArgumentError, "You must provide a strategy object, or a symbol for a known strategy along with initialization params." end + + if @strategy.respond_to? :db= + @strategy.db = self.db + elsif self.db != :default + raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database" + end end def strategy diff --git a/lib/database_cleaner/data_mapper/base.rb b/lib/database_cleaner/data_mapper/base.rb index f72f191..d15612b 100644 --- a/lib/database_cleaner/data_mapper/base.rb +++ b/lib/database_cleaner/data_mapper/base.rb @@ -7,12 +7,6 @@ module DatabaseCleaner module Base include ::DatabaseCleaner::Generic::Base - - def connection_klass - #TODO, multiple connections... - raise NotImplementedError - #::ActiveRecord::Base - end end end end \ No newline at end of file diff --git a/lib/database_cleaner/generic/base.rb b/lib/database_cleaner/generic/base.rb index d13077d..f2a32d6 100644 --- a/lib/database_cleaner/generic/base.rb +++ b/lib/database_cleaner/generic/base.rb @@ -8,20 +8,8 @@ module ::DatabaseCleaner end module InstanceMethods - def initialize(db = :default) - @db = db - end - - def db=(desired_db) - @db = desired_db - end - def db - @db - end - - def connection_klass - raise NotImplementedError + :default end end diff --git a/spec/database_cleaner/base_spec.rb b/spec/database_cleaner/base_spec.rb index 830652e..2293206 100644 --- a/spec/database_cleaner/base_spec.rb +++ b/spec/database_cleaner/base_spec.rb @@ -84,10 +84,10 @@ module DatabaseCleaner it "should be equal if orm, connection and strategy are the same" do strategy = :truncation - one = DatabaseCleaner::Base.new(:active_record,:connection => :one) + one = DatabaseCleaner::Base.new(:active_record,:connection => :default) one.strategy = strategy - two = DatabaseCleaner::Base.new(:active_record,:connection => :one) + two = DatabaseCleaner::Base.new(:active_record,:connection => :default) two.strategy = strategy one.should == two @@ -106,7 +106,31 @@ module DatabaseCleaner base.db.should == :my_db end - it "should pass through db to the strategy" + it "should check to see if strategy supports db specification" do + strategy = mock("strategy",:db= => :my_db) + strategy.stub(:respond_to?).with(anything) + strategy.should_receive(:respond_to?).with(:db=).and_return(true) + ::DatabaseCleaner::Base.new(:active_record,:connection => :my_db).strategy = strategy + end + + it "should pass db through to the strategy" do + strategy = mock("strategy") + + strategy.stub(:respond_to?).with(anything) + strategy.stub(:respond_to?).with(:db=).and_return(true) + + strategy.should_receive(:db=).with(:my_db) + ::DatabaseCleaner::Base.new(:active_record,:connection => :my_db).strategy = strategy + end + + it "should raise ArgumentError if db isn't default and strategy doesn't support different dbs" do + strategy = mock("strategy") + + strategy.stub(:respond_to?).with(anything) + strategy.stub(:respond_to?).with(:db=).and_return(false) + + expect { ::DatabaseCleaner::Base.new(:active_record,:connection => :my_db).strategy = strategy }.to raise_error ArgumentError + end end describe "orm integration" do diff --git a/spec/database_cleaner/configuration_spec.rb b/spec/database_cleaner/configuration_spec.rb index 44118c9..6fc8e44 100644 --- a/spec/database_cleaner/configuration_spec.rb +++ b/spec/database_cleaner/configuration_spec.rb @@ -228,9 +228,9 @@ describe ::DatabaseCleaner do describe "remove_duplicates" do it "should remove duplicates if they are identical" do - ::DatabaseCleaner[:active_record, {:connection => :one}].strategy = :truncation - ::DatabaseCleaner[:active_record, {:connection => :one}].strategy = :truncation - ::DatabaseCleaner[:active_record, {:connection => :one}].strategy = :truncation + ::DatabaseCleaner[:active_record, {:connection => :default}].strategy = :truncation + ::DatabaseCleaner[:active_record, {:connection => :default}].strategy = :truncation + ::DatabaseCleaner[:active_record, {:connection => :default}].strategy = :truncation ::DatabaseCleaner.connections.size.should == 3 ::DatabaseCleaner.remove_duplicates ::DatabaseCleaner.connections.size.should == 1 diff --git a/spec/database_cleaner/data_mapper/base_spec.rb b/spec/database_cleaner/data_mapper/base_spec.rb index 50603a1..2c1461a 100644 --- a/spec/database_cleaner/data_mapper/base_spec.rb +++ b/spec/database_cleaner/data_mapper/base_spec.rb @@ -14,7 +14,8 @@ module DatabaseCleaner describe ExampleStrategy do it_should_behave_like "a generic strategy" - #it { expect{ subject.connection_klass }.to_not raise_error } + it { should respond_to :db } + it { should respond_to :db= } end end end \ No newline at end of file diff --git a/spec/database_cleaner/generic/base_spec.rb b/spec/database_cleaner/generic/base_spec.rb index fa291a6..dbd4741 100644 --- a/spec/database_cleaner/generic/base_spec.rb +++ b/spec/database_cleaner/generic/base_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'database_cleaner/shared_strategy_spec' require 'database_cleaner/generic/base' module ::DatabaseCleaner @@ -16,15 +17,6 @@ module ::DatabaseCleaner it_should_behave_like "a generic strategy" its (:db) { should == :default } - - it "should raise NotImplementedError upon access of connection_klass" do - expect { subject.connection_klass }.to raise_error NotImplementedError - end - - it "should accept the desired database upon initalisation" do - eg = ExampleStrategy.new :my_database - eg.db.should == :my_database - end end end end \ No newline at end of file diff --git a/spec/database_cleaner/shared_strategy_spec.rb b/spec/database_cleaner/shared_strategy_spec.rb index 4147bb6..7bc75a7 100644 --- a/spec/database_cleaner/shared_strategy_spec.rb +++ b/spec/database_cleaner/shared_strategy_spec.rb @@ -1,7 +1,5 @@ shared_examples_for "a generic strategy" do it { should respond_to :db } - it { should respond_to :db= } - it { should respond_to :connection_klass } end shared_examples_for "a generic truncation strategy" do