renames :fast to :pre_count
This commit is contained in:
parent
cb1e9edf31
commit
d87f4ec066
|
@ -43,7 +43,7 @@ module DatabaseCleaner
|
||||||
tables.each { |t| truncate_table(t) }
|
tables.each { |t| truncate_table(t) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def fast_truncate_tables(tables, options = {:reset_ids => true})
|
def pre_count_truncate_tables(tables, options = {:reset_ids => true})
|
||||||
filter = options[:reset_ids] ? method(:has_been_used?) : method(:has_rows?)
|
filter = options[:reset_ids] ? method(:has_been_used?) : method(:has_rows?)
|
||||||
truncate_tables(tables.select(&filter))
|
truncate_tables(tables.select(&filter))
|
||||||
end
|
end
|
||||||
|
@ -124,7 +124,7 @@ module DatabaseCleaner
|
||||||
execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} #{restart_identity} #{cascade};")
|
execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} #{restart_identity} #{cascade};")
|
||||||
end
|
end
|
||||||
|
|
||||||
def fast_truncate_tables(tables, options = {:reset_ids => true})
|
def pre_count_truncate_tables(tables, options = {:reset_ids => true})
|
||||||
filter = options[:reset_ids] ? method(:has_been_used?) : method(:has_rows?)
|
filter = options[:reset_ids] ? method(:has_been_used?) : method(:has_rows?)
|
||||||
truncate_tables(tables.select(&filter))
|
truncate_tables(tables.select(&filter))
|
||||||
end
|
end
|
||||||
|
@ -226,8 +226,8 @@ module DatabaseCleaner::ActiveRecord
|
||||||
def clean
|
def clean
|
||||||
connection = connection_klass.connection
|
connection = connection_klass.connection
|
||||||
connection.disable_referential_integrity do
|
connection.disable_referential_integrity do
|
||||||
if fast? && connection.respond_to?(:fast_truncate_tables)
|
if pre_count? && connection.respond_to?(:pre_count_truncate_tables)
|
||||||
connection.fast_truncate_tables(tables_to_truncate(connection), {:reset_ids => reset_ids?})
|
connection.pre_count_truncate_tables(tables_to_truncate(connection), {:reset_ids => reset_ids?})
|
||||||
else
|
else
|
||||||
connection.truncate_tables(tables_to_truncate(connection))
|
connection.truncate_tables(tables_to_truncate(connection))
|
||||||
end
|
end
|
||||||
|
@ -245,8 +245,8 @@ module DatabaseCleaner::ActiveRecord
|
||||||
'schema_migrations'
|
'schema_migrations'
|
||||||
end
|
end
|
||||||
|
|
||||||
def fast?
|
def pre_count?
|
||||||
@fast == true
|
@pre_count == true
|
||||||
end
|
end
|
||||||
|
|
||||||
def reset_ids?
|
def reset_ids?
|
||||||
|
|
|
@ -2,8 +2,8 @@ module DatabaseCleaner
|
||||||
module Generic
|
module Generic
|
||||||
module Truncation
|
module Truncation
|
||||||
def initialize(opts={})
|
def initialize(opts={})
|
||||||
if !opts.empty? && !(opts.keys - [:only, :except, :fast, :reset_ids]).empty?
|
if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :reset_ids]).empty?
|
||||||
raise ArgumentError, "The only valid options are :only, :except, :fast or :reset_ids. You specified #{opts.keys.join(',')}."
|
raise ArgumentError, "The only valid options are :only, :except, :pre_count or :reset_ids. You specified #{opts.keys.join(',')}."
|
||||||
end
|
end
|
||||||
if opts.has_key?(:only) && opts.has_key?(:except)
|
if opts.has_key?(:only) && opts.has_key?(:except)
|
||||||
raise ArgumentError, "You may only specify either :only or :except. Doing both doesn't really make sense does it?"
|
raise ArgumentError, "You may only specify either :only or :except. Doing both doesn't really make sense does it?"
|
||||||
|
@ -12,7 +12,7 @@ module DatabaseCleaner
|
||||||
@only = opts[:only]
|
@only = opts[:only]
|
||||||
@tables_to_exclude = (opts[:except] || []).dup
|
@tables_to_exclude = (opts[:except] || []).dup
|
||||||
@tables_to_exclude << migration_storage_name if migration_storage_name
|
@tables_to_exclude << migration_storage_name if migration_storage_name
|
||||||
@fast = opts[:fast]
|
@pre_count = opts[:pre_count]
|
||||||
@reset_ids = opts[:reset_ids]
|
@reset_ids = opts[:reset_ids]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it_behaves_like "an adapter with fast truncation" do
|
it_behaves_like "an adapter with pre-count truncation" do
|
||||||
let(:adapter) { Mysql2Adapter }
|
let(:adapter) { Mysql2Adapter }
|
||||||
let(:connection) { active_record_mysql2_connection }
|
let(:connection) { active_record_mysql2_connection }
|
||||||
end
|
end
|
||||||
|
|
|
@ -30,7 +30,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it_behaves_like "an adapter with fast truncation" do
|
it_behaves_like "an adapter with pre-count truncation" do
|
||||||
let(:adapter) { MysqlAdapter }
|
let(:adapter) { MysqlAdapter }
|
||||||
let(:connection) { active_record_mysql_connection }
|
let(:connection) { active_record_mysql_connection }
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,7 +37,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it_behaves_like "an adapter with fast truncation" do
|
it_behaves_like "an adapter with pre-count truncation" do
|
||||||
let(:adapter) { PostgreSQLAdapter }
|
let(:adapter) { PostgreSQLAdapter }
|
||||||
let(:connection) { active_record_pg_connection }
|
let(:connection) { active_record_pg_connection }
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
shared_examples_for "an adapter with fast truncation" do
|
shared_examples_for "an adapter with pre-count truncation" do
|
||||||
describe "#fast_truncate_tables" do
|
describe "#pre_count_truncate_tables" do
|
||||||
|
|
||||||
context "with :reset_ids set true" do
|
context "with :reset_ids set true" do
|
||||||
it "truncates the table" do
|
it "truncates the table" do
|
||||||
2.times { User.create }
|
2.times { User.create }
|
||||||
|
|
||||||
connection.fast_truncate_tables(%w[users], :reset_ids => true)
|
connection.pre_count_truncate_tables(%w[users], :reset_ids => true)
|
||||||
User.count.should be_zero
|
User.count.should be_zero
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ shared_examples_for "an adapter with fast truncation" do
|
||||||
2.times { User.create }
|
2.times { User.create }
|
||||||
User.delete_all
|
User.delete_all
|
||||||
|
|
||||||
connection.fast_truncate_tables(%w[users]) # true is also the default
|
connection.pre_count_truncate_tables(%w[users]) # true is also the default
|
||||||
User.create.id.should == 1
|
User.create.id.should == 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -23,7 +23,7 @@ shared_examples_for "an adapter with fast truncation" do
|
||||||
it "truncates the table" do
|
it "truncates the table" do
|
||||||
2.times { User.create }
|
2.times { User.create }
|
||||||
|
|
||||||
connection.fast_truncate_tables(%w[users], :reset_ids => false)
|
connection.pre_count_truncate_tables(%w[users], :reset_ids => false)
|
||||||
User.count.should be_zero
|
User.count.should be_zero
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ shared_examples_for "an adapter with fast truncation" do
|
||||||
2.times { User.create }
|
2.times { User.create }
|
||||||
User.delete_all
|
User.delete_all
|
||||||
|
|
||||||
connection.fast_truncate_tables(%w[users], :reset_ids => false)
|
connection.pre_count_truncate_tables(%w[users], :reset_ids => false)
|
||||||
|
|
||||||
User.create.id.should == 3
|
User.create.id.should == 3
|
||||||
end
|
end
|
||||||
|
|
|
@ -70,28 +70,28 @@ module DatabaseCleaner
|
||||||
Truncation.new.clean
|
Truncation.new.clean
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "relying on #fast_truncate_tables if connection allows it" do
|
describe "relying on #pre_count_truncate_tables if connection allows it" do
|
||||||
subject { Truncation.new }
|
subject { Truncation.new }
|
||||||
|
|
||||||
it "should rely on #fast_truncate_tables if #fast? returns true" do
|
it "should rely on #pre_count_truncate_tables if #pre_count? returns true" do
|
||||||
connection.stub!(:database_cleaner_table_cache).and_return(%w[widgets dogs])
|
connection.stub!(:database_cleaner_table_cache).and_return(%w[widgets dogs])
|
||||||
connection.stub!(:database_cleaner_view_cache).and_return(["widgets"])
|
connection.stub!(:database_cleaner_view_cache).and_return(["widgets"])
|
||||||
|
|
||||||
subject.instance_variable_set(:"@fast", true)
|
subject.instance_variable_set(:"@pre_count", true)
|
||||||
|
|
||||||
connection.should_not_receive(:truncate_tables).with(['dogs'])
|
connection.should_not_receive(:truncate_tables).with(['dogs'])
|
||||||
connection.should_receive(:fast_truncate_tables).with(['dogs'], :reset_ids => true)
|
connection.should_receive(:pre_count_truncate_tables).with(['dogs'], :reset_ids => true)
|
||||||
|
|
||||||
subject.clean
|
subject.clean
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not rely on #fast_truncate_tables if #fast? return false" do
|
it "should not rely on #pre_count_truncate_tables if #pre_count? return false" do
|
||||||
connection.stub!(:database_cleaner_table_cache).and_return(%w[widgets dogs])
|
connection.stub!(:database_cleaner_table_cache).and_return(%w[widgets dogs])
|
||||||
connection.stub!(:database_cleaner_view_cache).and_return(["widgets"])
|
connection.stub!(:database_cleaner_view_cache).and_return(["widgets"])
|
||||||
|
|
||||||
subject.instance_variable_set(:"@fast", false)
|
subject.instance_variable_set(:"@pre_count", false)
|
||||||
|
|
||||||
connection.should_not_receive(:fast_truncate_tables).with(['dogs'], :reset_ids => true)
|
connection.should_not_receive(:pre_count_truncate_tables).with(['dogs'], :reset_ids => true)
|
||||||
connection.should_receive(:truncate_tables).with(['dogs'])
|
connection.should_receive(:truncate_tables).with(['dogs'])
|
||||||
|
|
||||||
subject.clean
|
subject.clean
|
||||||
|
@ -99,7 +99,7 @@ module DatabaseCleaner
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#fast?' do
|
describe '#pre_count?' do
|
||||||
before(:each) do
|
before(:each) do
|
||||||
connection.stub!(:disable_referential_integrity).and_yield
|
connection.stub!(:disable_referential_integrity).and_yield
|
||||||
connection.stub!(:database_cleaner_view_cache).and_return([])
|
connection.stub!(:database_cleaner_view_cache).and_return([])
|
||||||
|
@ -107,16 +107,16 @@ module DatabaseCleaner
|
||||||
end
|
end
|
||||||
|
|
||||||
subject { Truncation.new }
|
subject { Truncation.new }
|
||||||
its(:fast?) { should == false }
|
its(:pre_count?) { should == false }
|
||||||
|
|
||||||
it 'should return true if @reset_id is set and non false or nil' do
|
it 'should return true if @reset_id is set and non false or nil' do
|
||||||
subject.instance_variable_set(:"@fast", true)
|
subject.instance_variable_set(:"@pre_count", true)
|
||||||
subject.send(:fast?).should == true
|
subject.send(:pre_count?).should == true
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should return false if @reset_id is set to false' do
|
it 'should return false if @reset_id is set to false' do
|
||||||
subject.instance_variable_set(:"@fast", false)
|
subject.instance_variable_set(:"@pre_count", false)
|
||||||
subject.send(:fast?).should == false
|
subject.send(:pre_count?).should == false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,8 @@ module ::DatabaseCleaner
|
||||||
!!@reset_ids
|
!!@reset_ids
|
||||||
end
|
end
|
||||||
|
|
||||||
def fast?
|
def pre_count?
|
||||||
!!@fast
|
!!@pre_count
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ module ::DatabaseCleaner
|
||||||
it { expect{ TruncationExample.new( { :except => "something",:only => "something else" } ) }.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( { :only => "something" } ) }.to_not raise_error(ArgumentError) }
|
||||||
it { expect{ TruncationExample.new( { :except => "something" } ) }.to_not raise_error(ArgumentError) }
|
it { expect{ TruncationExample.new( { :except => "something" } ) }.to_not raise_error(ArgumentError) }
|
||||||
it { expect{ TruncationExample.new( { :fast => "something" } ) }.to_not raise_error(ArgumentError) }
|
it { expect{ TruncationExample.new( { :pre_count => "something" } ) }.to_not raise_error(ArgumentError) }
|
||||||
it { expect{ TruncationExample.new( { :reset_ids => "something" } ) }.to_not raise_error(ArgumentError) }
|
it { expect{ TruncationExample.new( { :reset_ids => "something" } ) }.to_not raise_error(ArgumentError) }
|
||||||
|
|
||||||
context "" do
|
context "" do
|
||||||
|
@ -78,13 +78,13 @@ module ::DatabaseCleaner
|
||||||
end
|
end
|
||||||
|
|
||||||
context "" do
|
context "" do
|
||||||
subject { TruncationExample.new( { :fast => ["something"] } ) }
|
subject { TruncationExample.new( { :pre_count => ["something"] } ) }
|
||||||
its(:fast?) { should == true }
|
its(:pre_count?) { should == true }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "" do
|
context "" do
|
||||||
subject { TruncationExample.new( { :fast => nil } ) }
|
subject { TruncationExample.new( { :pre_count => nil } ) }
|
||||||
its(:fast?) { should == false }
|
its(:pre_count?) { should == false }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "" do
|
context "" do
|
||||||
|
|
Loading…
Reference in New Issue