From e1e1116ea081e1b7da3178f0bcd7ca707c19d588 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Thu, 29 Jul 2021 18:51:02 -0700 Subject: [PATCH 1/3] Add DatabaseCleaner::strategy getter method Issue #511 proposes the addition of a ::strategy getter on the DatabaseCleaner module to aid new users with debugging. This commit introduces that method. Like the ::strategy= setter method, ::strategy is actually defined as an instance method on DatabaseCleaner::Cleaners, and is made available to the top-level module by delegation. Depending on the complexity of the user's configuration, this method may return three different kinds of values: 1. nil (if no strategies have been set on any cleaners); 2. a single strategy (if all cleaners share the same strategy); or 3. a hash of cleaner IDs and the strategies that go with them (if multiple strategies have been set on multiple cleaners). The first of these three cases has not explicitly been tested. === Note This commit also restores a spec context that was removed in c542fee: DatabaseCleaner::Cleaners top level api methods multiple cleaners multiple orm proxy methods with differing orms and dbs This spec context included a single example testing the #orm= method, which was removed in the aforementioned commit. Now, a modified version of the same context is used to test the #strategy method introduced here. --- lib/database_cleaner/cleaners.rb | 13 ++++++++++ lib/database_cleaner/core.rb | 1 + spec/database_cleaner/cleaners_spec.rb | 35 +++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/database_cleaner/cleaners.rb b/lib/database_cleaner/cleaners.rb index f875665..d8d08ac 100644 --- a/lib/database_cleaner/cleaners.rb +++ b/lib/database_cleaner/cleaners.rb @@ -13,6 +13,19 @@ module DatabaseCleaner fetch([orm, opts]) { add_cleaner(orm, **opts) } 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) 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..6411f90 100644 --- a/spec/database_cleaner/cleaners_spec.rb +++ b/spec/database_cleaner/cleaners_spec.rb @@ -55,7 +55,11 @@ 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 + expect(cleaners.strategy).to eq(cleaner.strategy) + end it "should proxy strategy=" do stratagem = double("stratagem") @@ -154,6 +158,27 @@ 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 + + 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 let(:active_record_1) { FakeStrategy.new(:active_record, :default, :truncation) } let(:active_record_2) { FakeStrategy.new(:active_record, :default, :transaction) } @@ -165,6 +190,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 } From e8d2c031ea890ce2a4126c9d9eaa9f4e6b1728c9 Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Mon, 18 Oct 2021 22:13:36 -0400 Subject: [PATCH 2/3] Change strategy method implementation to return a Hash. --- lib/database_cleaner/cleaners.rb | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/database_cleaner/cleaners.rb b/lib/database_cleaner/cleaners.rb index d8d08ac..7fb5940 100644 --- a/lib/database_cleaner/cleaners.rb +++ b/lib/database_cleaner/cleaners.rb @@ -13,17 +13,23 @@ 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 - strategies = transform_values(&:strategy) - - case strategies.values.uniq.compact.count - when 0 - nil - when 1 - strategies.values.first - else - strategies - end + transform_values(&:strategy) end def strategy=(strategy) From c60ce4d2c9a30369a611b425b5fc05b885430b37 Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Mon, 18 Oct 2021 22:13:52 -0400 Subject: [PATCH 3/3] Change expectation for the #strategy method in the Cleaners class --- spec/database_cleaner/cleaners_spec.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/spec/database_cleaner/cleaners_spec.rb b/spec/database_cleaner/cleaners_spec.rb index 6411f90..6971716 100644 --- a/spec/database_cleaner/cleaners_spec.rb +++ b/spec/database_cleaner/cleaners_spec.rb @@ -57,10 +57,6 @@ RSpec.describe DatabaseCleaner::Cleaners do context "single orm single db" do let!(:cleaner) { cleaners[:active_record] } - it "should proxy strategy" do - expect(cleaners.strategy).to eq(cleaner.strategy) - end - it "should proxy strategy=" do stratagem = double("stratagem") expect(cleaner).to receive(:strategy=).with(stratagem) @@ -171,11 +167,16 @@ RSpec.describe DatabaseCleaner::Cleaners do }) 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(active_record_1.strategy) - .and eq(active_record_2.strategy) - .and eq(data_mapper_1.strategy) + expect(cleaners.strategy).to eq(result) end end