diff --git a/test/rails3_acts_as_paranoid_test.rb b/test/rails3_acts_as_paranoid_test.rb index 8a9a8f8..05e3e41 100644 --- a/test/rails3_acts_as_paranoid_test.rb +++ b/test/rails3_acts_as_paranoid_test.rb @@ -172,7 +172,6 @@ def test_hard_destroy_callbacks assert @paranoid_with_callback.called_after_destroy assert @paranoid_with_callback.called_after_commit_on_destroy end - end class ValidatesUniquenessTest < ParanoidBase @@ -195,3 +194,36 @@ def test_should_validate_without_deleted end end end + +class AssociationsTest < ParanoidBase + def test_removal_with_associations + # This test shows that the current implementation doesn't handle + # assciation deletion correctly (when hard deleting via parent-object) + paranoid_company_1 = ParanoidDestroyCompany.create! :name => "ParanoidDestroyCompany #1" + paranoid_company_2 = ParanoidDeleteCompany.create! :name => "ParanoidDestroyCompany #1" + paranoid_company_1.paranoid_products.create! :name => "ParanoidProduct #1" + paranoid_company_2.paranoid_products.create! :name => "ParanoidProduct #2" + + assert_equal 1, ParanoidDestroyCompany.count + assert_equal 1, ParanoidDeleteCompany.count + assert_equal 2, ParanoidProduct.count + + ParanoidDestroyCompany.first.destroy + assert_equal 0, ParanoidDestroyCompany.count + assert_equal 1, ParanoidProduct.count + assert_equal 1, ParanoidDestroyCompany.with_deleted.count + assert_equal 2, ParanoidProduct.with_deleted.count + + ParanoidDestroyCompany.with_deleted.first.destroy! + assert_equal 0, ParanoidDestroyCompany.count + assert_equal 1, ParanoidProduct.count + assert_equal 0, ParanoidDestroyCompany.with_deleted.count + assert_equal 1, ParanoidProduct.with_deleted.count + + ParanoidDeleteCompany.with_deleted.first.destroy! + assert_equal 0, ParanoidDeleteCompany.count + assert_equal 0, ParanoidProduct.count + assert_equal 0, ParanoidDeleteCompany.with_deleted.count + assert_equal 0, ParanoidProduct.with_deleted.count + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 90b85b1..8867659 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -18,7 +18,6 @@ def setup_db t.string :name t.datetime :deleted_at t.integer :paranoid_belongs_dependant_id - t.integer :not_paranoid_id t.timestamps @@ -34,15 +33,13 @@ def setup_db create_table :not_paranoids do |t| t.string :name - t.integer :paranoid_time_id - + t.timestamps end create_table :has_one_not_paranoids do |t| t.string :name - t.integer :paranoid_time_id t.timestamps @@ -78,6 +75,29 @@ def setup_db t.timestamps end + + create_table :paranoid_destroy_companies do |t| + t.string :name + t.datetime :deleted_at + + t.timestamps + end + + create_table :paranoid_delete_companies do |t| + t.string :name + t.datetime :deleted_at + + t.timestamps + end + + create_table :paranoid_products do |t| + t.integer :paranoid_destroy_company_id + t.integer :paranoid_delete_company_id + t.string :name + t.datetime :deleted_at + + t.timestamps + end end end @@ -160,5 +180,23 @@ def call_me_after_destroy def call_me_after_commit_on_destroy @called_after_commit_on_destroy = true end - +end + +class ParanoidDestroyCompany < ActiveRecord::Base + acts_as_paranoid + validates :name, :presence => true + has_many :paranoid_products, :dependent => :destroy +end + +class ParanoidDeleteCompany < ActiveRecord::Base + acts_as_paranoid + validates :name, :presence => true + has_many :paranoid_products, :dependent => :delete_all +end + +class ParanoidProduct < ActiveRecord::Base + acts_as_paranoid + belongs_to :paranoid_destroy_company + belongs_to :paranoid_delete_company + validates_presence_of :name end