From fa8dfad7654baa831c8de515118008b3382df515 Mon Sep 17 00:00:00 2001 From: benedikt Date: Thu, 9 Jun 2011 13:10:20 +0200 Subject: Don't wrap operations on collection associations in transactions when they are not needed, so the connection adapter does not send empty BEGIN COMMIT transactions blocks to the database. --- .../associations/has_many_associations_test.rb | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 49999630b6..79b56d81df 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -537,6 +537,18 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 3, companies(:first_firm).clients_of_firm(true).size end + def test_transactions_when_adding_to_persisted + force_signal37_to_load_all_clients_of_firm + Client.expects(:transaction) + companies(:first_firm).clients_of_firm.concat(Client.new("name" => "Natural Company")) + end + + def test_transactions_when_adding_to_new_record + Client.expects(:transaction).never + firm = Firm.new + firm.clients_of_firm.concat(Client.new("name" => "Natural Company")) + end + def test_new_aliased_to_build company = companies(:first_firm) new_client = assert_no_queries { company.clients_of_firm.new("name" => "Another Client") } @@ -778,6 +790,21 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 0, companies(:first_firm).clients_of_firm(true).size end + def test_transaction_when_deleting_persisted + force_signal37_to_load_all_clients_of_firm + client = companies(:first_firm).clients_of_firm.create("name" => "Another Client") + Client.expects(:transaction) + companies(:first_firm).clients_of_firm.delete(client) + end + + def test_transaction_when_deleting_new_record + client = Client.new("name" => "New Client") + firm = Firm.new + firm.clients_of_firm << client + Client.expects(:transaction).never + firm.clients_of_firm.delete(client) + end + def test_clearing_an_association_collection firm = companies(:first_firm) client_id = firm.clients_of_firm.first.id @@ -1111,6 +1138,24 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal orig_accounts, firm.accounts end + def test_transactions_when_replacing_on_persisted + firm = Firm.find(:first, :order => "id") + firm.clients = [companies(:first_client)] + assert firm.save, "Could not save firm" + firm.reload + + Client.expects(:transaction) + firm.clients_of_firm = [Client.new("name" => "Natural Company")] + end + + def test_transactions_when_replacing_on_new_record + firm = Firm.new + firm.clients_of_firm << Client.new("name" => "Natural Company") + + Client.expects(:transaction).never + firm.clients_of_firm = [Client.new("name" => "New Client")] + end + def test_get_ids assert_equal [companies(:first_client).id, companies(:second_client).id], companies(:first_firm).client_ids end -- cgit v1.2.3