~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_graph.py

  • Committer: Aaron Bentley
  • Date: 2008-11-23 16:38:39 UTC
  • mto: This revision was merged to the branch mainline in revision 3892.
  • Revision ID: aaron@aaronbentley.com-20081123163839-ew27m17130fz85v6
Update documentation

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2011 Canonical Ltd
 
1
# Copyright (C) 2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
from bzrlib import (
18
18
    errors,
19
19
    graph as _mod_graph,
 
20
    symbol_versioning,
20
21
    tests,
21
22
    )
22
23
from bzrlib.revision import NULL_REVISION
236
237
                    'e':['d'], 'f':['e'], 'g':['f'], 'h':['d'], 'i':['g'],
237
238
                    'j':['h'], 'k':['h', 'i'], 'l':['k'], 'm':['l'], 'n':['m'],
238
239
                    'o':['n'], 'p':['o'], 'q':['p'], 'r':['q'], 's':['r'],
239
 
                    't':['i', 's'], 'u':['s', 'j'],
 
240
                    't':['i', 's'], 'u':['s', 'j'], 
240
241
                    }
241
242
 
242
243
# Graph where different walkers will race to find the common and uncommon
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
 
554
525
        graph = self.make_graph(history_shortcut)
555
526
        self.assertEqual(set(['rev2b']), graph.find_lca('rev3a', 'rev3b'))
556
527
 
557
 
    def test_lefthand_distance_smoke(self):
558
 
        """A simple does it work test for graph.lefthand_distance(keys)."""
559
 
        graph = self.make_graph(history_shortcut)
560
 
        distance_graph = graph.find_lefthand_distances(['rev3b', 'rev2a'])
561
 
        self.assertEqual({'rev2a': 2, 'rev3b': 3}, distance_graph)
562
 
 
563
 
    def test_lefthand_distance_ghosts(self):
564
 
        """A simple does it work test for graph.lefthand_distance(keys)."""
565
 
        nodes = {'nonghost':[NULL_REVISION], 'toghost':['ghost']}
566
 
        graph = self.make_graph(nodes)
567
 
        distance_graph = graph.find_lefthand_distances(['nonghost', 'toghost'])
568
 
        self.assertEqual({'nonghost': 1, 'toghost': -1}, distance_graph)
569
 
 
570
528
    def test_recursive_unique_lca(self):
571
529
        """Test finding a unique least common ancestor.
572
530
 
703
661
        self.assertEqual((set(['e']), set(['f', 'g'])),
704
662
                         graph.find_difference('e', 'f'))
705
663
 
 
664
    def test_stacked_parents_provider(self):
 
665
        parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev3']})
 
666
        parents2 = _mod_graph.DictParentsProvider({'rev1': ['rev4']})
 
667
        stacked = _mod_graph._StackedParentsProvider([parents1, parents2])
 
668
        self.assertEqual({'rev1':['rev4'], 'rev2':['rev3']},
 
669
                         stacked.get_parent_map(['rev1', 'rev2']))
 
670
        self.assertEqual({'rev2':['rev3'], 'rev1':['rev4']},
 
671
                         stacked.get_parent_map(['rev2', 'rev1']))
 
672
        self.assertEqual({'rev2':['rev3']},
 
673
                         stacked.get_parent_map(['rev2', 'rev2']))
 
674
        self.assertEqual({'rev1':['rev4']},
 
675
                         stacked.get_parent_map(['rev1', 'rev1']))
 
676
 
706
677
    def test_iter_topo_order(self):
707
678
        graph = self.make_graph(ancestry_1)
708
679
        args = ['rev2a', 'rev3', 'rev1']
727
698
        instrumented_graph.is_ancestor('rev2a', 'rev2b')
728
699
        self.assertTrue('null:' not in instrumented_provider.calls)
729
700
 
730
 
    def test_is_between(self):
731
 
        graph = self.make_graph(ancestry_1)
732
 
        self.assertEqual(True, graph.is_between('null:', 'null:', 'null:'))
733
 
        self.assertEqual(True, graph.is_between('rev1', 'null:', 'rev1'))
734
 
        self.assertEqual(True, graph.is_between('rev1', 'rev1', 'rev4'))
735
 
        self.assertEqual(True, graph.is_between('rev4', 'rev1', 'rev4'))
736
 
        self.assertEqual(True, graph.is_between('rev3', 'rev1', 'rev4'))
737
 
        self.assertEqual(False, graph.is_between('rev4', 'rev1', 'rev3'))
738
 
        self.assertEqual(False, graph.is_between('rev1', 'rev2a', 'rev4'))
739
 
        self.assertEqual(False, graph.is_between('null:', 'rev1', 'rev4'))
740
 
 
741
701
    def test_is_ancestor_boundary(self):
742
702
        """Ensure that we avoid searching the whole graph.
