~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_graph.py

  • Committer: Jonathan Riddell
  • Date: 2011-05-16 11:27:37 UTC
  • mto: This revision was merged to the branch mainline in revision 5869.
  • Revision ID: jriddell@canonical.com-20110516112737-gep642p24rtzp3jt
userĀ guideĀ licence

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
    )
22
22
from bzrlib.revision import NULL_REVISION
23
23
from bzrlib.tests import TestCaseWithMemoryTransport
 
24
from bzrlib.symbol_versioning import deprecated_in
24
25
 
25
26
 
26
27
# Ancestry 1:
424
425
    def __init__(self, parents_provider):
425
426
        self.calls = []
426
427
        self._real_parents_provider = parents_provider
427
 
        get_cached = getattr(parents_provider, 'get_cached_parent_map', None)
428
 
        if get_cached is not None:
429
 
            # Only expose the underlying 'get_cached_parent_map' function if
430
 
            # the wrapped provider has it.
431
 
            self.get_cached_parent_map = self._get_cached_parent_map
432
428
 
433
429
    def get_parent_map(self, nodes):
434
430
        self.calls.extend(nodes)
435
431
        return self._real_parents_provider.get_parent_map(nodes)
436
432
 
437
 
    def _get_cached_parent_map(self, nodes):
438
 
        self.calls.append(('cached', sorted(nodes)))
439
 
        return self._real_parents_provider.get_cached_parent_map(nodes)
440
 
 
441
 
 
442
 
class SharedInstrumentedParentsProvider(object):
443
 
 
444
 
    def __init__(self, parents_provider, calls, info):
445
 
        self.calls = calls
446
 
        self.info = info
447
 
        self._real_parents_provider = parents_provider
448
 
        get_cached = getattr(parents_provider, 'get_cached_parent_map', None)
449
 
        if get_cached is not None:
450
 
            # Only expose the underlying 'get_cached_parent_map' function if
451
 
            # the wrapped provider has it.
452
 
            self.get_cached_parent_map = self._get_cached_parent_map
453
 
 
454
 
    def get_parent_map(self, nodes):
455
 
        self.calls.append((self.info, sorted(nodes)))
456
 
        return self._real_parents_provider.get_parent_map(nodes)
457
 
 
458
 
    def _get_cached_parent_map(self, nodes):
459
 
        self.calls.append((self.info, 'cached', sorted(nodes)))
460
 
        return self._real_parents_provider.get_cached_parent_map(nodes)
461
 
 
462
433
 
463
434
class TestGraphBase(tests.TestCase):
464
435
 
703
674
        self.assertEqual((set(['e']), set(['f', 'g'])),
704
675
                         graph.find_difference('e', 'f'))
705
676
 
 
677
 
 
678
    def test_stacked_parents_provider(self):
 
679
        parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev3']})
 
680
        parents2 = _mod_graph.DictParentsProvider({'rev1': ['rev4']})
 
681
        stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
 
682
        self.assertEqual({'rev1':['rev4'], 'rev2':['rev3']},
 
683
                         stacked.get_parent_map(['rev1', 'rev2']))
 
684
        self.assertEqual({'rev2':['rev3'], 'rev1':['rev4']},
 
685
                         stacked.get_parent_map(['rev2', 'rev1']))
 
686
        self.assertEqual({'rev2':['rev3']},
 
687
                         stacked.get_parent_map(['rev2', 'rev2']))
 
688
        self.assertEqual({'rev1':['rev4']},
 
689
                         stacked.get_parent_map(['rev1', 'rev1']))
 
690
    
 
691
    def test_stacked_parents_provider_overlapping(self):
 
692
        # rev2 is availible in both providers.
 
693
        # 1
 
694
        # |
 
695
        # 2
 
696
        parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev1']})
 
697
        parents2 = _mod_graph.DictParentsProvider({'rev2': ['rev1']})
 
698
        stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
 
699
        self.assertEqual({'rev2': ['rev1']},
 
700
                         stacked.get_parent_map(['rev2']))
 
701
 
706
702
    def test_iter_topo_order(self):
