From 56ca7001d4f287f84aeee7d8098d27f3413873f1 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Sun, 16 Jan 2011 23:34:45 +0700 Subject: [PATCH] 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 --- README.textile | 15 +++++----- examples/features/support/env.rb | 4 +-- features/cleaning_default_strategy.feature | 19 ++++++++++++ lib/database_cleaner/base.rb | 10 +++++++ spec/database_cleaner/base_spec.rb | 34 +++++++++++++++++++++- 5 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 features/cleaning_default_strategy.feature diff --git a/README.textile b/README.textile index f730466..25c85b9 100644 --- a/README.textile +++ b/README.textile @@ -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 diff --git a/examples/features/support/env.rb b/examples/features/support/env.rb index e092c74..cd44839 100644 --- a/examples/features/support/env.rb +++ b/examples/features/support/env.rb @@ -54,9 +54,9 @@ if orm && strategy 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 diff --git a/features/cleaning_default_strategy.feature b/features/cleaning_default_strategy.feature new file mode 100644 index 0000000..7f96ec3 --- /dev/null +++ b/features/cleaning_default_strategy.feature @@ -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 + 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 | diff --git a/lib/database_cleaner/base.rb b/lib/database_cleaner/base.rb index dd6260c..41348d1 100644 --- a/lib/database_cleaner/base.rb +++ b/lib/database_cleaner/base.rb @@ -9,6 +9,7 @@ module DatabaseCleaner 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 @@ module DatabaseCleaner 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 diff --git a/spec/database_cleaner/base_spec.rb b/spec/database_cleaner/base_spec.rb index ade60f6..3906723 100644 --- a/spec/database_cleaner/base_spec.rb +++ b/spec/database_cleaner/base_spec.rb @@ -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