database_cleaner/spec/database_cleaner/active_record/base_spec.rb

167 lines
5.0 KiB
Ruby
Raw Normal View History

2010-05-16 20:45:46 +00:00
require 'active_record'
2010-05-04 20:07:21 +00:00
require 'database_cleaner/active_record/base'
require 'database_cleaner/spec'
class FakeModel
def self.connection
:fake_connection
end
end
2018-05-01 02:31:44 +00:00
RSpec.describe DatabaseCleaner::ActiveRecord do
2018-04-26 19:06:01 +00:00
it { is_expected.to respond_to(:available_strategies) }
2010-05-31 03:12:58 +00:00
2018-04-26 19:06:01 +00:00
describe "config_file_location" do
after do
# prevent global state leakage
DatabaseCleaner::ActiveRecord.config_file_location=nil
DatabaseCleaner.app_root = nil
end
2010-05-31 03:12:58 +00:00
it "should default to \#{DatabaseCleaner.app_root}/config/database.yml" do
DatabaseCleaner::ActiveRecord.config_file_location = nil
DatabaseCleaner.app_root = "/path/to"
2018-04-26 19:06:01 +00:00
expect(DatabaseCleaner::ActiveRecord.config_file_location).to eq '/path/to/config/database.yml'
end
2010-05-31 03:12:58 +00:00
end
2018-04-26 19:06:01 +00:00
end
2010-05-31 03:12:58 +00:00
2018-04-26 19:06:01 +00:00
module DatabaseCleaner
2010-05-31 03:12:58 +00:00
module ActiveRecord
class ExampleStrategy
2018-04-26 19:06:01 +00:00
include DatabaseCleaner::ActiveRecord::Base
end
2010-05-31 03:12:58 +00:00
2018-05-01 02:31:44 +00:00
RSpec.describe ExampleStrategy do
2018-04-26 19:06:01 +00:00
let(:config_location) { '/path/to/config/database.yml' }
2010-05-31 03:12:58 +00:00
around do |example|
DatabaseCleaner::ActiveRecord.config_file_location = config_location
example.run
DatabaseCleaner::ActiveRecord.config_file_location = nil
2018-04-26 19:06:01 +00:00
end
2010-05-31 03:12:58 +00:00
it_should_behave_like "a generic strategy"
describe "db" do
it "should store my desired db" do
subject.db = :my_db
2018-04-26 17:57:18 +00:00
expect(subject.db).to eq :my_db
end
2010-05-31 03:12:58 +00:00
it "should default to :default" do
2018-04-26 17:57:18 +00:00
expect(subject.db).to eq :default
end
end
2010-05-31 03:12:58 +00:00
describe "db=" do
let(:config_location) { "spec/support/example.database.yml" }
2010-05-31 03:12:58 +00:00
it "should process erb in the config" do
subject.db = :my_db
expect(subject.connection_hash).to eq({ "database" => "one" })
end
2010-05-31 03:12:58 +00:00
context 'when config file differs from established ActiveRecord configuration' do
before do
allow(::ActiveRecord::Base).to receive(:configurations).and_return({ "my_db" => { "database" => "two"} })
end
it 'uses the ActiveRecord configuration' do
subject.db = :my_db
2018-04-26 17:57:18 +00:00
expect(subject.connection_hash).to eq({ "database" => "two"})
end
end
context 'when config file agrees with ActiveRecord configuration' do
before do
allow(::ActiveRecord::Base).to receive(:configurations).and_return({ "my_db" => { "database" => "one"} })
end
it 'uses the config file' do
subject.db = :my_db
2018-04-26 17:57:18 +00:00
expect(subject.connection_hash).to eq({ "database" => "one"})
end
end
context 'when ::ActiveRecord::Base.configurations nil' do
before do
2018-04-26 17:57:18 +00:00
allow(::ActiveRecord::Base).to receive(:configurations).and_return(nil)
end
it 'uses the config file' do
subject.db = :my_db
2018-04-26 17:57:18 +00:00
expect(subject.connection_hash).to eq({ "database" => "one"})
end
end
context 'when ::ActiveRecord::Base.configurations empty' do
before do
2018-04-26 17:57:18 +00:00
allow(::ActiveRecord::Base).to receive(:configurations).and_return({})
end
it 'uses the config file' do
subject.db = :my_db
2018-04-26 17:57:18 +00:00
expect(subject.connection_hash).to eq({ "database" => "one"})
end
end
context 'when config file is not available' do
before do
allow(File).to receive(:file?).with(config_location).and_return(false)
end
it "should skip config" do
subject.db = :my_db
expect(subject.connection_hash).not_to be
end
end
it "skips the file when the model is set" do
subject.db = FakeModel
2018-04-26 17:57:18 +00:00
expect(subject.connection_hash).not_to be
end
it "skips the file when the db is set to :default" do
# to avoid https://github.com/bmabey/database_cleaner/issues/72
subject.db = :default
2018-04-26 17:57:18 +00:00
expect(subject.connection_hash).not_to be
end
end
2010-05-31 03:12:58 +00:00
2012-08-25 17:54:01 +00:00
describe "connection_class" do
it "should default to ActiveRecord::Base" do
2018-04-26 17:57:18 +00:00
expect(subject.connection_class).to eq ::ActiveRecord::Base
end
2010-05-31 03:12:58 +00:00
context "with database models" do
context "connection_hash is set" do
it "reuses the model's connection" do
subject.connection_hash = {}
subject.db = FakeModel
2018-04-26 17:57:18 +00:00
expect(subject.connection_class).to eq FakeModel
end
end
context "connection_hash is not set" do
it "reuses the model's connection" do
subject.db = FakeModel
2018-04-26 17:57:18 +00:00
expect(subject.connection_class).to eq FakeModel
end
end
end
context "when connection_hash is set" do
2018-04-26 16:29:13 +00:00
let(:hash) { {} }
before { subject.connection_hash = hash }
2010-05-31 03:12:58 +00:00
it "establishes a connection with it" do
2018-04-26 17:57:18 +00:00
expect(::ActiveRecord::Base).to receive(:establish_connection).with(hash)
expect(subject.connection_class).to eq ::ActiveRecord::Base
end
end
end
end
end
2010-05-31 03:12:58 +00:00
end