743
 
 
 
703
        
744
704
        This requires searching through b as a common ancestor, so we
745
705
        can identify that e is common.
746
706
        """
766
726
        # 'a' is not in the ancestry of 'c', and 'g' is a ghost
767
727
        expected['g'] = None
768
728
        self.assertEqual(expected, dict(graph.iter_ancestry(['a', 'c'])))
769
 
        expected.pop('a')
 
729
        expected.pop('a') 
770
730
        self.assertEqual(expected, dict(graph.iter_ancestry(['c'])))
771
731
 
772
732
    def test_filter_candidate_lca(self):
874
834
 
875
835
    def _run_heads_break_deeper(self, graph_dict, search):
876
836
        """Run heads on a graph-as-a-dict.
877
 
 
 
837
        
878
838
        If the search asks for the parents of 'deeper' the test will fail.
879
839
        """
880
840
        class stub(object):
1021
981
        :param next: A callable to advance the search.
1022
982
        """
1023
983
        for seen, recipe, included_keys, starts, stops in instructions:
1024
 
            # Adjust for recipe contract changes that don't vary for all the
1025
 
            # current tests.
1026
 
            recipe = ('search',) + recipe
1027
984
            next()
1028
985
            if starts is not None:
1029
986
                search.start_searching(starts)
1030
987
            if stops is not None:
1031
988
                search.stop_searching_any(stops)
1032
 
            state = search.get_state()
1033
 
            self.assertEqual(set(included_keys), state[2])
 
989
            result = search.get_result()
 
990
            self.assertEqual(recipe, result.get_recipe())
 
991
            self.assertEqual(set(included_keys), result.get_keys())
1034
992
            self.assertEqual(seen, search.seen)
1035
993
 
1036
994
    def test_breadth_first_get_result_excludes_current_pending(self):
1041
999
            })
1042
1000
        search = graph._make_breadth_first_searcher(['head'])
1043
1001
        # 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)
 
1002
        result = search.get_result()
 
1003
        self.assertEqual((set(['head']), set(['head']), 0),
 
1004
            result.get_recipe())
 
1005
        self.assertEqual(set(), result.get_keys())
1047
1006
        self.assertEqual(set(), search.seen)
1048
1007
        # using next:
