Permalink
Browse files

Merge pull request #182 from smasry/rails4

Fix for rails 4 deprecation warnings ... fixes #150
  • Loading branch information...
2 parents 43c382f + d0b1e55 commit f80af0ddb715934fc79ede9d3aa1b0f3c4d33025 @bmabey bmabey committed Feb 8, 2013
View
25 lib/database_cleaner/active_record/transaction.rb
@@ -7,10 +7,12 @@ class Transaction
include ::DatabaseCleaner::Generic::Transaction
def start
- if connection_class.connection.respond_to?(:increment_open_transactions)
- connection_class.connection.increment_open_transactions
- else
- connection_class.__send__(:increment_open_transactions)
+ if connection_maintains_transaction_count?
+ if connection_class.connection.respond_to?(:increment_open_transactions)
+ connection_class.connection.increment_open_transactions
+ else
+ connection_class.__send__(:increment_open_transactions)
+ end
end
connection_class.connection.begin_db_transaction
end
@@ -26,11 +28,18 @@ def clean
connection_class.connection.send(:rollback_transaction_records, true)
end
- if connection_class.connection.respond_to?(:decrement_open_transactions)
- connection_class.connection.decrement_open_transactions
- else
- connection_class.__send__(:decrement_open_transactions)
+ if connection_maintains_transaction_count?
+ if connection_class.connection.respond_to?(:decrement_open_transactions)
+ connection_class.connection.decrement_open_transactions
+ else
+ connection_class.__send__(:decrement_open_transactions)
+ end
end
end
+
+ def connection_maintains_transaction_count?
+ ActiveRecord::VERSION::MAJOR < 4
+ end
+
end
end
View
105 spec/database_cleaner/active_record/transaction_spec.rb
@@ -37,43 +37,98 @@ module ActiveRecord
end
describe "#clean" do
- it "should start a transaction" do
- connection.should_receive(:open_transactions).and_return(1)
+ context "manual accounting of transaction count" do
+ 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)
- Transaction.new.clean
- end
+ connection.should_receive(:rollback_db_transaction)
+ Transaction.new.clean
+ end
- it "should decrement open transactions if possible" do
- connection.should_receive(:open_transactions).and_return(1)
+ it "should decrement open transactions if possible" do
+ connection.should_receive(:open_transactions).and_return(1)
- connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(true)
- connection.stub!(:respond_to?).with(:rollback_transaction_records).and_return(false)
- connection.stub!(:rollback_db_transaction)
+ connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(true)
+ connection.stub!(:respond_to?).with(:rollback_transaction_records).and_return(false)
+ connection.stub!(:rollback_db_transaction)
- connection.should_receive(:decrement_open_transactions)
- Transaction.new.clean
- end
+ connection.should_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
- connection.should_receive(:open_transactions).and_return(0)
+ 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)
- Transaction.new.clean
- end
+ Transaction.new.clean
+ end
- it "should decrement connection via ActiveRecord::Base if connection won't" do
- connection.should_receive(:open_transactions).and_return(1)
- connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(false)
- connection.stub!(:respond_to?).with(:rollback_transaction_records).and_return(false)
- connection.stub!(:rollback_db_transaction)
+ it "should decrement connection via ActiveRecord::Base if connection won't" do
+ connection.should_receive(:open_transactions).and_return(1)
+ connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(false)
+ connection.stub!(:respond_to?).with(:rollback_transaction_records).and_return(false)
+ connection.stub!(:rollback_db_transaction)
- ::ActiveRecord::Base.should_receive(:decrement_open_transactions)
- Transaction.new.clean
+ ::ActiveRecord::Base.should_receive(:decrement_open_transactions)
+ 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

0 comments on commit f80af0d

Please sign in to comment.