Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DON'T MERGE] Add failing tests for association with abort on destroy #522

Merged
merged 2 commits into from
Mar 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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