From ce7670ce4794c8593c5f35e2fdadf40388758516 Mon Sep 17 00:00:00 2001 From: Sam Figueroa Date: Fri, 16 Oct 2015 08:14:26 +0200 Subject: [PATCH] Spec destroy_all respects paranoid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add a spec to ensure that paranoid models don’t get deleted by destroy_all call. - ActiveRecords current implementation enumerates over the relation and calls &:destroy on each element. If that happens to change sometime in the future this test should notice it. --- test/paranoia_test.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 321c97ea..d7d8cb3d 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -909,6 +909,34 @@ def test_delete_all_for_non_paranoid_model assert_equal 0, model.unscoped.count end + def test_destroy_all + model = ParanoidModel + count = 3 + models = count.times.map { model.new } + assert_equal 0, model.count + models.each(&:save!) + + assert_equal count, model.count + model.destroy_all + + assert_equal 0, model.count + assert_equal 3, model.unscoped.count + end + + def test_destroy_all_for_non_paranoid_model + model = PlainModel + count = 3 + models = count.times.map { model.new } + assert_equal 0, model.count + models.each(&:save!) + + assert_equal count, model.count + model.destroy_all + + assert_equal 0, model.count + assert_equal 0, model.unscoped.count + end + # TODO: find a fix for Rails 4.1 if ActiveRecord::VERSION::STRING !~ /\A4\.1/ def test_counter_cache_column_update_on_really_destroy