~parinporecha/gtg/config_parser_bug

« back to all changes in this revision

Viewing changes to GTG/tests/tree_testing.py

  • Committer: Izidor Matušov
  • Date: 2013-02-25 07:35:07 UTC
  • Revision ID: izidor.matusov@gmail.com-20130225073507-vgts69uthx7z2run
PEP8ification by Nimit

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# this program.  If not, see <http://www.gnu.org/licenses/>.
18
18
# -----------------------------------------------------------------------------
19
19
 
20
 
#If True, the TreeTester will automatically reorder node on the same level
21
 
#as a deleted node. If False, it means that Liblarch has the responsability
22
 
#to handle that itself.
 
20
# If True, the TreeTester will automatically reorder node on the same level
 
21
# as a deleted node. If False, it means that Liblarch has the responsability
 
22
# to handle that itself.
23
23
REORDER_ON_DELETE = False
24
24
 
25
25
 
29
29
 
30
30
    def __init__(self, viewtree):
31
31
        self.tree = viewtree
32
 
        #both dict should always be synchronized
33
 
        #They are the internal representation of the tree,
34
 
        #based only on received signals
 
32
        # both dict should always be synchronized
 
33
        # They are the internal representation of the tree,
 
34
        # based only on received signals
35
35
        self.nodes = {}
36
36
        self.paths = {}
37
37
        self.tree.register_cllbck('node-added-inview', self.add)
45
45
        currentnode = self.paths.get(path, None)
46
46
        if currentnode and currentnode != nid:
47
47
            raise Exception('path %s is already occupied by %s' % (
48
 
                                str(path), nid))
 
48
                str(path), nid))
49
49
        if nid in self.nodes:
50
50
            node = self.nodes[nid]
51
51
        else:
56
56
        self.paths[path] = nid
57
57
 
58
58
    def delete(self, nid, path):
59
 
        self.trace += "removing %s from path %s\n" %(nid, str(path))
 
59
        self.trace += "removing %s from path %s\n" % (nid, str(path))
60
60
        if nid != self.paths.get(path, None):
61
 
            error = '%s is not assigned to path %s\n'%(nid, str(path))
 
61
            error = '%s is not assigned to path %s\n' % (nid, str(path))
62
62
            error += self.print_tree()
63
63
            raise Exception(error)
64
64
        if path not in self.nodes.get(nid, []):
77
77
        index = path[-1]
78
78
 
79
79
        assert path_prefix + (index, ) == path, "%s vs %s" % (
80
 
                        path_prefix + (index, ), path)
 
80
            path_prefix + (index, ), path)
81
81
 
82
82
        def check_prefix(path):
83
83
            """ Is this path affected by the change?
107
107
                new_path = list(path)
108
108
                print "new_path: %s" % str(new_path)
109
109
                index = len(path_prefix)
110
 
                new_path[index] = str(int(new_path[index])-1)
 
110
                new_path[index] = str(int(new_path[index]) - 1)
111
111
                new_path = tuple(new_path)
112
112
 
113
113
                print "new_path: %s" % str(new_path)
141
141
#        if path not in self.nodes[n] or n != nid:
142
142
#            raise Exception('Mismatching node for path %s'%str(p))
143
143
 
144
 
        #Because of the asynchronousness of update, this test
145
 
        #doesn't work anymore
 
144
        # Because of the asynchronousness of update, this test
 
145
        # doesn't work anymore
146
146
        pass
147
147
 
148
148
    def reordered(self, nid, path, neworder):
149
149
        print "reordering"
150
150
        self.trace += "reordering children of %s (%s) : %s\n" % (nid,
151
 
                                                        str(path), neworder)
152
 
        self.trace += "VR is %s\n" %self.tree.node_all_children()
 
151
                                                                 str(path),
 
152
                                                                 neworder)
 
153
        self.trace += "VR is %s\n" % self.tree.node_all_children()
153
154
        if not path:
154
155
            path = ()
155
156
        i = 0
156
157
        newpaths = {}
157
158
        toremove = []
158
 
        #we first update self.nodes with the new paths
 
159
        # we first update self.nodes with the new paths
159
160
        while i < len(neworder):
160
161
            if i != neworder[i]:
161
162
                old = neworder[i]
169
170
                        newpp = newp + pp[le:]
170
171
                        self.nodes[n].append(newpp)
171
172
                        self.trace += "    change %s path from %s to %s\n" % (
172
 
                                                                  n, pp, newpp)
 
173
                            n, pp, newpp)
173
174
                        newpaths[newpp] = n
174
175
                        toremove.append(pp)
175
176
            i += 1
176
 
        #now we can update self.paths
 
177
        # now we can update self.paths
177
178
        for p in toremove:
178
179
            self.paths.pop(p)
179
180
        for p in newpaths:
180
 
            self.trace += "    adding %s to paths %s\n" %(newpaths[p], str(p))
 
181
            self.trace += "    adding %s to paths %s\n" % (newpaths[p], str(p))
181
182
            self.paths[p] = newpaths[p]
182
183
 
183
184
    def test_validity(self):
184
185
        for n in self.nodes.keys():
185
186
            paths = self.tree.get_paths_for_node(n)
186
187
            if len(self.nodes[n]) == 0:
187
 
                raise Exception('Node %s is stored without any path'%n)
 
188
                raise Exception('Node %s is stored without any path' % n)
188
189
            for p in self.nodes[n]:
189
190
                if self.paths[p] != n:
190
 
                    raise Exception('Mismatching path for %s'%n)
 
191
                    raise Exception('Mismatching path for %s' % n)
191
192
                if p not in paths:
192
 
                    error = 'we have a unknown stored path for %s\n' %n
 
193
                    error = 'we have a unknown stored path for %s\n' % n
193
194
                    nn = self.tree.get_node_for_path(p)
194
195
                    parent = self.tree.get_node_for_path(p[:-1])
195
 
                    error += '  path %s is the path of %s\n' %(str(p), str(nn))
 
196
                    error += '  path %s is the path of %s\n' % (
 
197
                        str(p), str(nn))
196
198
                    error += '  parent is %s' % parent
197
199
#                    error += self.trace
198
200
                    raise Exception(error)
199
201
                paths.remove(p)
200
202
            if len(paths) > 0:
201
 
                raise Exception('why is this path existing for %s' %n)
 
203
                raise Exception('why is this path existing for %s' % n)
202
204
        for p in self.paths.keys():
203
205
            node = self.tree.get_node_for_path(p)
204
206
            n = self.paths[p]
206
208
                error = 'Node for path is %s but should be %s' % (node, n)
207
209
                raise Exception(error)
208
210
            if p not in self.nodes[n]:
209
 
                error = 'Mismatching node for path %s\n'%str(p)
 
211
                error = 'Mismatching node for path %s\n' % str(p)
210
212
                error += self.print_tree()
211
213
                raise Exception(error)
212
214
            if len(p) == 1 and len(self.nodes[n]) > 1:
213
 
                error = 'Node %s has multiple paths and is in the VR\n' %n
 
215
                error = 'Node %s has multiple paths and is in the VR\n' % n
214
216
                error += self.print_tree()
215
217
                raise Exception(error)
216
218
        return True
217
219
 
218
220
    def print_tree(self):
219
221
        st = self.trace
220
 
        st += "nodes are %s\n" %self.nodes
221
 
        st += "paths are %s\n" %self.paths
 
222
        st += "nodes are %s\n" % self.nodes
 
223
        st += "paths are %s\n" % self.paths
222
224
        return st
223
225
 
224
226
    def quit(self):