1049
1008
        expected = [
1072
1031
        # Starting with nothing and adding a search works:
1073
1032
        search.start_searching(['head'])
1074
1033
        # head has been seen:
1075
 
        state = search.get_state()
1076
 
        self.assertEqual((set(['head']), set(['child']), set(['head'])),
1077
 
            state)
 
1034
        result = search.get_result()
 
1035
        self.assertEqual((set(['head']), set(['child']), 1),
 
1036
            result.get_recipe())
 
1037
        self.assertEqual(set(['head']), result.get_keys())
1078
1038
        self.assertEqual(set(['head']), search.seen)
1079
1039
        # using next:
1080
1040
        expected = [
1109
1069
        search = graph._make_breadth_first_searcher(['head'])
1110
1070
        expected = [
1111
1071
            # NULL_REVISION and ghost1 have not been returned
1112
 
            (set(['head']),
1113
 
             (set(['head']), set(['child', NULL_REVISION, 'ghost1']), 1),
 
1072
            (set(['head']), (set(['head']), set(['child', 'ghost1']), 1),
1114
1073
             ['head'], None, [NULL_REVISION, 'ghost1']),
1115
1074
            # ghost1 has been returned, NULL_REVISION is to be returned in the
1116
1075
            # next iteration.
1231
1190
        self.assertSeenAndResult(expected, search, search.next)
1232
1191
        self.assertRaises(StopIteration, search.next)
1233
1192
        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)
 
1193
        result = search.get_result()
 
1194
        self.assertEqual((set(['ghost', 'head']), set(['ghost']), 2),
 
1195
            result.get_recipe())
 
1196
        self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
1239
1197
        # using next_with_ghosts:
1240
1198
        search = graph._make_breadth_first_searcher(['head'])
1241
1199
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1242
1200
        self.assertRaises(StopIteration, search.next)
1243
1201
        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)
 
1202
        result = search.get_result()
 
1203
        self.assertEqual((set(['ghost', 'head']), set(['ghost']), 2),
 
1204
            result.get_recipe())
 
1205
        self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
1249
1206
 
1250
1207
 
1251
1208
class TestFindUniqueAncestors(TestGraphBase):
1412
1369
        self.assertMergeOrder(['rev3', 'rev1'], graph, 'rev4', ['rev1', 'rev3'])
1413
1370
 
1414
1371
 
1415
 
class TestFindDescendants(TestGraphBase):
1416
 
 
1417
 
    def test_find_descendants_rev1_rev3(self):
1418
 
        graph = self.make_graph(ancestry_1)
1419
 
        descendants = graph.find_descendants('rev1', 'rev3')
1420
 
        self.assertEqual(set(['rev1', 'rev2a', 'rev3']), descendants)
1421
 
 
1422
 
    def test_find_descendants_rev1_rev4(self):
1423
 
        graph = self.make_graph(ancestry_1)
1424
 
        descendants = graph.find_descendants('rev1', 'rev4')
1425
 
        self.assertEqual(set(['rev1', 'rev2a', 'rev2b', 'rev3', 'rev4']),
1426
 
                         descendants)
1427
 
 
1428
 
    def test_find_descendants_rev2a_rev4(self):
1429
 
        graph = self.make_graph(ancestry_1)
1430
 
        descendants = graph.find_descendants('rev2a', 'rev4')
1431
 
        self.assertEqual(set(['rev2a', 'rev3', 'rev4']), descendants)
1432
 
 
1433
 
class TestFindLefthandMerger(TestGraphBase):
1434
 
 
1435
 
    def check_merger(self, result, ancestry, merged, tip):
1436
 
        graph = self.make_graph(ancestry)
1437
 
        self.assertEqual(result, graph.find_lefthand_merger(merged, tip))
1438
 
 
1439
 
    def test_find_lefthand_merger_rev2b(self):
1440
 
        self.check_merger('rev4', ancestry_1, 'rev2b', 'rev4')
1441
 
 
1442
 
    def test_find_lefthand_merger_rev2a(self):
1443
 
        self.check_merger('rev2a', ancestry_1, 'rev2a', 'rev4')
1444
 
 
1445
 
    def test_find_lefthand_merger_rev4(self):
1446
 
        self.check_merger(None, ancestry_1, 'rev4', 'rev2a')
1447
 
 
1448
 
    def test_find_lefthand_merger_f(self):
1449
 
        self.check_merger('i', complex_shortcut, 'f', 'm')
1450
 
 
1451
 
    def test_find_lefthand_merger_g(self):
1452
 
        self.check_merger('i', complex_shortcut, 'g', 'm')
1453
 
 
1454
 
    def test_find_lefthand_merger_h(self):
1455
 
        self.check_merger('n', complex_shortcut, 'h', 'n')
1456
 
 
1457
 
 
1458
 
class TestGetChildMap(TestGraphBase):
1459
 
 
1460
 
    def test_get_child_map(self):
1461
 
        graph = self.make_graph(ancestry_1)
1462
 
        child_map = graph.get_child_map(['rev4', 'rev3', 'rev2a', 'rev2b'])
1463
 
        self.assertEqual({'rev1': ['rev2a', 'rev2b'],
1464
 
                          'rev2a': ['rev3'],
1465
 
                          'rev2b': ['rev4'],
1466
 
                          'rev3': ['rev4']},
1467
 
                          child_map)
1468
 
 
1469
 
 
1470
1372
class TestCachingParentsProvider(tests.TestCase):
1471
 
    """These tests run with:
1472
 
 
1473
 
    self.inst_pp, a recording parents provider with a graph of a->b, and b is a
1474
 
    ghost.
1475
 
    self.caching_pp, a CachingParentsProvider layered on inst_pp.
1476
 
    """
1477
1373
 
1478
1374
    def setUp(self):
1479
1375
        super(TestCachingParentsProvider, self).setUp()
1480
 
        dict_pp = _mod_graph.DictParentsProvider({'a': ('b',)})
 
1376
        dict_pp = _mod_graph.DictParentsProvider({'a':('b',)})
1481
1377
        self.inst_pp = InstrumentedParentsProvider(dict_pp)
1482
1378
        self.caching_pp = _mod_graph.CachingParentsProvider(self.inst_pp)
1483
1379
 
1484
1380
    def test_get_parent_map(self):
1485
1381
        """Requesting the same revision should be returned from cache"""
1486
 
        self.assertEqual({}, self.caching_pp._cache)
 
1382
        self.assertEqual({}, self.caching_pp.get_cached_map())
1487
1383
        self.assertEqual({'a':('b',)}, self.caching_pp.get_parent_map(['a']))
1488
1384
        self.assertEqual(['a'], self.inst_pp.calls)
1489
1385
        self.assertEqual({'a':('b',)}, self.caching_pp.get_parent_map(['a']))
1490
1386
        # No new call, as it should have been returned from the cache
1491
1387
        self.assertEqual(['a'], self.inst_pp.calls)
1492
 
        self.assertEqual({'a':('b',)}, self.caching_pp._cache)
 
1388
        self.assertEqual({'a':('b',)}, self.caching_pp.get_cached_map())
1493
1389
 
1494
1390
    def test_get_parent_map_not_present(self):
1495
1391
        """The cache should also track when a revision doesn't exist"""
1498
1394
        self.assertEqual({}, self.caching_pp.get_parent_map(['b']))
1499
1395
        # No new calls
1500
1396
        self.assertEqual(['b'], self.inst_pp.calls)
 
1397
        self.assertEqual({'b':None}, self.caching_pp._parents_map)
1501
1398
 
1502
1399
    def test_get_parent_map_mixed(self):
1503
1400
        """Anything that can be returned from cache, should be"""
1515
1412
        # only present 1 time.
1516
1413
        self.assertEqual(['a', 'b'], sorted(self.inst_pp.calls))
1517
1414
 
1518
 
    def test_note_missing_key(self):
1519
 
        """After noting that a key is missing it is cached."""
1520
 
        self.caching_pp.note_missing_key('b')
1521
 
        self.assertEqual({}, self.caching_pp.get_parent_map(['b']))
1522
 
        self.assertEqual([], self.inst_pp.calls)
1523
 
        self.assertEqual(set(['b']), self.caching_pp.missing_keys)
1524
 
 
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
1415
 
1534
1416
class TestCachingParentsProviderExtras(tests.TestCaseWithTransport):
1535
 
    """Test the behaviour when parents are provided that were not requested."""
1536
1417
 
1537
1418
    def setUp(self):
1538
1419
        super(TestCachingParentsProviderExtras, self).setUp()
1550
1431
        self.assertEqual({'rev1': []},
1551
1432
                         self.caching_pp.get_parent_map(['rev1']))
1552
1433
        self.assertEqual(['rev1'], self.inst_pp.calls)
1553
 
        self.assertIs(None, self.caching_pp._cache)
1554
 
 
1555
 
    def test_cache_initially_empty(self):
1556
 
        self.assertEqual({}, self.caching_pp._cache)
 
1434
        self.assertIs(None, self.caching_pp.get_cached_map())
1557
1435
 
1558
1436
    def test_cached(self):
 
1437
        self.assertEqual({}, self.caching_pp.get_cached_map())
1559
1438
        self.assertEqual({'rev1': []},
1560
1439
                         self.caching_pp.get_parent_map(['rev1']))
1561
1440
        self.assertEqual(['rev1'], self.inst_pp.calls)
1562
1441
        self.assertEqual({'rev1': [], 'rev2': ['rev1']},
1563
 
                         self.caching_pp._cache)
 
1442
                         self.caching_pp.get_cached_map())
1564
1443
        self.assertEqual({'rev1': []},
1565
1444
                          self.caching_pp.get_parent_map(['rev1']))
1566
1445
        self.assertEqual(['rev1'], self.inst_pp.calls)
1567
 
 
1568
 
    def test_disable_cache_clears_cache(self):
1569
 
        # Put something in the cache
1570
 
        self.caching_pp.get_parent_map(['rev1'])
1571
 
        self.assertEqual(2, len(self.caching_pp._cache))
1572
 
        self.caching_pp.disable_cache()
1573
 
        self.assertIs(None, self.caching_pp._cache)
1574
 
 
1575
 
    def test_enable_cache_raises(self):
1576
 
        e = self.assertRaises(AssertionError, self.caching_pp.enable_cache)
1577
 
        self.assertEqual('Cache enabled when already enabled.', str(e))
1578
 
 
1579
 
    def test_cache_misses(self):
1580
 
        self.caching_pp.get_parent_map(['rev3'])
1581
 
        self.caching_pp.get_parent_map(['rev3'])
1582
 
        self.assertEqual(['rev3'], self.inst_pp.calls)
1583
 
 
1584
 
    def test_no_cache_misses(self):
1585
 
        self.caching_pp.disable_cache()
1586
 
        self.caching_pp.enable_cache(cache_misses=False)
1587
 
        self.caching_pp.get_parent_map(['rev3'])
1588
 
        self.caching_pp.get_parent_map(['rev3'])
1589
 
        self.assertEqual(['rev3', 'rev3'], self.inst_pp.calls)
 
1446
        self.caching_pp.disable_cache()
 
1447
        self.assertIs(None, self.caching_pp.get_cached_map())
1590
1448
 
1591
1449
    def test_cache_extras(self):
1592
1450
        self.assertEqual({}, self.caching_pp.get_parent_map(['rev3']))
1594
1452
                         self.caching_pp.get_parent_map(['rev2']))
