~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__known_graph.py

  • Committer: John Arbash Meinel
  • Date: 2009-11-18 15:47:16 UTC
  • mto: This revision was merged to the branch mainline in revision 4810.
  • Revision ID: john@arbash-meinel.com-20091118154716-meiszr5ej7ohas3v
Move all the stat comparison and platform checkning code to assertEqualStat.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010, 2011 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
20
20
 
21
21
from bzrlib import (
22
22
    errors,
 
23
    graph as _mod_graph,
23
24
    _known_graph_py,
24
25
    tests,
25
26
    )
26
27
from bzrlib.tests import test_graph
27
28
from bzrlib.revision import NULL_REVISION
28
 
from bzrlib.tests.scenarios import load_tests_apply_scenarios
29
 
from bzrlib.tests import (
30
 
    features,
31
 
    )
32
 
 
33
 
 
34
 
def caching_scenarios():
 
29
 
 
30
 
 
31
def load_tests(standard_tests, module, loader):
 
32
    """Parameterize tests for all versions of groupcompress."""
35
33
    scenarios = [
36
34
        ('python', {'module': _known_graph_py, 'do_cache': True}),
37
35
    ]
38
 
    if compiled_known_graph_feature.available():
39
 
        scenarios.append(('C', {'module': compiled_known_graph_feature.module,
40
 
                                'do_cache': True}))
41
 
    return scenarios
42
 
 
43
 
 
44
 
def non_caching_scenarios():
45
 
    scenarios = [
 
36
    caching_scenarios = [
46
37
        ('python-nocache', {'module': _known_graph_py, 'do_cache': False}),
47
38
    ]
48
 
    if compiled_known_graph_feature.available():
49
 
        scenarios.append(
50
 
            ('C-nocache', {'module': compiled_known_graph_feature.module,
51
 
                           'do_cache': False}))
52
 
    return scenarios
53
 
 
54
 
 
55
 
load_tests = load_tests_apply_scenarios
56
 
 
57
 
 
58
 
compiled_known_graph_feature = features.ModuleAvailableFeature(
59
 
    'bzrlib._known_graph_pyx')
 
39
    suite = loader.suiteClass()
 
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}))
 
45
    else:
 
46
        # the compiled module isn't available, so we add a failing test
 
47
        class FailWithoutFeature(tests.TestCase):
 
48
            def test_fail(self):
 
49
                self.requireFeature(CompiledKnownGraphFeature)
 
50
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
 
51
    # TestKnownGraphHeads needs to be permutated with and without caching.
 
52
    # All other TestKnownGraph tests only need to be tested across module
 
53
    heads_suite, other_suite = tests.split_suite_by_condition(
 
54
        standard_tests, tests.condition_isinstance(TestKnownGraphHeads))
 
55
    suite = tests.multiply_tests(other_suite, scenarios, suite)
 
56
    suite = tests.multiply_tests(heads_suite, scenarios + caching_scenarios,
 
57
                                 suite)
 
58
    return suite
 
59
 
 
60
 
 
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()
60
74
 
61
75
 
62
76
#  a
71
85
 
72
86
class TestCaseWithKnownGraph(tests.TestCase):
73
87
 
74
 
    scenarios = caching_scenarios()
75
88
    module = None # Set by load_tests
76
89
 
77
90
    def make_known_graph(self, ancestry):
141
154
        self.assertGDFO(graph, 'a', 5)
142
155
        self.assertGDFO(graph, 'c', 5)
143
156
 
144
 
    def test_add_existing_node(self):
145
 
        graph = self.make_known_graph(test_graph.ancestry_1)
146
 
        # Add a node that already exists with identical content
147
 
        # This is a 'no-op'
148
 
        self.assertGDFO(graph, 'rev4', 5)
149
 
        graph.add_node('rev4', ['rev3', 'rev2b'])
150
 
        self.assertGDFO(graph, 'rev4', 5)
151
 
        # This also works if we use a tuple rather than a list
152
 
        graph.add_node('rev4', ('rev3', 'rev2b'))
153
 
 
154
 
    def test_add_existing_node_mismatched_parents(self):
155
 
        graph = self.make_known_graph(test_graph.ancestry_1)
156
 
        self.assertRaises(ValueError, graph.add_node, 'rev4',
157
 
                          ['rev2b', 'rev3'])
