From ea87874dc1059915f517ca3ef67a01620b8e8aa7 Mon Sep 17 00:00:00 2001 From: Ryunosuke Sato Date: Wed, 5 Jan 2022 21:11:13 +0900 Subject: [PATCH] Add failing tests for association with abort on destroy --- test/paranoia_test.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb index 84e60fcf..81f80c6a 100644 --- a/test/paranoia_test.rb +++ b/test/paranoia_test.rb @@ -31,6 +31,7 @@ def setup! 'plain_models' => 'deleted_at DATETIME', 'callback_models' => 'deleted_at DATETIME', 'fail_callback_models' => 'deleted_at DATETIME', + 'association_with_abort_models' => 'deleted_at DATETIME', 'related_models' => 'parent_model_id INTEGER, parent_model_with_counter_cache_column_id INTEGER, deleted_at DATETIME', 'asplode_models' => 'parent_model_id INTEGER, deleted_at DATETIME', 'employers' => 'name VARCHAR(32), deleted_at DATETIME', @@ -131,6 +132,35 @@ def test_destroy_behavior_for_plain_models_callbacks assert model.instance_variable_get(:@after_commit_callback_called) end + def test_destroy_behavior_for_association_with_abort + model = AssociationWithAbortModel.new + model.related_models.build + model.save + + assert_equal model.reload.related_models.count, 1 + + model = AssociationWithAbortModel.find(model.id) + return_value = model.destroy + + assert_equal return_value, false + assert_equal model.reload.related_models.count, 1 + end + + def test_destroy_bang_behavior_for_association_with_abort + model = AssociationWithAbortModel.new + model.related_models.build + model.save + + assert_equal model.reload.related_models.count, 1 + + model = AssociationWithAbortModel.find(model.id) + assert_raises ActiveRecord::RecordNotDestroyed do + model.destroy! + end + + assert_equal model.reload.related_models.count, 1 + end + def test_destroy_behavior_for_freshly_loaded_plain_models_callbacks model = CallbackModel.new model.save @@ -1136,6 +1166,18 @@ def remove_called_variables end end +class AssociationWithAbortModel < ActiveRecord::Base + acts_as_paranoid + has_many :related_models, class_name: 'RelatedModel', foreign_key: :parent_model_id, dependent: :destroy + before_destroy { |_| + if ActiveRecord::VERSION::MAJOR < 5 + false + else + throw :abort + end + } +end + class ParentModel < ActiveRecord::Base acts_as_paranoid has_many :paranoid_models