Permalink
Browse files

working

  • Loading branch information...
1 parent 08b0346 commit 988300ae4b2bf6316d96c50e49714b7cd61ff939 @klen committed Oct 12, 2012
Showing with 32 additions and 13 deletions.
  1. +1 −1 setup.py
  2. +7 −0 simpletree/models.py
  3. +24 −12 tests/main/tests.py
View
@@ -36,7 +36,7 @@ def read(fname):
packages=find_packages(),
package_data = {'': PACKAGE_DATA},
- install_requires = ('Django>=1.4.0'),
+ install_requires = ('Django>=1.2'),
test_suite = 'tests.run_tests',
tests_require = ['milkman']
)
View
@@ -39,6 +39,8 @@ class RelationMeta:
related_name='_parent_links'),
depth=models.PositiveIntegerField(default=0),
Meta=RelationMeta,
+ __repr__=lambda s: "{0}/{1} [{2}]".format(
+ s.parent_id, s.child_id, s.depth)
))
class_dict['{0}_child_set'.format(class_name.lower())] = models.ManyToManyField(
@@ -113,5 +115,10 @@ def clean_tree(self):
parent=self.parent_id,
child=self.pk)
+ def get_tree(self):
+ return self.__class__.objects.filter(
+ models.Q(_child_links__child=self) | models.Q(_parent_links__parent=self)
+ ).distinct()
+
# pymode:lint_ignore=E1123
View
@@ -1,19 +1,31 @@
from django.test import TestCase
from milkman.dairy import milkman
+from .models import Page
class TreeTestCase(TestCase):
+ def setUp(self):
+ """ Create test tree
+ """
+ root1 = milkman.deliver(Page)
+ page11 = milkman.deliver(Page, parent=root1)
+ page12 = milkman.deliver(Page, parent=root1)
+ page111 = milkman.deliver(Page, parent=page11)
+ page1111 = milkman.deliver(Page, parent=page111)
+ page1112 = milkman.deliver(Page, parent=page111)
+
+ self.__dict__.update(locals())
+
def test_models(self):
- from .models import Page
- self.assertFalse(Page.objects.count())
-
- root = milkman.deliver(Page)
- page11 = milkman.deliver(Page, parent=root)
- page12 = milkman.deliver(Page, parent=root)
- page21 = milkman.deliver(Page, parent=page11)
- page31 = milkman.deliver(Page, parent=page21)
-
- # Move tree
- page21.parent = page12
- page21.save()
+ self.assertEqual(Page.objects.count(), 6)
+
+ def test_get_root_nodes(self):
+ self.assertEqual(list(Page.objects.filter(parent=None)), [self.root1])
+
+ def test_get_tree(self):
+ with self.assertNumQueries(1):
+ pages = self.page11.get_tree()
+ self.assertEqual(len(pages), 5)
+
+# pymode:lint_ignore=F0401,W0612,W806

0 comments on commit 988300a

Please sign in to comment.