test/paranoia_test.rb in paranoia-2.0.4 vs test/paranoia_test.rb in paranoia-2.0.5

- old
+ new

@@ -1,6 +1,7 @@ require 'active_record' +ActiveRecord::Base.raise_in_transactional_callbacks = true if ActiveRecord::VERSION::STRING >= '4.2' test_framework = if ActiveRecord::VERSION::STRING >= "4.1" require 'minitest/autorun' MiniTest::Test else @@ -18,10 +19,11 @@ 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 paranoid_model_with_anthor_class_name_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 paranoid_model_with_foreign_key_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, has_one_foreign_key_id INTEGER)' + ActiveRecord::Base.connection.execute 'CREATE TABLE not_paranoid_model_with_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, 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)' @@ -30,10 +32,11 @@ 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)' ActiveRecord::Base.connection.execute 'CREATE TABLE custom_sentinel_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME NOT NULL)' ActiveRecord::Base.connection.execute 'CREATE TABLE non_paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER)' + ActiveRecord::Base.connection.execute 'CREATE TABLE polymorphic_models (id INTEGER NOT NULL PRIMARY KEY, parent_id INTEGER, parent_type STRING, deleted_at DATETIME)' end class WithDifferentConnection < ActiveRecord::Base establish_connection adapter: 'sqlite3', database: ':memory:' connection.execute 'CREATE TABLE with_different_connections (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)' @@ -452,13 +455,16 @@ # setup and destroy test objects hasOne = ParanoidModelWithHasOne.create belongsTo = ParanoidModelWithBelong.create anthorClassName = ParanoidModelWithAnthorClassNameBelong.create foreignKey = ParanoidModelWithForeignKeyBelong.create + notParanoidModel = NotParanoidModelWithBelong.create + hasOne.paranoid_model_with_belong = belongsTo hasOne.class_name_belong = anthorClassName hasOne.paranoid_model_with_foreign_key_belong = foreignKey + hasOne.not_paranoid_model_with_belong = notParanoidModel hasOne.save! hasOne.destroy assert_equal false, hasOne.deleted_at.nil? assert_equal false, belongsTo.deleted_at.nil? @@ -467,10 +473,11 @@ 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_equal true, notParanoidModel.destroyed? assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record" assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record" assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record" end @@ -478,13 +485,16 @@ # setup and destroy test objects hasOne = ParanoidModelWithHasOne.create belongsTo = ParanoidModelWithBelong.create anthorClassName = ParanoidModelWithAnthorClassNameBelong.create foreignKey = ParanoidModelWithForeignKeyBelong.create + notParanoidModel = NotParanoidModelWithBelong.create + hasOne.paranoid_model_with_belong = belongsTo hasOne.class_name_belong = anthorClassName hasOne.paranoid_model_with_foreign_key_belong = foreignKey + hasOne.not_paranoid_model_with_belong = notParanoidModel hasOne.save! hasOne.destroy assert_equal false, hasOne.deleted_at.nil? assert_equal false, belongsTo.deleted_at.nil? @@ -494,10 +504,11 @@ newHasOne.restore(:recursive => true) newHasOne.save! assert_equal true, hasOne.reload.deleted_at.nil? assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}" + assert_equal true, notParanoidModel.destroyed? assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record" assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record" assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record" end @@ -505,13 +516,16 @@ # setup and destroy test objects hasOne = ParanoidModelWithHasOne.create belongsTo = ParanoidModelWithBelong.create anthorClassName = ParanoidModelWithAnthorClassNameBelong.create foreignKey = ParanoidModelWithForeignKeyBelong.create + notParanoidModel = NotParanoidModelWithBelong.create + hasOne.paranoid_model_with_belong = belongsTo hasOne.class_name_belong = anthorClassName hasOne.paranoid_model_with_foreign_key_belong = foreignKey + hasOne.not_paranoid_model_with_belong = notParanoidModel hasOne.save! hasOne.destroy assert_equal false, hasOne.deleted_at.nil? assert_equal false, belongsTo.deleted_at.nil? @@ -520,10 +534,11 @@ ParanoidModelWithHasOne.restore(hasOne.id, :recursive => true) hasOne.save! assert_equal true, hasOne.reload.deleted_at.nil? assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}" + assert_equal true, notParanoidModel.destroyed? assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record" assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record" assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record" end @@ -533,11 +548,11 @@ 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 # covers #185 def test_restoring_recursive_has_one_restores_correct_object @@ -547,11 +562,11 @@ hasOnes[0].update paranoid_model_with_belong: belongsTos[0] hasOnes[1].update paranoid_model_with_belong: belongsTos[1] hasOnes.each(&:destroy) - ParanoidModelWithHasOne.restore(hasOnes[1], :recursive => true) + ParanoidModelWithHasOne.restore(hasOnes[1].id, :recursive => true) hasOnes.each(&:reload) belongsTos.each(&:reload) # without #185, belongsTos[0] will be restored instead of belongsTos[1] refute_nil hasOnes[0].deleted_at @@ -562,18 +577,18 @@ # covers #131 def test_has_one_really_destroy_with_nil model = ParanoidModelWithHasOne.create model.really_destroy! - + refute ParanoidModelWithBelong.unscoped.exists?(model.id) end - + def test_has_one_really_destroy_with_record model = ParanoidModelWithHasOne.create { |record| record.build_paranoid_model_with_belong } model.really_destroy! - + refute ParanoidModelWithBelong.unscoped.exists?(model.id) end def test_observers_notified a = ParanoidModelWithObservers.create @@ -590,16 +605,18 @@ a.restore! # essentially, we're just ensuring that this doesn't crash end def test_i_am_the_destroyer - output = capture(:stdout) { ParanoidModel.I_AM_THE_DESTROYER! } - assert_equal %Q{ + expected = %Q{ Sharon: "There should be a method called I_AM_THE_DESTROYER!" Ryan: "What should this method do?" Sharon: "It should fix all the spelling errors on the page!" -}, output +} + assert_output expected do + ParanoidModel.I_AM_THE_DESTROYER! + end end def test_destroy_fails_if_callback_raises_exception parent = AsplodeModel.create @@ -652,10 +669,38 @@ NoConnectionModel.class_eval{ acts_as_paranoid } ensure setup! end + def test_restore_recursive_on_polymorphic_has_one_association + parent = ParentModel.create + polymorphic = PolymorphicModel.create(parent: parent) + + parent.destroy + + assert_equal 0, polymorphic.class.count + + parent.restore(recursive: true) + + assert_equal 1, polymorphic.class.count + end + + # Ensure that we're checking parent_type when restoring + def test_missing_restore_recursive_on_polymorphic_has_one_association + parent = ParentModel.create + polymorphic = PolymorphicModel.create(parent_id: parent.id, parent_type: 'ParanoidModel') + + parent.destroy + polymorphic.destroy + + assert_equal 0, polymorphic.class.count + + parent.restore(recursive: true) + + assert_equal 0, polymorphic.class.count + end + private def get_featureful_model FeaturefulModel.new(:name => "not empty") end end @@ -704,10 +749,11 @@ has_many :paranoid_models has_many :related_models has_many :very_related_models, :class_name => 'RelatedModel', dependent: :destroy has_many :non_paranoid_models, dependent: :destroy has_many :asplode_models, dependent: :destroy + has_one :polymorphic_model, as: :parent, dependent: :destroy end class RelatedModel < ActiveRecord::Base acts_as_paranoid belongs_to :parent_model @@ -759,10 +805,11 @@ # refer back to regression test for #118 class ParanoidModelWithHasOne < ParanoidModel has_one :paranoid_model_with_belong, :dependent => :destroy has_one :class_name_belong, :dependent => :destroy, :class_name => "ParanoidModelWithAnthorClassNameBelong" has_one :paranoid_model_with_foreign_key_belong, :dependent => :destroy, :foreign_key => "has_one_foreign_key_id" + has_one :not_paranoid_model_with_belong, :dependent => :destroy end class ParanoidModelWithBelong < ActiveRecord::Base acts_as_paranoid belongs_to :paranoid_model_with_has_one @@ -776,22 +823,33 @@ class ParanoidModelWithForeignKeyBelong < ActiveRecord::Base acts_as_paranoid belongs_to :paranoid_model_with_has_one end +class NotParanoidModelWithBelong < ActiveRecord::Base + belongs_to :paranoid_model_with_has_one +end + class FlaggedModel < PlainModel acts_as_paranoid :flag_column => :is_deleted end class FlaggedModelWithCustomIndex < PlainModel acts_as_paranoid :flag_column => :is_deleted, :indexed_column => :is_deleted end + + class AsplodeModel < ActiveRecord::Base acts_as_paranoid before_destroy do |r| raise StandardError, 'ASPLODE!' end end class NoConnectionModel < ActiveRecord::Base +end + +class PolymorphicModel < ActiveRecord::Base + acts_as_paranoid + belongs_to :parent, polymorphic: true end