~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_graph.py

  • Committer: Vincent Ladeuil
  • Date: 2010-02-10 15:46:03 UTC
  • mfrom: (4985.3.21 update)
  • mto: This revision was merged to the branch mainline in revision 5021.
  • Revision ID: v.ladeuil+lp@free.fr-20100210154603-k4no1gvfuqpzrw7p
Update performs two merges in a more logical order but stop on conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2008, 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
from bzrlib import (
18
18
    errors,
22
22
    )
23
23
from bzrlib.revision import NULL_REVISION
24
24
from bzrlib.tests import TestCaseWithMemoryTransport
 
25
from bzrlib.symbol_versioning import deprecated_in
25
26
 
26
27
 
27
28
# Ancestry 1:
237
238
                    'e':['d'], 'f':['e'], 'g':['f'], 'h':['d'], 'i':['g'],
238
239
                    'j':['h'], 'k':['h', 'i'], 'l':['k'], 'm':['l'], 'n':['m'],
239
240
                    'o':['n'], 'p':['o'], 'q':['p'], 'r':['q'], 's':['r'],
240
 
                    't':['i', 's'], 'u':['s', 'j'], 
 
241
                    't':['i', 's'], 'u':['s', 'j'],
241
242
                    }
242
243
 
243
244
# Graph where different walkers will race to find the common and uncommon
525
526
        graph = self.make_graph(history_shortcut)
526
527
        self.assertEqual(set(['rev2b']), graph.find_lca('rev3a', 'rev3b'))
527
528
 
 
529
    def test_lefthand_distance_smoke(self):
 
530
        """A simple does it work test for graph.lefthand_distance(keys)."""
 
531
        graph = self.make_graph(history_shortcut)
 
532
        distance_graph = graph.find_lefthand_distances(['rev3b', 'rev2a'])
 
533
        self.assertEqual({'rev2a': 2, 'rev3b': 3}, distance_graph)
 
534
 
 
535
    def test_lefthand_distance_ghosts(self):
 
536
        """A simple does it work test for graph.lefthand_distance(keys)."""
 
537
        nodes = {'nonghost':[NULL_REVISION], 'toghost':['ghost']}
 
538
        graph = self.make_graph(nodes)
 
539
        distance_graph = graph.find_lefthand_distances(['nonghost', 'toghost'])
 
540
        self.assertEqual({'nonghost': 1, 'toghost': -1}, distance_graph)
 
541
 
528
542
    def test_recursive_unique_lca(self):
529
543
        """Test finding a unique least common ancestor.
530
544
 
661
675
        self.assertEqual((set(['e']), set(['f', 'g'])),
662
676
                         graph.find_difference('e', 'f'))
663
677
 
 
678
 
664
679
    def test_stacked_parents_provider(self):
665
680
        parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev3']})
666
681
        parents2 = _mod_graph.DictParentsProvider({'rev1': ['rev4']})
667
 
        stacked = _mod_graph._StackedParentsProvider([parents1, parents2])
 
682
        stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
 
683
        self.assertEqual({'rev1':['rev4'], 'rev2':['rev3']},
 
684
                         stacked.get_parent_map(['rev1', 'rev2']))
 
685
        self.assertEqual({'rev2':['rev3'], 'rev1':['rev4']},
 
686
                         stacked.get_parent_map(['rev2', 'rev1']))
 
687
        self.assertEqual({'rev2':['rev3']},
 
688
                         stacked.get_parent_map(['rev2', 'rev2']))
 
689
        self.assertEqual({'rev1':['rev4']},
 
690
                         stacked.get_parent_map(['rev1', 'rev1']))
 
691
    
 
692
    def test_stacked_parents_provider_overlapping(self):
 
693
        # rev2 is availible in both providers.
 
694
        # 1
 
695
        # |
 
696
        # 2
 
697
        parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev1']})
 
698
        parents2 = _mod_graph.DictParentsProvider({'rev2': ['rev1']})
 
699
        stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
 
700
        self.assertEqual({'rev2': ['rev1']},
 
701
                         stacked.get_parent_map(['rev2']))
 
702
 
 
703
    def test__stacked_parents_provider_deprecated(self):
 
704
        parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev3']})
 
705
        parents2 = _mod_graph.DictParentsProvider({'rev1': ['rev4']})
 
706
        stacked = self.applyDeprecated(deprecated_in((1, 16, 0)),
 
707
                    _mod_graph._StackedParentsProvider, [parents1, parents2])
668
708
        self.assertEqual({'rev1':['rev4'], 'rev2':['rev3']},
669
709
                         stacked.get_parent_map(['rev1', 'rev2']))
670
710
        self.assertEqual({'rev2':['rev3'], 'rev1':['rev4']},
711
751
 
712
752
    def test_is_ancestor_boundary(self):
713
753
        """Ensure that we avoid searching the whole graph.
