test/paranoia_test.rb in paranoia-2.2.1 vs test/paranoia_test.rb in paranoia-2.3.0
- old
+ new
@@ -19,10 +19,11 @@
'paranoid_model_with_build_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_and_build_id INTEGER, name VARCHAR(32)',
'paranoid_model_with_anthor_class_name_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER',
'paranoid_model_with_foreign_key_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, has_one_foreign_key_id INTEGER',
'paranoid_model_with_timestamps' => 'parent_model_id INTEGER, created_at DATETIME, updated_at DATETIME, deleted_at DATETIME',
'not_paranoid_model_with_belongs' => 'parent_model_id INTEGER, paranoid_model_with_has_one_id INTEGER',
+ 'not_paranoid_model_with_belongs_and_assocation_not_soft_destroyed_validator' => 'parent_model_id INTEGER, paranoid_model_with_has_one_id INTEGER',
'paranoid_model_with_has_one_and_builds' => 'parent_model_id INTEGER, color VARCHAR(32), deleted_at DATETIME, has_one_foreign_key_id INTEGER',
'featureful_models' => 'deleted_at DATETIME, name VARCHAR(32)',
'plain_models' => 'deleted_at DATETIME',
'callback_models' => 'deleted_at DATETIME',
'fail_callback_models' => 'deleted_at DATETIME',
@@ -115,13 +116,13 @@
model = CallbackModel.new
model.save
model.remove_called_variables # clear called callback flags
model.destroy
- assert_equal nil, model.instance_variable_get(:@update_callback_called)
- assert_equal nil, model.instance_variable_get(:@save_callback_called)
- assert_equal nil, model.instance_variable_get(:@validate_called)
+ assert_nil model.instance_variable_get(:@update_callback_called)
+ assert_nil model.instance_variable_get(:@save_callback_called)
+ assert_nil model.instance_variable_get(:@validate_called)
assert model.instance_variable_get(:@destroy_callback_called)
assert model.instance_variable_get(:@after_destroy_callback_called)
assert model.instance_variable_get(:@after_commit_callback_called)
end
@@ -131,31 +132,31 @@
model = CallbackModel.new
model.save
model.remove_called_variables # clear called callback flags
model.delete
- assert_equal nil, model.instance_variable_get(:@update_callback_called)
- assert_equal nil, model.instance_variable_get(:@save_callback_called)
- assert_equal nil, model.instance_variable_get(:@validate_called)
- assert_equal nil, model.instance_variable_get(:@destroy_callback_called)
- assert_equal nil, model.instance_variable_get(:@after_destroy_callback_called)
- assert_equal nil, model.instance_variable_get(:@after_commit_callback_called)
+ assert_nil model.instance_variable_get(:@update_callback_called)
+ assert_nil model.instance_variable_get(:@save_callback_called)
+ assert_nil model.instance_variable_get(:@validate_called)
+ assert_nil model.instance_variable_get(:@destroy_callback_called)
+ assert_nil model.instance_variable_get(:@after_destroy_callback_called)
+ assert_nil model.instance_variable_get(:@after_commit_callback_called)
end
def test_delete_in_transaction_behavior_for_plain_models_callbacks
model = CallbackModel.new
model.save
model.remove_called_variables # clear called callback flags
CallbackModel.transaction do
model.delete
end
- assert_equal nil, model.instance_variable_get(:@update_callback_called)
- assert_equal nil, model.instance_variable_get(:@save_callback_called)
- assert_equal nil, model.instance_variable_get(:@validate_called)
- assert_equal nil, model.instance_variable_get(:@destroy_callback_called)
- assert_equal nil, model.instance_variable_get(:@after_destroy_callback_called)
+ assert_nil model.instance_variable_get(:@update_callback_called)
+ assert_nil model.instance_variable_get(:@save_callback_called)
+ assert_nil model.instance_variable_get(:@validate_called)
+ assert_nil model.instance_variable_get(:@destroy_callback_called)
+ assert_nil model.instance_variable_get(:@after_destroy_callback_called)
assert model.instance_variable_get(:@after_commit_callback_called)
end
def test_destroy_behavior_for_paranoid_models
model = ParanoidModel.new
@@ -224,11 +225,11 @@
assert_equal 1, model.class.only_deleted.count
assert_equal 1, model.class.deleted.count
end
def test_default_sentinel_value
- assert_equal nil, ParanoidModel.paranoia_sentinel_value
+ assert_nil ParanoidModel.paranoia_sentinel_value
end
def test_without_default_scope_option
model = WithoutDefaultScopeModel.create
model.destroy
@@ -373,11 +374,11 @@
def test_delete_behavior_for_callbacks
model = CallbackModel.new
model.save
model.delete
- assert_equal nil, model.instance_variable_get(:@destroy_callback_called)
+ assert_nil model.instance_variable_get(:@destroy_callback_called)
end
def test_destroy_behavior_for_callbacks
model = CallbackModel.new
model.save
@@ -578,10 +579,42 @@
refute a.paranoia_destroyed?
assert b.paranoia_destroyed?
refute c.paranoia_destroyed?
end
+ def test_restore_with_associations_using_recovery_window
+ parent = ParentModel.create
+ first_child = parent.very_related_models.create
+ second_child = parent.very_related_models.create
+
+ parent.destroy
+ second_child.update(deleted_at: parent.deleted_at + 11.minutes)
+
+ parent.restore!(:recursive => true)
+ assert_equal true, parent.deleted_at.nil?
+ assert_equal true, first_child.reload.deleted_at.nil?
+ assert_equal true, second_child.reload.deleted_at.nil?
+
+ parent.destroy
+ second_child.update(deleted_at: parent.deleted_at + 11.minutes)
+
+ parent.restore(:recursive => true, :recovery_window => 10.minutes)
+ assert_equal true, parent.deleted_at.nil?
+ assert_equal true, first_child.reload.deleted_at.nil?
+ assert_equal false, second_child.reload.deleted_at.nil?
+
+ second_child.restore
+ parent.destroy
+ first_child.update(deleted_at: parent.deleted_at - 11.minutes)
+ second_child.update(deleted_at: parent.deleted_at - 9.minutes)
+
+ ParentModel.restore(parent.id, :recursive => true, :recovery_window => 10.minutes)
+ assert_equal true, parent.reload.deleted_at.nil?
+ assert_equal false, first_child.reload.deleted_at.nil?
+ assert_equal true, second_child.reload.deleted_at.nil?
+ end
+
def test_restore_with_associations
parent = ParentModel.create
first_child = parent.very_related_models.create
second_child = parent.non_paranoid_models.create
@@ -914,12 +947,12 @@
def test_callbacks_for_counter_cache_column_update_on_destroy
parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
related_model = parent_model_with_counter_cache_column.related_models.create
- assert_equal nil, related_model.instance_variable_get(:@after_destroy_callback_called)
- assert_equal nil, related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
+ assert_nil related_model.instance_variable_get(:@after_destroy_callback_called)
+ assert_nil related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
related_model.destroy
assert related_model.instance_variable_get(:@after_destroy_callback_called)
# assert related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
@@ -930,10 +963,22 @@
related = parent_model.unparanoid_unique_models.create
# will raise exception if model is not checked for paranoia
related.valid?
end
+ def test_assocation_not_soft_destroyed_validator
+ notParanoidModel = NotParanoidModelWithBelongsAndAssocationNotSoftDestroyedValidator.create
+ parentModel = ParentModel.create
+ assert notParanoidModel.valid?
+
+ notParanoidModel.parent_model = parentModel
+ assert notParanoidModel.valid?
+ parentModel.destroy
+ assert !notParanoidModel.valid?
+ assert notParanoidModel.errors.full_messages.include? "Parent model has been soft-deleted"
+ end
+
# TODO: find a fix for Rails 4.1
if ActiveRecord::VERSION::STRING !~ /\A4\.1/
def test_counter_cache_column_update_on_really_destroy
parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
related_model = parent_model_with_counter_cache_column.related_models.create
@@ -948,18 +993,56 @@
if ActiveRecord::VERSION::STRING >= '4.2'
def test_callbacks_for_counter_cache_column_update_on_really_destroy!
parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
related_model = parent_model_with_counter_cache_column.related_models.create
- assert_equal nil, related_model.instance_variable_get(:@after_destroy_callback_called)
- assert_equal nil, related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
+ assert_nil related_model.instance_variable_get(:@after_destroy_callback_called)
+ assert_nil related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
related_model.really_destroy!
assert related_model.instance_variable_get(:@after_destroy_callback_called)
assert related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
end
+
+ def test_counter_cache_column_on_double_destroy
+ parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
+ related_model = parent_model_with_counter_cache_column.related_models.create
+
+ related_model.destroy
+ related_model.destroy
+ assert_equal 0, parent_model_with_counter_cache_column.reload.related_models_count
+ end
+
+ def test_counter_cache_column_on_double_restore
+ parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
+ related_model = parent_model_with_counter_cache_column.related_models.create
+
+ related_model.destroy
+ related_model.restore
+ related_model.restore
+ assert_equal 1, parent_model_with_counter_cache_column.reload.related_models_count
+ end
+
+ def test_counter_cache_column_on_destroy_and_really_destroy
+ parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
+ related_model = parent_model_with_counter_cache_column.related_models.create
+
+ related_model.destroy
+ related_model.really_destroy!
+ assert_equal 0, parent_model_with_counter_cache_column.reload.related_models_count
+ end
+
+ def test_counter_cache_column_on_restore
+ parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
+ related_model = parent_model_with_counter_cache_column.related_models.create
+
+ related_model.destroy
+ assert_equal 0, parent_model_with_counter_cache_column.reload.related_models_count
+ related_model.restore
+ assert_equal 1, parent_model_with_counter_cache_column.reload.related_models_count
+ end
end
private
def get_featureful_model
FeaturefulModel.new(:name => "not empty")
@@ -1229,9 +1312,14 @@
acts_as_paranoid
end
class NotParanoidModelWithBelong < ActiveRecord::Base
belongs_to :paranoid_model_with_has_one
+end
+
+class NotParanoidModelWithBelongsAndAssocationNotSoftDestroyedValidator < NotParanoidModelWithBelong
+ belongs_to :parent_model
+ validates :parent_model, association_not_soft_destroyed: true
end
class FlaggedModel < PlainModel
acts_as_paranoid :flag_column => :is_deleted
end