From 2d3bc99b0d4d75209445b8f352e0b51b0d18a4c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 8 Jul 2010 13:43:41 +0200 Subject: [PATCH] test that ActiveRecord `destroy` and `destroy_all` return destroyed records Signed-off-by: Jeremy Kemper --- .../associations/association_collection.rb | 5 ++-- .../has_many_associations_test.rb | 7 ++++-- activerecord/test/cases/base_test.rb | 24 ++++++++++++------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 5972c10f3e258..2855073485ce6 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -234,8 +234,9 @@ def clear # See destroy for more info. def destroy_all load_target - destroy(@target) - reset_target! + destroy(@target).tap do + reset_target! + end end def create(attrs = {}) diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 49eca305f4d64..8a58972d1a2a9 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -834,8 +834,11 @@ def test_destroying_a_collection def test_destroy_all force_signal37_to_load_all_clients_of_firm - assert !companies(:first_firm).clients_of_firm.empty?, "37signals has clients after load" - companies(:first_firm).clients_of_firm.destroy_all + clients = companies(:first_firm).clients_of_firm.to_a + assert !clients.empty?, "37signals has clients after load" + destroyed = companies(:first_firm).clients_of_firm.destroy_all + assert_equal clients.sort_by(&:id), destroyed.sort_by(&:id) + assert destroyed.all? { |client| client.frozen? }, "destroyed clients should be frozen" assert companies(:first_firm).clients_of_firm.empty?, "37signals has no clients after destroy all" assert companies(:first_firm).clients_of_firm(true).empty?, "37signals has no clients after destroy all and refresh" end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 53aa9bcc2a3d5..e9af77c47a8f0 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -587,17 +587,25 @@ def test_table_name_guesses end def test_destroy_all - original_count = Topic.count - topics_by_mary = Topic.count(:conditions => mary = "author_name = 'Mary'") - - Topic.destroy_all mary - assert_equal original_count - topics_by_mary, Topic.count + conditions = "author_name = 'Mary'" + topics_by_mary = Topic.all(:conditions => conditions, :order => 'id') + assert ! topics_by_mary.empty? + + assert_difference('Topic.count', -topics_by_mary.size) do + destroyed = Topic.destroy_all(conditions).sort_by(&:id) + assert_equal topics_by_mary, destroyed + assert destroyed.all? { |topic| topic.frozen? } + end end def test_destroy_many - assert_equal 3, Client.count - Client.destroy([2, 3]) - assert_equal 1, Client.count + clients = Client.find([2, 3], :order => 'id') + + assert_difference('Client.count', -2) do + destroyed = Client.destroy([2, 3]).sort_by(&:id) + assert_equal clients, destroyed + assert destroyed.all? { |client| client.frozen? } + end end def test_delete_many