1595
1453
        self.assertEqual(['rev3'], self.inst_pp.calls)
1596
1454
 
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
1455
 
1606
1456
class TestCollapseLinearRegions(tests.TestCase):
1607
1457
 
1639
1489
        # 2 and 3 cannot be removed because 1 has 2 parents
1640
1490
        d = {1:[2, 3], 2:[4], 4:[6], 3:[5], 5:[6], 6:[7], 7:[]}
1641
1491
        self.assertCollapsed(d, d)
1642
 
 
1643
 
 
1644
 
class TestGraphThunkIdsToKeys(tests.TestCase):
1645
 
 
1646
 
    def test_heads(self):
1647
 
        # A
1648
 
        # |\
1649
 
        # B C
1650
 
        # |/
1651
 
        # D
1652
 
        d = {('D',): [('B',), ('C',)], ('C',):[('A',)],
1653
 
             ('B',): [('A',)], ('A',): []}
1654
 
        g = _mod_graph.Graph(_mod_graph.DictParentsProvider(d))
1655
 
        graph_thunk = _mod_graph.GraphThunkIdsToKeys(g)
1656
 
        self.assertEqual(['D'], sorted(graph_thunk.heads(['D', 'A'])))
1657
 
        self.assertEqual(['D'], sorted(graph_thunk.heads(['D', 'B'])))
1658
 
        self.assertEqual(['D'], sorted(graph_thunk.heads(['D', 'C'])))
1659
 
        self.assertEqual(['B', 'C'], sorted(graph_thunk.heads(['B', 'C'])))
1660
 
 
1661
 
    def test_add_node(self):
1662
 
        d = {('C',):[('A',)], ('B',): [('A',)], ('A',): []}
1663
 
        g = _mod_graph.KnownGraph(d)
1664
 
        graph_thunk = _mod_graph.GraphThunkIdsToKeys(g)
1665
 
        graph_thunk.add_node("D", ["A", "C"])
1666
 
        self.assertEqual(['B', 'D'],
1667
 
            sorted(graph_thunk.heads(['D', 'B', 'A'])))
1668
 
 
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)