From 21fb66fa9bf3ef9677b51d2e722c9cb6d5ef42d5 Mon Sep 17 00:00:00 2001 From: Paul Nikitochkin Date: Fri, 2 Nov 2012 21:42:15 +0200 Subject: [PATCH] Model has more priority over connection_hash * Skip load_config for ActiveRecord if model set for strategy. * If model and connection_hash are set, then should return model as connection_class instead of connection_hash builder --- lib/database_cleaner/active_record/base.rb | 18 ++++---- .../active_record/base_spec.rb | 43 ++++++++++++++----- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/lib/database_cleaner/active_record/base.rb b/lib/database_cleaner/active_record/base.rb index 7a90b3a..99b0c4b 100644 --- a/lib/database_cleaner/active_record/base.rb +++ b/lib/database_cleaner/active_record/base.rb @@ -32,9 +32,9 @@ module DatabaseCleaner end def load_config - if self.db != :default && File.file?(ActiveRecord.config_file_location) - connection_details = YAML::load(ERB.new(IO.read(ActiveRecord.config_file_location)).result) - @connection_hash = connection_details[self.db.to_s] + if self.db != :default && self.db.is_a?(Symbol) && File.file?(ActiveRecord.config_file_location) + connection_details = YAML::load(ERB.new(IO.read(ActiveRecord.config_file_location)).result) + @connection_hash = connection_details[self.db.to_s] end end @@ -43,12 +43,12 @@ module DatabaseCleaner end def connection_class - @connection_class ||= if @db == :default || (@db.nil? && connection_hash.nil?) - ::ActiveRecord::Base + @connection_class ||= if @db && !@db.is_a?(Symbol) + @db elsif connection_hash lookup_from_connection_pool || establish_connection else - @db # allows for an actual class to be passed in + ::ActiveRecord::Base end end @@ -57,8 +57,8 @@ module DatabaseCleaner def lookup_from_connection_pool if ::ActiveRecord::Base.respond_to?(:descendants) database_name = connection_hash["database"] || connection_hash[:database] - models = ::ActiveRecord::Base.descendants - models.detect {|m| m.connection_pool.spec.config[:database] == database_name} + models = ::ActiveRecord::Base.descendants + models.detect { |m| m.connection_pool.spec.config[:database] == database_name } end end @@ -67,7 +67,7 @@ module DatabaseCleaner strategy_class.send :establish_connection, connection_hash strategy_class end - + end end end diff --git a/spec/database_cleaner/active_record/base_spec.rb b/spec/database_cleaner/active_record/base_spec.rb index bf0d83a..4d8850e 100644 --- a/spec/database_cleaner/active_record/base_spec.rb +++ b/spec/database_cleaner/active_record/base_spec.rb @@ -62,7 +62,7 @@ module DatabaseCleaner before do subject.db = :my_db - yaml = <<-Y + yaml = <<-Y my_db: database: <%= "ONE".downcase %> Y @@ -71,7 +71,7 @@ my_db: end it "should parse the config" do - YAML.should_receive(:load).and_return( {:nil => nil} ) + YAML.should_receive(:load).and_return({ :nil => nil }) subject.load_config end @@ -80,19 +80,26 @@ my_db: my_db: database: one Y - YAML.should_receive(:load).with(transformed).and_return({ "my_db" => {"database" => "one"} }) + YAML.should_receive(:load).with(transformed).and_return({ "my_db" => { "database" => "one" } }) subject.load_config end it "should store the relevant config in connection_hash" do subject.load_config - subject.connection_hash.should == {"database" => "one"} + subject.connection_hash.should == { "database" => "one" } end it "should skip config if config file is not available" do File.should_receive(:file?).with(config_location).and_return(false) subject.load_config - subject.connection_hash.should be_blank + subject.connection_hash.should_not be + end + + it "skips the file when the model is set" do + subject.db = FakeModel + YAML.should_not_receive(:load) + subject.load_config + subject.connection_hash.should_not be end it "skips the file when the db is set to :default" do @@ -100,6 +107,7 @@ my_db: subject.db = :default YAML.should_not_receive(:load) subject.load_config + subject.connection_hash.should_not be end end @@ -122,19 +130,32 @@ my_db: end describe "connection_class" do - it { expect{ subject.connection_class }.to_not raise_error } + it { expect { subject.connection_class }.to_not raise_error } it "should default to ActiveRecord::Base" do subject.connection_class.should == ::ActiveRecord::Base end - it "allows for database models to be passed in" do - subject.db = FakeModel - subject.connection_class.should == FakeModel + context "with database models" do + context "connection_hash is set" do + it "allows for database models to be passed in" do + subject.db = FakeModel + subject.connection_hash = { } + subject.load_config + subject.connection_class.should == FakeModel + end + end + + context "connection_hash is not set" do + it "allows for database models to be passed in" do + subject.db = FakeModel + subject.connection_class.should == FakeModel + end + end end context "when connection_hash is set" do let(:hash) { mock("hash") } - before { ::ActiveRecord::Base.stub!(:respond_to?).and_return(false)} + before { ::ActiveRecord::Base.stub!(:respond_to?).and_return(false) } before { subject.stub(:connection_hash).and_return(hash) } it "should create connection_class if it doesnt exist if connection_hash is set" do @@ -142,7 +163,7 @@ my_db: subject.connection_class end - it "should configure the class from create_connection_class if connection_hash is set" do + it "should configure the class from create_connection_class if connection_hash is set" do strategy_class = mock('strategy_class') strategy_class.should_receive(:establish_connection).with(hash)