~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_graph.py

Merge next_with_ghosts support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
244
244
        self.calls = []
245
245
        self._real_parents_provider = parents_provider
246
246
 
247
 
    def get_parents(self, nodes):
248
 
        self.calls.extend(nodes)
249
 
        return self._real_parents_provider.get_parents(nodes)
250
 
 
251
247
    def get_parent_map(self, nodes):
252
248
        self.calls.extend(nodes)
253
249
        return self._real_parents_provider.get_parent_map(nodes)
450
446
        self.assertEqual((set(['e']), set(['f', 'g'])),
451
447
                         graph.find_difference('e', 'f'))
452
448
 
453
 
    def test_stacked_parents_provider_get_parents(self):
454
 
        parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev3']})
455
 
        parents2 = _mod_graph.DictParentsProvider({'rev1': ['rev4']})
456
 
        stacked = _mod_graph._StackedParentsProvider([parents1, parents2])
457
 
        self.assertEqual([['rev4',], ['rev3']],
458
 
             self.applyDeprecated(symbol_versioning.one_one,
459
 
                                  stacked.get_parents, ['rev1', 'rev2']))
460
 
        self.assertEqual([['rev3',], ['rev4']],
461
 
             self.applyDeprecated(symbol_versioning.one_one,
462
 
                                  stacked.get_parents, ['rev2', 'rev1']))
463
 
        self.assertEqual([['rev3',], ['rev3']],
464
 
             self.applyDeprecated(symbol_versioning.one_one,
465
 
                         stacked.get_parents, ['rev2', 'rev2']))
466
 
        self.assertEqual([['rev4',], ['rev4']],
467
 
             self.applyDeprecated(symbol_versioning.one_one,
468
 
                         stacked.get_parents, ['rev1', 'rev1']))
469
 
 
470
449
    def test_stacked_parents_provider(self):
471
450
        parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev3']})
472
451
        parents2 = _mod_graph.DictParentsProvider({'rev1': ['rev4']})
626
605
        """
627
606
        class stub(object):
628
607
            pass
629
 
        def get_parents(keys):
630
 
            result = []
631
 
            for key in keys:
632
 
                if key == 'deeper':
633
 
                    self.fail('key deeper was accessed')
634
 
                result.append(graph_dict[key])
635
 
            return result
636
608
        def get_parent_map(keys):
637
609
            result = {}
638
610
            for key in keys:
641
613
                result[key] = graph_dict[key]
642
614
            return result
643
615
        an_obj = stub()
644
 
        an_obj.get_parents = get_parents
645
616
        an_obj.get_parent_map = get_parent_map
646
617
        graph = _mod_graph.Graph(an_obj)
647
618
        return graph.heads(search)
681
652
        self.assertEqual(set(['h1', 'h2']),
682
653
            self._run_heads_break_deeper(graph_dict, ['h1', 'h2']))
683
654
 
 
655
    def test_breadth_first_search_start_ghosts(self):
 
656
        parent_graph = {}
 
657
        parents_provider = InstrumentedParentsProvider(
 
658
            _mod_graph.DictParentsProvider(parent_graph))
 
659
        graph = _mod_graph.Graph(parents_provider)
 
660
        # with_ghosts reports the ghosts
 
661
        search = graph._make_breadth_first_searcher(['a-ghost'])
 
662
        self.assertEqual((set(), set(['a-ghost'])), search.next_with_ghosts())
 
663
        self.assertRaises(StopIteration, search.next_with_ghosts)
 
664
        # next includes them
 
665
        search = graph._make_breadth_first_searcher(['a-ghost'])
 
666
        self.assertEqual(set(['a-ghost']), search.next())
 
667
        self.assertRaises(StopIteration, search.next)
 
668
 
 
669
    def test_breadth_first_search_deep_ghosts(self):
 
670
        parent_graph = {
 
671
            'head':['present'],
 
672
            'present':['child', 'ghost'],
 
673
            'child':[],
 
674
            }
 
675
        parents_provider = InstrumentedParentsProvider(
 
676
            _mod_graph.DictParentsProvider(parent_graph))
 
677
        graph = _mod_graph.Graph(parents_provider)
 
678
        # with_ghosts reports the ghosts
 
679
        search = graph._make_breadth_first_searcher(['head'])
 
680
        self.assertEqual((set(['head']), set()), search.next_with_ghosts())
 
681
        self.assertEqual((set(['present']), set()), search.next_with_ghosts())
 
682
        self.assertEqual((set(['child']), set(['ghost'])),
 
683
            search.next_with_ghosts())
 
684
        self.assertRaises(StopIteration, search.next_with_ghosts)
 
685
        # next includes them
 
686
        search = graph._make_breadth_first_searcher(['head'])
 
687
        self.assertEqual(set(['head']), search.next())
 
688
        self.assertEqual(set(['present']), search.next())
 
689
        self.assertEqual(set(['child', 'ghost']),
 
690
            search.next())
 
691
        self.assertRaises(StopIteration, search.next)
 
692
 
 
693
    def test_breadth_first_search_change_next_to_next_with_ghosts(self):
 
694
        # To make the API robust, we allow changing from next() to
 
695
        # next_with_ghosts() and vice verca.
 
696
        parent_graph = {
 
697
            'head':['present'],
 
698
            'present':['child', 'ghost'],
 
699
            'child':[],
 
700
            }
 
701
        parents_provider = InstrumentedParentsProvider(
 
702
            _mod_graph.DictParentsProvider(parent_graph))
 
703
        graph = _mod_graph.Graph(parents_provider)
 
704
        # with_ghosts reports the ghosts
 
705
        search = graph._make_breadth_first_searcher(['head'])
 
706
        self.assertEqual((set(['head']), set()), search.next_with_ghosts())
 
707
        self.assertEqual(set(['present']), search.next())
 
708
        self.assertEqual((set(['child']), set(['ghost'])),
 
709
            search.next_with_ghosts())
 
710
        self.assertRaises(StopIteration, search.next)
 
711
        # next includes them
 
712
        search = graph._make_breadth_first_searcher(['head'])
 
713
        self.assertEqual(set(['head']), search.next())
 
714
        self.assertEqual((set(['present']), set()), search.next_with_ghosts())
 
715
        self.assertEqual(set(['child', 'ghost']),
 
716
            search.next())
 
717
        self.assertRaises(StopIteration, search.next_with_ghosts)
 
718
 
684
719
 
685
720
class TestCachingParentsProvider(tests.TestCase):
686
721
 
690
725
        self.inst_pp = InstrumentedParentsProvider(dict_pp)
691
726
        self.caching_pp = _mod_graph.CachingParentsProvider(self.inst_pp)
692
727
 
693
 
    def test_get_parents(self):
694
 
        """Requesting the same revision should be returned from cache"""
695
 
        self.assertEqual({}, self.caching_pp._cache)
696
 
        self.assertEqual([('b',)],
697
 
            self.applyDeprecated(symbol_versioning.one_one,
698
 
            self.caching_pp.get_parents, ['a']))
699
 
        self.assertEqual(['a'], self.inst_pp.calls)
700
 
        self.assertEqual([('b',)],
701
 
            self.applyDeprecated(symbol_versioning.one_one,
702
 
            self.caching_pp.get_parents, ['a']))
703
 
        # No new call, as it should have been returned from the cache
704
 
        self.assertEqual(['a'], self.inst_pp.calls)
705
 
        self.assertEqual({'a':('b',)}, self.caching_pp._cache)
706
 
 
707
728
    def test_get_parent_map(self):
708
729
        """Requesting the same revision should be returned from cache"""
709
730
        self.assertEqual({}, self.caching_pp._cache)