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

View file

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

View file

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

View file

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

View file

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