Cleaner#orm is readonly after creation.

This commit is contained in:
Micah Geisel 2020-04-06 15:44:27 -07:00 committed by Micah Geisel
parent eb59319c5f
commit c542fee62a
5 changed files with 10 additions and 76 deletions

View file

@ -13,12 +13,14 @@ module DatabaseCleaner
[orm, db] <=> [other.orm, other.db]
end
def initialize(orm = :null, opts = {})
self.orm = orm
def initialize(orm, opts = {})
@orm = orm
self.db = opts[:connection] || opts[:model] if opts.has_key?(:connection) || opts.has_key?(:model)
Safeguard.new.run
end
attr_reader :orm
def db=(desired_db)
@db = self.strategy_db = desired_db
end
@ -44,13 +46,6 @@ module DatabaseCleaner
@strategy ||= NullStrategy.new
end
attr_reader :orm
def orm= orm
raise ArgumentError if orm.nil?
@orm = orm.to_sym
end
extend Forwardable
delegate [:start, :clean, :cleaning] => :strategy

View file

@ -17,11 +17,6 @@ module DatabaseCleaner
remove_duplicates
end
def orm=(orm)
values.each { |cleaner| cleaner.orm = orm }
remove_duplicates
end
def start
values.each { |connection| connection.start }
end

View file

@ -8,7 +8,6 @@ module DatabaseCleaner
delegate [
:[],
:strategy=,
:orm=,
:start,
:clean,
:clean_with,

View file

@ -41,25 +41,14 @@ module DatabaseCleaner
expect(cleaner.orm).to eq :a_orm
end
it "converts string to symbols" do
cleaner = Cleaner.new("mongoid")
expect(cleaner.orm).to eq :mongoid
end
it "should default to :null" do
cleaner = Cleaner.new
expect(cleaner.orm).to eq :null
end
it "raises ArgumentError when explicitly set to nil" do
cleaner = Cleaner.new
expect { cleaner.orm = nil }.to raise_error(ArgumentError)
it "raises ArgumentError when no orm is specified" do
expect { Cleaner.new }.to raise_error(ArgumentError)
end
end
end
describe "db" do
subject(:cleaner) { Cleaner.new }
subject(:cleaner) { Cleaner.new(:orm) }
it "should default to :default" do
expect(cleaner.db).to eq :default
@ -72,7 +61,7 @@ module DatabaseCleaner
end
describe "db=" do
subject(:cleaner) { Cleaner.new }
subject(:cleaner) { Cleaner.new(:orm) }
context "when strategy supports db specification" do
it "should pass db down to its current strategy" do
@ -182,18 +171,8 @@ module DatabaseCleaner
end
end
describe "orm" do
let(:mock_orm) { double("orm") }
it "should return orm if orm set" do
cleaner = Cleaner.new
cleaner.orm = :desired_orm
expect(cleaner.orm).to eq :desired_orm
end
end
describe "proxy methods" do
subject(:cleaner) { Cleaner.new }
subject(:cleaner) { Cleaner.new(:orm) }
let(:strategy) { double(:strategy) }

View file

@ -63,12 +63,6 @@ RSpec.describe DatabaseCleaner::Cleaners do
cleaners.strategy = stratagem
end
it "should proxy orm=" do
orm = double("orm")
expect(cleaner).to receive(:orm=).with(orm)
cleaners.orm = orm
end
it "should proxy start" do
expect(cleaner).to receive(:start)
cleaners.start
@ -105,13 +99,6 @@ RSpec.describe DatabaseCleaner::Cleaners do
})
end
it "should proxy orm to all cleaners" do
expect(active_record).to receive(:orm=)
expect(data_mapper).to receive(: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)
@ -163,31 +150,10 @@ RSpec.describe DatabaseCleaner::Cleaners do
end
# ah now we have some difficulty, we mustn't allow duplicate cleaners to exist, but they could
# plausably want to force orm/strategy change on two sets of orm that differ only on db
# plausably want to force strategy change on two sets of orm that differ only on db
context "multiple orm proxy methods" do
class FakeStrategy < Struct.new(:orm, :db, :strategy); end
context "with differing orms and dbs" do
let(:active_record_1) { FakeStrategy.new(:active_record) }
let(:active_record_2) { FakeStrategy.new(:active_record, :different) }
let(:data_mapper_1) { FakeStrategy.new(:data_mapper) }
subject(:cleaners) do
DatabaseCleaner::Cleaners.new({
active_record_1: active_record_1,
active_record_2: active_record_2,
data_mapper_1: data_mapper_1,
})
end
it "should proxy #orm= to all cleaners and remove duplicate cleaners" do
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
end
context "with differing strategies" do
let(:active_record_1) { FakeStrategy.new(:active_record, :default, :truncation) }
let(:active_record_2) { FakeStrategy.new(:active_record, :default, :transaction) }