WIP- a better approach to #72 making the it clear that the DB was unspecified

For some reason DataMapper does not like these changes.. I'm also unsure
how to handle the Sequel part since it has some different logic
concerning this.  Rethinking how DBCleaner handles this type of
situation would be beneficial..
This commit is contained in:
Ben Mabey 2011-11-03 17:00:19 -06:00
parent f8df3db3af
commit 77f9130a81
14 changed files with 50 additions and 98 deletions

View file

@ -20,13 +20,13 @@ module DatabaseCleaner
def strategy_db=(desired_db) def strategy_db=(desired_db)
if strategy.respond_to? :db= if strategy.respond_to? :db=
strategy.db = desired_db strategy.db = desired_db
elsif desired_db!= :default elsif desired_db != :unspecified
raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database" raise ArgumentError, "The #{strategy.class} strategy does not allow you to specify a database. Use a different strategy or remove the database specification."
end end
end end
def db def db
@db || :default @db || :unspecified
end end
def create_strategy(*args) def create_strategy(*args)

View file

@ -4,6 +4,7 @@ module DatabaseCleaner
class NoORMDetected < StandardError; end class NoORMDetected < StandardError; end
class UnknownStrategySpecified < ArgumentError; end class UnknownStrategySpecified < ArgumentError; end
class UnspecifiedDatabase < ArgumentError; end
class << self class << self
def [](orm,opts = {}) def [](orm,opts = {})

View file

@ -7,15 +7,7 @@ module DatabaseCleaner
module Base module Base
include ::DatabaseCleaner::Generic::Base include ::DatabaseCleaner::Generic::Base
include ::DatabaseCleaner::Generic::ConfigurableDB
def db=(desired_db)
@db = desired_db
end
def db
@db || :default
end
end end
end end
end end

View file

@ -1,13 +1,22 @@
module ::DatabaseCleaner module ::DatabaseCleaner
module Generic module Generic
module Base module ConfigurableDB
def db=(desired_db)
@db = desired_db
end
def db
@db || :unspecified
end
end
module Base
def self.included(base) def self.included(base)
base.extend(ClassMethods) base.extend(ClassMethods)
end end
def db def db
:default :unspecified
end end
module ClassMethods module ClassMethods

View file

@ -7,14 +7,7 @@ module DatabaseCleaner
module Base module Base
include ::DatabaseCleaner::Generic::Base include ::DatabaseCleaner::Generic::Base
include ::DatabaseCleaner::Generic::ConfigurableDB
def db=(desired_db)
@db = desired_db
end
def db
@db || :default
end
end end
end end
end end

View file

@ -7,14 +7,7 @@ module DatabaseCleaner
module Base module Base
include ::DatabaseCleaner::Generic::Base include ::DatabaseCleaner::Generic::Base
include ::DatabaseCleaner::Generic::ConfigurableDB
def db=(desired_db)
@db = desired_db
end
def db
@db || :default
end
end end
end end
end end

View file

@ -13,9 +13,11 @@ module DatabaseCleaner
end end
def db def db
return @db if @db && @db != :default return @db if @db && @db != :unspecified
raise "As you have more than one active sequel database you have to specify the one to use manually!" if ::Sequel::DATABASES.count > 1 if ::Sequel::DATABASES.count > 1
::Sequel::DATABASES.first || :default raise ::DatabaseCleaner::UnspecifiedDatabase, "You have more than one active sequel database. You must specify which one to use!"
end
::Sequel::DATABASES.first || :unspecified
end end
end end
end end

View file

@ -34,19 +34,14 @@ module DatabaseCleaner
it_should_behave_like "a generic strategy" it_should_behave_like "a generic strategy"
describe "db" do describe "db" do
it { should respond_to(:db=) } before(:each) { subject.stub(:load_config)}
it_should_behave_like "a strategy with configurable db"
it "should store my desired db" do it "should store my desired db" do
subject.stub(:load_config)
subject.db = :my_db subject.db = :my_db
subject.db.should == :my_db subject.db.should == :my_db
end end
it "should default to :default" do
subject.db.should == :default
end
it "should load_config when I set db" do it "should load_config when I set db" do
subject.should_receive(:load_config) subject.should_receive(:load_config)
subject.db = :my_db subject.db = :my_db
@ -54,9 +49,6 @@ module DatabaseCleaner
end end
describe "load_config" do describe "load_config" do
it { should respond_to(:load_config) }
before do before do
yaml = <<-Y yaml = <<-Y
my_db: my_db:

