test/paranoia_test.rb in paranoia-2.0.0 vs test/paranoia_test.rb in paranoia-2.0.1
- old
+ new
@@ -69,21 +69,22 @@
assert_equal false, model.deleted_at.nil?
assert_equal 0, model.class.count
assert_equal 1, model.class.unscoped.count
end
-
+
def test_scoping_behavior_for_paranoid_models
ParanoidModel.unscoped.delete_all
parent1 = ParentModel.create
parent2 = ParentModel.create
p1 = ParanoidModel.create(:parent_model => parent1)
p2 = ParanoidModel.create(:parent_model => parent2)
p1.destroy
p2.destroy
assert_equal 0, parent1.paranoid_models.count
assert_equal 1, parent1.paranoid_models.only_deleted.count
+ assert_equal 1, parent1.paranoid_models.deleted.count
p3 = ParanoidModel.create(:parent_model => parent1)
assert_equal 2, parent1.paranoid_models.with_deleted.count
assert_equal [p1,p3], parent1.paranoid_models.with_deleted
end
@@ -186,10 +187,34 @@
model.reload
assert_equal false, model.destroyed?
end
+ def test_destroy_twice
+ model = ParanoidModel.new
+ model.save
+ model.destroy
+ model.destroy
+
+ assert_equal 0, ParanoidModel.unscoped.where(id: model.id).count
+ end
+
+ def test_restore_behavior_for_callbacks
+ model = CallbackModel.new
+ model.save
+ id = model.id
+ model.destroy
+
+ assert model.destroyed?
+
+ model = CallbackModel.only_deleted.find(id)
+ model.restore!
+ model.reload
+
+ assert model.instance_variable_get(:@restore_callback_called)
+ end
+
def test_real_destroy
model = ParanoidModel.new
model.save
model.destroy!
@@ -202,10 +227,37 @@
model.delete!
refute ParanoidModel.unscoped.exists?(model.id)
end
+ def test_multiple_restore
+ a = ParanoidModel.new
+ a.save
+ a_id = a.id
+ a.destroy
+
+ b = ParanoidModel.new
+ b.save
+ b_id = b.id
+ b.destroy
+
+ c = ParanoidModel.new
+ c.save
+ c_id = c.id
+ c.destroy
+
+ ParanoidModel.restore([a_id, c_id])
+
+ a.reload
+ b.reload
+ c.reload
+
+ refute a.destroyed?
+ assert b.destroyed?
+ refute c.destroyed?
+ end
+
private
def get_featureful_model
FeaturefulModel.new(:name => "not empty")
end
end
@@ -230,9 +282,10 @@
end
class CallbackModel < ActiveRecord::Base
acts_as_paranoid
before_destroy {|model| model.instance_variable_set :@callback_called, true }
+ before_restore {|model| model.instance_variable_set :@restore_callback_called, true }
end
class ParentModel < ActiveRecord::Base
acts_as_paranoid
has_many :related_models