mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
removes whitespace
This commit is contained in:
parent
316453e8b5
commit
bf55f75431
45 changed files with 301 additions and 301 deletions
|
@ -1,10 +1,10 @@
|
|||
Feature: example
|
||||
In order to test DataBase Cleaner
|
||||
Here are some scenarios that rely on the DB being clean!
|
||||
|
||||
|
||||
Background:
|
||||
Given I have setup database cleaner to clean multiple databases using datamapper
|
||||
|
||||
|
||||
Scenario: dirty the db
|
||||
When I create a widget in one db using datamapper
|
||||
And I create a widget in another db using datamapper
|
||||
|
@ -15,9 +15,9 @@ Feature: example
|
|||
When I create a widget in one db using datamapper
|
||||
Then I should see 1 widget in one db using datamapper
|
||||
And I should see 0 widget in another db using datamapper
|
||||
|
||||
|
||||
Scenario: assume a clean db
|
||||
When I create a widget in another db using datamapper
|
||||
Then I should see 0 widget in one db using datamapper
|
||||
And I should see 1 widget in another db using datamapper
|
||||
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
Feature: example
|
||||
In order to test DataBase Cleaner
|
||||
Here are some scenarios that rely on the DB being clean!
|
||||
|
||||
|
||||
# Background:
|
||||
# Given I have setup DatabaseCleaner to clean multiple databases
|
||||
#
|
||||
# Given I have setup DatabaseCleaner to clean multiple databases
|
||||
#
|
||||
Scenario: dirty the db
|
||||
When I create a widget in one db
|
||||
And I create a widget in another db
|
||||
|
@ -12,12 +12,12 @@ Feature: example
|
|||
And I should see 1 widget in another db
|
||||
|
||||
Scenario: assume a clean db
|
||||
When I create a widget in one db
|
||||
When I create a widget in one db
|
||||
Then I should see 1 widget in one db
|
||||
And I should see 0 widget in another db
|
||||
|
||||
|
||||
Scenario: assume a clean db
|
||||
When I create a widget in another db
|
||||
When I create a widget in another db
|
||||
Then I should see 0 widget in one db
|
||||
And I should see 1 widget in another db
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ Feature: example
|
|||
|
||||
# Background:
|
||||
# Given I have setup DatabaseCleaner to clean multiple orms
|
||||
|
||||
|
||||
Scenario: dirty the db
|
||||
When I create a widget in one orm
|
||||
And I create a widget in another orm
|
||||
|
@ -15,7 +15,7 @@ Feature: example
|
|||
When I create a widget in one orm
|
||||
Then I should see 1 widget in one orm
|
||||
And I should see 0 widget in another orm
|
||||
|
||||
|
||||
Scenario: assume a clean db
|
||||
When I create a widget in another orm
|
||||
Then I should see 0 widget in one orm
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
Given /^I have setup database cleaner to clean multiple databases using activerecord$/ do
|
||||
#DatabaseCleaner
|
||||
# require "#{File.dirname(__FILE__)}/../../../lib/datamapper_models"
|
||||
#
|
||||
#
|
||||
# DatabaseCleaner[:datamapper, {:connection => :one} ].strategy = :truncation
|
||||
# DatabaseCleaner[:datamapper, {:connection => :two} ].strategy = :truncation
|
||||
end
|
||||
|
@ -28,4 +28,4 @@ end
|
|||
|
||||
Then /^I should see ([\d]+) widget in another db using activerecord$/ do |widget_count|
|
||||
ActiveRecordWidgetUsingDatabaseTwo.count.should == widget_count.to_i
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
Given /^I have setup database cleaner to clean multiple databases using couchpotato$/ do
|
||||
#DatabaseCleaner
|
||||
# require "#{File.dirname(__FILE__)}/../../../lib/couchpotato_models"
|
||||
#
|
||||
#
|
||||
# DatabaseCleaner[:couchpotato, {:connection => :one} ].strategy = :truncation
|
||||
# DatabaseCleaner[:couchpotato, {:connection => :two} ].strategy = :truncation
|
||||
end
|
||||
|
@ -28,4 +28,4 @@ end
|
|||
|
||||
Then /^I should see ([\d]+) widget in another db using couchpotato$/ do |widget_count|
|
||||
CouchPotatoWidgetUsingDatabaseTwo.count.should == widget_count.to_i
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
Given /^I have setup database cleaner to clean multiple databases using datamapper$/ do
|
||||
#DatabaseCleaner
|
||||
# require "#{File.dirname(__FILE__)}/../../../lib/datamapper_models"
|
||||
#
|
||||
#
|
||||
# DatabaseCleaner[:datamapper, {:connection => :one} ].strategy = :truncation
|
||||
# DatabaseCleaner[:datamapper, {:connection => :two} ].strategy = :truncation
|
||||
end
|
||||
|
@ -11,7 +11,7 @@ When /^I create a widget using datamapper$/ do
|
|||
end
|
||||
|
||||
Then /^I should see ([\d]+) widget using datamapper$/ do |widget_count|
|
||||
DataMapperWidget.count.should == widget_count.to_i
|
||||
DataMapperWidget.count.should == widget_count.to_i
|
||||
end
|
||||
|
||||
When /^I create a widget in one db using datamapper$/ do
|
||||
|
@ -34,4 +34,4 @@ end
|
|||
|
||||
Then /^I should see ([\d]+) widget in another db using datamapper$/ do |widget_count|
|
||||
DataMapperWidgetUsingDatabaseTwo.count.should == widget_count.to_i
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
Given /^I have setup database cleaner to clean multiple databases using mongomapper$/ do
|
||||
#DatabaseCleaner
|
||||
# require "#{File.dirname(__FILE__)}/../../../lib/datamapper_models"
|
||||
#
|
||||
#
|
||||
# DatabaseCleaner[:datamapper, {:connection => :one} ].strategy = :truncation
|
||||
# DatabaseCleaner[:datamapper, {:connection => :two} ].strategy = :truncation
|
||||
end
|
||||
|
@ -28,4 +28,4 @@ end
|
|||
|
||||
Then /^I should see ([\d]+) widget in another db using mongomapper$/ do |widget_count|
|
||||
MongoMapperWidgetUsingDatabaseTwo.count.should == widget_count.to_i
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,7 +3,7 @@ When /^I create a widget$/ do
|
|||
end
|
||||
|
||||
Then /^I should see 1 widget$/ do
|
||||
Then "I should see 1 widget using #{ENV['ORM'].downcase}"
|
||||
Then "I should see 1 widget using #{ENV['ORM'].downcase}"
|
||||
end
|
||||
|
||||
When /^I create a widget in one orm$/ do
|
||||
|
|
|
@ -16,31 +16,31 @@ if orm && strategy
|
|||
$:.unshift(File.dirname(__FILE__) + '/../../../lib')
|
||||
require 'database_cleaner'
|
||||
require 'database_cleaner/cucumber'
|
||||
|
||||
begin
|
||||
require "#{File.dirname(__FILE__)}/../../lib/#{orm.downcase}_models"
|
||||
rescue LoadError => e
|
||||
|
||||
begin
|
||||
require "#{File.dirname(__FILE__)}/../../lib/#{orm.downcase}_models"
|
||||
rescue LoadError => e
|
||||
raise "You don't have the #{orm} ORM installed"
|
||||
end
|
||||
|
||||
|
||||
if another_orm
|
||||
begin
|
||||
require "#{File.dirname(__FILE__)}/../../lib/#{another_orm.downcase}_models"
|
||||
rescue LoadError => e
|
||||
begin
|
||||
require "#{File.dirname(__FILE__)}/../../lib/#{another_orm.downcase}_models"
|
||||
rescue LoadError => e
|
||||
raise "You don't have the #{another_orm} ORM installed"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if multiple_db && another_orm
|
||||
DatabaseCleaner[ orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym, {:connection => :one} ].strategy = strategy.to_sym
|
||||
DatabaseCleaner[ another_orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym, {:connection => :two} ].strategy = strategy.to_sym
|
||||
DatabaseCleaner[ another_orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym, {:connection => :two} ].strategy = strategy.to_sym
|
||||
elsif multiple_db
|
||||
DatabaseCleaner.app_root = "#{File.dirname(__FILE__)}/../.."
|
||||
orm_sym = orm.gsub(/(.)([A-Z]+)/,'\1_\2').downcase.to_sym
|
||||
|
||||
|
||||
if orm_sym == :mongo_mapper
|
||||
DatabaseCleaner[ orm_sym, {:connection => 'database_cleaner_test_one'} ].strategy = strategy.to_sym
|
||||
DatabaseCleaner[ orm_sym, {:connection => 'database_cleaner_test_two'} ].strategy = strategy.to_sym
|
||||
|
@ -48,14 +48,14 @@ if orm && strategy
|
|||
DatabaseCleaner[ orm_sym, {:connection => :one} ].strategy = strategy.to_sym
|
||||
DatabaseCleaner[ orm_sym, {:connection => :two} ].strategy = strategy.to_sym
|
||||
end
|
||||
|
||||
|
||||
elsif another_orm
|
||||
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
|
||||
end
|
||||
|
||||
|
||||
else
|
||||
raise "Run 'ORM=activerecord|datamapper|mongomapper|couchpotato [ANOTHER_ORM=activerecord|datamapper|mongomapper|couchpotato] [MULTIPLE_DBS=true] STRATEGY=transaction|truncation cucumber examples/features'"
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
require 'active_record'
|
||||
databases_config = {
|
||||
databases_config = {
|
||||
"one" => {"adapter" => "#{"jdbc" if defined?(JRUBY_VERSION)}sqlite3", "database" => "#{DB_DIR}/activerecord_one.db"},
|
||||
"two" => {"adapter" => "#{"jdbc" if defined?(JRUBY_VERSION)}sqlite3", "database" => "#{DB_DIR}/activerecord_two.db"}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ end
|
|||
ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS "active_record_widgets"')
|
||||
ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS "active_record_widget_using_database_ones"')
|
||||
ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS "active_record_widget_using_database_twos"')
|
||||
|
||||
|
||||
ActiveRecord::Schema.define(:version => 1) do
|
||||
create_table :active_record_widgets do |t|
|
||||
t.string :name
|
||||
|
@ -22,7 +22,7 @@ end
|
|||
create_table :active_record_widget_using_database_ones do |t|
|
||||
t.string :name
|
||||
end
|
||||
|
||||
|
||||
create_table :active_record_widget_using_database_twos do |t|
|
||||
t.string :name
|
||||
end
|
||||
|
|
|
@ -4,17 +4,17 @@ require 'json/pure'
|
|||
|
||||
class CouchPotatoWidget
|
||||
include CouchPotato::Persistence
|
||||
|
||||
|
||||
property :name
|
||||
view :by_name, :key => :name
|
||||
|
||||
|
||||
|
||||
# mimic the AR interface used in example_steps
|
||||
|
||||
def self.create!(attrs = {})
|
||||
CouchPotato.database.save(self.new)
|
||||
end
|
||||
|
||||
|
||||
def self.count
|
||||
CouchPotato.database.view(self.by_name).size
|
||||
end
|
||||
|
@ -22,19 +22,19 @@ end
|
|||
|
||||
class CouchPotatoWidgetUsingDatabaseOne
|
||||
include CouchPotato::Persistence
|
||||
|
||||
|
||||
database_name = 'couch_potato_test_one'
|
||||
|
||||
|
||||
property :name
|
||||
view :by_name, :key => :name
|
||||
|
||||
|
||||
|
||||
# mimic the AR interface used in example_steps
|
||||
|
||||
def self.create!(attrs = {})
|
||||
CouchPotato.database.save(self.new)
|
||||
end
|
||||
|
||||
|
||||
def self.count
|
||||
CouchPotato.database.view(self.by_name).size
|
||||
end
|
||||
|
@ -44,17 +44,17 @@ class CouchPotatoWidgetUsingDatabaseTwo
|
|||
include CouchPotato::Persistence
|
||||
|
||||
database_name = 'couch_potato_test_two'
|
||||
|
||||
|
||||
property :name
|
||||
view :by_name, :key => :name
|
||||
|
||||
|
||||
|
||||
# mimic the AR interface used in example_steps
|
||||
|
||||
def self.create!(attrs = {})
|
||||
CouchPotato.database.save(self.new)
|
||||
end
|
||||
|
||||
|
||||
def self.count
|
||||
CouchPotato.database.view(self.by_name).size
|
||||
end
|
||||
|
|
|
@ -11,7 +11,7 @@ DataMapper.setup(:two, "sqlite3:#{DB_DIR}/datamapper_two.db")
|
|||
|
||||
class DataMapperWidget
|
||||
include DataMapper::Resource
|
||||
|
||||
|
||||
property :id, Serial
|
||||
property :name, String
|
||||
end
|
||||
|
@ -25,9 +25,9 @@ class DataMapperWidgetUsingDatabaseOne
|
|||
|
||||
property :id, Serial
|
||||
property :name, String
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
class DataMapperWidgetUsingDatabaseTwo
|
||||
include DataMapper::Resource
|
||||
|
||||
|
@ -37,7 +37,7 @@ class DataMapperWidgetUsingDatabaseTwo
|
|||
|
||||
property :id, Serial
|
||||
property :name, String
|
||||
|
||||
|
||||
end
|
||||
|
||||
DataMapperWidget.auto_migrate!
|
||||
|
|
|
@ -19,9 +19,9 @@ end
|
|||
class MongoMapperWidgetUsingDatabaseOne
|
||||
include MongoMapper::Document
|
||||
|
||||
connection = Mongo::Connection.new('127.0.0.1')
|
||||
connection = Mongo::Connection.new('127.0.0.1')
|
||||
set_database_name = 'database_cleaner_test_one'
|
||||
|
||||
|
||||
key :id, Integer
|
||||
key :name, String
|
||||
|
||||
|
@ -35,10 +35,10 @@ end
|
|||
|
||||
class MongoMapperWidgetUsingDatabaseTwo
|
||||
include MongoMapper::Document
|
||||
|
||||
|
||||
connection = Mongo::Connection.new('127.0.0.1')
|
||||
set_database_name = 'database_cleaner_test_two'
|
||||
|
||||
|
||||
key :id, Integer
|
||||
key :name, String
|
||||
|
||||
|
@ -48,4 +48,4 @@ class MongoMapperWidgetUsingDatabaseTwo
|
|||
new(*args).save!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,5 +15,5 @@ Feature: multiple database cleaning
|
|||
| ActiveRecord | truncation |
|
||||
| DataMapper | truncation |
|
||||
# | ActiveRecord | transaction |
|
||||
# | MongoMapper | truncation |
|
||||
# | DataMapper | transaction | # Tickers are ugly...and betty don't like ugly... otherwise known as this is fubar
|
||||
# | MongoMapper | truncation |
|
||||
# | DataMapper | transaction | # Tickers are ugly...and betty don't like ugly... otherwise known as this is fubar
|
||||
|
|
|
@ -29,4 +29,4 @@ end
|
|||
|
||||
Then "I should see all green" do
|
||||
fail "Feature failed with :#{@feature_runner.output}" unless @feature_runner.exit_status == 0
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,11 +5,11 @@ class FeatureRunner
|
|||
attr_accessor :strategy
|
||||
attr_accessor :exit_status
|
||||
attr_accessor :output
|
||||
|
||||
|
||||
def strategy
|
||||
@strategy || 'truncation'
|
||||
end
|
||||
|
||||
|
||||
def go feature
|
||||
full_dir ||= File.expand_path(File.dirname(__FILE__) + "/../../examples/")
|
||||
Dir.chdir(full_dir) do
|
||||
|
@ -21,8 +21,8 @@ class FeatureRunner
|
|||
|
||||
self.output = `#{"jruby -S " if defined?(JRUBY_VERSION)}cucumber features/#{feature}.feature`
|
||||
|
||||
self.exit_status = $?.exitstatus
|
||||
end
|
||||
self.exit_status = $?.exitstatus
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -3,38 +3,38 @@ require 'active_record'
|
|||
|
||||
module DatabaseCleaner
|
||||
module ActiveRecord
|
||||
|
||||
|
||||
def self.available_strategies
|
||||
%w[truncation transaction]
|
||||
end
|
||||
|
||||
|
||||
def self.config_file_location
|
||||
"#{DatabaseCleaner.app_root}/config/database.yml"
|
||||
end
|
||||
|
||||
|
||||
module Base
|
||||
include ::DatabaseCleaner::Generic::Base
|
||||
|
||||
|
||||
attr_accessor :connection_hash
|
||||
|
||||
|
||||
def db=(desired_db)
|
||||
@db = desired_db
|
||||
load_config
|
||||
end
|
||||
|
||||
|
||||
def db
|
||||
@db || super
|
||||
end
|
||||
|
||||
|
||||
def load_config
|
||||
connection_details = YAML.load_file(ActiveRecord.config_file_location)
|
||||
self.connection_hash = connection_details[self.db.to_s]
|
||||
end
|
||||
|
||||
|
||||
def create_connection_klass
|
||||
Class.new(::ActiveRecord::Base)
|
||||
end
|
||||
|
||||
|
||||
def connection_klass
|
||||
return ::ActiveRecord::Base if connection_hash.nil?
|
||||
klass = create_connection_klass
|
||||
|
@ -43,4 +43,4 @@ module DatabaseCleaner
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@ require 'database_cleaner/active_record/base'
|
|||
module DatabaseCleaner::ActiveRecord
|
||||
class Transaction
|
||||
include ::DatabaseCleaner::ActiveRecord::Base
|
||||
|
||||
|
||||
def start
|
||||
if connection_klass.connection.respond_to?(:increment_open_transactions)
|
||||
connection_klass.connection.increment_open_transactions
|
||||
|
@ -23,4 +23,4 @@ module DatabaseCleaner::ActiveRecord
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -38,8 +38,8 @@ module ActiveRecord
|
|||
|
||||
def self.db_version
|
||||
@db_version ||= connection.select_values(
|
||||
"SELECT CHARACTER_VALUE
|
||||
FROM INFORMATION_SCHEMA.SQL_IMPLEMENTATION_INFO
|
||||
"SELECT CHARACTER_VALUE
|
||||
FROM INFORMATION_SCHEMA.SQL_IMPLEMENTATION_INFO
|
||||
WHERE IMPLEMENTATION_INFO_NAME = 'DBMS VERSION' ").to_s
|
||||
end
|
||||
|
||||
|
@ -83,7 +83,7 @@ module DatabaseCleaner::ActiveRecord
|
|||
end
|
||||
|
||||
private
|
||||
|
||||
|
||||
def tables_to_truncate
|
||||
(@only || connection.tables) - @tables_to_exclude
|
||||
end
|
||||
|
@ -92,7 +92,7 @@ module DatabaseCleaner::ActiveRecord
|
|||
#::ActiveRecord::Base.connection
|
||||
connection_klass.connection
|
||||
end
|
||||
|
||||
|
||||
# overwritten
|
||||
def migration_storage_name
|
||||
'schema_migrations'
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
module DatabaseCleaner
|
||||
class Base
|
||||
|
||||
|
||||
def initialize(desired_orm = nil,opts = {})
|
||||
if desired_orm == :autodetect || desired_orm.nil?
|
||||
autodetect
|
||||
autodetect
|
||||
else
|
||||
self.orm = desired_orm
|
||||
end
|
||||
@db = opts[:connection] if opts.has_key? :connection
|
||||
end
|
||||
|
||||
|
||||
def db
|
||||
@db || :default
|
||||
end
|
||||
|
||||
|
||||
def create_strategy(*args)
|
||||
strategy, *strategy_args = args
|
||||
orm_strategy(strategy).new(*strategy_args)
|
||||
|
@ -36,14 +36,14 @@ module DatabaseCleaner
|
|||
else
|
||||
raise ArgumentError, "You must provide a strategy object, or a symbol for a known strategy along with initialization params."
|
||||
end
|
||||
|
||||
|
||||
if @strategy.respond_to? :db=
|
||||
@strategy.db = self.db
|
||||
elsif self.db != :default
|
||||
raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def strategy
|
||||
return @strategy if @strategy
|
||||
raise NoStrategySetError, "Please set a strategy with DatabaseCleaner.strategy=."
|
||||
|
@ -52,11 +52,11 @@ module DatabaseCleaner
|
|||
def orm=(desired_orm)
|
||||
@orm = desired_orm
|
||||
end
|
||||
|
||||
|
||||
def orm
|
||||
@orm || autodetect
|
||||
end
|
||||
|
||||
|
||||
def start
|
||||
strategy.start
|
||||
end
|
||||
|
@ -66,23 +66,23 @@ module DatabaseCleaner
|
|||
end
|
||||
|
||||
alias clean! clean
|
||||
|
||||
|
||||
def auto_detected
|
||||
return true unless @autodetected.nil?
|
||||
end
|
||||
|
||||
|
||||
#TODO make strategies directly comparable
|
||||
def ==(other)
|
||||
self.orm == other.orm && self.db == other.db && self.strategy.class == other.strategy.class
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def orm_module
|
||||
::DatabaseCleaner.orm_module(orm)
|
||||
end
|
||||
|
||||
def orm_strategy(strategy)
|
||||
|
||||
def orm_strategy(strategy)
|
||||
require "database_cleaner/#{orm.to_s}/#{strategy.to_s}"
|
||||
orm_module.const_get(strategy.to_s.capitalize)
|
||||
rescue LoadError => e
|
||||
|
@ -92,7 +92,7 @@ module DatabaseCleaner
|
|||
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM!"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def autodetect
|
||||
@orm ||= begin
|
||||
@autodetected = true
|
||||
|
@ -109,7 +109,7 @@ module DatabaseCleaner
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def orm_module
|
||||
case orm
|
||||
when :active_record
|
||||
|
@ -121,6 +121,6 @@ module DatabaseCleaner
|
|||
when :couch_potato
|
||||
DatabaseCleaner::CouchPotato
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,8 +5,8 @@ module DatabaseCleaner
|
|||
class NoStrategySetError < StandardError; end
|
||||
class NoORMDetected < StandardError; end
|
||||
class UnknownStrategySpecified < ArgumentError; end
|
||||
|
||||
class << self
|
||||
|
||||
class << self
|
||||
def [](orm,opts = {})
|
||||
raise NoORMDetected if orm.nil?
|
||||
@connections ||= []
|
||||
|
@ -14,41 +14,41 @@ module DatabaseCleaner
|
|||
connections.push cleaner
|
||||
cleaner
|
||||
end
|
||||
|
||||
|
||||
def app_root=(desired_root)
|
||||
@app_root = desired_root
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def app_root
|
||||
@app_root || Dir.pwd
|
||||
end
|
||||
|
||||
|
||||
def connections
|
||||
@connections ||= [::DatabaseCleaner::Base.new]
|
||||
end
|
||||
|
||||
|
||||
def strategy=(stratagem)
|
||||
self.connections.each { |connect| connect.strategy = stratagem }
|
||||
remove_duplicates
|
||||
end
|
||||
|
||||
|
||||
def orm=(orm)
|
||||
self.connections.each { |connect| connect.orm = orm }
|
||||
remove_duplicates
|
||||
end
|
||||
|
||||
|
||||
def start
|
||||
self.connections.each { |connection| connection.start }
|
||||
end
|
||||
|
||||
|
||||
def clean
|
||||
self.connections.each { |connection| connection.clean }
|
||||
end
|
||||
|
||||
|
||||
def clean_with(stratagem)
|
||||
self.connections.each { |connection| connection.clean_with stratagem }
|
||||
end
|
||||
|
||||
|
||||
def remove_duplicates
|
||||
temp = []
|
||||
self.connections.each do |connect|
|
||||
|
@ -56,5 +56,5 @@ module DatabaseCleaner
|
|||
end
|
||||
@connections = temp
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,4 +4,4 @@ module DatabaseCleaner
|
|||
%w[truncation]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ module DatabaseCleaner
|
|||
module CouchPotato
|
||||
class Truncation
|
||||
include ::DatabaseCleaner::Generic::Truncation
|
||||
|
||||
|
||||
def initialize(options = {})
|
||||
if options.has_key?(:only) || options.has_key?(:except)
|
||||
raise ArgumentError, "The :only and :except options are not available for use with CouchPotato/CouchDB."
|
||||
|
|
|
@ -119,18 +119,18 @@ module DatabaseCleaner
|
|||
def self.available_strategies
|
||||
%w[truncation transaction]
|
||||
end
|
||||
|
||||
|
||||
module Base
|
||||
include ::DatabaseCleaner::Generic::Base
|
||||
|
||||
|
||||
def db=(desired_db)
|
||||
@db = desired_db
|
||||
end
|
||||
|
||||
|
||||
def db
|
||||
@db || :default
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,7 +9,7 @@ module DatabaseCleaner
|
|||
class Truncation
|
||||
include ::DatabaseCleaner::DataMapper::Base
|
||||
include ::DatabaseCleaner::Generic::Truncation
|
||||
|
||||
|
||||
def clean(repository = nil)
|
||||
repository = self.db if repository.nil?
|
||||
adapter = ::DataMapper.repository(repository).adapter
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
module ::DatabaseCleaner
|
||||
module Generic
|
||||
module Base
|
||||
|
||||
|
||||
def self.included(base)
|
||||
base.extend(ClassMethods)
|
||||
base.send(:include, InstanceMethods)
|
||||
|
@ -20,4 +20,4 @@ module ::DatabaseCleaner
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -13,25 +13,25 @@ module DatabaseCleaner
|
|||
if opts.has_key?(:only) && opts.has_key?(:except)
|
||||
raise ArgumentError, "You may only specify either :only or :either. Doing both doesn't really make sense does it?"
|
||||
end
|
||||
|
||||
|
||||
@only = opts[:only]
|
||||
@tables_to_exclude = (opts[:except] || [])
|
||||
@tables_to_exclude << migration_storage_name unless migration_storage_name.nil?
|
||||
end
|
||||
|
||||
|
||||
def start
|
||||
#included for compatability reasons, do nothing if you don't need to
|
||||
end
|
||||
|
||||
def clean
|
||||
|
||||
def clean
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
def tables_to_truncate
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
|
||||
# overwrite in subclasses
|
||||
# default implementation given because migration storage need not be present
|
||||
def migration_storage_name
|
||||
|
@ -40,4 +40,4 @@ module DatabaseCleaner
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,17 +4,17 @@ module DatabaseCleaner
|
|||
def self.available_strategies
|
||||
%w[truncation]
|
||||
end
|
||||
|
||||
|
||||
module Base
|
||||
include ::DatabaseCleaner::Generic::Base
|
||||
|
||||
|
||||
def db=(desired_db)
|
||||
@db = desired_db
|
||||
end
|
||||
|
||||
|
||||
def db
|
||||
@db || :default
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,7 +6,7 @@ module DatabaseCleaner
|
|||
class Truncation
|
||||
include ::DatabaseCleaner::MongoMapper::Base
|
||||
include ::DatabaseCleaner::Generic::Truncation
|
||||
|
||||
|
||||
def clean
|
||||
if @only
|
||||
collections.each { |c| c.remove if @only.include?(c.name) }
|
||||
|
|
|
@ -6,39 +6,39 @@ require 'database_cleaner/shared_strategy_spec'
|
|||
module DatabaseCleaner
|
||||
describe ActiveRecord do
|
||||
it { should respond_to :available_strategies }
|
||||
|
||||
|
||||
describe "config_file_location" do
|
||||
subject { ActiveRecord.config_file_location }
|
||||
|
||||
|
||||
it "should default to DatabaseCleaner.root / config / database.yml" do
|
||||
DatabaseCleaner.should_receive(:app_root).and_return("/path/to")
|
||||
subject.should == '/path/to/config/database.yml'
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
module ActiveRecord
|
||||
|
||||
end
|
||||
|
||||
module ActiveRecord
|
||||
class ExampleStrategy
|
||||
include ::DatabaseCleaner::ActiveRecord::Base
|
||||
end
|
||||
|
||||
|
||||
describe ExampleStrategy do
|
||||
|
||||
before { ::DatabaseCleaner::ActiveRecord.stub(:config_file_location).and_return('/path/to/config/database.yml') }
|
||||
|
||||
it_should_behave_like "a generic strategy"
|
||||
|
||||
before { ::DatabaseCleaner::ActiveRecord.stub(:config_file_location).and_return('/path/to/config/database.yml') }
|
||||
|
||||
it_should_behave_like "a generic strategy"
|
||||
|
||||
describe "db" do
|
||||
it { should respond_to :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
|
||||
|
@ -48,25 +48,25 @@ module DatabaseCleaner
|
|||
subject.db = :my_db
|
||||
end
|
||||
end
|
||||
|
||||
describe "load_config" do
|
||||
|
||||
describe "load_config" do
|
||||
|
||||
it { should respond_to :load_config }
|
||||
|
||||
|
||||
before { YAML.stub(:load_file).with('/path/to/config/database.yml').and_return({ "my_db" => {"database" => "one"} }) }
|
||||
|
||||
|
||||
it "should parse the config" do
|
||||
YAML.should_receive(:load_file).with('/path/to/config/database.yml').and_return( {:nil => nil} )
|
||||
subject.load_config
|
||||
end
|
||||
|
||||
|
||||
it "should store the relevant config in connection_hash" do
|
||||
subject.should_receive(:db).and_return(:my_db)
|
||||
subject.load_config
|
||||
subject.connection_hash.should == {"database" => "one"}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe "connection_hash" do
|
||||
it { should respond_to :connection_hash }
|
||||
it { should respond_to :connection_hash= }
|
||||
|
@ -75,41 +75,41 @@ module DatabaseCleaner
|
|||
subject.connection_hash.should == { :key => "value" }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe "create_connection_klass" do
|
||||
it "should return a class" do
|
||||
subject.create_connection_klass.should be_a Class
|
||||
end
|
||||
|
||||
|
||||
it "should return a class extending ::ActiveRecord::Base" do
|
||||
subject.create_connection_klass.ancestors.should include ::ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe "connection_klass" do
|
||||
it { expect{ subject.connection_klass }.to_not raise_error }
|
||||
it "should default to ActiveRecord::Base" do
|
||||
subject.connection_klass.should == ::ActiveRecord::Base
|
||||
end
|
||||
|
||||
|
||||
context "when connection_hash is set" do
|
||||
let(:hash) { mock("hash") }
|
||||
before { subject.stub(:connection_hash).and_return(hash) }
|
||||
|
||||
|
||||
it "should create connection_klass if it doesnt exist if connection_hash is set" do
|
||||
subject.should_receive(:create_connection_klass).and_return(mock('class').as_null_object)
|
||||
subject.connection_klass
|
||||
end
|
||||
|
||||
|
||||
it "should configure the class from create_connection_klass if connection_hash is set" do
|
||||
klass = mock('klass')
|
||||
klass.should_receive(:establish_connection).with(hash)
|
||||
|
||||
|
||||
subject.should_receive(:create_connection_klass).and_return(klass)
|
||||
subject.connection_klass
|
||||
subject.connection_klass
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,7 +8,7 @@ module DatabaseCleaner
|
|||
describe Transaction do
|
||||
let (:connection) { mock("connection") }
|
||||
let (:another_connection) { mock("a different connection") }
|
||||
|
||||
|
||||
let (:model_klass) do
|
||||
model_klass = mock("klass")
|
||||
model_klass.stub!(:connection).and_return(another_connection)
|
||||
|
@ -23,27 +23,27 @@ module DatabaseCleaner
|
|||
it "should increment open transactions if possible" do
|
||||
connection.stub!(:respond_to?).with(:increment_open_transactions).and_return(true)
|
||||
connection.stub!(:begin_db_transaction)
|
||||
|
||||
|
||||
connection.should_receive(:increment_open_transactions)
|
||||
Transaction.new.start
|
||||
end
|
||||
|
||||
|
||||
it "should tell ActiveRecord to increment connection if its not possible to increment current connection" do
|
||||
connection.stub!(:respond_to?).with(:increment_open_transactions).and_return(false)
|
||||
connection.stub!(:begin_db_transaction)
|
||||
|
||||
|
||||
::ActiveRecord::Base.should_receive(:increment_open_transactions)
|
||||
Transaction.new.start
|
||||
end
|
||||
|
||||
|
||||
it "should start a transaction" do
|
||||
connection.stub!(:increment_open_transactions)
|
||||
|
||||
|
||||
connection.should_receive(:begin_db_transaction)
|
||||
Transaction.new.start
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe "clean" do
|
||||
it "should finish a transaction" do
|
||||
connection.stub!(:decrement_open_transactions)
|
||||
|
@ -51,7 +51,7 @@ module DatabaseCleaner
|
|||
connection.should_receive(:rollback_db_transaction)
|
||||
Transaction.new.clean
|
||||
end
|
||||
|
||||
|
||||
it "should decrement open transactions if possible" do
|
||||
connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(true)
|
||||
connection.stub!(:rollback_db_transaction)
|
||||
|
|
|
@ -17,9 +17,9 @@ module DatabaseCleaner
|
|||
module ActiveRecord
|
||||
|
||||
describe Truncation do
|
||||
let (:connection) { mock('connection') }
|
||||
|
||||
|
||||
let (:connection) { mock('connection') }
|
||||
|
||||
|
||||
before(:each) do
|
||||
connection.stub!(:disable_referential_integrity).and_yield
|
||||
::ActiveRecord::Base.stub!(:connection).and_return(connection)
|
||||
|
|
|
@ -4,7 +4,7 @@ require 'database_cleaner/data_mapper/transaction'
|
|||
|
||||
module DatabaseCleaner
|
||||
describe Base do
|
||||
|
||||
|
||||
#this could do with a better assertion
|
||||
it "should default to autodetect upon initalisation" do
|
||||
cleaner = ::DatabaseCleaner::Base.new
|
||||
|
@ -12,14 +12,14 @@ module DatabaseCleaner
|
|||
end
|
||||
|
||||
describe "autodetect" do
|
||||
|
||||
|
||||
#Cache all ORMs, we'll need them later but not now.
|
||||
before(:all) do
|
||||
Temp_AR = ::ActiveRecord if defined?(::ActiveRecord) and not defined?(Temp_AR)
|
||||
Temp_DM = ::DataMapper if defined?(::DataMapper) and not defined?(Temp_DM)
|
||||
Temp_MM = ::MongoMapper if defined?(::MongoMapper) and not defined?(Temp_MM)
|
||||
Temp_CP = ::CouchPotato if defined?(::CouchPotato) and not defined?(Temp_CP)
|
||||
end
|
||||
end
|
||||
|
||||
#Remove all ORM mocks and restore from cache
|
||||
after(:all) do
|
||||
|
@ -34,7 +34,7 @@ module DatabaseCleaner
|
|||
::MongoMapper = Temp_MM if defined? Temp_MM
|
||||
::CouchPotato = Temp_CP if defined? Temp_CP
|
||||
end
|
||||
|
||||
|
||||
#reset the orm mocks
|
||||
before(:each) do
|
||||
Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord)
|
||||
|
@ -42,12 +42,12 @@ module DatabaseCleaner
|
|||
Object.send(:remove_const, 'MongoMapper') if defined?(::MongoMapper)
|
||||
Object.send(:remove_const, 'CouchPotato') if defined?(::CouchPotato)
|
||||
end
|
||||
|
||||
|
||||
let(:cleaner) { DatabaseCleaner::Base.new :autodetect }
|
||||
|
||||
it "should raise an error when no ORM is detected" do
|
||||
running { cleaner }.should raise_error(DatabaseCleaner::NoORMDetected)
|
||||
end
|
||||
end
|
||||
|
||||
it "should detect ActiveRecord first" do
|
||||
Object.const_set('ActiveRecord','Actively mocking records.')
|
||||
|
@ -57,7 +57,7 @@ module DatabaseCleaner
|
|||
|
||||
cleaner.orm.should == :active_record
|
||||
end
|
||||
|
||||
|
||||
it "should detect DataMapper second" do
|
||||
Object.const_set('DataMapper', 'Mapping data mocks')
|
||||
Object.const_set('MongoMapper', 'Mapping mock mongos')
|
||||
|
@ -65,7 +65,7 @@ module DatabaseCleaner
|
|||
|
||||
cleaner.orm.should == :data_mapper
|
||||
end
|
||||
|
||||
|
||||
it "should detect MongoMapper third" do
|
||||
Object.const_set('MongoMapper', 'Mapping mock mongos')
|
||||
Object.const_set('CouchPotato', 'Couching mock potatos')
|
||||
|
@ -79,17 +79,17 @@ module DatabaseCleaner
|
|||
cleaner.orm.should == :couch_potato
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe "comparison" do
|
||||
it "should be equal if orm, connection and strategy are the same" do
|
||||
strategy = mock("strategy")
|
||||
|
||||
|
||||
one = DatabaseCleaner::Base.new(:active_record,:connection => :default)
|
||||
one.strategy = strategy
|
||||
|
||||
|
||||
two = DatabaseCleaner::Base.new(:active_record,:connection => :default)
|
||||
two.strategy = strategy
|
||||
|
||||
|
||||
one.should == two
|
||||
two.should == one
|
||||
end
|
||||
|
@ -100,51 +100,51 @@ module DatabaseCleaner
|
|||
base = ::DatabaseCleaner::Base.new(:active_record)
|
||||
base.db.should == :default
|
||||
end
|
||||
|
||||
|
||||
it "should accept connection as part of a hash of options" do
|
||||
base = ::DatabaseCleaner::Base.new(:active_record,:connection => :my_db)
|
||||
base.db.should == :my_db
|
||||
end
|
||||
|
||||
|
||||
it "should check to see if strategy supports db specification" do
|
||||
strategy = mock("strategy",:db= => :my_db)
|
||||
strategy.stub(:respond_to?).with(anything)
|
||||
strategy.should_receive(:respond_to?).with(:db=).and_return(true)
|
||||
::DatabaseCleaner::Base.new(:active_record,:connection => :my_db).strategy = strategy
|
||||
end
|
||||
|
||||
|
||||
it "should pass db through to the strategy" do
|
||||
strategy = mock("strategy")
|
||||
|
||||
|
||||
strategy.stub(:respond_to?).with(anything)
|
||||
strategy.stub(:respond_to?).with(:db=).and_return(true)
|
||||
|
||||
|
||||
strategy.should_receive(:db=).with(:my_db)
|
||||
::DatabaseCleaner::Base.new(:active_record,:connection => :my_db).strategy = strategy
|
||||
end
|
||||
|
||||
|
||||
it "should raise ArgumentError if db isn't default and strategy doesn't support different dbs" do
|
||||
strategy = mock("strategy")
|
||||
|
||||
|
||||
strategy.stub(:respond_to?).with(anything)
|
||||
strategy.stub(:respond_to?).with(:db=).and_return(false)
|
||||
|
||||
|
||||
expect { ::DatabaseCleaner::Base.new(:active_record,:connection => :my_db).strategy = strategy }.to raise_error ArgumentError
|
||||
end
|
||||
end
|
||||
|
||||
describe "orm integration" do
|
||||
let(:strategy) { mock("stratagem, attack all robots") }
|
||||
|
||||
context "active record" do
|
||||
|
||||
context "active record" do
|
||||
let(:cleaner) { ::DatabaseCleaner::Base.new(:active_record) }
|
||||
|
||||
|
||||
before(:each) do
|
||||
::DatabaseCleaner::ActiveRecord::Transaction.stub!(:new).and_return(strategy)
|
||||
#Object.const_set('ActiveRecord', "just mocking out the constant here...") unless defined?(::ActiveRecord)
|
||||
cleaner.strategy = nil
|
||||
end
|
||||
|
||||
|
||||
describe ".create_strategy" do
|
||||
it "should initialize and return the appropriate strategy" do
|
||||
DatabaseCleaner::ActiveRecord::Transaction.should_receive(:new).with('options' => 'hash')
|
||||
|
@ -166,21 +166,21 @@ module DatabaseCleaner
|
|||
DatabaseCleaner::ActiveRecord::Transaction.should_receive(:new).with('options' => 'hash')
|
||||
cleaner.strategy = :transaction, {'options' => 'hash'}
|
||||
end
|
||||
|
||||
|
||||
it "should allow any object to be set as the strategy" do
|
||||
mock_strategy = mock('strategy')
|
||||
running { DatabaseCleaner.strategy = mock_strategy }.should_not raise_error
|
||||
end
|
||||
|
||||
|
||||
it "should raise an error when the specified strategy is not found" do
|
||||
running { DatabaseCleaner.strategy = :foo }.should raise_error(DatabaseCleaner::UnknownStrategySpecified)
|
||||
end
|
||||
|
||||
|
||||
it "should raise an error when multiple args is passed in and the first is not a symbol" do
|
||||
running { DatabaseCleaner.strategy=Object.new, {:foo => 'bar'} }.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
%w[start clean].each do |strategy_method|
|
||||
describe ".#{strategy_method}" do
|
||||
it "should be delgated to the strategy set with strategy=" do
|
||||
|
@ -200,7 +200,7 @@ module DatabaseCleaner
|
|||
|
||||
context "data mapper" do
|
||||
let(:cleaner) { ::DatabaseCleaner::Base.new(:data_mapper) }
|
||||
|
||||
|
||||
before(:each) do
|
||||
::DatabaseCleaner::DataMapper::Transaction.stub!(:new).and_return(strategy)
|
||||
cleaner.strategy = nil
|
||||
|
@ -221,27 +221,27 @@ module DatabaseCleaner
|
|||
cleaner.clean_with(:transaction)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe ".strategy=" do
|
||||
it "should initalize the appropriate strategy for data mapper" do
|
||||
DatabaseCleaner::DataMapper::Transaction.should_receive(:new).with(no_args)
|
||||
cleaner.strategy = :transaction
|
||||
end
|
||||
|
||||
|
||||
it "should allow any object to be set as the strategy" do
|
||||
mock_strategy = mock('strategy')
|
||||
running { cleaner.strategy = mock_strategy }.should_not raise_error
|
||||
end
|
||||
|
||||
|
||||
it "should raise an error when the specified strategy is not found" do
|
||||
running { cleaner.strategy = :foo }.should raise_error(DatabaseCleaner::UnknownStrategySpecified)
|
||||
end
|
||||
|
||||
|
||||
it "should raise an error when multiple args is passed in and the first is not a symbol" do
|
||||
running { cleaner.strategy=Object.new, {:foo => 'bar'} }.should raise_error(ArgumentError)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
%w[start clean].each do |strategy_method|
|
||||
describe ".#{strategy_method}" do
|
||||
it "should be delgated to the strategy set with strategy=" do
|
||||
|
@ -258,6 +258,6 @@ module DatabaseCleaner
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,49 +5,49 @@ module DatabaseCleaner
|
|||
def reset
|
||||
@connections = nil
|
||||
end
|
||||
|
||||
|
||||
def connections_stub!(array)
|
||||
@connections = array
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe ::DatabaseCleaner do
|
||||
describe ::DatabaseCleaner do
|
||||
before (:each) { ::DatabaseCleaner.reset }
|
||||
|
||||
|
||||
context "orm specification" do
|
||||
it "should not accept unrecognised orms" do
|
||||
lambda { ::DatabaseCleaner[nil] }.should raise_error ::DatabaseCleaner::NoORMDetected
|
||||
end
|
||||
|
||||
|
||||
it "should accept :active_record" do
|
||||
cleaner = ::DatabaseCleaner[:active_record]
|
||||
cleaner.should be_a ::DatabaseCleaner::Base
|
||||
cleaner.orm.should == :active_record
|
||||
::DatabaseCleaner.connections.size.should == 1
|
||||
end
|
||||
|
||||
|
||||
it "should accept :data_mapper" do
|
||||
cleaner = ::DatabaseCleaner[:data_mapper]
|
||||
cleaner.should be_a ::DatabaseCleaner::Base
|
||||
cleaner.orm.should == :data_mapper
|
||||
::DatabaseCleaner.connections.size.should == 1
|
||||
end
|
||||
|
||||
|
||||
it "should accept :mongo_mapper" do
|
||||
cleaner = ::DatabaseCleaner[:mongo_mapper]
|
||||
cleaner.should be_a ::DatabaseCleaner::Base
|
||||
cleaner.orm.should == :mongo_mapper
|
||||
::DatabaseCleaner.connections.size.should == 1
|
||||
end
|
||||
|
||||
|
||||
it "should accept :couch_potato" do
|
||||
cleaner = ::DatabaseCleaner[:couch_potato]
|
||||
cleaner.should be_a ::DatabaseCleaner::Base
|
||||
cleaner.orm.should == :couch_potato
|
||||
::DatabaseCleaner.connections.size.should == 1
|
||||
end
|
||||
|
||||
|
||||
it "should accept multiple orm's" do
|
||||
::DatabaseCleaner[:couch_potato]
|
||||
::DatabaseCleaner[:data_mapper]
|
||||
|
@ -56,7 +56,7 @@ describe ::DatabaseCleaner do
|
|||
::DatabaseCleaner.connections[1].orm.should == :data_mapper
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
context "connection/db specification" do
|
||||
it "should accept a connection parameter and store it" do
|
||||
cleaner = ::DatabaseCleaner[:active_record, {:connection => :first_connection}]
|
||||
|
@ -78,41 +78,41 @@ describe ::DatabaseCleaner do
|
|||
::DatabaseCleaner[:active_record,{:connection => :second_db}]
|
||||
::DatabaseCleaner[:active_record,{:connection => :first_db} ]
|
||||
::DatabaseCleaner[:data_mapper, {:connection => :second_db}]
|
||||
|
||||
|
||||
::DatabaseCleaner.connections.size.should == 4
|
||||
|
||||
|
||||
::DatabaseCleaner.connections[0].orm.should == :data_mapper
|
||||
::DatabaseCleaner.connections[0].db.should == :first_db
|
||||
|
||||
|
||||
::DatabaseCleaner.connections[1].orm.should == :active_record
|
||||
::DatabaseCleaner.connections[1].db.should == :second_db
|
||||
|
||||
|
||||
::DatabaseCleaner.connections[2].orm.should == :active_record
|
||||
::DatabaseCleaner.connections[2].db.should == :first_db
|
||||
|
||||
|
||||
::DatabaseCleaner.connections[3].orm.should == :data_mapper
|
||||
::DatabaseCleaner.connections[3].db.should == :second_db
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
context "connection/db retrieval" do
|
||||
it "should retrieve a db rather than create a new one" do
|
||||
pending
|
||||
connection = ::DatabaseCleaner[:active_record].strategy = :truncation
|
||||
connection = ::DatabaseCleaner[:active_record].strategy = :truncation
|
||||
::DatabaseCleaner[:active_record].should == connection
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
context "class methods" do
|
||||
subject { ::DatabaseCleaner }
|
||||
|
||||
|
||||
its(:connections) { should respond_to :each }
|
||||
|
||||
|
||||
it "should give me a default (autodetection) databasecleaner by default" do
|
||||
cleaner = mock("cleaner").as_null_object
|
||||
::DatabaseCleaner::Base.should_receive(:new).with().and_return(cleaner)
|
||||
|
||||
|
||||
::DatabaseCleaner.connections.should have(1).items
|
||||
::DatabaseCleaner.connections.first.should == cleaner
|
||||
end
|
||||
|
@ -120,72 +120,72 @@ describe ::DatabaseCleaner do
|
|||
|
||||
context "single orm single connection" do
|
||||
let (:connection) { ::DatabaseCleaner.connections.first }
|
||||
|
||||
|
||||
it "should proxy strategy=" do
|
||||
stratagum = mock("stratagum")
|
||||
connection.should_receive(:strategy=).with(stratagum)
|
||||
::DatabaseCleaner.strategy = stratagum
|
||||
end
|
||||
|
||||
|
||||
it "should proxy orm=" do
|
||||
orm = mock("orm")
|
||||
connection.should_receive(:orm=).with(orm)
|
||||
::DatabaseCleaner.orm = orm
|
||||
end
|
||||
|
||||
|
||||
it "should proxy start" do
|
||||
connection.should_receive(:start)
|
||||
::DatabaseCleaner.start
|
||||
end
|
||||
|
||||
|
||||
it "should proxy clean" do
|
||||
connection.should_receive(:clean)
|
||||
::DatabaseCleaner.clean
|
||||
end
|
||||
|
||||
|
||||
it "should proxy clean_with" do
|
||||
stratagem = mock("stratgem")
|
||||
connection.should_receive(:clean_with).with(stratagem)
|
||||
::DatabaseCleaner.clean_with stratagem
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
context "multiple connections" do
|
||||
|
||||
|
||||
#these are relativly simple, all we need to do is make sure all connections are cleaned/started/cleaned_with appropriatly.
|
||||
context "simple proxy methods" do
|
||||
|
||||
|
||||
let(:active_record) { mock("active_mock") }
|
||||
let(:data_mapper) { mock("data_mock") }
|
||||
|
||||
|
||||
before(:each) do
|
||||
::DatabaseCleaner.stub!(:connections).and_return([active_record,data_mapper])
|
||||
end
|
||||
|
||||
|
||||
it "should proxy start to all connections" do
|
||||
active_record.should_receive(:start)
|
||||
data_mapper.should_receive(:start)
|
||||
|
||||
|
||||
::DatabaseCleaner.start
|
||||
end
|
||||
|
||||
|
||||
it "should proxy clean to all connections" do
|
||||
active_record.should_receive(:clean)
|
||||
data_mapper.should_receive(:clean)
|
||||
|
||||
|
||||
::DatabaseCleaner.clean
|
||||
end
|
||||
|
||||
|
||||
it "should proxy clean_with to all connections" do
|
||||
stratagem = mock("stratgem")
|
||||
active_record.should_receive(:clean_with).with(stratagem)
|
||||
data_mapper.should_receive(:clean_with).with(stratagem)
|
||||
|
||||
|
||||
::DatabaseCleaner.clean_with stratagem
|
||||
end
|
||||
end
|
||||
|
||||
# ah now we have some difficulty, we mustn't allow duplicate connections to exist, but they could
|
||||
|
||||
# ah now we have some difficulty, we mustn't allow duplicate connections to exist, but they could
|
||||
# plausably want to force orm/strategy change on two sets of orm that differ only on db
|
||||
context "multiple orm proxy methods" do
|
||||
|
||||
|
@ -199,14 +199,14 @@ describe ::DatabaseCleaner do
|
|||
active_record_1.should_receive(:orm=).with(:data_mapper)
|
||||
active_record_2.should_receive(:orm=).with(:data_mapper)
|
||||
data_mapper_1.should_receive(:orm=).with(:data_mapper)
|
||||
|
||||
|
||||
active_record_1.should_receive(:==).with(data_mapper_1).and_return(true)
|
||||
|
||||
::DatabaseCleaner.connections.size.should == 3
|
||||
::DatabaseCleaner.orm = :data_mapper
|
||||
::DatabaseCleaner.connections.size.should == 2
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
it "should proxy strategy to all connections and remove duplicate connections" do
|
||||
active_record_1 = mock("active_mock_strategy_one").as_null_object
|
||||
active_record_2 = mock("active_mock_strategy_two").as_null_object
|
||||
|
@ -218,7 +218,7 @@ describe ::DatabaseCleaner do
|
|||
active_record_2.should_receive(:strategy=).with(strategy)
|
||||
|
||||
active_record_1.should_receive(:==).with(active_record_2).and_return(true)
|
||||
|
||||
|
||||
::DatabaseCleaner.connections.size.should == 2
|
||||
::DatabaseCleaner.strategy = strategy
|
||||
::DatabaseCleaner.connections.size.should == 1
|
||||
|
@ -236,12 +236,12 @@ describe ::DatabaseCleaner do
|
|||
::DatabaseCleaner.connections.size.should == 1
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe "app_root" do
|
||||
it "should default to Dir.pwd" do
|
||||
DatabaseCleaner.app_root.should == Dir.pwd
|
||||
end
|
||||
|
||||
|
||||
it "should store specific paths" do
|
||||
DatabaseCleaner.app_root = '/path/to'
|
||||
DatabaseCleaner.app_root.should == '/path/to'
|
||||
|
|
|
@ -7,7 +7,7 @@ module DatabaseCleaner
|
|||
|
||||
describe Truncation do
|
||||
let (:database) { mock('database') }
|
||||
|
||||
|
||||
before(:each) do
|
||||
::CouchPotato.stub!(:couchrest_database).and_return(database)
|
||||
end
|
||||
|
|
|
@ -5,26 +5,26 @@ require 'database_cleaner/shared_strategy_spec'
|
|||
module DatabaseCleaner
|
||||
describe DataMapper do
|
||||
it { should respond_to :available_strategies }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
module DataMapper
|
||||
class ExampleStrategy
|
||||
include ::DatabaseCleaner::DataMapper::Base
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
|
|
@ -9,11 +9,11 @@ module DatabaseCleaner
|
|||
describe Transaction do
|
||||
it_should_behave_like "a generic strategy"
|
||||
it_should_behave_like "a generic transaction strategy"
|
||||
|
||||
|
||||
describe "start" do
|
||||
it "should start a transaction"
|
||||
end
|
||||
|
||||
|
||||
describe "clean" do
|
||||
it "should finish a transaction"
|
||||
end
|
||||
|
|
|
@ -8,4 +8,4 @@ module DatabaseCleaner
|
|||
it_should_behave_like "a generic truncation strategy"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,16 +7,16 @@ module ::DatabaseCleaner
|
|||
class ExampleStrategy
|
||||
include ::DatabaseCleaner::Generic::Base
|
||||
end
|
||||
|
||||
|
||||
describe ExampleStrategy do
|
||||
context "class methods" do
|
||||
subject { ExampleStrategy }
|
||||
its (:available_strategies) { should be_empty }
|
||||
end
|
||||
|
||||
it_should_behave_like "a generic strategy"
|
||||
|
||||
|
||||
it_should_behave_like "a generic strategy"
|
||||
|
||||
its (:db) { should == :default }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,58 +5,58 @@ module ::DatabaseCleaner
|
|||
module Generic
|
||||
class TruncationExample
|
||||
include ::DatabaseCleaner::Generic::Truncation
|
||||
|
||||
|
||||
def only
|
||||
@only
|
||||
end
|
||||
|
||||
|
||||
def except
|
||||
@tables_to_exclude
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class MigrationExample < TruncationExample
|
||||
def migration_storage_name
|
||||
"migration_storage_name"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe TruncationExample do
|
||||
its (:start) { expect{ subject }.to_not raise_error }
|
||||
its (:clean) { expect{ subject }.to raise_error NotImplementedError }
|
||||
|
||||
|
||||
context "private methods" do
|
||||
it { should_not respond_to :tables_to_truncate }
|
||||
its (:tables_to_truncate) { expect{ subject }.to raise_error NotImplementedError }
|
||||
|
||||
|
||||
it { should_not respond_to :migration_storage_name }
|
||||
its (:migration_storage_name) { should be_nil }
|
||||
end
|
||||
|
||||
|
||||
describe "initialize" do
|
||||
it { expect{ subject }.to_not raise_error }
|
||||
|
||||
|
||||
it "should accept a hash of options" do
|
||||
expect{ TruncationExample.new {} }.to_not raise_error
|
||||
end
|
||||
|
||||
|
||||
it { expect{ TruncationExample.new( { :a_random_param => "should raise ArgumentError" } ) }.to raise_error ArgumentError }
|
||||
it { expect{ TruncationExample.new( { :except => "something",:only => "something else" } ) }.to raise_error ArgumentError }
|
||||
it { expect{ TruncationExample.new( { :only => "something" } ) }.to_not raise_error ArgumentError }
|
||||
it { expect{ TruncationExample.new( { :except => "something" } ) }.to_not raise_error ArgumentError }
|
||||
|
||||
|
||||
context "" do
|
||||
subject { TruncationExample.new( { :only => ["something"] } ) }
|
||||
its (:only) { should == ["something"] }
|
||||
its (:except) { should == [] }
|
||||
end
|
||||
|
||||
|
||||
context "" do
|
||||
subject { TruncationExample.new( { :except => ["something"] } ) }
|
||||
its (:only) { should == nil }
|
||||
its (:except) { should include "something" }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "" do
|
||||
subject { MigrationExample.new }
|
||||
its (:only) { should == nil }
|
||||
|
@ -65,4 +65,4 @@ module ::DatabaseCleaner
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,29 +5,29 @@ require 'database_cleaner/shared_strategy_spec'
|
|||
module DatabaseCleaner
|
||||
describe MongoMapper do
|
||||
it { should respond_to :available_strategies }
|
||||
end
|
||||
|
||||
module MongoMapper
|
||||
end
|
||||
|
||||
module MongoMapper
|
||||
class ExampleStrategy
|
||||
include ::DatabaseCleaner::MongoMapper::Base
|
||||
end
|
||||
|
||||
|
||||
describe ExampleStrategy do
|
||||
|
||||
it_should_behave_like "a generic strategy"
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,4 +5,4 @@ end
|
|||
class Gadget
|
||||
include ::MongoMapper::Document
|
||||
key :name, String
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,20 +7,20 @@ module DatabaseCleaner
|
|||
module MongoMapper
|
||||
|
||||
describe Truncation do
|
||||
|
||||
|
||||
#doing this in the file root breaks autospec, doing it before(:all) just fails the specs
|
||||
before(:all) do
|
||||
::MongoMapper.connection = ::Mongo::Connection.new('127.0.0.1')
|
||||
@test_db = 'database_cleaner_specs'
|
||||
::MongoMapper.database = @test_db
|
||||
end
|
||||
|
||||
|
||||
before(:each) do
|
||||
::MongoMapper.connection.drop_database(@test_db)
|
||||
end
|
||||
|
||||
def ensure_counts(expected_counts)
|
||||
# I had to add this sanity_check garbage because I was getting non-determinisc results from mongomapper at times..
|
||||
# I had to add this sanity_check garbage because I was getting non-determinisc results from mongomapper at times..
|
||||
# very odd and disconcerting...
|
||||
sanity_check = expected_counts.delete(:sanity_check)
|
||||
begin
|
||||
|
@ -71,4 +71,4 @@ module DatabaseCleaner
|
|||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,4 +10,4 @@ end
|
|||
shared_examples_for "a generic transaction strategy" do
|
||||
it { should respond_to :start }
|
||||
it { should respond_to :clean }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,7 +6,7 @@ $:.unshift(File.dirname(__FILE__) + '/../lib')
|
|||
require 'database_cleaner'
|
||||
|
||||
Spec::Runner.configure do |config|
|
||||
|
||||
|
||||
end
|
||||
|
||||
alias running lambda
|
||||
|
|
Loading…
Add table
Reference in a new issue