From f1b8d958346c73ed15a1e9f31464da7be1d5ac39 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 17 Jul 2023 18:14:03 -0400 Subject: [PATCH] test orphan_strategy: :destroy deletes leafs first validates https://github.com/stefankroes/ancestry/pull/635 --- test/concerns/orphan_strategies_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/concerns/orphan_strategies_test.rb b/test/concerns/orphan_strategies_test.rb index bb4d12cc..19147681 100644 --- a/test/concerns/orphan_strategies_test.rb +++ b/test/concerns/orphan_strategies_test.rb @@ -24,6 +24,18 @@ def test_orphan_rootify_strategy def test_orphan_destroy_strategy AncestryTestDatabase.with_model orphan_strategy: :destroy, :depth => 3, :width => 3 do |model, roots| + model.class_eval do + # verify we are destroying leafs to child + # some implementations leverage the parent + before_destroy :verify_parent_exists + + def verify_parent_exists + if has_parent? && !self.class.where(:id => parent_id).exists? + raise "issues with #{id} (ancestry #{ancestry || "root"})" + end + end + end + root = roots.first.first assert_difference 'model.count', -root.subtree.size do root.destroy @@ -68,6 +80,7 @@ def test_orphan_adopt_strategy end end + # DEPRECATED - please see test_apply_orphan_strategy_none for pattern instead def test_override_apply_orphan_strategy AncestryTestDatabase.with_model orphan_strategy: :destroy do |model, roots| root = model.create!