~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_graph.py

(gz) Never raise KnownFailure in tests,
 use knownFailure method instead (Martin [gz])

Show diffs side-by-side

added added

removed removed

Lines of Context:
424
424
    def __init__(self, parents_provider):
425
425
        self.calls = []
426
426
        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
427
 
433
428
    def get_parent_map(self, nodes):
434
429
        self.calls.extend(nodes)
435
430
        return self._real_parents_provider.get_parent_map(nodes)
436
431
 
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
432
 
463
433
class TestGraphBase(tests.TestCase):
464
434
 
703
673
        self.assertEqual((set(['e']), set(['f', 'g'])),
704
674
                         graph.find_difference('e', 'f'))
705
675
 
 
676
 
 
677
    def test_stacked_parents_provider(self):
 
678
        parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev3']})
 
679
        parents2 = _mod_graph.DictParentsProvider({'rev1': ['rev4']})
 
680
        stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
 
681
        self.assertEqual({'rev1':['rev4'], 'rev2':['rev3']},
 
682
                         stacked.get_parent_map(['rev1', 'rev2']))
 
683
        self.assertEqual({'rev2':['rev3'], 'rev1':['rev4']},
 
684
                         stacked.get_parent_map(['rev2', 'rev1']))
 
685
        self.assertEqual({'rev2':['rev3']},
 
686
                         stacked.get_parent_map(['rev2', 'rev2']))
 
687
        self.assertEqual({'rev1':['rev4']},
 
688
                         stacked.get_parent_map(['rev1', 'rev1']))
 
689
    
 
690
    def test_stacked_parents_provider_overlapping(self):
 
691
        # rev2 is availible in both providers.
 
692
        # 1
 
693
        # |
 
694
        # 2
 
695
        parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev1']})
 
696
        parents2 = _mod_graph.DictParentsProvider({'rev2': ['rev1']})
 
697
        stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
 
698
        self.assertEqual({'rev2': ['rev1']},
 
699
                         stacked.get_parent_map(['rev2']))
 
700
 
706
701
    def test_iter_topo_order(self):
707
702
        graph = self.make_graph(ancestry_1)
708
703
        args = ['rev2a', 'rev3', 'rev1']
1029
1024
                search.start_searching(starts)
1030
1025
            if stops is not None:
1031
1026
                search.stop_searching_any(stops)
1032
 
            state = search.get_state()
1033
 
            self.assertEqual(set(included_keys), state[2])
 
1027
            result = search.get_result()
 
1028
            self.assertEqual(recipe, result.get_recipe())
 
1029
            self.assertEqual(set(included_keys), result.get_keys())
1034
1030
            self.assertEqual(seen, search.seen)
1035
1031
 
1036
1032
    def test_breadth_first_get_result_excludes_current_pending(self):
1041
1037
            })
1042
1038
        search = graph._make_breadth_first_searcher(['head'])
1043
1039
        # 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)
 
1040
        result = search.get_result()
 
1041
        self.assertEqual(('search', set(['head']), set(['head']), 0),
 
1042
            result.get_recipe())
 
1043
        self.assertEqual(set(), result.get_keys())
1047
1044
        self.assertEqual(set(), search.seen)
1048
1045
        # using next:
1049
1046
        expected = [
1072
1069
        # Starting with nothing and adding a search works:
1073
1070
        search.start_searching(['head'])
1074
1071
        # head has been seen:
1075
 
        state = search.get_state()
1076
 
        self.assertEqual((set(['head']), set(['child']), set(['head'])),
1077
 
            state)
 
1072
        result = search.get_result()
 
1073
        self.assertEqual(('search', set(['head']), set(['child']), 1),
 
1074
            result.get_recipe())
 
1075
        self.assertEqual(set(['head']), result.get_keys())
1078
1076
        self.assertEqual(set(['head']), search.seen)
1079
1077
        # using next:
1080
1078
        expected = [
1231
1229
        self.assertSeenAndResult(expected, search, search.next)
1232
1230
        self.assertRaises(StopIteration, search.next)
1233
1231
        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)
 
1232
        result = search.get_result()
 
1233
        self.assertEqual(('search', set(['ghost', 'head']), set(['ghost']), 2),
 
1234
            result.get_recipe())
 
1235
        self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
1239
1236
        # using next_with_ghosts:
1240
1237
        search = graph._make_breadth_first_searcher(['head'])
1241
1238
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1242
1239
        self.assertRaises(StopIteration, search.next)
1243
1240
        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)
 
1241
        result = search.get_result()
 
1242
        self.assertEqual(('search', set(['ghost', 'head']), set(['ghost']), 2),
 
1243
            result.get_recipe())
 
1244
        self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
1249
1245
 
1250
1246
 
1251
1247
class TestFindUniqueAncestors(TestGraphBase):
1477
1473
 
1478
1474
    def setUp(self):
1479
1475
        super(TestCachingParentsProvider, self).setUp()
1480
 
        dict_pp = _mod_graph.DictParentsProvider({'a': ('b',)})
 
1476
        dict_pp = _mod_graph.DictParentsProvider({'a':('b',)})
1481
1477
        self.inst_pp = InstrumentedParentsProvider(dict_pp)
1482
1478
        self.caching_pp = _mod_graph.CachingParentsProvider(self.inst_pp)
1483
1479
 
1522
1518
        self.assertEqual([], self.inst_pp.calls)
1523
1519
        self.assertEqual(set(['b']), self.caching_pp.missing_keys)
1524
1520
 
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
1521
 
1534
1522
class TestCachingParentsProviderExtras(tests.TestCaseWithTransport):
1535
1523
    """Test the behaviour when parents are provided that were not requested."""
1594
1582
                         self.caching_pp.get_parent_map(['rev2']))
