~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__known_graph.py

  • Committer: Vincent Ladeuil
  • Date: 2009-09-16 10:37:29 UTC
  • mto: (4695.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4696.
  • Revision ID: v.ladeuil+lp@free.fr-20090916103729-m6h1pyvjprm6puvm
Respect items() protocol for registry objects.

* bzrlib/tests/test_registry.py:
(TestRegistryIter): Test some corner cases where object are
registered while the registry is iterated.

* bzrlib/registry.py:
(Registry): iteritems() and items() have different intents, don't
mix them under the covers or devs get tricked (see bug #277048
which seemed to have fixed the issue).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
37
37
        ('python-nocache', {'module': _known_graph_py, 'do_cache': False}),
38
38
    ]
39
39
    suite = loader.suiteClass()
40
 
    if compiled_known_graph_feature.available():
41
 
        scenarios.append(('C', {'module': compiled_known_graph_feature.module,
42
 
                                'do_cache': True}))
43
 
        caching_scenarios.append(
44
 
            ('C-nocache', {'module': compiled_known_graph_feature.module,
45
 
                           'do_cache': False}))
 
40
    if CompiledKnownGraphFeature.available():
 
41
        from bzrlib import _known_graph_pyx
 
42
        scenarios.append(('C', {'module': _known_graph_pyx, 'do_cache': True}))
 
43
        caching_scenarios.append(('C-nocache',
 
44
                          {'module': _known_graph_pyx, 'do_cache': False}))
46
45
    else:
47
46
        # the compiled module isn't available, so we add a failing test
48
47
        class FailWithoutFeature(tests.TestCase):
49
48
            def test_fail(self):
50
 
                self.requireFeature(compiled_known_graph_feature)
 
49
                self.requireFeature(CompiledKnownGraphFeature)
51
50
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
52
51
    # TestKnownGraphHeads needs to be permutated with and without caching.
53
52
    # All other TestKnownGraph tests only need to be tested across module
59
58
    return suite
60
59
 
61
60
 
62
 
compiled_known_graph_feature = tests.ModuleAvailableFeature(
63
 
                                    'bzrlib._known_graph_pyx')
 
61
class _CompiledKnownGraphFeature(tests.Feature):
 
62
 
 
63
    def _probe(self):
 
64
        try:
 
65
            import bzrlib._known_graph_pyx
 
66
        except ImportError:
 
67
            return False
 
68
        return True
 
69
 
 
70
    def feature_name(self):
 
71
        return 'bzrlib._known_graph_pyx'
 
72
 
 
73
CompiledKnownGraphFeature = _CompiledKnownGraphFeature()
64
74
 
65
75
 
66
76
#  a
144
154
        self.assertGDFO(graph, 'a', 5)
145
155
        self.assertGDFO(graph, 'c', 5)
146
156
 
147
 
    def test_add_existing_node(self):
148
 
        graph = self.make_known_graph(test_graph.ancestry_1)
149
 
        # Add a node that already exists with identical content
150
 
        # This is a 'no-op'
151
 
        self.assertGDFO(graph, 'rev4', 5)
152
 
        graph.add_node('rev4', ['rev3', 'rev2b'])
153
 
        self.assertGDFO(graph, 'rev4', 5)
154
 
        # This also works if we use a tuple rather than a list
155
 
        graph.add_node('rev4', ('rev3', 'rev2b'))
156
 
 
157
 
    def test_add_existing_node_mismatched_parents(self):
158
 
        graph = self.make_known_graph(test_graph.ancestry_1)
159
 
        self.assertRaises(ValueError, graph.add_node, 'rev4',
160
 
                          ['rev2b', 'rev3'])
161
 
 
162
 
    def test_add_node_with_ghost_parent(self):
163
 
        graph = self.make_known_graph(test_graph.ancestry_1)
164
 
        graph.add_node('rev5', ['rev2b', 'revGhost'])