158
 
 
159
 
    def test_add_node_with_ghost_parent(self):
160
 
        graph = self.make_known_graph(test_graph.ancestry_1)
161
 
        graph.add_node('rev5', ['rev2b', 'revGhost'])
162
 
        self.assertGDFO(graph, 'rev5', 4)
163
 
        self.assertGDFO(graph, 'revGhost', 1)
164
 
 
165
 
    def test_add_new_root(self):
166
 
        graph = self.make_known_graph(test_graph.ancestry_1)
167
 
        graph.add_node('rev5', [])
168
 
        self.assertGDFO(graph, 'rev5', 1)
169
 
 
170
 
    def test_add_with_all_ghost_parents(self):
171
 
        graph = self.make_known_graph(test_graph.ancestry_1)
172
 
        graph.add_node('rev5', ['ghost'])
173
 
        self.assertGDFO(graph, 'rev5', 2)
174
 
        self.assertGDFO(graph, 'ghost', 1)
175
 
 
176
 
    def test_gdfo_after_add_node(self):
177
 
        graph = self.make_known_graph(test_graph.ancestry_1)
178
 
        self.assertEqual([], graph.get_child_keys('rev4'))
179
 
        graph.add_node('rev5', ['rev4'])
180
 
        self.assertEqual(['rev4'], graph.get_parent_keys('rev5'))
181
 
        self.assertEqual(['rev5'], graph.get_child_keys('rev4'))
182
 
        self.assertEqual([], graph.get_child_keys('rev5'))
183
 
        self.assertGDFO(graph, 'rev5', 6)
184
 
        graph.add_node('rev6', ['rev2b'])
185
 
        graph.add_node('rev7', ['rev6'])
186
 
        graph.add_node('rev8', ['rev7', 'rev5'])
187
 
        self.assertGDFO(graph, 'rev5', 6)
188
 
        self.assertGDFO(graph, 'rev6', 4)
189
 
        self.assertGDFO(graph, 'rev7', 5)
190
 
        self.assertGDFO(graph, 'rev8', 7)
191
 
 
192
 
    def test_fill_in_ghost(self):
193
 
        graph = self.make_known_graph(test_graph.with_ghost)
194
 
        # Add in a couple nodes and then fill in the 'ghost' so that it should
195
 
        # cause renumbering of children nodes
196
 
        graph.add_node('x', [])
197
 
        graph.add_node('y', ['x'])
198
 
        graph.add_node('z', ['y'])
199
 
        graph.add_node('g', ['z'])
200
 
        self.assertGDFO(graph, 'f', 2)
201
 
        self.assertGDFO(graph, 'e', 3)
202
 
        self.assertGDFO(graph, 'x', 1)
203
 
        self.assertGDFO(graph, 'y', 2)
204
 
        self.assertGDFO(graph, 'z', 3)
205
 
        self.assertGDFO(graph, 'g', 4)
206
 
        self.assertGDFO(graph, 'b', 4)
207
 
        self.assertGDFO(graph, 'd', 5)
208
 
        self.assertGDFO(graph, 'a', 5)
209
 
        self.assertGDFO(graph, 'c', 6)
210
 
 
211
157
 
212
158
class TestKnownGraphHeads(TestCaseWithKnownGraph):
213
159
 
214
 
    scenarios = caching_scenarios() + non_caching_scenarios()
215
160
    do_cache = None # Set by load_tests
216
161
 
217
162
    def test_heads_null(self):
315
260
        self.assertEqual(set(['a', 'c']), graph.heads(['a', 'c', 'e', 'g']))
316
261
        self.assertEqual(set(['a', 'c']), graph.heads(['a', 'c', 'f']))
317
262
 
318
 
    def test_filling_in_ghosts_resets_head_cache(self):
319
 
        graph = self.make_known_graph(test_graph.with_ghost)
320
 
        self.assertEqual(set(['e', 'g']), graph.heads(['e', 'g']))
321
 
        # 'g' is filled in, and decends from 'e', so the heads result is now
322
 
        # different
323
 
        graph.add_node('g', ['e'])
324
 
        self.assertEqual(set(['g']), graph.heads(['e', 'g']))
325
 
 
326
263
 
327
264
class TestKnownGraphTopoSort(TestCaseWithKnownGraph):
328
265