- r1 = Node.objects.get(slug='recursive1')
- r2 = Node.objects.get(slug='recursive2')
- pr1 = Node.objects.get(slug='postrecursive1')
-
- # Simple case: straight path
- self.assertEqual(third.has_ancestor(root), True)
- self.assertEqual(root.has_ancestor(root), False)
- self.assertEqual(root.has_ancestor(None), True)
- self.assertEqual(third.has_ancestor(None), True)
- self.assertEqual(root.has_ancestor(root, inclusive=True), True)
-
- # Recursive case
- self.assertEqual(r1.has_ancestor(r1), True)
- self.assertEqual(r1.has_ancestor(r2), True)
- self.assertEqual(r2.has_ancestor(r1), True)
- self.assertEqual(r2.has_ancestor(None), False)
-
- # Post-recursive case
- self.assertEqual(pr1.has_ancestor(r1), True)
- self.assertEqual(pr1.has_ancestor(pr1), False)
- self.assertEqual(pr1.has_ancestor(pr1, inclusive=True), True)
- self.assertEqual(pr1.has_ancestor(None), False)
- self.assertEqual(pr1.has_ancestor(root), False)
+ second2 = Node.objects.get(slug='second2')
+ fifth = Node.objects.get(slug='fifth')
+ e = Node.DoesNotExist
+
+ # Empty segments
+ self.assertQueryLimit(0, root, '', root=root)
+ self.assertQueryLimit(0, e, '')
+ self.assertQueryLimit(0, (root, None), '', root=root, absolute_result=False)
+
+ # Absolute result
+ self.assertQueryLimit(1, third, 'root/second/third')
+ self.assertQueryLimit(1, third, 'second/third', root=root)
+ self.assertQueryLimit(1, third, 'root//////second/third///')
+
+ self.assertQueryLimit(1, e, 'root/secont/third')
+ self.assertQueryLimit(1, e, 'second/third')
+
+ # Non-absolute result (binary search)
+ self.assertQueryLimit(2, (second2, 'sub/path/tail'), 'root/second2/sub/path/tail', absolute_result=False)
+ self.assertQueryLimit(3, (second2, 'sub/'), 'root/second2/sub/', absolute_result=False)
+ self.assertQueryLimit(2, e, 'invalid/path/1/2/3/4/5/6/7/8/9/1/2/3/4/5/6/7/8/9/0', absolute_result=False)
+ self.assertQueryLimit(1, (root, None), 'root', absolute_result=False)
+ self.assertQueryLimit(2, (second2, None), 'root/second2', absolute_result=False)
+ self.assertQueryLimit(3, (third, None), 'root/second/third', absolute_result=False)
+
+ # with root != None
+ self.assertQueryLimit(1, (second2, None), 'second2', root=root, absolute_result=False)
+ self.assertQueryLimit(2, (third, None), 'second/third', root=root, absolute_result=False)
+
+ # Preserve trailing slash
+ self.assertQueryLimit(2, (second2, 'sub/path/tail/'), 'root/second2/sub/path/tail/', absolute_result=False)
+
+ # Speed increase for leaf nodes - should this be tested?
+ self.assertQueryLimit(1, (fifth, 'sub/path/tail/len/five'), 'root/second/third/fourth/fifth/sub/path/tail/len/five', absolute_result=False)