Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Make clean_with work for multiple db connections #195

Merged
merged 2 commits into from

2 participants

@johnf

clean_with avoids using strategy= so that it doesn't actually change the
strategy. This means it was bypassing the section that set the database
to actually clean and therefore it was always cleaning the default.

@johnf johnf Make clean_with work for multiple db connections
clean_with avoids using strategy= so that it doesn't actually change the
strategy. This means it was bypassing the section that set the database
to actually clean and therefore it was always cleaning the default.
52b6f9f
@johnf

Not 100% happy with this it duplicates the code already in strategy_db=, but wasn't sure how to best avoid that without a couple of major code changes.

Also I haven't added any tests. @bmabey Could you help point me in the right direction here. I figure something should go into spec/database_cleaner/base_spec.rb but I wasn't sure the best way to achieve it.

@bmabey
Owner

Hmm... yeah, I see the problem. The only straight forward way I see of reducing the duplication without adding additional complexity is to extract strategy_db= as a utility function. Something like:

module DatabaseCleaner
  module StrategyHelper
    def set_strategy_db(strategy, desired_db)
      if strategy.respond_to? :db=
        strategy.db = desired_db
      elsif desired_db!= :default
        raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database"
      end
    end
    module_function :set_strategy_db
  end

  class Base

    #...

We could mix in something to all the strategies to do something similar, but I think having a utility function is less complicated. WDYT?

@johnf

I think that could work well. Would this be a pattern that could be useful elsewhere? Should I create a helpers directory with one file per helper inside?

@johnf

Hmm actually this code isn't used anywhere else so is their any advantage to the helper? I've just commited above without

@bmabey
Owner

No real advantage other than appeasing the code smell/refactoring gods (i.e. pedantic reasons only). What you've done looks good, I'll merge it in. Thanks!

@bmabey bmabey merged commit 466ccfd into DatabaseCleaner:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 24, 2013
  1. @johnf

    Make clean_with work for multiple db connections

    johnf authored
    clean_with avoids using strategy= so that it doesn't actually change the
    strategy. This means it was bypassing the section that set the database
    to actually clean and therefore it was always cleaning the default.
Commits on Mar 29, 2013
  1. @johnf

    refactor our common code

    johnf authored
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 2 deletions.
  1. +11 −1 lib/database_cleaner/base.rb
  2. +1 −1  spec/database_cleaner/base_spec.rb
View
12 lib/database_cleaner/base.rb
@@ -36,12 +36,22 @@ def create_strategy(*args)
def clean_with(*args)
strategy = create_strategy(*args)
+ set_strategy_db strategy, self.db
+
strategy.clean
strategy
end
alias clean_with! clean_with
+ def set_strategy_db(strategy, desired_db)
+ if strategy.respond_to? :db=
+ strategy.db = desired_db
+ elsif desired_db != :default
+ raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database"
+ end
+ end
+
def strategy=(args)
strategy, *strategy_args = args
if strategy.is_a?(Symbol)
@@ -52,7 +62,7 @@ def strategy=(args)
raise ArgumentError, "You must provide a strategy object, or a symbol for a known strategy along with initialization params."
end
- self.strategy_db = self.db
+ set_strategy_db @strategy, self.db
@strategy
end
View
2  spec/database_cleaner/base_spec.rb
@@ -334,7 +334,7 @@ module DatabaseCleaner
it "should attempt to set strategy db" do
subject.stub(:db).and_return(:my_db)
- subject.should_receive(:strategy_db=).with(:my_db)
+ subject.should_receive(:set_strategy_db).with(mock_strategy, :my_db)
subject.strategy = mock_strategy
end
Something went wrong with that request. Please try again.