change configuration test to test cleaners directly.

This commit is contained in:
Micah Geisel 2020-04-05 00:02:47 -07:00 committed by Micah Geisel
parent b194419fcd
commit 9055df104f

View file

@ -1,45 +1,45 @@
RSpec.describe DatabaseCleaner::Configuration do
subject(:config) { described_class.new }
RSpec.describe DatabaseCleaner::Cleaners do
subject(:cleaners) { described_class.new }
context "orm specification" do
it "should not accept nil orms" do
expect { config[nil] }.to raise_error(ArgumentError)
expect { cleaners[nil] }.to raise_error(ArgumentError)
end
it "should accept :active_record" do
cleaner = config[:active_record]
cleaner = cleaners[:active_record]
expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :active_record
expect(config.cleaners.values).to eq [cleaner]
expect(cleaners.values).to eq [cleaner]
end
it "should accept multiple orm's" do
cleaners = [config[:couch_potato], config[:data_mapper]]
expect(config.cleaners.values.map(&:orm)).to eq [:couch_potato, :data_mapper]
expect(config.cleaners.values).to eq cleaners
cleaners_values = [cleaners[:couch_potato], cleaners[:data_mapper]]
expect(cleaners.values.map(&:orm)).to eq [:couch_potato, :data_mapper]
expect(cleaners.values).to eq cleaners_values
end
it "should accept a connection parameter and store it" do
cleaner = config[:active_record, connection: :first_connection]
cleaner = cleaners[:active_record, connection: :first_connection]
expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :active_record
expect(cleaner.db).to eq :first_connection
end
it "should accept multiple connections for a single orm" do
config[:data_mapper, connection: :first_db]
config[:data_mapper, connection: :second_db]
expect(config.cleaners.values.map(&:orm)).to eq [:data_mapper, :data_mapper]
expect(config.cleaners.values.map(&:db)).to eq [:first_db, :second_db]
cleaners[:data_mapper, connection: :first_db]
cleaners[:data_mapper, connection: :second_db]
expect(cleaners.values.map(&:orm)).to eq [:data_mapper, :data_mapper]
expect(cleaners.values.map(&:db)).to eq [:first_db, :second_db]
end
it "should accept multiple connections and multiple orms" do
config[:data_mapper, connection: :first_db ]
config[:active_record, connection: :second_db]
config[:active_record, connection: :first_db ]
config[:data_mapper, connection: :second_db]
expect(config.cleaners.values.map(&:orm)).to eq [:data_mapper, :active_record, :active_record, :data_mapper]
expect(config.cleaners.values.map(&:db)).to eq [:first_db, :second_db, :first_db, :second_db]
cleaners[:data_mapper, connection: :first_db ]
cleaners[:active_record, connection: :second_db]
cleaners[:active_record, connection: :first_db ]
cleaners[:data_mapper, connection: :second_db]
expect(cleaners.values.map(&:orm)).to eq [:data_mapper, :active_record, :active_record, :data_mapper]
expect(cleaners.values.map(&:db)).to eq [:first_db, :second_db, :first_db, :second_db]
end
it "should retrieve a db rather than create a new one" do
@ -47,47 +47,47 @@ RSpec.describe DatabaseCleaner::Configuration do
strategy_class = class_double("DatabaseCleaner::ActiveRecord::Truncation").as_stubbed_const
allow(strategy_class).to receive(:new)
cleaner = config[:active_record]
config[:active_record].strategy = :truncation
expect(config[:active_record]).to equal cleaner
cleaner = cleaners[:active_record]
cleaners[:active_record].strategy = :truncation
expect(cleaners[:active_record]).to equal cleaner
end
end
context "top level api methods" do
context "single orm single connection" do
let(:connection) { config[:active_record] }
let(:cleaner) { cleaners[:active_record] }
it "should proxy strategy=" do
stratagem = double("stratagem")
expect(connection).to receive(:strategy=).with(stratagem)
config.strategy = stratagem
expect(cleaner).to receive(:strategy=).with(stratagem)
cleaners.strategy = stratagem
end
it "should proxy orm=" do
orm = double("orm")
expect(connection).to receive(:orm=).with(orm)
config.orm = orm
expect(cleaner).to receive(:orm=).with(orm)
cleaners.orm = orm
end
it "should proxy start" do
expect(connection).to receive(:start)
config.start
expect(cleaner).to receive(:start)
cleaners.start
end
it "should proxy clean" do
expect(connection).to receive(:clean)
config.clean
expect(cleaner).to receive(:clean)
cleaners.clean
end
it 'should proxy cleaning' do
expect(connection).to receive(:cleaning)
config.cleaning { }
expect(cleaner).to receive(:cleaning)
cleaners.cleaning { }
end
it "should proxy clean_with" do
stratagem = double("stratgem")
expect(connection).to receive(:clean_with).with(stratagem, {})
config.clean_with stratagem, {}
expect(cleaner).to receive(:clean_with).with(stratagem, {})
cleaners.clean_with stratagem, {}
end
end
@ -98,8 +98,8 @@ RSpec.describe DatabaseCleaner::Configuration do
let(:active_record) { double("active_mock") }
let(:data_mapper) { double("data_mock") }
before do
config.cleaners = DatabaseCleaner::Cleaners.new({
subject(:cleaners) do
DatabaseCleaner::Cleaners.new({
active_record: active_record,
data_mapper: data_mapper,
})
@ -109,21 +109,21 @@ RSpec.describe DatabaseCleaner::Configuration do
expect(active_record).to receive(:orm=)
expect(data_mapper).to receive(:orm=)
config.orm = :orm
cleaners.orm = :orm
end
it "should proxy start to all cleaners" do
expect(active_record).to receive(:start)
expect(data_mapper).to receive(:start)
config.start
cleaners.start
end
it "should proxy clean to all cleaners" do
expect(active_record).to receive(:clean)
expect(data_mapper).to receive(:clean)
config.clean
cleaners.clean
end
it "should proxy clean_with to all cleaners" do
@ -131,7 +131,7 @@ RSpec.describe DatabaseCleaner::Configuration do
expect(active_record).to receive(:clean_with).with(stratagem)
expect(data_mapper).to receive(:clean_with).with(stratagem)
config.clean_with stratagem
cleaners.clean_with stratagem
end
it "should initiate cleaning on each connection, yield, and finish cleaning each connection" do
@ -148,7 +148,7 @@ RSpec.describe DatabaseCleaner::Configuration do
end
yielded = false
config.cleaning do
cleaners.cleaning do
expect(active_record.started).to eq(true)
expect(data_mapper.started).to eq(true)
expect(active_record.cleaned).to eq(nil)
@ -172,8 +172,8 @@ RSpec.describe DatabaseCleaner::Configuration do
let(:active_record_2) { FakeStrategy.new(:active_record, :different) }
let(:data_mapper_1) { FakeStrategy.new(:data_mapper) }
before do
config.cleaners = DatabaseCleaner::Cleaners.new({
subject(:cleaners) do
DatabaseCleaner::Cleaners.new({
active_record_1: active_record_1,
active_record_2: active_record_2,
data_mapper_1: data_mapper_1,
@ -181,8 +181,8 @@ RSpec.describe DatabaseCleaner::Configuration do
end
it "should proxy #orm= to all cleaners and remove duplicate cleaners" do
expect { config.orm = :data_mapper }
.to change { config.cleaners.values }
expect { cleaners.orm = :data_mapper }
.to change { cleaners.values }
.from([active_record_1,active_record_2,data_mapper_1])
.to([active_record_1,active_record_2])
end
@ -192,16 +192,16 @@ RSpec.describe DatabaseCleaner::Configuration do
let(:active_record_1) { FakeStrategy.new(:active_record, :default, :truncation) }
let(:active_record_2) { FakeStrategy.new(:active_record, :default, :transaction) }
before do
config.cleaners = DatabaseCleaner::Cleaners.new({
subject(:cleaners) do
DatabaseCleaner::Cleaners.new({
active_record_1: active_record_1,
active_record_2: active_record_2,
})
end
it "should proxy #strategy= to all cleaners and remove duplicate cleaners" do
expect { config.strategy = :truncation }
.to change { config.cleaners.values }
expect { cleaners.strategy = :truncation }
.to change { cleaners.values }
.from([active_record_1,active_record_2])
.to([active_record_1])
end