View file

@ -128,12 +128,12 @@ module DatabaseCleaner
describe "comparison" do describe "comparison" do
it "should be equal if orm, connection and strategy are the same" do it "should be equal if orm, connection and strategy are the same" do
strategy = mock("strategy") strategy = mock("strategy", {:db= => nil})
one = DatabaseCleaner::Base.new(:active_record,:connection => :default) one = DatabaseCleaner::Base.new(:active_record,:connection => :foo)
one.strategy = strategy one.strategy = strategy
two = DatabaseCleaner::Base.new(:active_record,:connection => :default) two = DatabaseCleaner::Base.new(:active_record,:connection => :foo)
two.strategy = strategy two.strategy = strategy
one.should == two one.should == two
@ -178,8 +178,8 @@ module DatabaseCleaner
end end
describe "db" do describe "db" do
it "should default to :default" do it "should default to :unspecified" do
subject.db.should == :default subject.db.should == :unspecified
end end
it "should return any stored db value" do it "should return any stored db value" do
@ -219,14 +219,7 @@ module DatabaseCleaner
context "when strategy doesn't supports db specification" do context "when strategy doesn't supports db specification" do
before(:each) { strategy.stub(:respond_to?).with(:db=).and_return false } before(:each) { strategy.stub(:respond_to?).with(:db=).and_return false }
it "should check to see if db is :default" do it "raises an error when db isn't default" do
db = mock("default")
db.should_receive(:==).with(:default).and_return(true)
subject.strategy_db = db
end
it "should raise an argument error when db isn't default" do
db = mock("a db") db = mock("a db")
expect{ subject.strategy_db = db }.to raise_error ArgumentError expect{ subject.strategy_db = db }.to raise_error ArgumentError
end end

View file

@ -14,17 +14,7 @@ module DatabaseCleaner
describe ExampleStrategy do describe ExampleStrategy do
it_should_behave_like "a generic strategy" it_should_behave_like "a generic strategy"
it { should respond_to(:db) } it_should_behave_like "a strategy with configurable db"
it { should respond_to(:db=) }
it "should store my desired db" do
subject.db = :my_db
subject.db.should == :my_db
end
it "should default to :default" do
subject.db.should == :default
end
end end
end end
end end

View file

@ -15,8 +15,7 @@ module ::DatabaseCleaner
end end
it_should_behave_like "a generic strategy" it_should_behave_like "a generic strategy"
its(:db) { should == :unspecified }
its(:db) { should == :default }
end end
end end
end end

View file

@ -13,21 +13,8 @@ module DatabaseCleaner
end end
describe ExampleStrategy do describe ExampleStrategy do
it_should_behave_like "a generic strategy" it_should_behave_like "a generic strategy"
it_should_behave_like "a strategy with configurable db"
describe "db" do
it { should respond_to(:db=) }
it "should store my desired db" do
subject.db = :my_db
subject.db.should == :my_db
end
it "should default to :default" do
subject.db.should == :default
end
end
end end
end end
end end

View file

@ -15,17 +15,6 @@ module DatabaseCleaner
describe ExampleStrategy do describe ExampleStrategy do
it_should_behave_like "a generic strategy" it_should_behave_like "a generic strategy"
it { should respond_to(:db) }
it { should respond_to(:db=) }
it "should store my desired db" do
subject.db = :my_db
subject.db.should == :my_db
end
it "should default to :default" do
subject.db.should == :default
end
end end
end end
end end

View file

@ -2,6 +2,18 @@ shared_examples_for "a generic strategy" do
it { should respond_to(:db) } it { should respond_to(:db) }
end end
shared_examples_for "a strategy with configurable db" do
it "stores the desired db" do
subject.db = :my_db
subject.db.should == :my_db
end
it "defaults #db to :unspecified" do
subject.db.should == :unspecified
end
end
shared_examples_for "a generic truncation strategy" do shared_examples_for "a generic truncation strategy" do
it { should respond_to(:start) } it { should respond_to(:start) }
it { should respond_to(:clean) } it { should respond_to(:clean) }