21
21
from bzrlib import (
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 (
34
def caching_scenarios():
31
def load_tests(standard_tests, module, loader):
32
"""Parameterize tests for all versions of groupcompress."""
36
34
('python', {'module': _known_graph_py, 'do_cache': True}),
38
if compiled_known_graph_feature.available():
39
scenarios.append(('C', {'module': compiled_known_graph_feature.module,
44
def non_caching_scenarios():
46
37
('python-nocache', {'module': _known_graph_py, 'do_cache': False}),
48
if compiled_known_graph_feature.available():
50
('C-nocache', {'module': compiled_known_graph_feature.module,
55
load_tests = load_tests_apply_scenarios
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}))
46
# the compiled module isn't available, so we add a failing test
47
class FailWithoutFeature(tests.TestCase):
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,
61
class _CompiledKnownGraphFeature(tests.Feature):
65
import bzrlib._known_graph_pyx
70
def feature_name(self):
71
return 'bzrlib._known_graph_pyx'
73
CompiledKnownGraphFeature = _CompiledKnownGraphFeature()
141
154
self.assertGDFO(graph, 'a', 5)
142
155
self.assertGDFO(graph, 'c', 5)
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
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'))
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',
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)
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)
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)
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)
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)
212
158
class TestKnownGraphHeads(TestCaseWithKnownGraph):
214
scenarios = caching_scenarios() + non_caching_scenarios()
215
160
do_cache = None # Set by load_tests
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']))
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
323
graph.add_node('g', ['e'])
324
self.assertEqual(set(['g']), graph.heads(['e', 'g']))
327
264
class TestKnownGraphTopoSort(TestCaseWithKnownGraph):