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)
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"
elsif desired_db != :unspecified
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
def db
@db || :default
@db || :unspecified
end
def create_strategy(*args)

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -34,19 +34,14 @@ module DatabaseCleaner
it_should_behave_like "a generic strategy"
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
subject.stub(:load_config)
subject.db = :my_db
subject.db.should == :my_db
end
it "should default to :default" do
subject.db.should == :default
end
it "should load_config when I set db" do
subject.should_receive(:load_config)
subject.db = :my_db
@ -54,9 +49,6 @@ module DatabaseCleaner
end
describe "load_config" do
it { should respond_to(:load_config) }
before do
yaml = <<-Y
my_db:

View file

@ -47,7 +47,7 @@ module DatabaseCleaner
Object.send(:remove_const, 'CouchPotato') if defined?(::CouchPotato)
Object.send(:remove_const, 'Sequel') if defined?(::Sequel)
end
let(:cleaner) { DatabaseCleaner::Base.new :autodetect }
it "should raise an error when no ORM is detected" do
@ -103,7 +103,7 @@ module DatabaseCleaner
cleaner.orm.should == :couch_potato
cleaner.should be_auto_detected
end
it "should detect Sequel last" do
Object.const_set('Sequel', 'Sequel mock')
@ -128,12 +128,12 @@ module DatabaseCleaner
describe "comparison" 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
two = DatabaseCleaner::Base.new(:active_record,:connection => :default)
two = DatabaseCleaner::Base.new(:active_record,:connection => :foo)
two.strategy = strategy
one.should == two
@ -160,7 +160,7 @@ module DatabaseCleaner
cleaner = ::DatabaseCleaner::Base.new "mongoid"
cleaner.orm.should == :mongoid
end
it "is autodetected if orm is not provided" do
cleaner = ::DatabaseCleaner::Base.new
cleaner.should be_auto_detected
@ -178,8 +178,8 @@ module DatabaseCleaner
end
describe "db" do
it "should default to :default" do
subject.db.should == :default
it "should default to :unspecified" do
subject.db.should == :unspecified
end
it "should return any stored db value" do
@ -219,14 +219,7 @@ module DatabaseCleaner
context "when strategy doesn't supports db specification" do
before(:each) { strategy.stub(:respond_to?).with(:db=).and_return false }
it "should check to see if db is :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
it "raises an error when db isn't default" do
db = mock("a db")
expect{ subject.strategy_db = db }.to raise_error ArgumentError
end

View file

@ -14,17 +14,7 @@ module DatabaseCleaner
describe ExampleStrategy do
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
it_should_behave_like "a strategy with configurable db"
end
end
end

View file

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

View file

@ -13,21 +13,8 @@ module DatabaseCleaner
end
describe ExampleStrategy do
it_should_behave_like "a generic strategy"
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
it_should_behave_like "a strategy with configurable db"
end
end
end

View file

@ -15,17 +15,6 @@ module DatabaseCleaner
describe ExampleStrategy do
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

View file

@ -2,6 +2,18 @@ shared_examples_for "a generic strategy" do
it { should respond_to(:db) }
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
it { should respond_to(:start) }
it { should respond_to(:clean) }