mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
use ActiveRecord::Base configuration when different with config file
This commit is contained in:
parent
d42481813c
commit
c428633514
2 changed files with 38 additions and 1 deletions
|
@ -34,10 +34,19 @@ module DatabaseCleaner
|
|||
def load_config
|
||||
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]
|
||||
@connection_hash = valid_config(connection_details)[self.db.to_s]
|
||||
end
|
||||
end
|
||||
|
||||
def valid_config(connection_file)
|
||||
if !::ActiveRecord::Base.configurations.nil? && !::ActiveRecord::Base.configurations.empty?
|
||||
if connection_file != ::ActiveRecord::Base.configurations
|
||||
return ::ActiveRecord::Base.configurations
|
||||
end
|
||||
end
|
||||
connection_file
|
||||
end
|
||||
|
||||
def connection_class
|
||||
@connection_class ||= if db && !db.is_a?(Symbol)
|
||||
db
|
||||
|
|
|
@ -84,6 +84,34 @@ my_db:
|
|||
subject.load_config
|
||||
end
|
||||
|
||||
context 'use ActiveRecord::Base.configuration' do
|
||||
it 'when config file different with it' do
|
||||
::ActiveRecord::Base.stub(:configurations).and_return({ "my_db" =>{ "database" => "two"} })
|
||||
subject.load_config
|
||||
subject.connection_hash.should eq({ "database" => "two"})
|
||||
end
|
||||
end
|
||||
|
||||
context 'use config file' do
|
||||
it 'when config file same with it' do
|
||||
::ActiveRecord::Base.stub(:configurations).and_return({ "my_db" =>{ "database" => "one"} })
|
||||
subject.load_config
|
||||
subject.connection_hash.should eq({ "database" => "one"})
|
||||
end
|
||||
|
||||
it 'when ::ActiveRecord::Base.configurations nil' do
|
||||
::ActiveRecord::Base.stub(:configurations).and_return(nil)
|
||||
subject.load_config
|
||||
subject.connection_hash.should eq({ "database" => "one"})
|
||||
end
|
||||
|
||||
it 'when ::ActiveRecord::Base.configurations empty' do
|
||||
::ActiveRecord::Base.stub(:configurations).and_return({})
|
||||
subject.load_config
|
||||
subject.connection_hash.should eq({ "database" => "one"})
|
||||
end
|
||||
end
|
||||
|
||||
it "should store the relevant config in connection_hash" do
|
||||
subject.load_config
|
||||
subject.connection_hash.should eq( "database" => "one" )
|
||||
|
|
Loading…
Reference in a new issue