Skip to content

Commit

Permalink
WIP- a better approach to #72 making the it clear that the DB was uns…
Browse files Browse the repository at this point in the history
…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
bmabey committed Nov 3, 2011
1 parent f8df3db commit 77f9130
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 98 deletions.
6 changes: 3 additions & 3 deletions lib/database_cleaner/base.rb
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/database_cleaner/configuration.rb
Expand Up @@ -4,6 +4,7 @@ module DatabaseCleaner

class NoORMDetected < StandardError; end
class UnknownStrategySpecified < ArgumentError; end
class UnspecifiedDatabase < ArgumentError; end

class << self
def [](orm,opts = {})
Expand Down
10 changes: 1 addition & 9 deletions lib/database_cleaner/data_mapper/base.rb
Expand Up @@ -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
15 changes: 12 additions & 3 deletions 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
Expand Down
9 changes: 1 addition & 8 deletions lib/database_cleaner/mongo_mapper/base.rb
Expand Up @@ -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
9 changes: 1 addition & 8 deletions lib/database_cleaner/mongoid/base.rb
Expand Up @@ -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
8 changes: 5 additions & 3 deletions lib/database_cleaner/sequel/base.rb
Expand Up @@ -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
Expand Down
12 changes: 2 additions & 10 deletions spec/database_cleaner/active_record/base_spec.rb
Expand Up @@ -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:
Expand Down
25 changes: 9 additions & 16 deletions spec/database_cleaner/base_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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')

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 1 addition & 11 deletions spec/database_cleaner/data_mapper/base_spec.rb
Expand Up @@ -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
3 changes: 1 addition & 2 deletions spec/database_cleaner/generic/base_spec.rb
Expand Up @@ -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
15 changes: 1 addition & 14 deletions spec/database_cleaner/mongo_mapper/base_spec.rb
Expand Up @@ -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
11 changes: 0 additions & 11 deletions spec/database_cleaner/sequel/base_spec.rb
Expand Up @@ -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
12 changes: 12 additions & 0 deletions spec/database_cleaner/shared_strategy_spec.rb
Expand Up @@ -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) }
Expand Down

0 comments on commit 77f9130

Please sign in to comment.