Skip to content

Commit

Permalink
Add failing tests for association with abort on destroy (#522)
Browse files Browse the repository at this point in the history
Co-authored-by: Mathieu Jobin <[email protected]>
  • Loading branch information
tricknotes and mathieujobin authored Mar 23, 2022
1 parent cd60ab2 commit 2af6700
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def setup!
'callback_models' => 'deleted_at DATETIME',
'after_commit_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',
Expand Down Expand Up @@ -143,6 +144,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
Expand Down Expand Up @@ -1211,6 +1241,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 AfterCommitCallbackModel < ActiveRecord::Base
acts_as_paranoid

Expand Down

0 comments on commit 2af6700

Please sign in to comment.