test/paranoia_test.rb in paranoia-1.3.3 vs test/paranoia_test.rb in paranoia-1.3.4

- old
+ new

@@ -8,13 +8,15 @@ 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 paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME)' +ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_model_with_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER)' 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)' ActiveRecord::Base.connection.execute 'CREATE TABLE callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)' +ActiveRecord::Base.connection.execute 'CREATE TABLE fail_callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE related_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER NOT NULL, deleted_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE employers (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE employees (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE jobs (id INTEGER NOT NULL PRIMARY KEY, employer_id INTEGER NOT NULL, employee_id INTEGER NOT NULL, deleted_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE custom_column_models (id INTEGER NOT NULL PRIMARY KEY, destroyed_at DATETIME)' @@ -98,10 +100,11 @@ model.save! assert_equal 1, model.class.count model.destroy assert_equal false, model.deleted_at.nil? + assert_equal false, model.really_destroyed? assert_equal 0, model.class.count assert_equal 1, model.class.unscoped.count end @@ -248,10 +251,24 @@ model.destroy assert_equal 1, ParanoidModel.unscoped.where(id: model.id).count end + def test_destroy_return_value_on_success + model = ParanoidModel.create + return_value = model.destroy + + assert_equal(return_value, model) + end + + def test_destroy_return_value_on_failure + model = FailCallbackModel.create + return_value = model.destroy + + assert_equal(return_value, false) + end + def test_restore_behavior_for_callbacks model = CallbackModel.new model.save id = model.id model.destroy @@ -263,18 +280,52 @@ model.reload assert model.instance_variable_get(:@restore_callback_called) end - def test_real_destroy + def test_really_destroy model = ParanoidModel.new model.save model.destroy! assert_equal 0, ParanoidModel.unscoped.where(id: model.id).count end + def test_really_destroyed + model = ParanoidModel.new + model.save + model.destroy! + + assert model.really_destroyed? + end + + def test_real_destroy_dependent_destroy + parent = ParentModel.create + child = parent.very_related_models.create + parent.really_destroy! + refute RelatedModel.unscoped.exists?(child.id) + end + + def test_real_destroy_dependent_destroy_after_normal_destroy + parent = ParentModel.create + child = parent.very_related_models.create + parent.destroy + parent.really_destroy! + refute RelatedModel.unscoped.exists?(child.id) + end + + def test_real_destroy_dependent_destroy_after_normal_destroy_does_not_delete_other_children + parent_1 = ParentModel.create + child_1 = parent_1.very_related_models.create + + parent_2 = ParentModel.create + child_2 = parent_2.very_related_models.create + parent_1.destroy + parent_1.really_destroy! + assert RelatedModel.unscoped.exists?(child_2.id) + end + def test_real_delete model = ParanoidModel.new model.save model.delete! assert_equal 0, ParanoidModel.unscoped.where(id: model.id).count @@ -333,10 +384,43 @@ assert_equal true, parent.reload.deleted_at.nil? assert_equal true, first_child.reload.deleted_at.nil? assert_equal true, second_child.destroyed? end + # regression tests for #118 + def test_restore_with_has_one_association + # setup and destroy test objects + hasOne = ParanoidModelWithHasOne.create + belongsTo = ParanoidModelWithBelong.create + hasOne.paranoid_model_with_belong = belongsTo + hasOne.save! + + hasOne.destroy + assert_equal false, hasOne.deleted_at.nil? + assert_equal false, belongsTo.deleted_at.nil? + + # Does it restore has_one associations? + hasOne.restore(:recursive => true) + hasOne.save! + + assert_equal true, hasOne.reload.deleted_at.nil? + assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}" + assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record" + end + + def test_restore_with_nil_has_one_association + # setup and destroy test object + hasOne = ParanoidModelWithHasOne.create + hasOne.destroy + assert_equal false, hasOne.reload.deleted_at.nil? + + # Does it raise NoMethodException on restore of nil + hasOne.restore(:recursive => true) + + assert hasOne.reload.deleted_at.nil? + end + def test_observers_notified a = ParanoidModelWithObservers.create a.destroy a.restore! @@ -366,10 +450,17 @@ class ParanoidModel < ActiveRecord::Base belongs_to :parent_model acts_as_paranoid end +class FailCallbackModel < ActiveRecord::Base + belongs_to :parent_model + acts_as_paranoid + + before_destroy { |_| false } +end + class FeaturefulModel < ActiveRecord::Base acts_as_paranoid validates :name, :presence => true, :uniqueness => true end @@ -440,6 +531,16 @@ end end class ParanoidModelWithoutObservers < ParanoidModel self.class.send(remove_method :notify_observers) if method_defined?(:notify_observers) +end + +# refer back to regression test for #118 +class ParanoidModelWithHasOne < ParanoidModel + has_one :paranoid_model_with_belong, :dependent => :destroy +end + +class ParanoidModelWithBelong < ActiveRecord::Base + acts_as_paranoid + belongs_to :paranoid_model_with_has_one end