Skip to content

Commit

Permalink
Merge pull request #182 from smasry/rails4
Browse files Browse the repository at this point in the history
Fix for rails 4 deprecation warnings ... fixes #150
  • Loading branch information
bmabey committed Feb 8, 2013
2 parents 43c382f + d0b1e55 commit f80af0d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 33 deletions.
25 changes: 17 additions & 8 deletions lib/database_cleaner/active_record/transaction.rb
Expand Up @@ -7,10 +7,12 @@ class Transaction
include ::DatabaseCleaner::Generic::Transaction include ::DatabaseCleaner::Generic::Transaction


def start def start
if connection_class.connection.respond_to?(:increment_open_transactions) if connection_maintains_transaction_count?
connection_class.connection.increment_open_transactions if connection_class.connection.respond_to?(:increment_open_transactions)
else connection_class.connection.increment_open_transactions
connection_class.__send__(:increment_open_transactions) else
connection_class.__send__(:increment_open_transactions)
end
end end
connection_class.connection.begin_db_transaction connection_class.connection.begin_db_transaction
end end
Expand All @@ -26,11 +28,18 @@ def clean
connection_class.connection.send(:rollback_transaction_records, true) connection_class.connection.send(:rollback_transaction_records, true)
end end


if connection_class.connection.respond_to?(:decrement_open_transactions) if connection_maintains_transaction_count?
connection_class.connection.decrement_open_transactions if connection_class.connection.respond_to?(:decrement_open_transactions)
else connection_class.connection.decrement_open_transactions
connection_class.__send__(:decrement_open_transactions) else
connection_class.__send__(:decrement_open_transactions)
end
end end
end end

def connection_maintains_transaction_count?
ActiveRecord::VERSION::MAJOR < 4
end

end end
end end
105 changes: 80 additions & 25 deletions spec/database_cleaner/active_record/transaction_spec.rb
Expand Up @@ -37,43 +37,98 @@ module ActiveRecord
end end


describe "#clean" do describe "#clean" do
it "should start a transaction" do context "manual accounting of transaction count" do
connection.should_receive(:open_transactions).and_return(1) it "should start a transaction" do
connection.should_receive(:open_transactions).and_return(1)


connection.stub!(:decrement_open_transactions) connection.stub!(:decrement_open_transactions)


connection.should_receive(:rollback_db_transaction) connection.should_receive(:rollback_db_transaction)
Transaction.new.clean Transaction.new.clean
end end


it "should decrement open transactions if possible" do it "should decrement open transactions if possible" do
connection.should_receive(:open_transactions).and_return(1) connection.should_receive(:open_transactions).and_return(1)


connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(true) connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(true)
connection.stub!(:respond_to?).with(:rollback_transaction_records).and_return(false) connection.stub!(:respond_to?).with(:rollback_transaction_records).and_return(false)
connection.stub!(:rollback_db_transaction) connection.stub!(:rollback_db_transaction)


connection.should_receive(:decrement_open_transactions) connection.should_receive(:decrement_open_transactions)
Transaction.new.clean Transaction.new.clean
end end


it "should not try to decrement or rollback if open_transactions is 0 for whatever reason" do it "should not try to decrement or rollback if open_transactions is 0 for whatever reason" do
connection.should_receive(:open_transactions).and_return(0) connection.should_receive(:open_transactions).and_return(0)


Transaction.new.clean Transaction.new.clean
end end


it "should decrement connection via ActiveRecord::Base if connection won't" do it "should decrement connection via ActiveRecord::Base if connection won't" do
connection.should_receive(:open_transactions).and_return(1) connection.should_receive(:open_transactions).and_return(1)
connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(false) connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(false)
connection.stub!(:respond_to?).with(:rollback_transaction_records).and_return(false) connection.stub!(:respond_to?).with(:rollback_transaction_records).and_return(false)
connection.stub!(:rollback_db_transaction) connection.stub!(:rollback_db_transaction)


::ActiveRecord::Base.should_receive(:decrement_open_transactions) ::ActiveRecord::Base.should_receive(:decrement_open_transactions)
Transaction.new.clean Transaction.new.clean
end
end

context "automatic accounting of transaction count" do

it "should start a transaction" do
stub_const("ActiveRecord::VERSION::MAJOR", 4)
connection.stub!(:rollback_db_transaction)
connection.should_receive(:open_transactions).and_return(1)

connection.should_not_receive(:decrement_open_transactions)
connection.should_receive(:rollback_db_transaction)
Transaction.new.clean
end

it "should decrement open transactions if possible" do
stub_const("ActiveRecord::VERSION::MAJOR", 4)
connection.stub!(:rollback_db_transaction)
connection.should_receive(:open_transactions).and_return(1)

connection.should_not_receive(:decrement_open_transactions)
Transaction.new.clean
end

it "should not try to decrement or rollback if open_transactions is 0 for whatever reason" do
stub_const("ActiveRecord::VERSION::MAJOR", 4)
connection.should_receive(:open_transactions).and_return(0)

Transaction.new.clean
end

it "should decrement connection via ActiveRecord::Base if connection won't" do
stub_const("ActiveRecord::VERSION::MAJOR", 4)
connection.should_receive(:open_transactions).and_return(1)
connection.stub!(:respond_to?).with(:rollback_transaction_records).and_return(false)
connection.stub!(:rollback_db_transaction)

::ActiveRecord::Base.should_not_receive(:decrement_open_transactions)
Transaction.new.clean
end
end
end

describe "#connection_maintains_transaction_count?" do
it "should return true if the major active record version is < 4" do
stub_const("ActiveRecord::VERSION::MAJOR", 3)
Transaction.new.connection_maintains_transaction_count?.should be_true
end
it "should return false if the major active record version is > 3" do
stub_const("ActiveRecord::VERSION::MAJOR", 4)
Transaction.new.connection_maintains_transaction_count?.should be_false
end end
end end

end end




end end
end end

0 comments on commit f80af0d

Please sign in to comment.