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'
|
2018-05-23 17:38:23 +00:00
|
|
|
require 'database_cleaner/spec'
|
2010-04-22 16:41:02 +00:00
|
|
|
|
2012-08-25 18:08:12 +00:00
|
|
|
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
|
2018-05-19 04:43:52 +00:00
|
|
|
DatabaseCleaner.app_root = nil
|
2010-05-30 22:09:30 +00:00
|
|
|
end
|
2010-05-31 03:12:58 +00:00
|
|
|
|
2018-05-19 04:43:52 +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
|
2010-04-22 16:41:02 +00:00
|
|
|
class ExampleStrategy
|
2018-04-26 19:06:01 +00:00
|
|
|
include DatabaseCleaner::ActiveRecord::Base
|
2010-05-30 22:09:30 +00:00
|
|
|
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
|
|
|
|
2018-05-19 04:43:52 +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"
|
2010-05-30 22:09:30 +00:00
|
|
|
|
|
|
|
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
|
2010-05-30 22:09:30 +00:00
|
|
|
end
|
2010-05-31 03:12:58 +00:00
|
|
|
|
2010-05-30 22:09:30 +00:00
|
|
|
it "should default to :default" do
|
2018-04-26 17:57:18 +00:00
|
|
|
expect(subject.db).to eq :default
|
2010-05-30 22:09:30 +00:00
|
|
|
end
|
|
|
|
end
|
2010-05-31 03:12:58 +00:00
|
|
|
|
2018-05-19 05:26:43 +00:00
|
|
|
describe "db=" do
|
2018-05-22 14:42:17 +00:00
|
|
|
let(:config_location) { "spec/support/example.database.yml" }
|
2010-05-31 03:12:58 +00:00
|
|
|
|
2010-08-03 02:53:11 +00:00
|
|
|
it "should process erb in the config" do
|
2018-05-19 05:26:43 +00:00
|
|
|
subject.db = :my_db
|
|
|
|
expect(subject.connection_hash).to eq({ "database" => "one" })
|
2010-05-30 22:09:30 +00:00
|
|
|
end
|
2010-05-31 03:12:58 +00:00
|
|
|
|
2018-05-19 05:26:43 +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"})
|
2014-12-13 16:48:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-19 05:26:43 +00:00
|
|
|
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"})
|
2014-12-13 16:48:07 +00:00
|
|
|
end
|
2018-05-19 05:26:43 +00:00
|
|
|
end
|
2014-12-13 16:48:07 +00:00
|
|
|
|
2018-05-19 05:26:43 +00:00
|
|
|
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)
|
2018-05-19 05:26:43 +00:00
|
|
|
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"})
|
2014-12-13 16:48:07 +00:00
|
|
|
end
|
2018-05-19 05:26:43 +00:00
|
|
|
end
|
2014-12-13 16:48:07 +00:00
|
|
|
|
2018-05-19 05:26:43 +00:00
|
|
|
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({})
|
2018-05-19 05:26:43 +00:00
|
|
|
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"})
|
2014-12-13 16:48:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-19 05:26:43 +00:00
|
|
|
context 'when config file is not available' do
|
|
|
|
before do
|
|
|
|
allow(File).to receive(:file?).with(config_location).and_return(false)
|
|
|
|
end
|
2010-12-03 00:39:07 +00:00
|
|
|
|
2018-05-19 05:26:43 +00:00
|
|
|
it "should skip config" do
|
|
|
|
subject.db = :my_db
|
|
|
|
expect(subject.connection_hash).not_to be
|
|
|
|
end
|
2012-11-02 19:42:15 +00:00
|
|
|
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
|
2010-12-03 00:39:07 +00:00
|
|
|
end
|
2011-12-16 02:12:22 +00:00
|
|
|
|
|
|
|
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
|
2011-12-16 02:12:22 +00:00
|
|
|
end
|
2010-05-30 22:09:30 +00:00
|
|
|
end
|
2010-05-31 03:12:58 +00:00
|
|
|
|
2012-08-25 17:54:01 +00:00
|
|
|
describe "connection_class" do
|
2010-05-30 22:09:30 +00:00
|
|
|
it "should default to ActiveRecord::Base" do
|
2018-04-26 17:57:18 +00:00
|
|
|
expect(subject.connection_class).to eq ::ActiveRecord::Base
|
2010-05-30 22:09:30 +00:00
|
|
|
end
|
2010-05-31 03:12:58 +00:00
|
|
|
|
2012-11-02 19:42:15 +00:00
|
|
|
context "with database models" do
|
|
|
|
context "connection_hash is set" do
|
2018-05-19 05:44:22 +00:00
|
|
|
it "reuses the model's connection" do
|
|
|
|
subject.connection_hash = {}
|
2012-11-02 19:42:15 +00:00
|
|
|
subject.db = FakeModel
|
2018-04-26 17:57:18 +00:00
|
|
|
expect(subject.connection_class).to eq FakeModel
|
2012-11-02 19:42:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "connection_hash is not set" do
|
2018-05-19 05:44:22 +00:00
|
|
|
it "reuses the model's connection" do
|
2012-11-02 19:42:15 +00:00
|
|
|
subject.db = FakeModel
|
2018-04-26 17:57:18 +00:00
|
|
|
expect(subject.connection_class).to eq FakeModel
|
2012-11-02 19:42:15 +00:00
|
|
|
end
|
|
|
|
end
|
2012-08-25 18:08:12 +00:00
|
|
|
end
|
|
|
|
|
2010-05-30 22:09:30 +00:00
|
|
|
context "when connection_hash is set" do
|
2018-04-26 16:29:13 +00:00
|
|
|
let(:hash) { {} }
|
2018-05-19 05:44:22 +00:00
|
|
|
before { subject.connection_hash = hash }
|
2010-05-31 03:12:58 +00:00
|
|
|
|
2018-05-19 05:44:22 +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)
|
2017-05-24 15:03:14 +00:00
|
|
|
expect(subject.connection_class).to eq ::ActiveRecord::Base
|
2010-05-30 22:09:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-04-22 16:41:02 +00:00
|
|
|
end
|
|
|
|
end
|
2010-05-31 03:12:58 +00:00
|
|
|
end
|