714
 
        
 
754
 
715
755
        This requires searching through b as a common ancestor, so we
716
756
        can identify that e is common.
717
757
        """
737
777
        # 'a' is not in the ancestry of 'c', and 'g' is a ghost
738
778
        expected['g'] = None
739
779
        self.assertEqual(expected, dict(graph.iter_ancestry(['a', 'c'])))
740
 
        expected.pop('a') 
 
780
        expected.pop('a')
741
781
        self.assertEqual(expected, dict(graph.iter_ancestry(['c'])))
742
782
 
743
783
    def test_filter_candidate_lca(self):
845
885
 
846
886
    def _run_heads_break_deeper(self, graph_dict, search):
847
887
        """Run heads on a graph-as-a-dict.
848
 
        
 
888
 
849
889
        If the search asks for the parents of 'deeper' the test will fail.
850
890
        """
851
891
        class stub(object):
992
1032
        :param next: A callable to advance the search.
993
1033
        """
994
1034
        for seen, recipe, included_keys, starts, stops in instructions:
 
1035
            # Adjust for recipe contract changes that don't vary for all the
 
1036
            # current tests.
 
1037
            recipe = ('search',) + recipe
995
1038
            next()
996
1039
            if starts is not None:
997
1040
                search.start_searching(starts)
1011
1054
        search = graph._make_breadth_first_searcher(['head'])
1012
1055
        # At the start, nothing has been seen, to its all excluded:
1013
1056
        result = search.get_result()