707
703
        graph = self.make_graph(ancestry_1)
708
704
        args = ['rev2a', 'rev3', 'rev1']
1029
1025
                search.start_searching(starts)
1030
1026
            if stops is not None:
1031
1027
                search.stop_searching_any(stops)
1032
 
            state = search.get_state()
1033
 
            self.assertEqual(set(included_keys), state[2])
 
1028
            result = search.get_result()
 
1029
            self.assertEqual(recipe, result.get_recipe())
 
1030
            self.assertEqual(set(included_keys), result.get_keys())
1034
1031
            self.assertEqual(seen, search.seen)
1035
1032
 
1036
1033
    def test_breadth_first_get_result_excludes_current_pending(self):
1041
1038
            })
1042
1039
        search = graph._make_breadth_first_searcher(['head'])
1043
1040
        # At the start, nothing has been seen, to its all excluded:
1044
 
        state = search.get_state()
1045
 
        self.assertEqual((set(['head']), set(['head']), set()),
1046
 
            state)
 
1041
        result = search.get_result()
 
1042
        self.assertEqual(('search', set(['head']), set(['head']), 0),
 
1043
            result.get_recipe())
 
1044
        self.assertEqual(set(), result.get_keys())
1047
1045
        self.assertEqual(set(), search.seen)
1048
1046
        # using next:
