Permalink
Browse files

Add support for default strategy for each ORM

DatabaseCleaner will select the best default strategy based on the ORM you're using without having you to select it by yourself.

* ActiveRecord, DataMapper => :transaction
* MongoMapper, Mongoid, CouchPotato => :truncation
  • Loading branch information...
1 parent 9bd76e7 commit 56ca7001d4f287f84aeee7d8098d27f3413873f1 @sikachu sikachu committed Jan 16, 2011
View
@@ -10,19 +10,20 @@ ActiveRecord, DataMapper, MongoMapper, Mongoid, and CouchPotato are supported.
Here is an overview of the strategies supported for each library:
|_. ORM |_. Truncation |_. Transaction |_. Deletion |
-| ActiveRecord | Yes | Yes | Yes |
-| DataMapper | Yes | Yes | No |
-| CouchPotato | Yes | No | No |
-| MongoMapper | Yes | No | No |
-| Mongoid | Yes | No | No |
+| ActiveRecord | Yes | **Yes** | Yes |
+| DataMapper | Yes | **Yes** | No |
+| CouchPotato | **Yes** | No | No |
+| MongoMapper | **Yes** | No | No |
+| Mongoid | **Yes** | No | No |
+
+(Default strategy for each library is denoted in bold)
The ActiveRecord @:deletion@ strategy is only useful for when the @:truncation@ strategy causes
locks (as reported by some Oracle DB users). The @:truncation@ strategy is the preferred option
since it is much faster.
Database Cleaner also includes a @null@ strategy (that does no cleaning at all) which can be used
-with any ORM library. _This is the default strategy and will be used unless you
-specify one._ You can also explicitly use it by setting your strategy to @nil@.
+with any ORM library. You can also explicitly use it by setting your strategy to @nil@.
h2. How to use
@@ -54,9 +54,9 @@
DatabaseCleaner[ orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym ].strategy = strategy.to_sym
DatabaseCleaner[ another_orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym ].strategy = strategy.to_sym
else
- DatabaseCleaner.strategy = strategy.to_sym
+ DatabaseCleaner.strategy = strategy.to_sym unless strategy == "default"
end
else
- raise "Run 'ORM=ActiveRecord|DataMapper|MongoMapper|CouchPotato [ANOTHER_ORM=...] [MULTIPLE_DBS=true] STRATEGY=transaction|truncation cucumber examples/features'"
+ raise "Run 'ORM=ActiveRecord|DataMapper|MongoMapper|CouchPotato [ANOTHER_ORM=...] [MULTIPLE_DBS=true] STRATEGY=transaction|truncation|default cucumber examples/features'"
end
@@ -0,0 +1,19 @@
+Feature: database cleaning
+ In order to ease example and feature writing
+ As a developer
+ I want to have my database in a clean state with default strategy
+
+ Scenario Outline: ruby app
+ Given I am using <ORM>
+ And the default cleaning strategy
+
+ When I run my scenarios that rely on a clean database
+ Then I should see all green
+
+ Examples:
+ | ORM |
+ | ActiveRecord |
+ | DataMapper |
+ | MongoMapper |
+ | Mongoid |
+ | CouchPotato |
@@ -9,6 +9,7 @@ def initialize(desired_orm = nil,opts = {})
self.orm = desired_orm
end
self.db = opts[:connection] if opts.has_key? :connection
+ set_default_orm_strategy
end
def db=(desired_db)
@@ -122,5 +123,14 @@ def autodetect
end
end
end
+
+ def set_default_orm_strategy
+ case orm
+ when :active_record, :data_mapper
+ self.strategy = :transaction
+ when :mongo_mapper, :mongoid, :couch_potato
+ self.strategy = :truncation
+ end
+ end
end
end
@@ -1,6 +1,9 @@
require File.dirname(__FILE__) + '/../spec_helper'
require 'database_cleaner/active_record/transaction'
require 'database_cleaner/data_mapper/transaction'
+require 'database_cleaner/mongo_mapper/truncation'
+require 'database_cleaner/mongoid/truncation'
+require 'database_cleaner/couch_potato/truncation'
module DatabaseCleaner
describe Base do
@@ -310,7 +313,9 @@ module DatabaseCleaner
end
describe "strategy" do
- it "returns a null strategy when strategy no set" do
+ subject { ::DatabaseCleaner::Base.new :a_orm }
+
+ it "returns a null strategy when strategy no set and undetectable" do
subject.instance_values["@strategy"] = nil
subject.strategy.should == DatabaseCleaner::NullStrategy
end
@@ -442,5 +447,32 @@ module DatabaseCleaner
end
+ describe 'set_default_orm_strategy' do
+ it 'sets strategy to :transaction for ActiveRecord' do
+ cleaner = DatabaseCleaner::Base.new(:active_record)
+ cleaner.strategy.should be_instance_of DatabaseCleaner::ActiveRecord::Transaction
+ end
+
+ it 'sets strategy to :transaction for DataMapper' do
+ cleaner = DatabaseCleaner::Base.new(:data_mapper)
+ cleaner.strategy.should be_instance_of DatabaseCleaner::DataMapper::Transaction
+ end
+
+ it 'sets strategy to :truncation for MongoMapper' do
+ cleaner = DatabaseCleaner::Base.new(:mongo_mapper)
+ cleaner.strategy.should be_instance_of DatabaseCleaner::MongoMapper::Truncation
+ end
+
+ it 'sets strategy to :truncation for Mongoid' do
+ cleaner = DatabaseCleaner::Base.new(:mongoid)
+ cleaner.strategy.should be_instance_of DatabaseCleaner::Mongoid::Truncation
+ end
+
+ it 'sets strategy to :truncation for CouchPotato' do
+ cleaner = DatabaseCleaner::Base.new(:couch_potato)
+ cleaner.strategy.should be_instance_of DatabaseCleaner::CouchPotato::Truncation
+ end
+ end
+
end
end

0 comments on commit 56ca700

Please sign in to comment.