From 8d3b1128e7925825332eafc7d269cf34e1af170c Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Mon, 22 Nov 2010 10:45:31 -0500 Subject: [PATCH] Added get_path tests back in. Special-cased get_path if root and self are equal to return '' instead of raising an exception. --- models/base.py | 3 +++ tests.py | 22 +++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/models/base.py b/models/base.py index 0598407..202c2f3 100644 --- a/models/base.py +++ b/models/base.py @@ -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) diff --git a/tests.py b/tests.py index 7d5fc45..1dc48b7 100644 --- 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 -- 2.20.1