1
# -*- coding: utf-8 -*-
2
# -----------------------------------------------------------------------------
3
# Gettings Things Gnome! - a personal organizer for the GNOME desktop
4
# Copyright (c) 2008-2009 - Lionel Dricot & Bertrand Rousseau
6
# This program is free software: you can redistribute it and/or modify it under
7
# the terms of the GNU General Public License as published by the Free Software
8
# Foundation, either version 3 of the License, or (at your option) any later
11
# This program is distributed in the hope that it will be useful, but WITHOUT
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
# You should have received a copy of the GNU General Public License along with
17
# this program. If not, see <http://www.gnu.org/licenses/>.
18
# -----------------------------------------------------------------------------
20
"""Tests for the tagstore."""
24
from GTG.core.tree import Tree,TreeNode
28
class TestTree(unittest.TestCase):
29
"""Tests for `Tree`."""
31
#build a tree. If flat, all nodes are added to the root
32
#else, we build a stairs-like structure
33
def _build_tree(self,nbr_of_nodes=0,flat=True):
37
while i <= nbr_of_nodes:
38
node_name1 = '%s@1' %(i)
39
node_name2 = '%s@%s' %(i,i)
40
node1 = TreeNode(node_name1)
42
if previous_node and not flat:
43
node2 = TreeNode(node_name2)
44
tree.add_node(node2,parent=previous_node)
45
#previous_node.add_child(node)
54
#A tree created without an argument has a root
55
tree = self._build_tree(0)
56
root = tree.get_root()
57
self.assertEqual('root', root.get_id())
59
def test_add_node(self):
60
#Add a node to a tree the retrieve it with its id.
61
tree = self._build_tree(1)
62
get = tree.get_node('1@1')
63
self.assertEqual('1@1',get.get_id())
65
def test_remove_node(self):
66
#Add a node to a tree the retrieve it with its id.
67
tree = self._build_tree(1)
68
tree.remove_node('1@1')
69
get = tree.get_node('1@1')
70
self.assertEqual(None,get)
72
def test_all_nodes(self):
73
#you can retrieve all nodes, the tree being flat or not
74
tree1 = self._build_tree(4)
75
tree2 = self._build_tree(4,flat=False)
76
flat = len(tree1.get_all_nodes())
77
stair = len(tree2.get_all_nodes())
78
self.assertEqual(4,flat)
79
#not flat have n + n - 1 nodes
80
self.assertEqual(7,stair)
82
def test_parent(self):
83
tree = self._build_tree(4,flat=False)
85
mynode = tree.get_node('3@3')
86
self.assertEqual(True,mynode.has_parent())
87
p = mynode.get_parents()[0]
88
par = tree.get_node(p)
89
self.assertEqual('2@2',par.get_id())
91
def test_get_path(self):
92
tree = self._build_tree(4,flat=False)
93
mynode = tree.get_node('2@2')
94
node_path = tree.get_path_for_node(mynode)
95
self.assertEqual((0,0),node_path)
96
mynode = tree.get_node('2@1')
97
node_path = tree.get_path_for_node(mynode)
98
self.assertEqual((1,),node_path)
100
def test_visit(self):
107
tree = self._build_tree(4,flat=False)
108
tree.visit_tree(pre_func=pre,post_func=post)
109
self.assertEqual(7,self.counter)
110
self.assertEqual(7,self.counter2)
112
def test_get_node(self):
113
tree = self._build_tree(4,flat=False)
114
node1 = tree.get_node('3@1')
115
node2 = tree.get_node('3@3')
116
znode1 = tree.get_node_for_path((2,))
117
znode2 = tree.get_node_for_path((0,0,0))
118
self.assertEqual(node1,znode1)
119
self.assertEqual(node2,znode2)
124
return unittest.TestLoader().loadTestsFromName(__name__)