1014
 
        self.assertEqual((set(['head']), set(['head']), 0),
 
1057
        self.assertEqual(('search', set(['head']), set(['head']), 0),
1015
1058
            result.get_recipe())
1016
1059
        self.assertEqual(set(), result.get_keys())
1017
1060
        self.assertEqual(set(), search.seen)
1043
1086
        search.start_searching(['head'])
1044
1087
        # head has been seen:
1045
1088
        result = search.get_result()
1046
 
        self.assertEqual((set(['head']), set(['child']), 1),
 
1089
        self.assertEqual(('search', set(['head']), set(['child']), 1),
1047
1090
            result.get_recipe())
1048
1091
        self.assertEqual(set(['head']), result.get_keys())
1049
1092
        self.assertEqual(set(['head']), search.seen)
1080
1123
        search = graph._make_breadth_first_searcher(['head'])
1081
1124
        expected = [
1082
1125
            # NULL_REVISION and ghost1 have not been returned
1083
 
            (set(['head']), (set(['head']), set(['child', 'ghost1']), 1),
 
1126
            (set(['head']),
 
1127
             (set(['head']), set(['child', NULL_REVISION, 'ghost1']), 1),
1084
1128
             ['head'], None, [NULL_REVISION, 'ghost1']),
1085
1129
            # ghost1 has been returned, NULL_REVISION is to be returned in the
1086
1130
            # next iteration.
1202
1246
        self.assertRaises(StopIteration, search.next)
1203
1247
        self.assertEqual(set(['head', 'ghost', NULL_REVISION]), search.seen)
1204
1248
        result = search.get_result()
1205
 
        self.assertEqual((set(['ghost', 'head']), set(['ghost']), 2),
 
1249
        self.assertEqual(('search', set(['ghost', 'head']), set(['ghost']), 2),
1206
1250
            result.get_recipe())
1207
1251
        self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
1208
1252
        # using next_with_ghosts:
1211
1255
        self.assertRaises(StopIteration, search.next)
1212
1256
        self.assertEqual(set(['head', 'ghost', NULL_REVISION]), search.seen)
1213
1257
        result = search.get_result()
1214
 
        self.assertEqual((set(['ghost', 'head']), set(['ghost']), 2),
 
1258
        self.assertEqual(('search', set(['ghost', 'head']), set(['ghost']), 2),
1215
1259
            result.get_recipe())
1216
1260
        self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
1217
1261
 
1381
1425
 
1382
1426
 
1383
1427
class TestCachingParentsProvider(tests.TestCase):
 
1428
    """These tests run with:
 
1429
 
 
1430
    self.inst_pp, a recording parents provider with a graph of a->b, and b is a
 
1431
    ghost.
 
1432
    self.caching_pp, a CachingParentsProvider layered on inst_pp.
 
1433
    """
1384
1434
 
1385
1435
    def setUp(self):
1386
1436
        super(TestCachingParentsProvider, self).setUp()
1405
1455
        self.assertEqual({}, self.caching_pp.get_parent_map(['b']))
1406
1456
        # No new calls
1407
1457
        self.assertEqual(['b'], self.inst_pp.calls)
1408
 
        self.assertEqual({'b':None}, self.caching_pp._cache)
1409
1458
 
1410
1459
    def test_get_parent_map_mixed(self):
1411
1460
        """Anything that can be returned from cache, should be"""
1423
1472
        # only present 1 time.
1424
1473
        self.assertEqual(['a', 'b'], sorted(self.inst_pp.calls))
1425
1474
 
 
1475
    def test_note_missing_key(self):
 
1476
        """After noting that a key is missing it is cached."""
 
1477
        self.caching_pp.note_missing_key('b')
 
1478
        self.assertEqual({}, self.caching_pp.get_parent_map(['b']))
 
1479
        self.assertEqual([], self.inst_pp.calls)
 
1480
        self.assertEqual(set(['b']), self.caching_pp.missing_keys)
 
1481
 
1426
1482
 
1427
1483
class TestCachingParentsProviderExtras(tests.TestCaseWithTransport):
1428
1484
    """Test the behaviour when parents are provided that were not requested."""
1524
1580
        # 2 and 3 cannot be removed because 1 has 2 parents
1525
1581
        d = {1:[2, 3], 2:[4], 4:[6], 3:[5], 5:[6], 6:[7], 7:[]}
1526
1582
        self.assertCollapsed(d, d)
 
1583
 
 
1584
 
 
1585
class TestGraphThunkIdsToKeys(tests.TestCase):
 
1586
 
 
1587
    def test_heads(self):
 
1588
        # A
 
1589
        # |\
 
1590
        # B C
 
1591
        # |/
 
1592
        # D
 
1593
        d = {('D',): [('B',), ('C',)], ('C',):[('A',)],
 
1594
             ('B',): [('A',)], ('A',): []}
 
1595
        g = _mod_graph.Graph(_mod_graph.DictParentsProvider(d))
 
1596
        graph_thunk = _mod_graph.GraphThunkIdsToKeys(g)
 
1597
        self.assertEqual(['D'], sorted(graph_thunk.heads(['D', 'A'])))
 
1598
        self.assertEqual(['D'], sorted(graph_thunk.heads(['D', 'B'])))
 
1599
        self.assertEqual(['D'], sorted(graph_thunk.heads(['D', 'C'])))
 
1600
        self.assertEqual(['B', 'C'], sorted(graph_thunk.heads(['B', 'C'])))
 
1601
 
 
1602
 
 
1603
class TestPendingAncestryResultGetKeys(TestCaseWithMemoryTransport):
 
1604
    """Tests for bzrlib.graph.PendingAncestryResult."""
 
1605
 
 
1606
    def test_get_keys(self):
 
1607
        builder = self.make_branch_builder('b')
 
1608
        builder.start_series()
 
1609
        builder.build_snapshot('rev-1', None, [
 
1610
            ('add', ('', 'root-id', 'directory', ''))])
 
1611
        builder.build_snapshot('rev-2', ['rev-1'], [])
 
1612
        builder.finish_series()
 
1613
        repo = builder.get_branch().repository
 
1614
        repo.lock_read()
 
1615
        self.addCleanup(repo.unlock)
 
1616
        result = _mod_graph.PendingAncestryResult(['rev-2'], repo)
 
1617
        self.assertEqual(set(['rev-1', 'rev-2']), set(result.get_keys()))
 
1618
 
 
1619
    def test_get_keys_excludes_ghosts(self):
 
1620
        builder = self.make_branch_builder('b')
 
1621
        builder.start_series()
 
1622
        builder.build_snapshot('rev-1', None, [
 
1623
            ('add', ('', 'root-id', 'directory', ''))])
 
1624
        builder.build_snapshot('rev-2', ['rev-1', 'ghost'], [])
 
1625
        builder.finish_series()
 
1626
        repo = builder.get_branch().repository
 
1627
        repo.lock_read()
 
1628
        self.addCleanup(repo.unlock)
 
1629
        result = _mod_graph.PendingAncestryResult(['rev-2'], repo)
 
1630
        self.assertEqual(sorted(['rev-1', 'rev-2']), sorted(result.get_keys()))
 
1631
 
 
1632
    def test_get_keys_excludes_null(self):
 
1633
        # Make a 'graph' with an iter_ancestry that returns NULL_REVISION
 
1634
        # somewhere other than the last element, which can happen in real
 
1635
        # ancestries.
 
1636
        class StubGraph(object):
 
1637
            def iter_ancestry(self, keys):
 
1638
                return [(NULL_REVISION, ()), ('foo', (NULL_REVISION,))]
 
1639
        result = _mod_graph.PendingAncestryResult(['rev-3'], None)
 
1640
        result_keys = result._get_keys(StubGraph())
 
1641
        # Only the non-null keys from the ancestry appear.
 
1642
        self.assertEqual(set(['foo']), set(result_keys))
 
1643
 
 
1644
 
 
1645
class TestPendingAncestryResultRefine(TestGraphBase):
 
1646
 
 
1647
    def test_refine(self):
 
1648
        # Used when pulling from a stacked repository, so test some revisions
 
1649
        # being satisfied from the stacking branch.
 
1650
        g = self.make_graph(
 
1651
            {"tip":["mid"], "mid":["base"], "tag":["base"],
 
1652
             "base":[NULL_REVISION], NULL_REVISION:[]})
 
1653
        result = _mod_graph.PendingAncestryResult(['tip', 'tag'], None)
 
1654
        result = result.refine(set(['tip']), set(['mid']))
 
1655
        self.assertEqual(set(['mid', 'tag']), result.heads)
 
1656
        result = result.refine(set(['mid', 'tag', 'base']),
 
1657
            set([NULL_REVISION]))
 
1658
        self.assertEqual(set([NULL_REVISION]), result.heads)
 
1659
        self.assertTrue(result.is_empty())
 
1660
 
 
1661
 
 
1662
class TestSearchResultRefine(TestGraphBase):
 
1663
 
 
1664
    def test_refine(self):
 
1665
        # Used when pulling from a stacked repository, so test some revisions
 
1666
        # being satisfied from the stacking branch.
 
1667
        g = self.make_graph(
 
1668
            {"tip":["mid"], "mid":["base"], "tag":["base"],
 
1669
             "base":[NULL_REVISION], NULL_REVISION:[]})
 
1670
        result = _mod_graph.SearchResult(set(['tip', 'tag']),
 
1671
            set([NULL_REVISION]), 4, set(['tip', 'mid', 'tag', 'base']))
 
1672
        result = result.refine(set(['tip']), set(['mid']))
 
1673
        recipe = result.get_recipe()
 
1674
        # We should be starting from tag (original head) and mid (seen ref)
 
1675
        self.assertEqual(set(['mid', 'tag']), recipe[1])
 
1676
        # We should be stopping at NULL (original stop) and tip (seen head)
 
1677
        self.assertEqual(set([NULL_REVISION, 'tip']), recipe[2])
 
1678
        self.assertEqual(3, recipe[3])
 
1679
        result = result.refine(set(['mid', 'tag', 'base']),
 
1680
            set([NULL_REVISION]))
 
1681
        recipe = result.get_recipe()
 
1682
        # We should be starting from nothing (NULL was known as a cut point)
 
1683
        self.assertEqual(set([]), recipe[1])
 
1684
        # We should be stopping at NULL (original stop) and tip (seen head) and
 
1685
        # tag (seen head) and mid(seen mid-point head). We could come back and
 
1686
        # define this as not including mid, for minimal results, but it is
 
1687
        # still 'correct' to include mid, and simpler/easier.
 
1688
        self.assertEqual(set([NULL_REVISION, 'tip', 'tag', 'mid']), recipe[2])
 
1689
        self.assertEqual(0, recipe[3])
 
1690
        self.assertTrue(result.is_empty())