Skip to content

Commit

Permalink
Tests to check ancestry changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Nov 2, 2018
1 parent 84ffa0e commit bdf39ea
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
19 changes: 19 additions & 0 deletions test/concerns/arrangement_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,23 @@ def test_arrangement_nesting
assert_equal 1, model.arrange.count
end
end

def test_arrange_partial
AncestryTestDatabase.with_model do |model|
# - n1
# - n2
# - n3
# - n4
# - n5
# - n6
n1 = model.create!
n2 = model.create!(parent: n1)
n3 = model.create!(parent: n2)
n4 = model.create!(parent: n2)
n5 = model.create!(parent: n1)
assert_equal({n5 => {}, n3 => {}}, model.arrange_nodes([n5, n3]))
# the previous test did not ensure n5 came before n3 in the ordered hash
assert_equal(n5.id, model.arrange_nodes([n5, n3]).keys.first.id)
end
end
end
49 changes: 31 additions & 18 deletions test/concerns/sort_by_ancestry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,53 @@ def test_sort_by_ancestry
# - n3
# - n4
# - n5
# - n6
n1 = model.create!
n2 = model.create!(:parent => n1)
n3 = model.create!(:parent => n2)
n4 = model.create!(:parent => n2)
n5 = model.create!(:parent => n1)
n6 = model.create!(:parent => n5)
nodes = [n1, n2, n3, n4, n5, n6]

records = model.sort_by_ancestry(model.all.sort_by(&:id).reverse)
if records[1] == n2
if records[2] == n3
assert_equal [n1, n2, n3, n4, n5].map(&:id), records.map(&:id)
else
assert_equal [n1, n2, n4, n3, n5].map(&:id), records.map(&:id)
end
else
if records[3] == n3
assert_equal [n1, n5, n2, n3, n4].map(&:id), records.map(&:id)
else
assert_equal [n1, n5, n2, n4, n3].map(&:id), records.map(&:id)
end
end
# n1 needs to move to front, and n2 needs to move in front of n4, n3
assert_equal [n1, n5, n6, n2, n4, n3].map(&:id), model.sort_by_ancestry(model.all.sort_by(&:id).reverse).map(&:id)
# none are parents
assert_equal [n5, n4, n3].map(&:id), model.sort_by_ancestry([n5, n4, n3]).map(&:id)
# at the same level
assert_equal [n3, n4].map(&:id), model.sort_by_ancestry([n3, n4]).map(&:id)
# n1 needs to move before both
assert_equal [n1, n5, n2].map(&:id), model.sort_by_ancestry([n5, n2, n1]).map(&:id)
# n1 needs to move before even a double descendant
#assert_equal [n1, n5, n4].map(&:id), model.sort_by_ancestry([n5, n4, n1]).map(&:id)
end
end

def test_sort_by_ancestry_with_block
AncestryTestDatabase.with_model :extra_columns => {:rank => :integer} do |model|
# - n1 (0)
# - n5 (0)
# - n3 (3)
# - n2 (1)
# - n4 (0)
# - n6 (1)
n1 = model.create!(:rank => 0)
n2 = model.create!(:rank => 1)
n3 = model.create!(:rank => 0, :parent => n1)
n3 = model.create!(:rank => 3, :parent => n1)
n4 = model.create!(:rank => 0, :parent => n2)
n5 = model.create!(:rank => 1, :parent => n1)
n5 = model.create!(:rank => 0, :parent => n1)
n6 = model.create!(:rank => 1, :parent => n2)

records = model.sort_by_ancestry(model.all.sort_by(&:rank).reverse) {|a, b| a.rank <=> b.rank}
assert_equal [n1, n3, n5, n2, n4, n6].map(&:id), records.map(&:id)
sort = -> (a, b) { a.rank <=> b.rank }
records = model.sort_by_ancestry(model.all.sort_by(&:rank).reverse, &sort)
# all parents, all children
assert_equal [n1, n5, n3, n2, n4, n6].map(&:id), records.map(&:id)
# all parents, some children
assert_equal [n1, n5, n2].map(&:id), model.sort_by_ancestry([n2, n1, n5], &sort).map(&:id)
# does not work for all children no parents
# assert_equal [n5, n3, n4, n6].map(&:id), model.sort_by_ancestry([n3, n4, n5, n6], &sort).map(&:id)
# ranked does not work for ranked case with missing parents
# assert_equal [n3, n2, n4].map(&:id), model.sort_by_ancestry([n3, n2, n4], &sort).map(&:id)
end
end
end

0 comments on commit bdf39ea

Please sign in to comment.