1
# -*- coding: utf-8 -*-
2
# -----------------------------------------------------------------------------
3
# Gettings Things Gnome! - a personal organizer for the GNOME desktop
4
# Copyright (c) 2008-2010- 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.tools.larch import Tree
25
from GTG.tools.larch.tree import TreeNode
28
#This is a dummy treenode that only have one properties: a color
29
class DummyNode(TreeNode):
30
def __init__(self,tid):
31
TreeNode.__init__(self, tid)
34
def add_color(self,color):
35
if color not in self.colors:
36
self.colors.append(color)
38
def has_color(self,color):
39
return color in self.colors
41
def remove_color(self,color):
42
if color in self.colors:
43
self.colors.pop(color)
45
class TestFilteredTree(unittest.TestCase):
46
"""Tests for `Tree`."""
51
#node numbers, used to check
55
#Larch, is the tree. Learn to recognize it.
57
#first, we add some red nodes at the root
61
self.tree.add_node(node)
64
#then, we add some blue nodes also at the root
67
node.add_color('blue')
68
self.tree.add_node(node)
71
#finally, we add some green nodes as children of the last nodes
74
node.add_color('green')
75
self.tree.add_node(node,parent=i-1)
81
#A tree created without an argument has a root
82
root = tree.get_root()
83
self.assertEqual('root', root.get_id())
85
def test_add_node(self):
86
#Add a node to a tree the retrieve it with its id.
87
tree = self._build_tree(1)
88
get = tree.get_node('1@1')
89
self.assertEqual('1@1',get.get_id())
91
def test_remove_node(self):
92
#Add a node to a tree the retrieve it with its id.
93
tree = self._build_tree(1)
94
tree.remove_node('1@1')
95
get = tree.get_node('1@1')
96
self.assertEqual(None,get)
98
def test_all_nodes(self):
99
#you can retrieve all nodes, the tree being flat or not
100
tree1 = self._build_tree(4)
101
tree2 = self._build_tree(4,flat=False)
102
flat = len(tree1.get_all_nodes())
103
stair = len(tree2.get_all_nodes())
104
self.assertEqual(4,flat)
105
#not flat have n + n - 1 nodes
106
self.assertEqual(7,stair)
108
def test_parent(self):
109
tree = self._build_tree(4,flat=False)
111
mynode = tree.get_node('3@3')
112
self.assertEqual(True,mynode.has_parent())
113
p = mynode.get_parents()[0]
114
par = tree.get_node(p)
115
self.assertEqual('2@2',par.get_id())
117
def test_get_path(self):
118
tree = self._build_tree(4,flat=False)
119
mynode = tree.get_node('2@2')
120
node_path = tree.get_path_for_node(mynode)
121
self.assertEqual((0,0),node_path)
122
mynode = tree.get_node('2@1')
123
node_path = tree.get_path_for_node(mynode)
124
self.assertEqual((1,),node_path)
126
def test_visit(self):
133
tree = self._build_tree(4,flat=False)
134
tree.visit_tree(pre_func=pre,post_func=post)
135
self.assertEqual(7,self.counter)
136
self.assertEqual(7,self.counter2)
138
def test_get_node(self):
139
tree = self._build_tree(4,flat=False)
140
node1 = tree.get_node('3@1')
141
node2 = tree.get_node('3@3')
142
znode1 = tree.get_node_for_path((2,))
143
znode2 = tree.get_node_for_path((0,0,0))
144
self.assertEqual(node1,znode1)
145
self.assertEqual(node2,znode2)
150
return unittest.TestLoader().loadTestsFromName(__name__)