Added get_path tests back in. Special-cased get_path if root and self are equal to...
authorStephen Burrows <stephen.r.burrows@gmail.com>
Mon, 22 Nov 2010 15:45:31 +0000 (10:45 -0500)
committerStephen Burrows <stephen.r.burrows@gmail.com>
Mon, 29 Nov 2010 18:24:01 +0000 (13:24 -0500)
models/base.py
tests.py

index 0598407..202c2f3 100644 (file)
@@ -402,6 +402,9 @@ class TreeModel(MPTTModel):
        slug = models.SlugField(max_length=255)
        
        def get_path(self, root=None, pathsep='/', field='slug'):
+               if root == self:
+                       return ''
+               
                if root is not None and not self.is_descendant_of(root):
                        raise AncestorDoesNotExist(root)
                
index 7d5fc45..1dc48b7 100644 (file)
--- a/tests.py
+++ b/tests.py
@@ -61,12 +61,13 @@ class TreePathTestCase(TestCase):
        def assertQueryLimit(self, max, expected_result, *args, **kwargs):
                # As a rough measure of efficiency, limit the number of queries required for a given operation.
                settings.DEBUG = True
+               call = kwargs.pop('callable', Node.objects.get_with_path)
                try:
                        queries = len(connection.queries)
                        if isinstance(expected_result, type) and issubclass(expected_result, Exception):
-                               self.assertRaises(expected_result, Node.objects.get_with_path, *args, **kwargs)
+                               self.assertRaises(expected_result, call, *args, **kwargs)
                        else:
-                               self.assertEqual(Node.objects.get_with_path(*args, **kwargs), expected_result)
+                               self.assertEqual(call(*args, **kwargs), expected_result)
                        queries = len(connection.queries) - queries
                        if queries > max:
                                raise AssertionError('"%d" unexpectedly not less than or equal to "%s"' % (queries, max))
@@ -109,4 +110,19 @@ class TreePathTestCase(TestCase):
                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)
\ No newline at end of file
+               self.assertQueryLimit(1, (fifth, 'sub/path/tail/len/five'), 'root/second/third/fourth/fifth/sub/path/tail/len/five', absolute_result=False)
+       
+       def test_get_path(self):
+               root = Node.objects.get(slug='root')
+               root2 = Node.objects.get(slug='root')
+               third = Node.objects.get(slug='third')
+               second2 = Node.objects.get(slug='second2')
+               fifth = Node.objects.get(slug='fifth')
+               e = AncestorDoesNotExist
+               
+               self.assertQueryLimit(0, 'root', callable=root.get_path)
+               self.assertQueryLimit(0, '', root2, callable=root.get_path)
+               self.assertQueryLimit(1, 'root/second/third', callable=third.get_path)
+               self.assertQueryLimit(1, 'second/third', root, callable=third.get_path)
+               self.assertQueryLimit(1, e, third, callable=second2.get_path)
+               self.assertQueryLimit(1, '? - ?', root, ' - ', 'title', callable=third.get_path)
\ No newline at end of file