Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixed #22531 and added tests for `tree.Node` #2628

Closed
wants to merge 1 commit into from

2 participants

@mmardini

While Node class has a useful __str__, its __repr__ is not that
useful. Added a __repr__ that makes use of the current __str__.
This is especially useful since the more popular Q class inherits
tree.Node.

@timgraham
Owner

A test would be good.

@mmardini

Thanks Tim for your feedback. After examining the internal tests of Django, I couldn't find a suitable place where a test for this function would really belong. queries/tests.py has tests which use Q objects, but in the context of creating querysets. Would using that file be feasible? Any other recommendation?

@timgraham
Owner

I would add a new file: tests/utils_tests/test_tree.py

@timgraham
Owner

Can one of the admins verify this patch?

@mmardini mmardini Fixed #22531 and added tests for `tree.Node`
While Node class has a useful `__str__`, its `__repr__` is not that
useful. Added a `__repr__` that makes use of the current `__str__`.
This is especially useful since the more popular `Q` class inherits
`tree.Node`. Also created new tests that cover most of `Node` class
functionality.
c3e5458
@mmardini mmardini changed the title from Fixed #22531 -- Added `__repr__` for `tree.Node` to Fixed #22531 and added tests for `tree.Node`
@mmardini

Added a new tests file. Please verify it too.

@timgraham
Owner

Looks good. There were a couple minor flake8 warnings I fixed. merged in 393ddc1.

@timgraham timgraham closed this
@mmardini

Thanks Tim for the review. I will check using flake8 in my future PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 16, 2014
  1. @mmardini

    Fixed #22531 and added tests for `tree.Node`

    mmardini authored
    While Node class has a useful `__str__`, its `__repr__` is not that
    useful. Added a `__repr__` that makes use of the current `__str__`.
    This is especially useful since the more popular `Q` class inherits
    `tree.Node`. Also created new tests that cover most of `Node` class
    functionality.
This page is out of date. Refresh to see the latest.
Showing with 59 additions and 0 deletions.
  1. +3 −0  django/utils/tree.py
  2. +56 −0 tests/utils_tests/test_tree.py
View
3  django/utils/tree.py
@@ -48,6 +48,9 @@ def __str__(self):
return '(%s: %s)' % (self.connector, ', '.join([str(c) for c in
self.children]))
+ def __repr__(self):
+ return "<%s: %s>" % (self.__class__.__name__, self)
+
def __deepcopy__(self, memodict):
"""
Utility method used by copy.deepcopy().
View
56 tests/utils_tests/test_tree.py
@@ -0,0 +1,56 @@
+import unittest
+
+import copy
+from django.utils.tree import Node
+
+class NodeTests(unittest.TestCase):
+ def setUp(self):
+ self.node1_children = [('a', 1), ('b', 2)]
+ self.node1 = Node(self.node1_children)
+ self.node2 = Node()
+
+ def test_str(self):
+ self.assertEqual(str(self.node1), "(DEFAULT: ('a', 1), ('b', 2))")
+ self.assertEqual(str(self.node2), "(DEFAULT: )")
+
+ def test_repr(self):
+ self.assertEqual(repr(self.node1),
+ "<Node: (DEFAULT: ('a', 1), ('b', 2))>")
+ self.assertEqual(repr(self.node2), "<Node: (DEFAULT: )>")
+
+ def test_len(self):
+ self.assertEqual(len(self.node1), 2)
+ self.assertEqual(len(self.node2), 0)
+
+ def test_bool(self):
+ self.assertTrue(self.node1)
+ self.assertFalse(self.node2)
+
+ def test_contains(self):
+ self.assertIn(('a', 1), self.node1)
+ self.assertNotIn(('a', 1), self.node2)
+
+ def test_add(self):
+ # start with the same children of node1 then add an item
+ node3 = Node(self.node1_children)
+ node3_added_child = ('c', 3)
+ # add() returns the added data
+ self.assertEqual(node3.add(node3_added_child, Node.default),
+ node3_added_child)
+ # we added exactly one item, len() should reflect that
+ self.assertEqual(len(self.node1)+1, len(node3))
+ self.assertEqual(str(node3), "(DEFAULT: ('a', 1), ('b', 2), ('c', 3))")
+
+ def test_negate(self):
+ # negated is False by default
+ self.assertFalse(self.node1.negated)
+ self.node1.negate()
+ self.assertTrue(self.node1.negated)
+ self.node1.negate()
+ self.assertFalse(self.node1.negated)
+
+ def test_deepcopy(self):
+ node4 = copy.copy(self.node1)
+ node5 = copy.deepcopy(self.node1)
+ self.assertIs(self.node1.children, node4.children)
+ self.assertIsNot(self.node1.children, node5.children)
Something went wrong with that request. Please try again.