Browse files

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

…pecified

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..
  • Loading branch information...
1 parent f8df3db commit 77f9130a81d51df305068a245387dbe3711d2ece @bmabey bmabey committed Nov 3, 2011
View
6 lib/database_cleaner/base.rb
@@ -20,13 +20,13 @@ def db=(desired_db)
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
1 lib/database_cleaner/configuration.rb
@@ -4,6 +4,7 @@ module DatabaseCleaner
class NoORMDetected < StandardError; end
class UnknownStrategySpecified < ArgumentError; end
+ class UnspecifiedDatabase < ArgumentError; end
class << self
def [](orm,opts = {})
View
10 lib/database_cleaner/data_mapper/base.rb
@@ -7,15 +7,7 @@ def self.available_strategies
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
15 lib/database_cleaner/generic/base.rb
@@ -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
9 lib/database_cleaner/mongo_mapper/base.rb
@@ -7,14 +7,7 @@ def self.available_strategies
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
9 lib/database_cleaner/mongoid/base.rb
@@ -7,14 +7,7 @@ def self.available_strategies
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
8 lib/database_cleaner/sequel/base.rb
@@ -13,9 +13,11 @@ def db=(desired_db)
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
12 spec/database_cleaner/active_record/base_spec.rb
@@ -34,29 +34,21 @@ class ExampleStrategy
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
end
end
describe "load_config" do
-
- it { should respond_to(:load_config) }
-
before do
yaml = <<-Y
my_db:
View
25 spec/database_cleaner/base_spec.rb
@@ -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
12 spec/database_cleaner/data_mapper/base_spec.rb
@@ -14,17 +14,7 @@ class ExampleStrategy
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
3 spec/database_cleaner/generic/base_spec.rb
@@ -15,8 +15,7 @@ class ExampleStrategy
end
it_should_behave_like "a generic strategy"
-
- its(:db) { should == :default }
+ its(:db) { should == :unspecified }
end
end
end
View
15 spec/database_cleaner/mongo_mapper/base_spec.rb
@@ -13,21 +13,8 @@ class ExampleStrategy
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
11 spec/database_cleaner/sequel/base_spec.rb
@@ -15,17 +15,6 @@ class ExampleStrategy
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
12 spec/database_cleaner/shared_strategy_spec.rb
@@ -2,6 +2,18 @@
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) }

0 comments on commit 77f9130

Please sign in to comment.