@@ -538,15 +538,27 @@ def test_adding_a_collection
538
538
end
539
539
540
540
def test_transactions_when_adding_to_persisted
541
- force_signal37_to_load_all_clients_of_firm
542
- Client . expects ( :transaction )
543
- companies ( :first_firm ) . clients_of_firm . concat ( Client . new ( "name" => "Natural Company" ) )
541
+ good = Client . new ( :name => "Good" )
542
+ bad = Client . new ( :name => "Bad" , :raise_on_save => true )
543
+
544
+ begin
545
+ companies ( :first_firm ) . clients_of_firm . concat ( good , bad )
546
+ rescue Client ::RaisedOnSave
547
+ end
548
+
549
+ assert !companies ( :first_firm ) . clients_of_firm ( true ) . include? ( good )
544
550
end
545
551
546
552
def test_transactions_when_adding_to_new_record
547
- Client . expects ( :transaction ) . never
548
- firm = Firm . new
549
- firm . clients_of_firm . concat ( Client . new ( "name" => "Natural Company" ) )
553
+ prev_ignored_sql = ActiveRecord ::SQLCounter . ignored_sql
554
+ ActiveRecord ::SQLCounter . ignored_sql = [ ]
555
+
556
+ assert_no_queries do
557
+ firm = Firm . new
558
+ firm . clients_of_firm . concat ( Client . new ( "name" => "Natural Company" ) )
559
+ end
560
+ ensure
561
+ ActiveRecord ::SQLCounter . ignored_sql = prev_ignored_sql
550
562
end
551
563
552
564
def test_new_aliased_to_build
@@ -791,18 +803,31 @@ def test_delete_all_with_not_yet_loaded_association_collection
791
803
end
792
804
793
805
def test_transaction_when_deleting_persisted
794
- force_signal37_to_load_all_clients_of_firm
795
- client = companies ( :first_firm ) . clients_of_firm . create ( "name" => "Another Client" )
796
- Client . expects ( :transaction )
797
- companies ( :first_firm ) . clients_of_firm . delete ( client )
806
+ good = Client . new ( :name => "Good" )
807
+ bad = Client . new ( :name => "Bad" , :raise_on_destroy => true )
808
+
809
+ companies ( :first_firm ) . clients_of_firm = [ good , bad ]
810
+
811
+ begin
812
+ companies ( :first_firm ) . clients_of_firm . destroy ( good , bad )
813
+ rescue Client ::RaisedOnDestroy
814
+ end
815
+
816
+ assert_equal [ good , bad ] , companies ( :first_firm ) . clients_of_firm ( true )
798
817
end
799
818
800
819
def test_transaction_when_deleting_new_record
801
- client = Client . new ( "name" => "New Client" )
802
- firm = Firm . new
803
- firm . clients_of_firm << client
804
- Client . expects ( :transaction ) . never
805
- firm . clients_of_firm . delete ( client )
820
+ prev_ignored_sql = ActiveRecord ::SQLCounter . ignored_sql
821
+ ActiveRecord ::SQLCounter . ignored_sql = [ ]
822
+
823
+ assert_no_queries do
824
+ firm = Firm . new
825
+ client = Client . new ( "name" => "New Client" )
826
+ firm . clients_of_firm << client
827
+ firm . clients_of_firm . destroy ( client )
828
+ end
829
+ ensure
830
+ ActiveRecord ::SQLCounter . ignored_sql = prev_ignored_sql
806
831
end
807
832
808
833
def test_clearing_an_association_collection
@@ -1139,21 +1164,29 @@ def test_replace_failure
1139
1164
end
1140
1165
1141
1166
def test_transactions_when_replacing_on_persisted
1142
- firm = Firm . find ( :first , :order => "id" )
1143
- firm . clients = [ companies ( :first_client ) ]
1144
- assert firm . save , "Could not save firm"
1145
- firm . reload
1167
+ good = Client . new ( :name => "Good" )
1168
+ bad = Client . new ( :name => "Bad" , :raise_on_save => true )
1146
1169
1147
- Client . expects ( :transaction )
1148
- firm . clients_of_firm = [ Client . new ( "name" => "Natural Company" ) ]
1170
+ companies ( :first_firm ) . clients_of_firm = [ good ]
1171
+
1172
+ begin
1173
+ companies ( :first_firm ) . clients_of_firm = [ bad ]
1174
+ rescue Client ::RaisedOnSave
1175
+ end
1176
+
1177
+ assert_equal [ good ] , companies ( :first_firm ) . clients_of_firm ( true )
1149
1178
end
1150
1179
1151
1180
def test_transactions_when_replacing_on_new_record
1152
- firm = Firm . new
1153
- firm . clients_of_firm << Client . new ( "name" => "Natural Company" )
1181
+ prev_ignored_sql = ActiveRecord :: SQLCounter . ignored_sql
1182
+ ActiveRecord :: SQLCounter . ignored_sql = [ ]
1154
1183
1155
- Client . expects ( :transaction ) . never
1156
- firm . clients_of_firm = [ Client . new ( "name" => "New Client" ) ]
1184
+ assert_no_queries do
1185
+ firm = Firm . new
1186
+ firm . clients_of_firm = [ Client . new ( "name" => "New Client" ) ]
1187
+ end
1188
+ ensure
1189
+ ActiveRecord ::SQLCounter . ignored_sql = prev_ignored_sql
1157
1190
end
1158
1191
1159
1192
def test_get_ids
0 commit comments