~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_graph.py

  • Committer: Martin Packman
  • Date: 2012-03-27 17:32:19 UTC
  • mto: (6437.54.3 2.5)
  • mto: This revision was merged to the branch mainline in revision 6525.
  • Revision ID: martin.packman@canonical.com-20120327173219-401pil42gke8j0xh
Fall back to sys.prefix not /usr when looking for .mo files

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
427
432
 
428
433
    def get_parent_map(self, nodes):
429
434
        self.calls.extend(nodes)
430
435
        return self._real_parents_provider.get_parent_map(nodes)
431
436
 
 
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
 
432
462
 
433
463
class TestGraphBase(tests.TestCase):
434
464
 
673
703
        self.assertEqual((set(['e']), set(['f', 'g'])),
674
704
                         graph.find_difference('e', 'f'))
675
705
 
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
 
 
701
706
    def test_iter_topo_order(self):
702
707
        graph = self.make_graph(ancestry_1)
703
708
        args = ['rev2a', 'rev3', 'rev1']
1024
1029
                search.start_searching(starts)
1025
1030
            if stops is not None:
1026
1031
                search.stop_searching_any(stops)
1027
 
            result = search.get_result()
1028
 
            self.assertEqual(recipe, result.get_recipe())
1029
 
            self.assertEqual(set(included_keys), result.get_keys())
 
1032
            state = search.get_state()
 
1033
            self.assertEqual(set(included_keys), state[2])
1030
1034
            self.assertEqual(seen, search.seen)
1031
1035
 
1032
1036
    def test_breadth_first_get_result_excludes_current_pending(self):
1037
1041
            })
1038
1042
        search = graph._make_breadth_first_searcher(['head'])
1039
1043
        # At the start, nothing has been seen, to its all excluded:
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())
 
1044
        state = search.get_state()
 
1045
        self.assertEqual((set(['head']), set(['head']), set()),
 
1046
            state)
1044
1047
        self.assertEqual(set(), search.seen)
1045
1048
        # using next:
1046
1049
        expected = [
1069
1072
        # Starting with nothing and adding a search works:
1070
1073
        search.start_searching(['head'])
1071
1074
        # head has been seen:
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())
 
1075
        state = search.get_state()
 
1076
        self.assertEqual((set(['head']), set(['child']), set(['head'])),
 
1077
            state)
1076
1078
        self.assertEqual(set(['head']), search.seen)
1077
1079
        # using next:
1078
1080
        expected = [
1229
1231
        self.assertSeenAndResult(expected, search, search.next)
1230
1232
        self.assertRaises(StopIteration, search.next)
1231
1233
        self.assertEqual(set(['head', 'ghost', NULL_REVISION]), search.seen)
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())
 
1234
        state = search.get_state()
 
1235
        self.assertEqual(
 
1236
            (set(['ghost', 'head']), set(['ghost']),
 
1237
                set(['head', NULL_REVISION])),
 
1238
            state)
1236
1239
        # using next_with_ghosts:
1237
1240
        search = graph._make_breadth_first_searcher(['head'])
1238
1241
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1239
1242
        self.assertRaises(StopIteration, search.next)
1240
1243
        self.assertEqual(set(['head', 'ghost', NULL_REVISION]), search.seen)
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())
 
1244
        state = search.get_state()
 
1245
        self.assertEqual(
 
1246
            (set(['ghost', 'head']), set(['ghost']),
 
1247
                set(['head', NULL_REVISION])),
 
1248
            state)
1245
1249
 
1246
1250
 
1247
1251
class TestFindUniqueAncestors(TestGraphBase):
1473
1477
 
1474
1478
    def setUp(self):
1475
1479
        super(TestCachingParentsProvider, self).setUp()
1476
 
        dict_pp = _mod_graph.DictParentsProvider({'a':('b',)})
 
1480
        dict_pp = _mod_graph.DictParentsProvider({'a': ('b',)})
1477
1481
        self.inst_pp = InstrumentedParentsProvider(dict_pp)
1478
1482
        self.caching_pp = _mod_graph.CachingParentsProvider(self.inst_pp)
1479
1483
 
1518
1522
        self.assertEqual([], self.inst_pp.calls)
1519
1523
        self.assertEqual(set(['b']), self.caching_pp.missing_keys)
1520
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
 
1521
1533
 
1522
1534
class TestCachingParentsProviderExtras(tests.TestCaseWithTransport):
1523
1535
    """Test the behaviour when parents are provided that were not requested."""
1582
1594
                         self.caching_pp.get_parent_map(['rev2']))
1583
1595
        self.assertEqual(['rev3'], self.inst_pp.calls)
1584
1596
 
 
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
 
1585
1605
 
1586
1606
class TestCollapseLinearRegions(tests.TestCase):
1587
1607
 
1656
1676
                 for n in graph_thunk.merge_sort('C')])
1657
1677
 
1658
1678
 
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())
 
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)