165
 
        self.assertGDFO(graph, 'rev5', 4)
166
 
        self.assertGDFO(graph, 'revGhost', 1)
167
 
 
168
 
    def test_add_new_root(self):
169
 
        graph = self.make_known_graph(test_graph.ancestry_1)
170
 
        graph.add_node('rev5', [])
171
 
        self.assertGDFO(graph, 'rev5', 1)
172
 
 
173
 
    def test_add_with_all_ghost_parents(self):
174
 
        graph = self.make_known_graph(test_graph.ancestry_1)
175
 
        graph.add_node('rev5', ['ghost'])
176
 
        self.assertGDFO(graph, 'rev5', 2)
177
 
        self.assertGDFO(graph, 'ghost', 1)
178
 
 
179
 
    def test_gdfo_after_add_node(self):
180
 
        graph = self.make_known_graph(test_graph.ancestry_1)
181
 
        self.assertEqual([], graph.get_child_keys('rev4'))
182
 
        graph.add_node('rev5', ['rev4'])
183
 
        self.assertEqual(['rev4'], graph.get_parent_keys('rev5'))
184
 
        self.assertEqual(['rev5'], graph.get_child_keys('rev4'))
185
 
        self.assertEqual([], graph.get_child_keys('rev5'))
186
 
        self.assertGDFO(graph, 'rev5', 6)
187
 
        graph.add_node('rev6', ['rev2b'])
188
 
        graph.add_node('rev7', ['rev6'])
189
 
        graph.add_node('rev8', ['rev7', 'rev5'])
190
 
        self.assertGDFO(graph, 'rev5', 6)
191
 
        self.assertGDFO(graph, 'rev6', 4)
192
 
        self.assertGDFO(graph, 'rev7', 5)
193
 
        self.assertGDFO(graph, 'rev8', 7)
194
 
 
195
 
    def test_fill_in_ghost(self):
196
 
        graph = self.make_known_graph(test_graph.with_ghost)
197
 
        # Add in a couple nodes and then fill in the 'ghost' so that it should
198
 
        # cause renumbering of children nodes
199
 
        graph.add_node('x', [])
200
 
        graph.add_node('y', ['x'])
201
 
        graph.add_node('z', ['y'])
202
 
        graph.add_node('g', ['z'])
203
 
        self.assertGDFO(graph, 'f', 2)
204
 
        self.assertGDFO(graph, 'e', 3)
205
 
        self.assertGDFO(graph, 'x', 1)
206
 
        self.assertGDFO(graph, 'y', 2)
207
 
        self.assertGDFO(graph, 'z', 3)
208
 
        self.assertGDFO(graph, 'g', 4)
209
 
        self.assertGDFO(graph, 'b', 4)
210
 
        self.assertGDFO(graph, 'd', 5)
211
 
        self.assertGDFO(graph, 'a', 5)
212
 
        self.assertGDFO(graph, 'c', 6)
213
 
 
214
157
 
215
158
class TestKnownGraphHeads(TestCaseWithKnownGraph):
216
159
 
317
260
        self.assertEqual(set(['a', 'c']), graph.heads(['a', 'c', 'e', 'g']))
318
261
        self.assertEqual(set(['a', 'c']), graph.heads(['a', 'c', 'f']))
319
262
 
320
 
    def test_filling_in_ghosts_resets_head_cache(self):
321
 
        graph = self.make_known_graph(test_graph.with_ghost)
322
 
        self.assertEqual(set(['e', 'g']), graph.heads(['e', 'g']))
323
 
        # 'g' is filled in, and decends from 'e', so the heads result is now
324
 
        # different
325
 
        graph.add_node('g', ['e'])
326
 
        self.assertEqual(set(['g']), graph.heads(['e', 'g']))
327
 
 
328
263
 
329
264
class TestKnownGraphTopoSort(TestCaseWithKnownGraph):
330
265