Merge branch 'strategy-getter' of https://github.com/rlue/database_cleaner into rlue-strategy-getter

This commit is contained in:
Ernesto Tagwerker 2021-10-18 22:04:46 -04:00
commit 933df38680
3 changed files with 48 additions and 1 deletions

View file

@ -13,6 +13,19 @@ module DatabaseCleaner
fetch([orm, opts]) { add_cleaner(orm, **opts) } fetch([orm, opts]) { add_cleaner(orm, **opts) }
end end
def strategy
strategies = transform_values(&:strategy)
case strategies.values.uniq.compact.count
when 0
nil
when 1
strategies.values.first
else
strategies
end
end
def strategy=(strategy) def strategy=(strategy)
values.each { |cleaner| cleaner.strategy = strategy } values.each { |cleaner| cleaner.strategy = strategy }
remove_duplicates remove_duplicates

View file

@ -7,6 +7,7 @@ module DatabaseCleaner
extend Forwardable extend Forwardable
delegate [ delegate [
:[], :[],
:strategy,
:strategy=, :strategy=,
:start, :start,
:clean, :clean,

View file

@ -55,7 +55,11 @@ RSpec.describe DatabaseCleaner::Cleaners do
context "top level api methods" do context "top level api methods" do
context "single orm single db" do context "single orm single db" do
let(:cleaner) { cleaners[:active_record] } let!(:cleaner) { cleaners[:active_record] }
it "should proxy strategy" do
expect(cleaners.strategy).to eq(cleaner.strategy)
end
it "should proxy strategy=" do it "should proxy strategy=" do
stratagem = double("stratagem") stratagem = double("stratagem")
@ -154,6 +158,27 @@ RSpec.describe DatabaseCleaner::Cleaners do
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 same strategy but differing orms and dbs" do
let(:active_record_1) { FakeStrategy.new(:active_record, nil, :truncation) }
let(:active_record_2) { FakeStrategy.new(:active_record, :different, :truncation) }
let(:data_mapper_1) { FakeStrategy.new(:data_mapper, nil, :truncation) }
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 #strategy to all cleaners and return a single result" do
expect(cleaners.strategy)
.to eq(active_record_1.strategy)
.and eq(active_record_2.strategy)
.and eq(data_mapper_1.strategy)
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) }
@ -165,6 +190,14 @@ RSpec.describe DatabaseCleaner::Cleaners do
}) })
end end
it "should proxy #strategy to all cleaners and return a hash of results" do
expect(cleaners.strategy)
.to eq({
active_record_1: active_record_1.strategy,
active_record_2: active_record_2.strategy
})
end
it "should proxy #strategy= to all cleaners and remove duplicate cleaners" do it "should proxy #strategy= to all cleaners and remove duplicate cleaners" do
expect { cleaners.strategy = :truncation } expect { cleaners.strategy = :truncation }
.to change { cleaners.values } .to change { cleaners.values }