truncation AR strategy now takes :only and :except so you can manage the tables you want to truncate

This commit is contained in:
Ben Mabey 2009-03-04 21:31:00 -07:00
parent 9fa3eb0818
commit a96c0d5f93
5 changed files with 98 additions and 3 deletions

View file

@ -2,7 +2,7 @@
module ActiveRecord
module ConnectionAdapters
class MysqlAdapter
def truncate_tabe(table_name)
def truncate_table(table_name)
execute("TRUNCATE TABLE #{table_name};")
end
end
@ -21,6 +21,18 @@ end
module DatabaseCleaner::ActiveRecord
class Truncation
def initialize(options={})
if !options.empty? && !(options.keys - [:only, :except]).empty?
raise ArgumentError, "The only valid options are :only and :except. You specified #{options.keys.join(',')}."
end
if options.has_key?(:only) && options.has_key?(:except)
raise ArgumentError, "You may only specify either :only or :either. Doing both doesn't really make sense does it?"
end
@only = options[:only]
@tables_to_exclude = (options[:except] || []) << 'schema_migrations'
end
def start
# no-op
end
@ -28,7 +40,7 @@ module DatabaseCleaner::ActiveRecord
def clean
connection.disable_referential_integrity do
(connection.tables - %w{schema_migrations}).each do |table_name|
tables_to_truncate.each do |table_name|
connection.truncate_table table_name
end
end
@ -36,8 +48,12 @@ module DatabaseCleaner::ActiveRecord
private
def tables_to_truncate
(@only || connection.tables) - @tables_to_exclude
end
def connection
ActiveRecord::Base.connection
::ActiveRecord::Base.connection
end
end

View file

@ -0,0 +1,66 @@
require File.dirname(__FILE__) + '/../../spec_helper'
require 'database_cleaner/active_record/truncation'
require 'active_record'
#module ActiveRecord
#module ConnectionAdapters
#[MysqlAdapter, SQLite3Adapter].each do |adapter|
#describe adapter, "#truncate_table" do
#it "should truncate the table"
#end
#end
#end
#end
module DatabaseCleaner
module ActiveRecord
describe Truncation do
before(:each) do
@connection = mock('connection')
@connection.stub!(:disable_referential_integrity).and_yield
::ActiveRecord::Base.stub!(:connection).and_return(@connection)
end
it "should truncate all tables except for schema_migrations" do
@connection.stub!(:tables).and_return(%w[schema_migrations widgets dogs])
@connection.should_receive(:truncate_table).with('widgets')
@connection.should_receive(:truncate_table).with('dogs')
@connection.should_not_receive(:truncate_table).with('schema_migrations')
Truncation.new.clean
end
it "should only truncate the tables specified in the :only option when provided" do
@connection.stub!(:tables).and_return(%w[schema_migrations widgets dogs])
@connection.should_receive(:truncate_table).with('widgets')
@connection.should_not_receive(:truncate_table).with('dogs')
Truncation.new(:only => ['widgets']).clean
end
it "should not truncate the tables specified in the :except option" do
@connection.stub!(:tables).and_return(%w[schema_migrations widgets dogs])
@connection.should_receive(:truncate_table).with('dogs')
@connection.should_not_receive(:truncate_table).with('widgets')
Truncation.new(:except => ['widgets']).clean
end
it "should raise an error when :only and :except options are used" do
running {
Truncation.new(:except => ['widgets'], :only => ['widgets'])
}.should raise_error(ArgumentError)
end
it "should raise an error when invalid options are provided" do
running { Truncation.new(:foo => 'bar') }.should raise_error(ArgumentError)
end
end
end
end

View file

@ -3,6 +3,16 @@ require 'database_cleaner/active_record/transaction'
require 'database_cleaner/data_mapper/transaction'
describe DatabaseCleaner do
# These examples muck around with the constants for autodetection so we need to clean up....
before(:all) do
TempAR = ActiveRecord unless defined?(TempAR)
# need to add one for each ORM that we load in the spec helper...
end
after(:all) do
ActiveRecord = TempAR
end
before(:each) do
DatabaseCleaner::ActiveRecord::Transaction.stub!(:new).and_return(@strategy = mock('strategy'))
Object.const_set('ActiveRecord', "just mocking out the constant here...") unless defined?(::ActiveRecord)
@ -64,4 +74,5 @@ describe DatabaseCleaner do
end
end
end
end

View file

@ -3,3 +3,4 @@
--loadby
mtime
--reverse
--backtrace

View file

@ -1,5 +1,6 @@
require 'rubygems'
require 'spec'
require 'activerecord'
$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'database_cleaner'