~bryce/gtg/dbus-service-name

« back to all changes in this revision

Viewing changes to GTG/tests/test_filteredtree.py

  • Committer: Lionel Dricot
  • Date: 2010-06-22 09:43:55 UTC
  • mto: (825.1.1 gtg)
  • mto: This revision was merged to the branch mainline in revision 822.
  • Revision ID: ploum@ploum.net-20100622094355-nw1zubdpk4ro8dpd
starting liblarch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
5
#
 
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
 
9
# version.
 
10
#
 
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
 
14
# details.
 
15
#
 
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
# -----------------------------------------------------------------------------
 
19
 
 
20
"""Tests for the tagstore."""
 
21
 
 
22
import unittest
 
23
 
 
24
from GTG.tools.larch import Tree
 
25
from GTG.tools.larch.tree import TreeNode
 
26
 
 
27
 
 
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)
 
32
        self.colors = []
 
33
 
 
34
    def add_color(self,color):
 
35
        if color not in self.colors:
 
36
            self.colors.append(color)
 
37
 
 
38
    def has_color(self,color):
 
39
        return color in self.colors
 
40
 
 
41
    def remove_color(self,color):
 
42
        if color in self.colors:
 
43
            self.colors.pop(color)
 
44
 
 
45
class TestFilteredTree(unittest.TestCase):
 
46
    """Tests for `Tree`."""
 
47
 
 
48
 
 
49
    def setUp(self):
 
50
        i = 0
 
51
        #node numbers, used to check
 
52
        self.red_nodes = 0
 
53
        self.blue_nodes = 0
 
54
        self.green_nodes = 0
 
55
        #Larch, is the tree. Learn to recognize it.
 
56
        self.tree = Larch()
 
57
        #first, we add some red nodes at the root
 
58
        while i < 5:
 
59
            node = DummyNode(i)
 
60
            node.add_color('red')
 
61
            self.tree.add_node(node)
 
62
            i += 1
 
63
            self.red_nodes += 1
 
64
        #then, we add some blue nodes also at the root
 
65
        while i < 10:
 
66
            node = DummyNode(i)
 
67
            node.add_color('blue')
 
68
            self.tree.add_node(node)
 
69
            i+=1
 
70
            self.blue_nodes += 1
 
71
        #finally, we add some green nodes as children of the last nodes
 
72
        while i < 15:
 
73
            node = DummyNode(i)
 
74
            node.add_color('green')
 
75
            self.tree.add_node(node,parent=i-1)
 
76
            i+=1
 
77
            self.green_nodes += 1
 
78
 
 
79
 
 
80
    def test_root(self):
 
81
        #A tree created without an argument has a root
 
82
        root = tree.get_root()
 
83
        self.assertEqual('root', root.get_id())
 
84
        
 
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())
 
90
        
 
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)
 
97
        
 
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)
 
107
        
 
108
    def test_parent(self):
 
109
        tree = self._build_tree(4,flat=False)
 
110
        #tree.print_tree()
 
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())
 
116
        
 
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)
 
125
        
 
126
    def test_visit(self):
 
127
        self.counter = 0
 
128
        self.counter2 = 0
 
129
        def pre(node):
 
130
            self.counter += 1
 
131
        def post(node):
 
132
            self.counter2 += 1
 
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)
 
137
        
 
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)
 
146
 
 
147
 
 
148
 
 
149
def test_suite():
 
150
    return unittest.TestLoader().loadTestsFromName(__name__)