1595
1583
        self.assertEqual(['rev3'], self.inst_pp.calls)
1596
1584
 
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
1585
 
1606
1586
class TestCollapseLinearRegions(tests.TestCase):
1607
1587
 
1676
1656
                 for n in graph_thunk.merge_sort('C')])
1677
1657
 
1678
1658
 
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)
 
1659
class TestPendingAncestryResultGetKeys(TestCaseWithMemoryTransport):
 
1660
    """Tests for bzrlib.graph.PendingAncestryResult."""
 
1661
 
 
1662
    def test_get_keys(self):
 
1663
        builder = self.make_branch_builder('b')
 
1664
        builder.start_series()
 
1665
        builder.build_snapshot('rev-1', None, [
 
1666
            ('add', ('', 'root-id', 'directory', ''))])
 
1667
        builder.build_snapshot('rev-2', ['rev-1'], [])
 
1668
        builder.finish_series()
 
1669
        repo = builder.get_branch().repository
 
1670
        repo.lock_read()
 
1671
        self.addCleanup(repo.unlock)
 
1672
        result = _mod_graph.PendingAncestryResult(['rev-2'], repo)
 
1673
        self.assertEqual(set(['rev-1', 'rev-2']), set(result.get_keys()))
 
1674
 
 
1675
    def test_get_keys_excludes_ghosts(self):
 
1676
        builder = self.make_branch_builder('b')
 
1677
        builder.start_series()
 
1678
        builder.build_snapshot('rev-1', None, [
 
1679
            ('add', ('', 'root-id', 'directory', ''))])
 
1680
        builder.build_snapshot('rev-2', ['rev-1', 'ghost'], [])
 
1681
        builder.finish_series()
 
1682
        repo = builder.get_branch().repository
 
1683
        repo.lock_read()
 
1684
        self.addCleanup(repo.unlock)
 
1685
        result = _mod_graph.PendingAncestryResult(['rev-2'], repo)
 
1686
        self.assertEqual(sorted(['rev-1', 'rev-2']), sorted(result.get_keys()))
 
1687
 
 
1688
    def test_get_keys_excludes_null(self):
 
1689
        # Make a 'graph' with an iter_ancestry that returns NULL_REVISION
 
1690
        # somewhere other than the last element, which can happen in real
 
1691
        # ancestries.
 
1692
        class StubGraph(object):
 
1693
            def iter_ancestry(self, keys):
 
1694
                return [(NULL_REVISION, ()), ('foo', (NULL_REVISION,))]
 
1695
        result = _mod_graph.PendingAncestryResult(['rev-3'], None)
 
1696
        result_keys = result._get_keys(StubGraph())
 
1697
        # Only the non-null keys from the ancestry appear.
 
1698
        self.assertEqual(set(['foo']), set(result_keys))
 
1699
 
 
1700
 
 
1701
class TestPendingAncestryResultRefine(TestGraphBase):
 
1702
 
 
1703
    def test_refine(self):
 
1704
        # Used when pulling from a stacked repository, so test some revisions
 
1705
        # being satisfied from the stacking branch.
 
1706
        g = self.make_graph(
 
1707
            {"tip":["mid"], "mid":["base"], "tag":["base"],
 
1708
             "base":[NULL_REVISION], NULL_REVISION:[]})
 
1709
        result = _mod_graph.PendingAncestryResult(['tip', 'tag'], None)
 
1710
        result = result.refine(set(['tip']), set(['mid']))
 
1711
        self.assertEqual(set(['mid', 'tag']), result.heads)
 
1712
        result = result.refine(set(['mid', 'tag', 'base']),
 
1713
            set([NULL_REVISION]))
 
1714
        self.assertEqual(set([NULL_REVISION]), result.heads)
 
1715
        self.assertTrue(result.is_empty())
 
1716
 
 
1717
 
 
1718
class TestSearchResultRefine(TestGraphBase):
 
1719
 
 
1720
    def test_refine(self):
 
1721
        # Used when pulling from a stacked repository, so test some revisions
 
1722
        # being satisfied from the stacking branch.
 
1723
        g = self.make_graph(
 
1724
            {"tip":["mid"], "mid":["base"], "tag":["base"],
 
1725
             "base":[NULL_REVISION], NULL_REVISION:[]})
 
1726
        result = _mod_graph.SearchResult(set(['tip', 'tag']),
 
1727
            set([NULL_REVISION]), 4, set(['tip', 'mid', 'tag', 'base']))
 
1728
        result = result.refine(set(['tip']), set(['mid']))
 
1729
        recipe = result.get_recipe()
 
1730
        # We should be starting from tag (original head) and mid (seen ref)
 
1731
        self.assertEqual(set(['mid', 'tag']), recipe[1])
 
1732
        # We should be stopping at NULL (original stop) and tip (seen head)
 
1733
        self.assertEqual(set([NULL_REVISION, 'tip']), recipe[2])
 
1734
        self.assertEqual(3, recipe[3])
 
1735
        result = result.refine(set(['mid', 'tag', 'base']),
 
1736
            set([NULL_REVISION]))
 
1737
        recipe = result.get_recipe()
 
1738
        # We should be starting from nothing (NULL was known as a cut point)
 
1739
        self.assertEqual(set([]), recipe[1])
 
1740
        # We should be stopping at NULL (original stop) and tip (seen head) and
 
1741
        # tag (seen head) and mid(seen mid-point head). We could come back and
 
1742
        # define this as not including mid, for minimal results, but it is
 
1743
        # still 'correct' to include mid, and simpler/easier.
 
1744
        self.assertEqual(set([NULL_REVISION, 'tip', 'tag', 'mid']), recipe[2])
 
1745
        self.assertEqual(0, recipe[3])
 
1746
        self.assertTrue(result.is_empty())