mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
renames :fast to :pre_count
This commit is contained in:
parent
cb1e9edf31
commit
d87f4ec066
8 changed files with 38 additions and 38 deletions
|
@ -43,7 +43,7 @@ module DatabaseCleaner
|
|||
tables.each { |t| truncate_table(t) }
|
||||
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?)
|
||||
truncate_tables(tables.select(&filter))
|
||||
end
|
||||
|
@ -124,7 +124,7 @@ module DatabaseCleaner
|
|||
execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} #{restart_identity} #{cascade};")
|
||||
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?)
|
||||
truncate_tables(tables.select(&filter))
|
||||
end
|
||||
|
@ -226,8 +226,8 @@ module DatabaseCleaner::ActiveRecord
|
|||
def clean
|
||||
connection = connection_klass.connection
|
||||
connection.disable_referential_integrity do
|
||||
if fast? && connection.respond_to?(:fast_truncate_tables)
|
||||
connection.fast_truncate_tables(tables_to_truncate(connection), {:reset_ids => reset_ids?})
|
||||
if pre_count? && connection.respond_to?(:pre_count_truncate_tables)
|
||||
connection.pre_count_truncate_tables(tables_to_truncate(connection), {:reset_ids => reset_ids?})
|
||||
else
|
||||
connection.truncate_tables(tables_to_truncate(connection))
|
||||
end
|
||||
|
@ -245,8 +245,8 @@ module DatabaseCleaner::ActiveRecord
|
|||
'schema_migrations'
|
||||
end
|
||||
|
||||
def fast?
|
||||
@fast == true
|
||||
def pre_count?
|
||||
@pre_count == true
|
||||
end
|
||||
|
||||
def reset_ids?
|
||||
|
|
|
@ -2,8 +2,8 @@ module DatabaseCleaner
|
|||
module Generic
|
||||
module Truncation
|
||||
def initialize(opts={})
|
||||
if !opts.empty? && !(opts.keys - [:only, :except, :fast, :reset_ids]).empty?
|
||||
raise ArgumentError, "The only valid options are :only, :except, :fast or :reset_ids. You specified #{opts.keys.join(',')}."
|
||||
if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :reset_ids]).empty?
|
||||
raise ArgumentError, "The only valid options are :only, :except, :pre_count or :reset_ids. You specified #{opts.keys.join(',')}."
|
||||
end
|
||||
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?"
|
||||
|
@ -12,7 +12,7 @@ module DatabaseCleaner
|
|||
@only = opts[:only]
|
||||
@tables_to_exclude = (opts[:except] || []).dup
|
||||
@tables_to_exclude << migration_storage_name if migration_storage_name
|
||||
@fast = opts[:fast]
|
||||
@pre_count = opts[:pre_count]
|
||||
@reset_ids = opts[:reset_ids]
|
||||
end
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ module ActiveRecord
|
|||
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(:connection) { active_record_mysql2_connection }
|
||||
end
|
||||
|
|
|
@ -30,7 +30,7 @@ module ActiveRecord
|
|||
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(:connection) { active_record_mysql_connection }
|
||||
end
|
||||
|
|
|
@ -37,7 +37,7 @@ module ActiveRecord
|
|||
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(:connection) { active_record_pg_connection }
|
||||
end
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
shared_examples_for "an adapter with fast truncation" do
|
||||
describe "#fast_truncate_tables" do
|
||||
shared_examples_for "an adapter with pre-count truncation" do
|
||||
describe "#pre_count_truncate_tables" do
|
||||
|
||||
context "with :reset_ids set true" do
|
||||
it "truncates the table" do
|
||||
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
|
||||
end
|
||||
|
||||
|
@ -13,7 +13,7 @@ shared_examples_for "an adapter with fast truncation" do
|
|||
2.times { User.create }
|
||||
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
|
||||
end
|
||||
end
|
||||
|
@ -23,7 +23,7 @@ shared_examples_for "an adapter with fast truncation" do
|
|||
it "truncates the table" do
|
||||
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
|
||||
end
|
||||
|
||||
|
@ -31,7 +31,7 @@ shared_examples_for "an adapter with fast truncation" do
|
|||
2.times { User.create }
|
||||
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
|
||||
end
|
||||
|
|
|
@ -70,28 +70,28 @@ module DatabaseCleaner
|
|||
Truncation.new.clean
|
||||
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 }
|
||||
|
||||
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_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_receive(:fast_truncate_tables).with(['dogs'], :reset_ids => true)
|
||||
connection.should_receive(:pre_count_truncate_tables).with(['dogs'], :reset_ids => true)
|
||||
|
||||
subject.clean
|
||||
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_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'])
|
||||
|
||||
subject.clean
|
||||
|
@ -99,7 +99,7 @@ module DatabaseCleaner
|
|||
end
|
||||
end
|
||||
|
||||
describe '#fast?' do
|
||||
describe '#pre_count?' do
|
||||
before(:each) do
|
||||
connection.stub!(:disable_referential_integrity).and_yield
|
||||
connection.stub!(:database_cleaner_view_cache).and_return([])
|
||||
|
@ -107,16 +107,16 @@ module DatabaseCleaner
|
|||
end
|
||||
|
||||
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
|
||||
subject.instance_variable_set(:"@fast", true)
|
||||
subject.send(:fast?).should == true
|
||||
subject.instance_variable_set(:"@pre_count", true)
|
||||
subject.send(:pre_count?).should == true
|
||||
end
|
||||
|
||||
it 'should return false if @reset_id is set to false' do
|
||||
subject.instance_variable_set(:"@fast", false)
|
||||
subject.send(:fast?).should == false
|
||||
subject.instance_variable_set(:"@pre_count", false)
|
||||
subject.send(:pre_count?).should == false
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -18,8 +18,8 @@ module ::DatabaseCleaner
|
|||
!!@reset_ids
|
||||
end
|
||||
|
||||
def fast?
|
||||
!!@fast
|
||||
def pre_count?
|
||||
!!@pre_count
|
||||
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( { :only => "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) }
|
||||
|
||||
context "" do
|
||||
|
@ -78,13 +78,13 @@ module ::DatabaseCleaner
|
|||
end
|
||||
|
||||
context "" do
|
||||
subject { TruncationExample.new( { :fast => ["something"] } ) }
|
||||
its(:fast?) { should == true }
|
||||
subject { TruncationExample.new( { :pre_count => ["something"] } ) }
|
||||
its(:pre_count?) { should == true }
|
||||
end
|
||||
|
||||
context "" do
|
||||
subject { TruncationExample.new( { :fast => nil } ) }
|
||||
its(:fast?) { should == false }
|
||||
subject { TruncationExample.new( { :pre_count => nil } ) }
|
||||
its(:pre_count?) { should == false }
|
||||
end
|
||||
|
||||
context "" do
|
||||
|
|
Loading…
Reference in a new issue