Merge branch 'rlue-strategy-getter'

This commit is contained in:
Ernesto Tagwerker 2021-10-18 22:14:30 -04:00
commit 22b87c882f
3 changed files with 55 additions and 1 deletions

View file

@ -13,6 +13,25 @@ module DatabaseCleaner
fetch([orm, opts]) { add_cleaner(orm, **opts) }
end
# It returns a hash with all the strategies associated with
# all the cleaners.
#
# For example:
#
# ```
# cleaners.strategy
# => {
# :active_record_1 => :truncation,
# :active_record_2 => :truncation,
# :data_mapper_1 => :truncation
# }
# ```
#
# @return [Hash]
def strategy
transform_values(&:strategy)
end
def strategy=(strategy)
values.each { |cleaner| cleaner.strategy = strategy }
remove_duplicates

View file

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

View file

@ -55,7 +55,7 @@ RSpec.describe DatabaseCleaner::Cleaners do
context "top level api methods" do
context "single orm single db" do
let(:cleaner) { cleaners[:active_record] }
let!(:cleaner) { cleaners[:active_record] }
it "should proxy strategy=" do
stratagem = double("stratagem")
@ -154,6 +154,32 @@ RSpec.describe DatabaseCleaner::Cleaners do
context "multiple orm proxy methods" do
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
let(:result) do
{
:active_record_1 => :truncation,
:active_record_2 => :truncation,
:data_mapper_1 => :truncation
}
end
it "should proxy #strategy to all cleaners and return a single result" do
expect(cleaners.strategy).to eq(result)
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) }
@ -165,6 +191,14 @@ RSpec.describe DatabaseCleaner::Cleaners do
})
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
expect { cleaners.strategy = :truncation }
.to change { cleaners.values }