Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Pull out predicates' tests to test_tree_predicates.

  • Loading branch information...
commit 7f5149637b2262654507cfd856e59b2af5144b4d 1 parent 376d763
@neglectedvalue authored
Showing with 36 additions and 37 deletions.
  1. +36 −37 test/has_ancestry_test.rb
View
73 test/has_ancestry_test.rb
@@ -88,18 +88,12 @@ def test_tree_navigation
# Root assertions
assert_equal lvl0_node.id, lvl0_node.root_id
assert_equal lvl0_node, lvl0_node.root
- assert lvl0_node.is_root?
# Children assertions
assert_equal lvl0_children.map(&:first).map(&:id), lvl0_node.child_ids
assert_equal lvl0_children.map(&:first), lvl0_node.children
- assert lvl0_node.has_children?
- assert !lvl0_node.is_childless?
# Siblings assertions
assert_equal roots.map(&:first).map(&:id), lvl0_node.sibling_ids
assert_equal roots.map(&:first), lvl0_node.siblings
- assert lvl0_node.has_siblings?
- assert !lvl0_node.is_only_child?
- assert lvl0_node.sibling_of?(lvl0_node)
# Descendants assertions
descendants = model.all.find_all do |node|
node.ancestor_ids.include? lvl0_node.id
@@ -115,29 +109,18 @@ def test_tree_navigation
assert_equal [lvl0_node.id, lvl1_node.id], lvl1_node.path_ids
assert_equal [lvl0_node, lvl1_node], lvl1_node.path
assert_equal 1, lvl1_node.depth
- assert lvl0_node.ancestor_of?(lvl1_node)
# Parent assertions
assert_equal lvl0_node.id, lvl1_node.parent_id
assert_equal lvl0_node, lvl1_node.parent
- assert lvl0_node.parent_of?(lvl1_node)
# Root assertions
assert_equal lvl0_node.id, lvl1_node.root_id
assert_equal lvl0_node, lvl1_node.root
- assert !lvl1_node.is_root?
- assert lvl0_node.root_of?(lvl1_node)
# Children assertions
assert_equal lvl1_children.map(&:first).map(&:id), lvl1_node.child_ids
assert_equal lvl1_children.map(&:first), lvl1_node.children
- assert lvl1_node.has_children?
- assert !lvl1_node.is_childless?
- assert lvl1_node.child_of?(lvl0_node)
# Siblings assertions
assert_equal lvl0_children.map(&:first).map(&:id), lvl1_node.sibling_ids
assert_equal lvl0_children.map(&:first), lvl1_node.siblings
- assert lvl1_node.has_siblings?
- assert !lvl1_node.is_only_child?
- assert !lvl1_node.sibling_of?(lvl0_node)
- assert lvl1_node.sibling_of?(lvl1_node)
# Descendants assertions
descendants = model.all.find_all do |node|
node.ancestor_ids.include? lvl1_node.id
@@ -145,7 +128,6 @@ def test_tree_navigation
assert_equal descendants.map(&:id), lvl1_node.descendant_ids
assert_equal descendants, lvl1_node.descendants
assert_equal [lvl1_node] + descendants, lvl1_node.subtree
- assert lvl1_node.descendant_of?(lvl0_node)
lvl1_children.each do |lvl2_node, lvl2_children|
# Ancestors assertions
@@ -154,33 +136,18 @@ def test_tree_navigation
assert_equal [lvl0_node.id, lvl1_node.id, lvl2_node.id], lvl2_node.path_ids
assert_equal [lvl0_node, lvl1_node, lvl2_node], lvl2_node.path
assert_equal 2, lvl2_node.depth
- assert lvl0_node.ancestor_of?(lvl2_node)
- assert lvl1_node.ancestor_of?(lvl2_node)
# Parent assertions
assert_equal lvl1_node.id, lvl2_node.parent_id
assert_equal lvl1_node, lvl2_node.parent
- assert !lvl0_node.parent_of?(lvl2_node)
- assert lvl1_node.parent_of?(lvl2_node)
# Root assertions
assert_equal lvl0_node.id, lvl2_node.root_id
assert_equal lvl0_node, lvl2_node.root
- assert !lvl2_node.is_root?
- assert lvl0_node.root_of?(lvl2_node)
# Children assertions
assert_equal [], lvl2_node.child_ids
assert_equal [], lvl2_node.children
- assert !lvl2_node.has_children?
- assert lvl2_node.is_childless?
- assert lvl2_node.child_of?(lvl1_node)
- assert !lvl2_node.child_of?(lvl0_node)
# Siblings assertions
assert_equal lvl1_children.map(&:first).map(&:id), lvl2_node.sibling_ids
assert_equal lvl1_children.map(&:first), lvl2_node.siblings
- assert lvl2_node.has_siblings?
- assert !lvl2_node.is_only_child?
- assert !lvl2_node.sibling_of?(lvl0_node)
- assert !lvl2_node.sibling_of?(lvl1_node)
- assert lvl2_node.sibling_of?(lvl2_node)
# Descendants assertions
descendants = model.all.find_all do |node|
node.ancestor_ids.include? lvl2_node.id
@@ -188,14 +155,46 @@ def test_tree_navigation
assert_equal descendants.map(&:id), lvl2_node.descendant_ids
assert_equal descendants, lvl2_node.descendants
assert_equal [lvl2_node] + descendants, lvl2_node.subtree
- assert lvl2_node.descendant_of?(lvl0_node)
- assert lvl2_node.descendant_of?(lvl1_node)
end
end
end
end
end
+ def test_tree_predicates
+ AncestryTestDatabase.with_model :depth => 2, :width => 3 do |model, roots|
+ roots.each do |lvl0_node, lvl0_children|
+ root, children = lvl0_node, lvl0_children.map(&:first)
+ # Ancestors assertions
+ assert children.map { |n| root.ancestor_of?(n) }.all?
+ assert children.map { |n| !n.ancestor_of?(root) }.all?
+ # Parent assertions
+ assert children.map { |n| root.parent_of?(n) }.all?
+ assert children.map { |n| !n.parent_of?(root) }.all?
+ # Root assertions
+ assert root.is_root?
+ assert children.map { |n| !n.is_root? }.all?
+ assert children.map { |n| root.root_of?(n) }.all?
+ assert children.map { |n| !n.root_of?(root) }.all?
+ # Children assertions
+ assert root.has_children?
+ assert !root.is_childless?
+ assert children.map { |n| n.is_childless? }.all?
+ assert children.map { |n| !root.child_of?(n) }.all?
+ assert children.map { |n| n.child_of?(root) }.all?
+ # Siblings assertions
+ assert root.has_siblings?
+ assert !root.is_only_child?
+ assert children.map { |n| !n.is_only_child? }.all?
+ assert children.map { |n| !root.sibling_of?(n) }.all?
+ assert children.permutation(2).map { |l, r| l.sibling_of?(r) }.all?
+ # Descendants assertions
+ assert children.map { |n| !root.descendant_of?(n) }.all?
+ assert children.map { |n| n.descendant_of?(root) }.all?
+ end
+ end
+ end
+
def test_ancestors_with_string_primary_keys
AncestryTestDatabase.with_model :depth => 3, :width => 3, :primary_key_type => :string, :primary_key_format => /[a-z0-9]+/ do |model, roots|
roots.each do |lvl0_node, lvl0_children|
@@ -711,7 +710,7 @@ def test_arrange_order_option
end
end
end
-
+
def test_sort_by_ancestry
AncestryTestDatabase.with_model do |model|
n1 = model.create!
@@ -719,7 +718,7 @@ def test_sort_by_ancestry
n3 = model.create!(:parent => n2)
n4 = model.create!(:parent => n2)
n5 = model.create!(:parent => n1)
-
+
arranged = model.sort_by_ancestry(model.all.sort_by(&:id).reverse)
assert_equal [n1, n2, n4, n3, n5].map(&:id), arranged.map(&:id)
end
Please sign in to comment.
Something went wrong with that request. Please try again.