diff --git a/lib/database_cleaner/cleaners.rb b/lib/database_cleaner/cleaners.rb index f875665..7fb5940 100644 --- a/lib/database_cleaner/cleaners.rb +++ b/lib/database_cleaner/cleaners.rb @@ -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 diff --git a/lib/database_cleaner/core.rb b/lib/database_cleaner/core.rb index d773dcb..124cfaa 100644 --- a/lib/database_cleaner/core.rb +++ b/lib/database_cleaner/core.rb @@ -7,6 +7,7 @@ module DatabaseCleaner extend Forwardable delegate [ :[], + :strategy, :strategy=, :start, :clean, diff --git a/spec/database_cleaner/cleaners_spec.rb b/spec/database_cleaner/cleaners_spec.rb index 9f5cd5b..6971716 100644 --- a/spec/database_cleaner/cleaners_spec.rb +++ b/spec/database_cleaner/cleaners_spec.rb @@ -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 }