~nmu-sscheel/gtg/rework-task-editor

« back to all changes in this revision

Viewing changes to GTG/tests/test_tree.py

  • Committer: Luca Invernizzi
  • Date: 2010-09-08 14:15:11 UTC
  • mfrom: (825.1.227 liblarch_rebased)
  • Revision ID: invernizzi.l@gmail.com-20100908141511-vsctgw74dj1xp0wi
Liblarch is now in trunk.
Note that performances are still bad and it misses DnD and multi-select
support, but its state is better that the current trunk.
Backends are added in this merge too.

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-2009 - 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.core.tree import Tree,TreeNode
25
 
 
26
 
 
27
 
 
28
 
class TestTree(unittest.TestCase):
29
 
    """Tests for `Tree`."""
30
 
    
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):
34
 
        i = 1
35
 
        previous_node = None
36
 
        tree = Tree()
37
 
        while i <= nbr_of_nodes:
38
 
            node_name1 = '%s@1' %(i)
39
 
            node_name2 = '%s@%s' %(i,i)
40
 
            node1 = TreeNode(node_name1)
41
 
            tree.add_node(node1)
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)
46
 
                previous_node = node2
47
 
            else:
48
 
                previous_node = node1
49
 
            i+=1
50
 
        return tree
51
 
            
52
 
 
53
 
    def test_root(self):
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())
58
 
        
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())
64
 
        
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)
71
 
        
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)
81
 
        
82
 
    def test_parent(self):
83
 
        tree = self._build_tree(4,flat=False)
84
 
        #tree.print_tree()
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())
90
 
        
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)
99
 
        
100
 
    def test_visit(self):
101
 
        self.counter = 0
102
 
        self.counter2 = 0
103
 
        def pre(node):
104
 
            self.counter += 1
105
 
        def post(node):
106
 
            self.counter2 += 1
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)
111
 
        
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)
120
 
 
121
 
 
122
 
 
123
 
def test_suite():
124
 
    return unittest.TestLoader().loadTestsFromName(__name__)