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

Added test for failing destroy on parent model destroying very related models anyway #110

Open
wants to merge 2 commits into
base: rails3
Choose a base branch
from
Open
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
29 changes: 28 additions & 1 deletion test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
FileUtils.rm_f DB_FILE

ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_FILE
ActiveRecord::Base.connection.execute 'CREATE TABLE parent_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE parent_models (id INTEGER NOT NULL PRIMARY KEY, type VARCHAR(50), deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE featureful_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME, name VARCHAR(32))'
ActiveRecord::Base.connection.execute 'CREATE TABLE plain_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
Expand Down Expand Up @@ -78,6 +78,28 @@ def test_destroy_behavior_for_plain_models_callbacks
assert model.instance_variable_get(:@after_commit_callback_called)
end

def test_failing_destroy_on_parent_model_does_not_destroy_very_related_model
model = FailDestroyParentModel.new
very_related_model = RelatedModel.new
model.very_related_models << very_related_model
model.save
model.destroy
assert !model.destroyed?
assert !very_related_model.destroyed?
end

def test_join_applies_paranoid_behavior_to_both_models
ParentModel.unscoped.delete_all
assert_equal 0, ParentModel.count
model = ParentModel.create
model.related_models.create
model.related_models.create
first_related_model_id = model.related_models.first.id

assert_equal 1, ParentModel.joins(:related_models).where(related_models: {id: first_related_model_id}).count
model.related_models.first.destroy
assert_equal 0, ParentModel.joins(:related_models).where(related_models: {id: first_related_model_id}).count
end

def test_delete_behavior_for_plain_models_callbacks
model = CallbackModel.new
Expand Down Expand Up @@ -427,6 +449,11 @@ class RelatedModel < ActiveRecord::Base
belongs_to :parent_model
end

class FailDestroyParentModel < ParentModel
acts_as_paranoid
before_destroy { |_| false }
end

class Employer < ActiveRecord::Base
acts_as_paranoid
has_many :jobs
Expand Down