test/paranoia_test.rb in paranoia-1.3.1 vs test/paranoia_test.rb in paranoia-1.3.2

- old
+ new

@@ -1,8 +1,8 @@ require 'test/unit' require 'active_record' -require File.expand_path(File.dirname(__FILE__) + "/../lib/paranoia") +require File.expand_path(File.dirname(__FILE__) + '/../lib/paranoia') DB_FILE = 'tmp/test_db' FileUtils.mkdir_p File.dirname(DB_FILE) FileUtils.rm_f DB_FILE @@ -69,24 +69,25 @@ 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 + assert_equal [p1, p3], parent1.paranoid_models.with_deleted end def test_destroy_behavior_for_featureful_paranoid_models model = get_featureful_model assert_equal 0, model.class.count @@ -100,13 +101,12 @@ assert_equal 1, model.class.unscoped.count end # Regression test for #24 def test_chaining_for_paranoid_models - scope = FeaturefulModel.where(:name => "foo").only_deleted - - assert_equal "foo", scope.where_values_hash["name"] + scope = FeaturefulModel.where(:name => 'foo').only_deleted + assert_equal 'foo', scope.where_values_hash['name'] assert_equal 2, scope.where_values.count end def test_only_destroyed_scope_for_paranoid_models model = ParanoidModel.new @@ -115,10 +115,11 @@ model2 = ParanoidModel.new model2.save assert_equal model, ParanoidModel.only_deleted.last assert_equal false, ParanoidModel.only_deleted.include?(model2) + assert_equal false, ParanoidModel.deleted.include?(model2) end def test_default_scope_for_has_many_relationships parent = ParentModel.create assert_equal 0, parent.related_models.count @@ -187,29 +188,79 @@ 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! - assert_equal false, !!ParanoidModel.unscoped.exists?(model.id) + assert_equal 0, ParanoidModel.unscoped.where(id: model.id).count end def test_real_delete model = ParanoidModel.new model.save model.delete! + assert_equal 0, ParanoidModel.unscoped.where(id: model.id).count + end - assert_equal false, !!ParanoidModel.unscoped.exists?(model.id) + 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") + FeaturefulModel.new(:name => 'not empty') end end # Helper classes @@ -231,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