662
661
self.assertEqual((set(['e']), set(['f', 'g'])),
663
662
graph.find_difference('e', 'f'))
666
664
def test_stacked_parents_provider(self):
667
665
parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev3']})
668
666
parents2 = _mod_graph.DictParentsProvider({'rev1': ['rev4']})
669
stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
670
self.assertEqual({'rev1':['rev4'], 'rev2':['rev3']},
671
stacked.get_parent_map(['rev1', 'rev2']))
672
self.assertEqual({'rev2':['rev3'], 'rev1':['rev4']},
673
stacked.get_parent_map(['rev2', 'rev1']))
674
self.assertEqual({'rev2':['rev3']},
675
stacked.get_parent_map(['rev2', 'rev2']))
676
self.assertEqual({'rev1':['rev4']},
677
stacked.get_parent_map(['rev1', 'rev1']))
679
def test_stacked_parents_provider_overlapping(self):
680
# rev2 is availible in both providers.
684
parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev1']})
685
parents2 = _mod_graph.DictParentsProvider({'rev2': ['rev1']})
686
stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
687
self.assertEqual({'rev2': ['rev1']},
688
stacked.get_parent_map(['rev2']))
690
def test__stacked_parents_provider_deprecated(self):
691
parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev3']})
692
parents2 = _mod_graph.DictParentsProvider({'rev1': ['rev4']})
693
stacked = self.applyDeprecated(deprecated_in((1, 16, 0)),
694
_mod_graph._StackedParentsProvider, [parents1, parents2])
667
stacked = _mod_graph._StackedParentsProvider([parents1, parents2])
695
668
self.assertEqual({'rev1':['rev4'], 'rev2':['rev3']},
696
669
stacked.get_parent_map(['rev1', 'rev2']))
697
670
self.assertEqual({'rev2':['rev3'], 'rev1':['rev4']},
725
698
instrumented_graph.is_ancestor('rev2a', 'rev2b')
726
699
self.assertTrue('null:' not in instrumented_provider.calls)
728
def test_is_between(self):
729
graph = self.make_graph(ancestry_1)
730
self.assertEqual(True, graph.is_between('null:', 'null:', 'null:'))
731
self.assertEqual(True, graph.is_between('rev1', 'null:', 'rev1'))
732
self.assertEqual(True, graph.is_between('rev1', 'rev1', 'rev4'))
733
self.assertEqual(True, graph.is_between('rev4', 'rev1', 'rev4'))
734
self.assertEqual(True, graph.is_between('rev3', 'rev1', 'rev4'))
735
self.assertEqual(False, graph.is_between('rev4', 'rev1', 'rev3'))
736
self.assertEqual(False, graph.is_between('rev1', 'rev2a', 'rev4'))
737
self.assertEqual(False, graph.is_between('null:', 'rev1', 'rev4'))
739
701
def test_is_ancestor_boundary(self):
740
702
"""Ensure that we avoid searching the whole graph.
742
704
This requires searching through b as a common ancestor, so we
743
705
can identify that e is common.
1124
1082
search = graph._make_breadth_first_searcher(['head'])
1125
1083
self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1127
def test_breadth_first_stop_searching_late(self):
1128
# A client should be able to say 'stop node X' and have it excluded
1129
# from the result even if X was seen in an older iteration of the
1131
graph = self.make_graph({
1134
'child':[NULL_REVISION],
1137
search = graph._make_breadth_first_searcher(['head'])
1139
(set(['head']), (set(['head']), set(['middle']), 1),
1140
['head'], None, None),
1141
(set(['head', 'middle']), (set(['head']), set(['child']), 2),
1142
['head', 'middle'], None, None),
1143
# 'middle' came from the previous iteration, but we don't stop
1144
# searching it until *after* advancing the searcher.
1145
(set(['head', 'middle', 'child']),
1146
(set(['head']), set(['middle', 'child']), 1),
1147
['head'], None, ['middle', 'child']),
1149
self.assertSeenAndResult(expected, search, search.next)
1150
# using next_with_ghosts:
1151
search = graph._make_breadth_first_searcher(['head'])
1152
self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1154
1085
def test_breadth_first_get_result_ghosts_are_excluded(self):
1155
1086
graph = self.make_graph({
1156
1087
'head':['child', 'ghost'],
1459
1385
# only present 1 time.
1460
1386
self.assertEqual(['a', 'b'], sorted(self.inst_pp.calls))
1462
def test_note_missing_key(self):
1463
"""After noting that a key is missing it is cached."""
1464
self.caching_pp.note_missing_key('b')
1465
self.assertEqual({}, self.caching_pp.get_parent_map(['b']))
1466
self.assertEqual([], self.inst_pp.calls)
1467
self.assertEqual(set(['b']), self.caching_pp.missing_keys)
1470
class TestCachingParentsProviderExtras(tests.TestCaseWithTransport):
1471
"""Test the behaviour when parents are provided that were not requested."""
1474
super(TestCachingParentsProviderExtras, self).setUp()
1475
class ExtraParentsProvider(object):
1477
def get_parent_map(self, keys):
1478
return {'rev1': [], 'rev2': ['rev1',]}
1480
self.inst_pp = InstrumentedParentsProvider(ExtraParentsProvider())
1481
self.caching_pp = _mod_graph.CachingParentsProvider(
1482
get_parent_map=self.inst_pp.get_parent_map)
1484
def test_uncached(self):
1485
self.caching_pp.disable_cache()
1486
self.assertEqual({'rev1': []},
1487
self.caching_pp.get_parent_map(['rev1']))
1488
self.assertEqual(['rev1'], self.inst_pp.calls)
1489
self.assertIs(None, self.caching_pp._cache)
1491
def test_cache_initially_empty(self):
1492
self.assertEqual({}, self.caching_pp._cache)
1494
def test_cached(self):
1495
self.assertEqual({'rev1': []},
1496
self.caching_pp.get_parent_map(['rev1']))
1497
self.assertEqual(['rev1'], self.inst_pp.calls)
1498
self.assertEqual({'rev1': [], 'rev2': ['rev1']},
1499
self.caching_pp._cache)
1500
self.assertEqual({'rev1': []},
1501
self.caching_pp.get_parent_map(['rev1']))
1502
self.assertEqual(['rev1'], self.inst_pp.calls)
1504
def test_disable_cache_clears_cache(self):
1505
# Put something in the cache
1506
self.caching_pp.get_parent_map(['rev1'])
1507
self.assertEqual(2, len(self.caching_pp._cache))
1508
self.caching_pp.disable_cache()
1509
self.assertIs(None, self.caching_pp._cache)
1511
def test_enable_cache_raises(self):
1512
e = self.assertRaises(AssertionError, self.caching_pp.enable_cache)
1513
self.assertEqual('Cache enabled when already enabled.', str(e))
1515
def test_cache_misses(self):
1516
self.caching_pp.get_parent_map(['rev3'])
1517
self.caching_pp.get_parent_map(['rev3'])
1518
self.assertEqual(['rev3'], self.inst_pp.calls)
1520
def test_no_cache_misses(self):
1521
self.caching_pp.disable_cache()
1522
self.caching_pp.enable_cache(cache_misses=False)
1523
self.caching_pp.get_parent_map(['rev3'])
1524
self.caching_pp.get_parent_map(['rev3'])
1525
self.assertEqual(['rev3', 'rev3'], self.inst_pp.calls)
1527
def test_cache_extras(self):
1528
self.assertEqual({}, self.caching_pp.get_parent_map(['rev3']))
1529
self.assertEqual({'rev2': ['rev1']},
1530
self.caching_pp.get_parent_map(['rev2']))
1531
self.assertEqual(['rev3'], self.inst_pp.calls)
1534
1389
class TestCollapseLinearRegions(tests.TestCase):
1567
1422
# 2 and 3 cannot be removed because 1 has 2 parents
1568
1423
d = {1:[2, 3], 2:[4], 4:[6], 3:[5], 5:[6], 6:[7], 7:[]}
1569
1424
self.assertCollapsed(d, d)
1572
class TestPendingAncestryResultGetKeys(TestCaseWithMemoryTransport):
1573
"""Tests for bzrlib.graph.PendingAncestryResult."""
1575
def test_get_keys(self):
1576
builder = self.make_branch_builder('b')
1577
builder.start_series()
1578
builder.build_snapshot('rev-1', None, [
1579
('add', ('', 'root-id', 'directory', ''))])
1580
builder.build_snapshot('rev-2', ['rev-1'], [])
1581
builder.finish_series()
1582
repo = builder.get_branch().repository
1584
self.addCleanup(repo.unlock)
1585
result = _mod_graph.PendingAncestryResult(['rev-2'], repo)
1586
self.assertEqual(set(['rev-1', 'rev-2']), set(result.get_keys()))
1588
def test_get_keys_excludes_ghosts(self):
1589
builder = self.make_branch_builder('b')
1590
builder.start_series()
1591
builder.build_snapshot('rev-1', None, [
1592
('add', ('', 'root-id', 'directory', ''))])
1593
builder.build_snapshot('rev-2', ['rev-1', 'ghost'], [])
1594
builder.finish_series()
1595
repo = builder.get_branch().repository
1597
self.addCleanup(repo.unlock)
1598
result = _mod_graph.PendingAncestryResult(['rev-2'], repo)
1599
self.assertEqual(sorted(['rev-1', 'rev-2']), sorted(result.get_keys()))
1601
def test_get_keys_excludes_null(self):
1602
# Make a 'graph' with an iter_ancestry that returns NULL_REVISION
1603
# somewhere other than the last element, which can happen in real
1605
class StubGraph(object):
1606
def iter_ancestry(self, keys):
1607
return [(NULL_REVISION, ()), ('foo', (NULL_REVISION,))]
1608
result = _mod_graph.PendingAncestryResult(['rev-3'], None)
1609
result_keys = result._get_keys(StubGraph())
1610
# Only the non-null keys from the ancestry appear.
1611
self.assertEqual(set(['foo']), set(result_keys))
1614
class TestPendingAncestryResultRefine(TestGraphBase):
1616
def test_refine(self):
1617
# Used when pulling from a stacked repository, so test some revisions
1618
# being satisfied from the stacking branch.
1619
g = self.make_graph(
1620
{"tip":["mid"], "mid":["base"], "tag":["base"],
1621
"base":[NULL_REVISION], NULL_REVISION:[]})
1622
result = _mod_graph.PendingAncestryResult(['tip', 'tag'], None)
1623
result = result.refine(set(['tip']), set(['mid']))
1624
self.assertEqual(set(['mid', 'tag']), result.heads)
1625
result = result.refine(set(['mid', 'tag', 'base']),
1626
set([NULL_REVISION]))
1627
self.assertEqual(set([NULL_REVISION]), result.heads)
1628
self.assertTrue(result.is_empty())
1631
class TestSearchResultRefine(TestGraphBase):
1633
def test_refine(self):
1634
# Used when pulling from a stacked repository, so test some revisions
1635
# being satisfied from the stacking branch.
1636
g = self.make_graph(
1637
{"tip":["mid"], "mid":["base"], "tag":["base"],
1638
"base":[NULL_REVISION], NULL_REVISION:[]})
1639
result = _mod_graph.SearchResult(set(['tip', 'tag']),
1640
set([NULL_REVISION]), 4, set(['tip', 'mid', 'tag', 'base']))
1641
result = result.refine(set(['tip']), set(['mid']))
1642
recipe = result.get_recipe()
1643
# We should be starting from tag (original head) and mid (seen ref)
1644
self.assertEqual(set(['mid', 'tag']), recipe[1])
1645
# We should be stopping at NULL (original stop) and tip (seen head)
1646
self.assertEqual(set([NULL_REVISION, 'tip']), recipe[2])
1647
self.assertEqual(3, recipe[3])
1648
result = result.refine(set(['mid', 'tag', 'base']),
1649
set([NULL_REVISION]))
1650
recipe = result.get_recipe()
1651
# We should be starting from nothing (NULL was known as a cut point)
1652
self.assertEqual(set([]), recipe[1])
1653
# We should be stopping at NULL (original stop) and tip (seen head) and
1654
# tag (seen head) and mid(seen mid-point head). We could come back and
1655
# define this as not including mid, for minimal results, but it is
1656
# still 'correct' to include mid, and simpler/easier.
1657
self.assertEqual(set([NULL_REVISION, 'tip', 'tag', 'mid']), recipe[2])
1658
self.assertEqual(0, recipe[3])
1659
self.assertTrue(result.is_empty())