1049
1047
        expected = [
1072
1070
        # Starting with nothing and adding a search works:
1073
1071
        search.start_searching(['head'])
1074
1072
        # head has been seen:
1075
 
        state = search.get_state()
1076
 
        self.assertEqual((set(['head']), set(['child']), set(['head'])),
1077
 
            state)
 
1073
        result = search.get_result()
 
1074
        self.assertEqual(('search', set(['head']), set(['child']), 1),
 
1075
            result.get_recipe())
 
1076
        self.assertEqual(set(['head']), result.get_keys())
1078
1077
        self.assertEqual(set(['head']), search.seen)
1079
1078
        # using next:
1080
1079
        expected = [
1231
1230
        self.assertSeenAndResult(expected, search, search.next)
1232
1231
        self.assertRaises(StopIteration, search.next)
1233
1232
        self.assertEqual(set(['head', 'ghost', NULL_REVISION]), search.seen)
1234
 
        state = search.get_state()
1235
 
        self.assertEqual(
1236
 
            (set(['ghost', 'head']), set(['ghost']),
1237
 
                set(['head', NULL_REVISION])),
1238
 
            state)
 
1233
        result = search.get_result()
 
1234
        self.assertEqual(('search', set(['ghost', 'head']), set(['ghost']), 2),
 
1235
            result.get_recipe())
 
1236
        self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
1239
1237
        # using next_with_ghosts:
1240
1238
        search = graph._make_breadth_first_searcher(['head'])
1241
1239
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1242
1240
        self.assertRaises(StopIteration, search.next)
1243
1241
        self.assertEqual(set(['head', 'ghost', NULL_REVISION]), search.seen)
1244
 
        state = search.get_state()
1245
 
        self.assertEqual(
1246
 
            (set(['ghost', 'head']), set(['ghost']),
1247
 
                set(['head', NULL_REVISION])),
1248
 
            state)
 
1242
        result = search.get_result()
 
1243
        self.assertEqual(('search', set(['ghost', 'head']), set(['ghost']), 2),
 
1244
            result.get_recipe())
 
1245
        self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
1249
1246
 
1250
1247
 
1251
1248
class TestFindUniqueAncestors(TestGraphBase):
1477
1474
 
1478
1475
    def setUp(self):
1479
1476
        super(TestCachingParentsProvider, self).setUp()
1480
 
        dict_pp = _mod_graph.DictParentsProvider({'a': ('b',)})
 
1477
        dict_pp = _mod_graph.DictParentsProvider({'a':('b',)})
1481
1478
        self.inst_pp = InstrumentedParentsProvider(dict_pp)
1482
1479
        self.caching_pp = _mod_graph.CachingParentsProvider(self.inst_pp)
1483
1480
 
1522
1519
        self.assertEqual([], self.inst_pp.calls)
1523
1520
        self.assertEqual(set(['b']), self.caching_pp.missing_keys)
1524
1521
 
1525
 
    def test_get_cached_parent_map(self):
1526
 
        self.assertEqual({}, self.caching_pp.get_cached_parent_map(['a']))
1527
 
        self.assertEqual([], self.inst_pp.calls)
1528
 
        self.assertEqual({'a': ('b',)}, self.caching_pp.get_parent_map(['a']))
1529
 
        self.assertEqual(['a'], self.inst_pp.calls)
1530
 
        self.assertEqual({'a': ('b',)},
1531
 
                         self.caching_pp.get_cached_parent_map(['a']))
1532
 
 
1533
1522
 
1534
1523
class TestCachingParentsProviderExtras(tests.TestCaseWithTransport):
1535
1524
    """Test the behaviour when parents are provided that were not requested."""
1594
1583
                         self.caching_pp.get_parent_map(['rev2']))
1595
1584
        self.assertEqual(['rev3'], self.inst_pp.calls)
1596
1585
 
1597
 
    def test_extras_using_cached(self):
1598
 
        self.assertEqual({}, self.caching_pp.get_cached_parent_map(['rev3']))
1599
 
        self.assertEqual({}, self.caching_pp.get_parent_map(['rev3']))
1600
 
        self.assertEqual({'rev2': ['rev1']},
1601
 
                         self.caching_pp.get_cached_parent_map(['rev2']))
1602
 
        self.assertEqual(['rev3'], self.inst_pp.calls)
1603
 
 
1604
 
 
1605
1586
 
1606
1587
class TestCollapseLinearRegions(tests.TestCase):
1607
1588
 
1666
1647
        self.assertEqual(['B', 'D'],
1667
1648
            sorted(graph_thunk.heads(['D', 'B', 'A'])))
1668
1649
 
1669
 
    def test_merge_sort(self):
1670
 
        d = {('C',):[('A',)], ('B',): [('A',)], ('A',): []}
1671
 
        g = _mod_graph.KnownGraph(d)
1672
 
        graph_thunk = _mod_graph.GraphThunkIdsToKeys(g)
1673
 
        graph_thunk.add_node("D", ["A", "C"])
1674
 
        self.assertEqual([('C', 0, (2,), False), ('A', 0, (1,), True)],
1675
 
            [(n.key, n.merge_depth, n.revno, n.end_of_merge)
1676
 
                 for n in graph_thunk.merge_sort('C')])
1677
 
 
1678
 
 
1679
 
class TestStackedParentsProvider(tests.TestCase):
1680
 
 
1681
 
    def setUp(self):
1682
 
        super(TestStackedParentsProvider, self).setUp()
1683
 
        self.calls = []
1684
 
 
1685
 
    def get_shared_provider(self, info, ancestry, has_cached):
1686
 
        pp = _mod_graph.DictParentsProvider(ancestry)
1687
 
        if has_cached:
1688
 
            pp.get_cached_parent_map = pp.get_parent_map
1689
 
        return SharedInstrumentedParentsProvider(pp, self.calls, info)
1690
 
 
1691
 
    def test_stacked_parents_provider(self):
1692
 
        parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev3']})
1693
 
        parents2 = _mod_graph.DictParentsProvider({'rev1': ['rev4']})
1694
 
        stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
1695
 
        self.assertEqual({'rev1':['rev4'], 'rev2':['rev3']},
1696
 
                         stacked.get_parent_map(['rev1', 'rev2']))
1697
 
        self.assertEqual({'rev2':['rev3'], 'rev1':['rev4']},
1698
 
                         stacked.get_parent_map(['rev2', 'rev1']))
1699
 
        self.assertEqual({'rev2':['rev3']},
1700
 
                         stacked.get_parent_map(['rev2', 'rev2']))
1701
 
        self.assertEqual({'rev1':['rev4']},
1702
 
                         stacked.get_parent_map(['rev1', 'rev1']))
1703
 
 
1704
 
    def test_stacked_parents_provider_overlapping(self):
1705
 
        # rev2 is availible in both providers.
1706
 
        # 1
1707
 
        # |
1708
 
        # 2
1709
 
        parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev1']})
1710
 
        parents2 = _mod_graph.DictParentsProvider({'rev2': ['rev1']})
1711
 
        stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
1712
 
        self.assertEqual({'rev2': ['rev1']},
1713
 
                         stacked.get_parent_map(['rev2']))
1714
 
 
1715
 
    def test_handles_no_get_cached_parent_map(self):
1716
 
        # this shows that we both handle when a provider doesn't implement
1717
 
        # get_cached_parent_map
1718
 
        pp1 = self.get_shared_provider('pp1', {'rev2': ('rev1',)},
1719
 
                                       has_cached=False)
1720
 
        pp2 = self.get_shared_provider('pp2', {'rev2': ('rev1',)},
1721
 
                                       has_cached=True)
1722
 
        stacked = _mod_graph.StackedParentsProvider([pp1, pp2])
1723
 
        self.assertEqual({'rev2': ('rev1',)}, stacked.get_parent_map(['rev2']))
1724
 
        # No call on 'pp1' because it doesn't provide get_cached_parent_map
1725
 
        self.assertEqual([('pp2', 'cached', ['rev2'])], self.calls)
1726
 
 
1727
 
    def test_query_order(self):
1728
 
        # We should call get_cached_parent_map on all providers before we call
1729
 
        # get_parent_map. Further, we should track what entries we have found,
1730
 
        # and not re-try them.
1731
 
        pp1 = self.get_shared_provider('pp1', {'a': ()}, has_cached=True)
1732
 
        pp2 = self.get_shared_provider('pp2', {'c': ('b',)}, has_cached=False)
1733
 
        pp3 = self.get_shared_provider('pp3', {'b': ('a',)}, has_cached=True)
1734
 
        stacked = _mod_graph.StackedParentsProvider([pp1, pp2, pp3])
1735
 
        self.assertEqual({'a': (), 'b': ('a',), 'c': ('b',)},
1736
 
                         stacked.get_parent_map(['a', 'b', 'c', 'd']))
1737
 
        self.assertEqual([('pp1', 'cached', ['a', 'b', 'c', 'd']),
1738
 
                          # No call to pp2, because it doesn't have cached
1739
 
                          ('pp3', 'cached', ['b', 'c', 'd']),
1740
 
                          ('pp1', ['c', 'd']),
1741
 
                          ('pp2', ['c', 'd']),
1742
 
                          ('pp3', ['d']),
1743
 
                         ], self.calls)
 
1650
 
 
1651
class TestPendingAncestryResultGetKeys(TestCaseWithMemoryTransport):
 
1652
    """Tests for bzrlib.graph.PendingAncestryResult."""
 
1653
 
 
1654
    def test_get_keys(self):
 
1655
        builder = self.make_branch_builder('b')
 
1656
        builder.start_series()
 
1657
        builder.build_snapshot('rev-1', None, [
 
1658
            ('add', ('', 'root-id', 'directory', ''))])
 
1659
        builder.build_snapshot('rev-2', ['rev-1'], [])
 
1660
        builder.finish_series()
 
1661
        repo = builder.get_branch().repository
 
1662
        repo.lock_read()
 
1663
        self.addCleanup(repo.unlock)
 
1664
        result = _mod_graph.PendingAncestryResult(['rev-2'], repo)
 
1665
        self.assertEqual(set(['rev-1', 'rev-2']), set(result.get_keys()))
 
1666
 
 
1667
    def test_get_keys_excludes_ghosts(self):
 
1668
        builder = self.make_branch_builder('b')
 
1669
        builder.start_series()
 
1670
        builder.build_snapshot('rev-1', None, [
 
1671
            ('add', ('', 'root-id', 'directory', ''))])
 
1672
        builder.build_snapshot('rev-2', ['rev-1', 'ghost'], [])
 
1673
        builder.finish_series()
 
1674
        repo = builder.get_branch().repository
 
1675
        repo.lock_read()
 
1676
        self.addCleanup(repo.unlock)
 
1677
        result = _mod_graph.PendingAncestryResult(['rev-2'], repo)
 
1678
        self.assertEqual(sorted(['rev-1', 'rev-2']), sorted(result.get_keys()))
 
1679
 
 
1680
    def test_get_keys_excludes_null(self):
 
1681
        # Make a 'graph' with an iter_ancestry that returns NULL_REVISION
 
1682
        # somewhere other than the last element, which can happen in real
 
1683
        # ancestries.
 
1684
        class StubGraph(object):
 
1685
            def iter_ancestry(self, keys):
 
1686
                return [(NULL_REVISION, ()), ('foo', (NULL_REVISION,))]
 
1687
        result = _mod_graph.PendingAncestryResult(['rev-3'], None)
 
1688
        result_keys = result._get_keys(StubGraph())
 
1689
        # Only the non-null keys from the ancestry appear.
 
1690
        self.assertEqual(set(['foo']), set(result_keys))
 
1691
 
 
1692
 
 
1693
class TestPendingAncestryResultRefine(TestGraphBase):
 
1694
 
 
1695
    def test_refine(self):
 
1696
        # Used when pulling from a stacked repository, so test some revisions
 
1697
        # being satisfied from the stacking branch.
 
1698
        g = self.make_graph(
 
1699
            {"tip":["mid"], "mid":["base"], "tag":["base"],
 
1700
             "base":[NULL_REVISION], NULL_REVISION:[]})
 
1701
        result = _mod_graph.PendingAncestryResult(['tip', 'tag'], None)
 
1702
        result = result.refine(set(['tip']), set(['mid']))
 
1703
        self.assertEqual(set(['mid', 'tag']), result.heads)
 
1704
        result = result.refine(set(['mid', 'tag', 'base']),
 
1705
            set([NULL_REVISION]))
 
1706
        self.assertEqual(set([NULL_REVISION]), result.heads)
 
1707
        self.assertTrue(result.is_empty())
 
1708
 
 
1709
 
 
1710
class TestSearchResultRefine(TestGraphBase):
 
1711
 
 
1712
    def test_refine(self):
 
1713
        # Used when pulling from a stacked repository, so test some revisions
 
1714
        # being satisfied from the stacking branch.
 
1715
        g = self.make_graph(
 
1716
            {"tip":["mid"], "mid":["base"], "tag":["base"],
 
1717
             "base":[NULL_REVISION], NULL_REVISION:[]})
 
1718
        result = _mod_graph.SearchResult(set(['tip', 'tag']),
 
1719
            set([NULL_REVISION]), 4, set(['tip', 'mid', 'tag', 'base']))
 
1720
        result = result.refine(set(['tip']), set(['mid']))
 
1721
        recipe = result.get_recipe()
 
1722
        # We should be starting from tag (original head) and mid (seen ref)
 
1723
        self.assertEqual(set(['mid', 'tag']), recipe[1])
 
1724
        # We should be stopping at NULL (original stop) and tip (seen head)
 
1725
        self.assertEqual(set([NULL_REVISION, 'tip']), recipe[2])
 
1726
        self.assertEqual(3, recipe[3])
 
1727
        result = result.refine(set(['mid', 'tag', 'base']),
 
1728
            set([NULL_REVISION]))
 
1729
        recipe = result.get_recipe()
 
1730
        # We should be starting from nothing (NULL was known as a cut point)
 
1731
        self.assertEqual(set([]), recipe[1])
 
1732
        # We should be stopping at NULL (original stop) and tip (seen head) and
 
1733
        # tag (seen head) and mid(seen mid-point head). We could come back and
 
1734
        # define this as not including mid, for minimal results, but it is
 
1735
        # still 'correct' to include mid, and simpler/easier.
 
1736
        self.assertEqual(set([NULL_REVISION, 'tip', 'tag', 'mid']), recipe[2])
 
1737
        self.assertEqual(0, recipe[3])
 
1738
        self.assertTrue(result.is_empty())