/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_graph.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-07-04 12:50:55 UTC
  • mfrom: (7027.2.8 git-fixes)
  • Revision ID: breezy.the.bot@gmail.com-20180704125055-8nni25pn2439p48v
Fix eol handling in knits on Python 3, port fastimport plugin to Python 3.

Merged from https://code.launchpad.net/~jelmer/brz/fastimport-fixes/+merge/348924

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007-2011 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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from bzrlib import (
 
17
from .. import (
18
18
    errors,
19
19
    graph as _mod_graph,
20
 
    symbol_versioning,
21
20
    tests,
22
21
    )
23
 
from bzrlib.revision import NULL_REVISION
24
 
from bzrlib.tests import TestCaseWithMemoryTransport
25
 
from bzrlib.symbol_versioning import deprecated_in
 
22
from ..revision import NULL_REVISION
 
23
from . import TestCaseWithMemoryTransport
26
24
 
27
25
 
28
26
# Ancestry 1:
36
34
#   rev3  /
37
35
#     |  /
38
36
#   rev4
39
 
ancestry_1 = {'rev1': [NULL_REVISION], 'rev2a': ['rev1'], 'rev2b': ['rev1'],
40
 
              'rev3': ['rev2a'], 'rev4': ['rev3', 'rev2b']}
 
37
ancestry_1 = {b'rev1': [NULL_REVISION], b'rev2a': [b'rev1'], b'rev2b': [b'rev1'],
 
38
              b'rev3': [b'rev2a'], b'rev4': [b'rev3', b'rev2b']}
41
39
 
42
40
 
43
41
# Ancestry 2:
51
49
# rev3a
52
50
#   |
53
51
# rev4a
54
 
ancestry_2 = {'rev1a': [NULL_REVISION], 'rev2a': ['rev1a'],
55
 
              'rev1b': [NULL_REVISION], 'rev3a': ['rev2a'], 'rev4a': ['rev3a']}
 
52
ancestry_2 = {b'rev1a': [NULL_REVISION], b'rev2a': [b'rev1a'],
 
53
              b'rev1b': [NULL_REVISION], b'rev3a': [b'rev2a'], b'rev4a': [b'rev3a']}
56
54
 
57
55
 
58
56
# Criss cross ancestry
66
64
#       |  X |
67
65
#       |/  \|
68
66
#    rev3a  rev3b
69
 
criss_cross = {'rev1': [NULL_REVISION], 'rev2a': ['rev1'], 'rev2b': ['rev1'],
70
 
               'rev3a': ['rev2a', 'rev2b'], 'rev3b': ['rev2b', 'rev2a']}
 
67
criss_cross = {b'rev1': [NULL_REVISION], b'rev2a': [b'rev1'], b'rev2b': [b'rev1'],
 
68
               b'rev3a': [b'rev2a', b'rev2b'], b'rev3b': [b'rev2b', b'rev2a']}
71
69
 
72
70
 
73
71
# Criss-cross 2
81
79
#   | / \ |
82
80
#   |/   \|
83
81
# rev2a  rev2b
84
 
criss_cross2 = {'rev1a': [NULL_REVISION], 'rev1b': [NULL_REVISION],
85
 
                'rev2a': ['rev1a', 'rev1b'], 'rev2b': ['rev1b', 'rev1a']}
 
82
criss_cross2 = {b'rev1a': [NULL_REVISION], b'rev1b': [NULL_REVISION],
 
83
                b'rev2a': [b'rev1a', b'rev1b'], b'rev2b': [b'rev1b', b'rev1a']}
86
84
 
87
85
 
88
86
# Mainline:
94
92
#      | rev2b
95
93
#      |  /
96
94
#     rev2a
97
 
mainline = {'rev1': [NULL_REVISION], 'rev2a': ['rev1', 'rev2b'],
98
 
            'rev2b': ['rev1']}
 
95
mainline = {b'rev1': [NULL_REVISION], b'rev2a': [b'rev1', b'rev2b'],
 
96
            b'rev2b': [b'rev1']}
99
97
 
100
98
 
101
99
# feature branch:
107
105
#     rev2b
108
106
#       |
109
107
#     rev3b
110
 
feature_branch = {'rev1': [NULL_REVISION],
111
 
                  'rev2b': ['rev1'], 'rev3b': ['rev2b']}
 
108
feature_branch = {b'rev1': [NULL_REVISION],
 
109
                  b'rev2b': [b'rev1'], b'rev3b': [b'rev2b']}
112
110
 
113
111
 
114
112
# History shortcut
119
117
#  rev2a rev2b rev2c
120
118
#    |  /   \   /
121
119
#  rev3a    rev3b
122
 
history_shortcut = {'rev1': [NULL_REVISION], 'rev2a': ['rev1'],
123
 
                    'rev2b': ['rev1'], 'rev2c': ['rev1'],
124
 
                    'rev3a': ['rev2a', 'rev2b'], 'rev3b': ['rev2b', 'rev2c']}
 
120
history_shortcut = {b'rev1': [NULL_REVISION], b'rev2a': [b'rev1'],
 
121
                    b'rev2b': [b'rev1'], b'rev2c': [b'rev1'],
 
122
                    b'rev3a': [b'rev2a', b'rev2b'], b'rev3b': [b'rev2b', b'rev2c']}
125
123
 
126
124
# Extended history shortcut
127
125
#  NULL_REVISION
135
133
#       d |
136
134
#       |\|
137
135
#       e f
138
 
extended_history_shortcut = {'a': [NULL_REVISION],
139
 
                             'b': ['a'],
140
 
                             'c': ['b'],
141
 
                             'd': ['c'],
142
 
                             'e': ['d'],
143
 
                             'f': ['a', 'd'],
 
136
extended_history_shortcut = {b'a': [NULL_REVISION],
 
137
                             b'b': [b'a'],
 
138
                             b'c': [b'b'],
 
139
                             b'd': [b'c'],
 
140
                             b'e': [b'd'],
 
141
                             b'f': [b'a', b'd'],
144
142
                            }
145
143
 
146
144
# Double shortcut
147
 
# Both sides will see 'A' first, even though it is actually a decendent of a
 
145
# Both sides will see b'A' first, even though it is actually a decendent of a
148
146
# different common revision.
149
147
#
150
148
#  NULL_REVISION
159
157
#   |/     \|
160
158
#   f       g
161
159
 
162
 
double_shortcut = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'],
163
 
                   'd':['c'], 'e':['c'], 'f':['a', 'd'],
164
 
                   'g':['a', 'e']}
 
160
double_shortcut = {b'a':[NULL_REVISION], b'b':[b'a'], b'c':[b'b'],
 
161
                   b'd':[b'c'], b'e':[b'c'], b'f':[b'a', b'd'],
 
162
                   b'g':[b'a', b'e']}
165
163
 
166
164
# Complex shortcut
167
165
# This has a failure mode in that a shortcut will find some nodes in common,
191
189
#     | l |
192
190
#     |/|/
193
191
#     m n
194
 
complex_shortcut = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'], 'd':['c'],
195
 
                    'e':['d'], 'f':['d'], 'g':['f'], 'h':['f'],
196
 
                    'i':['e', 'g'], 'j':['g'], 'k':['j'],
197
 
                    'l':['k'], 'm':['i', 'l'], 'n':['l', 'h']}
 
192
complex_shortcut = {b'a':[NULL_REVISION], b'b':[b'a'], b'c':[b'b'], b'd':[b'c'],
 
193
                    b'e':[b'd'], b'f':[b'd'], b'g':[b'f'], b'h':[b'f'],
 
194
                    b'i':[b'e', b'g'], b'j':[b'g'], b'k':[b'j'],
 
195
                    b'l':[b'k'], b'm':[b'i', b'l'], b'n':[b'l', b'h']}
198
196
 
199
197
# NULL_REVISION
200
198
#     |
234
232
#     | | |
235
233
#     |/|/
236
234
#     t u
237
 
complex_shortcut2 = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'], 'd':['c'],
238
 
                    'e':['d'], 'f':['e'], 'g':['f'], 'h':['d'], 'i':['g'],
239
 
                    'j':['h'], 'k':['h', 'i'], 'l':['k'], 'm':['l'], 'n':['m'],
240
 
                    'o':['n'], 'p':['o'], 'q':['p'], 'r':['q'], 's':['r'],
241
 
                    't':['i', 's'], 'u':['s', 'j'],
 
235
complex_shortcut2 = {b'a': [NULL_REVISION], b'b': [b'a'], b'c': [b'b'], b'd': [b'c'],
 
236
                    b'e': [b'd'], b'f': [b'e'], b'g': [b'f'], b'h': [b'd'], b'i': [b'g'],
 
237
                    b'j': [b'h'], b'k': [b'h', b'i'], b'l': [b'k'], b'm': [b'l'], b'n': [b'm'],
 
238
                    b'o': [b'n'], b'p': [b'o'], b'q': [b'p'], b'r': [b'q'], b's': [b'r'],
 
239
                    b't': [b'i', b's'], b'u': [b's', b'j'],
242
240
                    }
243
241
 
244
242
# Graph where different walkers will race to find the common and uncommon
292
290
# k-n exists so that the second pass still has nodes that are worth searching,
293
291
# rather than instantly cancelling the extra walker.
294
292
 
295
 
racing_shortcuts = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'], 'd':['c'],
296
 
    'e':['d'], 'f':['e'], 'g':['f'], 'h':['g'], 'i':['h', 'o'], 'j':['i', 'y'],
297
 
    'k':['d'], 'l':['k'], 'm':['l'], 'n':['m'], 'o':['n', 'g'], 'p':['f'],
298
 
    'q':['p', 'm'], 'r':['o'], 's':['r'], 't':['s'], 'u':['t'], 'v':['u'],
299
 
    'w':['v'], 'x':['w'], 'y':['x'], 'z':['x', 'q']}
 
293
racing_shortcuts = {b'a':[NULL_REVISION], b'b':[b'a'], b'c':[b'b'], b'd':[b'c'],
 
294
    b'e':[b'd'], b'f':[b'e'], b'g':[b'f'], b'h':[b'g'], b'i':[b'h', b'o'], b'j':[b'i', b'y'],
 
295
    b'k':[b'd'], b'l':[b'k'], b'm':[b'l'], b'n':[b'm'], b'o':[b'n', b'g'], b'p':[b'f'],
 
296
    b'q':[b'p', b'm'], b'r':[b'o'], b's':[b'r'], b't':[b's'], b'u':[b't'], b'v':[b'u'],
 
297
    b'w':[b'v'], b'x':[b'w'], b'y':[b'x'], b'z':[b'x', b'q']}
300
298
 
301
299
 
302
300
# A graph with multiple nodes unique to one side.
338
336
#     y   z
339
337
#
340
338
 
341
 
multiple_interesting_unique = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'],
342
 
    'd':['c'], 'e':['d'], 'f':['d'], 'g':['e'], 'h':['e'], 'i':['f'],
343
 
    'j':['g'], 'k':['g'], 'l':['h'], 'm':['i'], 'n':['k', 'l'],
344
 
    'o':['m'], 'p':['m', 'l'], 'q':['n', 'o'], 'r':['q'], 's':['r'],
345
 
    't':['s'], 'u':['t'], 'v':['u'], 'w':['v'], 'x':['w'],
346
 
    'y':['j', 'x'], 'z':['x', 'p']}
 
339
multiple_interesting_unique = {b'a':[NULL_REVISION], b'b':[b'a'], b'c':[b'b'],
 
340
    b'd':[b'c'], b'e':[b'd'], b'f':[b'd'], b'g':[b'e'], b'h':[b'e'], b'i':[b'f'],
 
341
    b'j':[b'g'], b'k':[b'g'], b'l':[b'h'], b'm':[b'i'], b'n':[b'k', b'l'],
 
342
    b'o':[b'm'], b'p':[b'm', b'l'], b'q':[b'n', b'o'], b'r':[b'q'], b's':[b'r'],
 
343
    b't':[b's'], b'u':[b't'], b'v':[b'u'], b'w':[b'v'], b'x':[b'w'],
 
344
    b'y':[b'j', b'x'], b'z':[b'x', b'p']}
347
345
 
348
346
 
349
347
# Shortcut with extra root
360
358
#       d | g
361
359
#       |\|/
362
360
#       e f
363
 
shortcut_extra_root = {'a': [NULL_REVISION],
364
 
                       'b': ['a'],
365
 
                       'c': ['b'],
366
 
                       'd': ['c'],
367
 
                       'e': ['d'],
368
 
                       'f': ['a', 'd', 'g'],
369
 
                       'g': [NULL_REVISION],
 
361
shortcut_extra_root = {b'a': [NULL_REVISION],
 
362
                       b'b': [b'a'],
 
363
                       b'c': [b'b'],
 
364
                       b'd': [b'c'],
 
365
                       b'e': [b'd'],
 
366
                       b'f': [b'a', b'd', b'g'],
 
367
                       b'g': [NULL_REVISION],
370
368
                      }
371
369
 
372
370
#  NULL_REVISION
379
377
#     | \ |
380
378
#     a   c
381
379
 
382
 
boundary = {'a': ['b'], 'c': ['b', 'd'], 'b':['e'], 'd':['e'], 'e': ['f'],
383
 
            'f':[NULL_REVISION]}
 
380
boundary = {b'a': [b'b'], b'c': [b'b', b'd'], b'b':[b'e'], b'd':[b'e'], b'e': [b'f'],
 
381
            b'f':[NULL_REVISION]}
384
382
 
385
383
 
386
384
# A graph that contains a ghost
394
392
#     | \ |
395
393
#     a   c
396
394
 
397
 
with_ghost = {'a': ['b'], 'c': ['b', 'd'], 'b':['e'], 'd':['e', 'g'],
398
 
              'e': ['f'], 'f':[NULL_REVISION], NULL_REVISION:()}
 
395
with_ghost = {b'a': [b'b'], b'c': [b'b', b'd'], b'b':[b'e'], b'd':[b'e', b'g'],
 
396
              b'e': [b'f'], b'f':[NULL_REVISION], NULL_REVISION:()}
399
397
 
400
398
# A graph that shows we can shortcut finding revnos when reaching them from the
401
399
# side.
417
415
#     |
418
416
#     i
419
417
 
420
 
with_tail = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'], 'd':['c'], 'e':['d'],
421
 
             'f':['e'], 'g':['e'], 'h':['f'], 'i':['h']}
 
418
with_tail = {b'a':[NULL_REVISION], b'b':[b'a'], b'c':[b'b'], b'd':[b'c'], b'e':[b'd'],
 
419
             b'f':[b'e'], b'g':[b'e'], b'h':[b'f'], b'i':[b'h']}
422
420
 
423
421
 
424
422
class InstrumentedParentsProvider(object):
426
424
    def __init__(self, parents_provider):
427
425
        self.calls = []
428
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
429
432
 
430
433
    def get_parent_map(self, nodes):
431
434
        self.calls.extend(nodes)
432
435
        return self._real_parents_provider.get_parent_map(nodes)
433
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
 
434
462
 
435
463
class TestGraphBase(tests.TestCase):
436
464
 
469
497
        """
470
498
        pending = [NULL_REVISION]
471
499
        descendants = {}
472
 
        for descendant, parents in ancestors.iteritems():
 
500
        for descendant, parents in ancestors.items():
473
501
            for parent in parents:
474
502
                descendants.setdefault(parent, []).append(descendant)
475
503
        while len(pending) > 0:
500
528
        """
501
529
        graph = self.make_graph(ancestry_1)
502
530
        self.assertRaises(errors.InvalidRevisionId, graph.find_lca, None)
503
 
        self.assertEqual(set([NULL_REVISION]),
 
531
        self.assertEqual({NULL_REVISION},
504
532
                         graph.find_lca(NULL_REVISION, NULL_REVISION))
505
 
        self.assertEqual(set([NULL_REVISION]),
506
 
                         graph.find_lca(NULL_REVISION, 'rev1'))
507
 
        self.assertEqual(set(['rev1']), graph.find_lca('rev1', 'rev1'))
508
 
        self.assertEqual(set(['rev1']), graph.find_lca('rev2a', 'rev2b'))
 
533
        self.assertEqual({NULL_REVISION},
 
534
                         graph.find_lca(NULL_REVISION, b'rev1'))
 
535
        self.assertEqual({b'rev1'}, graph.find_lca(b'rev1', b'rev1'))
 
536
        self.assertEqual({b'rev1'}, graph.find_lca(b'rev2a', b'rev2b'))
509
537
 
510
538
    def test_no_unique_lca(self):
511
539
        """Test error when one revision is not in the graph"""
512
540
        graph = self.make_graph(ancestry_1)
513
541
        self.assertRaises(errors.NoCommonAncestor, graph.find_unique_lca,
514
 
                          'rev1', '1rev')
 
542
                          b'rev1', b'1rev')
515
543
 
516
544
    def test_lca_criss_cross(self):
517
545
        """Test least-common-ancestor after a criss-cross merge."""
518
546
        graph = self.make_graph(criss_cross)
519
 
        self.assertEqual(set(['rev2a', 'rev2b']),
520
 
                         graph.find_lca('rev3a', 'rev3b'))
521
 
        self.assertEqual(set(['rev2b']),
522
 
                         graph.find_lca('rev3a', 'rev3b', 'rev2b'))
 
547
        self.assertEqual({b'rev2a', b'rev2b'},
 
548
                         graph.find_lca(b'rev3a', b'rev3b'))
 
549
        self.assertEqual({b'rev2b'},
 
550
                         graph.find_lca(b'rev3a', b'rev3b', b'rev2b'))
523
551
 
524
552
    def test_lca_shortcut(self):
525
553
        """Test least-common ancestor on this history shortcut"""
526
554
        graph = self.make_graph(history_shortcut)
527
 
        self.assertEqual(set(['rev2b']), graph.find_lca('rev3a', 'rev3b'))
 
555
        self.assertEqual({b'rev2b'}, graph.find_lca(b'rev3a', b'rev3b'))
528
556
 
529
557
    def test_lefthand_distance_smoke(self):
530
558
        """A simple does it work test for graph.lefthand_distance(keys)."""
531
559
        graph = self.make_graph(history_shortcut)
532
 
        distance_graph = graph.find_lefthand_distances(['rev3b', 'rev2a'])
533
 
        self.assertEqual({'rev2a': 2, 'rev3b': 3}, distance_graph)
 
560
        distance_graph = graph.find_lefthand_distances([b'rev3b', b'rev2a'])
 
561
        self.assertEqual({b'rev2a': 2, b'rev3b': 3}, distance_graph)
534
562
 
535
563
    def test_lefthand_distance_ghosts(self):
536
564
        """A simple does it work test for graph.lefthand_distance(keys)."""
537
 
        nodes = {'nonghost':[NULL_REVISION], 'toghost':['ghost']}
 
565
        nodes = {b'nonghost': [NULL_REVISION], b'toghost': [b'ghost']}
538
566
        graph = self.make_graph(nodes)
539
 
        distance_graph = graph.find_lefthand_distances(['nonghost', 'toghost'])
540
 
        self.assertEqual({'nonghost': 1, 'toghost': -1}, distance_graph)
 
567
        distance_graph = graph.find_lefthand_distances([b'nonghost', b'toghost'])
 
568
        self.assertEqual({b'nonghost': 1, b'toghost': -1}, distance_graph)
541
569
 
542
570
    def test_recursive_unique_lca(self):
543
571
        """Test finding a unique least common ancestor.
548
576
        self.assertEqual(NULL_REVISION,
549
577
                         graph.find_unique_lca(NULL_REVISION, NULL_REVISION))
550
578
        self.assertEqual(NULL_REVISION,
551
 
                         graph.find_unique_lca(NULL_REVISION, 'rev1'))
552
 
        self.assertEqual('rev1', graph.find_unique_lca('rev1', 'rev1'))
553
 
        self.assertEqual('rev1', graph.find_unique_lca('rev2a', 'rev2b'))
554
 
        self.assertEqual(('rev1', 1,),
555
 
                         graph.find_unique_lca('rev2a', 'rev2b',
 
579
                         graph.find_unique_lca(NULL_REVISION, b'rev1'))
 
580
        self.assertEqual(b'rev1', graph.find_unique_lca(b'rev1', b'rev1'))
 
581
        self.assertEqual(b'rev1', graph.find_unique_lca(b'rev2a', b'rev2b'))
 
582
        self.assertEqual((b'rev1', 1,),
 
583
                         graph.find_unique_lca(b'rev2a', b'rev2b',
556
584
                         count_steps=True))
557
585
 
558
586
    def assertRemoveDescendants(self, expected, graph, revisions):
562
590
 
563
591
    def test__remove_simple_descendants(self):
564
592
        graph = self.make_graph(ancestry_1)
565
 
        self.assertRemoveDescendants(set(['rev1']), graph,
566
 
            set(['rev1', 'rev2a', 'rev2b', 'rev3', 'rev4']))
 
593
        self.assertRemoveDescendants({b'rev1'}, graph,
 
594
            {b'rev1', b'rev2a', b'rev2b', b'rev3', b'rev4'})
567
595
 
568
596
    def test__remove_simple_descendants_disjoint(self):
569
597
        graph = self.make_graph(ancestry_1)
570
 
        self.assertRemoveDescendants(set(['rev1', 'rev3']), graph,
571
 
            set(['rev1', 'rev3']))
 
598
        self.assertRemoveDescendants({b'rev1', b'rev3'}, graph,
 
599
            {b'rev1', b'rev3'})
572
600
 
573
601
    def test__remove_simple_descendants_chain(self):
574
602
        graph = self.make_graph(ancestry_1)
575
 
        self.assertRemoveDescendants(set(['rev1']), graph,
576
 
            set(['rev1', 'rev2a', 'rev3']))
 
603
        self.assertRemoveDescendants({b'rev1'}, graph,
 
604
            {b'rev1', b'rev2a', b'rev3'})
577
605
 
578
606
    def test__remove_simple_descendants_siblings(self):
579
607
        graph = self.make_graph(ancestry_1)
580
 
        self.assertRemoveDescendants(set(['rev2a', 'rev2b']), graph,
581
 
            set(['rev2a', 'rev2b', 'rev3']))
 
608
        self.assertRemoveDescendants({b'rev2a', b'rev2b'}, graph,
 
609
            {b'rev2a', b'rev2b', b'rev3'})
582
610
 
583
611
    def test_unique_lca_criss_cross(self):
584
612
        """Ensure we don't pick non-unique lcas in a criss-cross"""
585
613
        graph = self.make_graph(criss_cross)
586
 
        self.assertEqual('rev1', graph.find_unique_lca('rev3a', 'rev3b'))
587
 
        lca, steps = graph.find_unique_lca('rev3a', 'rev3b', count_steps=True)
588
 
        self.assertEqual('rev1', lca)
 
614
        self.assertEqual(b'rev1', graph.find_unique_lca(b'rev3a', b'rev3b'))
 
615
        lca, steps = graph.find_unique_lca(b'rev3a', b'rev3b', count_steps=True)
 
616
        self.assertEqual(b'rev1', lca)
589
617
        self.assertEqual(2, steps)
590
618
 
591
619
    def test_unique_lca_null_revision(self):
592
620
        """Ensure we pick NULL_REVISION when necessary"""
593
621
        graph = self.make_graph(criss_cross2)
594
 
        self.assertEqual('rev1b', graph.find_unique_lca('rev2a', 'rev1b'))
 
622
        self.assertEqual(b'rev1b', graph.find_unique_lca(b'rev2a', b'rev1b'))
595
623
        self.assertEqual(NULL_REVISION,
596
 
                         graph.find_unique_lca('rev2a', 'rev2b'))
 
624
                         graph.find_unique_lca(b'rev2a', b'rev2b'))
597
625
 
598
626
    def test_unique_lca_null_revision2(self):
599
627
        """Ensure we pick NULL_REVISION when necessary"""
600
628
        graph = self.make_graph(ancestry_2)
601
629
        self.assertEqual(NULL_REVISION,
602
 
                         graph.find_unique_lca('rev4a', 'rev1b'))
 
630
                         graph.find_unique_lca(b'rev4a', b'rev1b'))
603
631
 
604
632
    def test_lca_double_shortcut(self):
605
633
        graph = self.make_graph(double_shortcut)
606
 
        self.assertEqual('c', graph.find_unique_lca('f', 'g'))
 
634
        self.assertEqual(b'c', graph.find_unique_lca(b'f', b'g'))
607
635
 
608
636
    def test_common_ancestor_two_repos(self):
609
637
        """Ensure we do unique_lca using data from two repos"""
619
647
 
620
648
        graph = mainline_tree.branch.repository.get_graph(
621
649
            feature_tree.branch.repository)
622
 
        self.assertEqual('rev2b', graph.find_unique_lca('rev2a', 'rev3b'))
 
650
        self.assertEqual(b'rev2b', graph.find_unique_lca(b'rev2a', b'rev3b'))
623
651
 
624
652
    def test_graph_difference(self):
625
653
        graph = self.make_graph(ancestry_1)
626
 
        self.assertEqual((set(), set()), graph.find_difference('rev1', 'rev1'))
627
 
        self.assertEqual((set(), set(['rev1'])),
628
 
                         graph.find_difference(NULL_REVISION, 'rev1'))
629
 
        self.assertEqual((set(['rev1']), set()),
630
 
                         graph.find_difference('rev1', NULL_REVISION))
631
 
        self.assertEqual((set(['rev2a', 'rev3']), set(['rev2b'])),
632
 
                         graph.find_difference('rev3', 'rev2b'))
633
 
        self.assertEqual((set(['rev4', 'rev3', 'rev2a']), set()),
634
 
                         graph.find_difference('rev4', 'rev2b'))
 
654
        self.assertEqual((set(), set()), graph.find_difference(b'rev1', b'rev1'))
 
655
        self.assertEqual((set(), {b'rev1'}),
 
656
                         graph.find_difference(NULL_REVISION, b'rev1'))
 
657
        self.assertEqual(({b'rev1'}, set()),
 
658
                         graph.find_difference(b'rev1', NULL_REVISION))
 
659
        self.assertEqual(({b'rev2a', b'rev3'}, {b'rev2b'}),
 
660
                         graph.find_difference(b'rev3', b'rev2b'))
 
661
        self.assertEqual(({b'rev4', b'rev3', b'rev2a'}, set()),
 
662
                         graph.find_difference(b'rev4', b'rev2b'))
635
663
 
636
664
    def test_graph_difference_separate_ancestry(self):
637
665
        graph = self.make_graph(ancestry_2)
638
 
        self.assertEqual((set(['rev1a']), set(['rev1b'])),
639
 
                         graph.find_difference('rev1a', 'rev1b'))
640
 
        self.assertEqual((set(['rev1a', 'rev2a', 'rev3a', 'rev4a']),
641
 
                          set(['rev1b'])),
642
 
                         graph.find_difference('rev4a', 'rev1b'))
 
666
        self.assertEqual(({b'rev1a'}, {b'rev1b'}),
 
667
                         graph.find_difference(b'rev1a', b'rev1b'))
 
668
        self.assertEqual(({b'rev1a', b'rev2a', b'rev3a', b'rev4a'},
 
669
                          {b'rev1b'}),
 
670
                         graph.find_difference(b'rev4a', b'rev1b'))
643
671
 
644
672
    def test_graph_difference_criss_cross(self):
645
673
        graph = self.make_graph(criss_cross)
646
 
        self.assertEqual((set(['rev3a']), set(['rev3b'])),
647
 
                         graph.find_difference('rev3a', 'rev3b'))
648
 
        self.assertEqual((set([]), set(['rev3b', 'rev2b'])),
649
 
                         graph.find_difference('rev2a', 'rev3b'))
 
674
        self.assertEqual(({b'rev3a'}, {b'rev3b'}),
 
675
                         graph.find_difference(b'rev3a', b'rev3b'))
 
676
        self.assertEqual((set([]), {b'rev3b', b'rev2b'}),
 
677
                         graph.find_difference(b'rev2a', b'rev3b'))
650
678
 
651
679
    def test_graph_difference_extended_history(self):
652
680
        graph = self.make_graph(extended_history_shortcut)
653
 
        self.assertEqual((set(['e']), set(['f'])),
654
 
                         graph.find_difference('e', 'f'))
655
 
        self.assertEqual((set(['f']), set(['e'])),
656
 
                         graph.find_difference('f', 'e'))
 
681
        self.assertEqual(({b'e'}, {b'f'}),
 
682
                         graph.find_difference(b'e', b'f'))
 
683
        self.assertEqual(({b'f'}, {b'e'}),
 
684
                         graph.find_difference(b'f', b'e'))
657
685
 
658
686
    def test_graph_difference_double_shortcut(self):
659
687
        graph = self.make_graph(double_shortcut)
660
 
        self.assertEqual((set(['d', 'f']), set(['e', 'g'])),
661
 
                         graph.find_difference('f', 'g'))
 
688
        self.assertEqual(({b'd', b'f'}, {b'e', b'g'}),
 
689
                         graph.find_difference(b'f', b'g'))
662
690
 
663
691
    def test_graph_difference_complex_shortcut(self):
664
692
        graph = self.make_graph(complex_shortcut)
665
 
        self.assertEqual((set(['m', 'i', 'e']), set(['n', 'h'])),
666
 
                         graph.find_difference('m', 'n'))
 
693
        self.assertEqual(({b'm', b'i', b'e'}, {b'n', b'h'}),
 
694
                         graph.find_difference(b'm', b'n'))
667
695
 
668
696
    def test_graph_difference_complex_shortcut2(self):
669
697
        graph = self.make_graph(complex_shortcut2)
670
 
        self.assertEqual((set(['t']), set(['j', 'u'])),
671
 
                         graph.find_difference('t', 'u'))
 
698
        self.assertEqual(({b't'}, {b'j', b'u'}),
 
699
                         graph.find_difference(b't', b'u'))
672
700
 
673
701
    def test_graph_difference_shortcut_extra_root(self):
674
702
        graph = self.make_graph(shortcut_extra_root)
675
 
        self.assertEqual((set(['e']), set(['f', 'g'])),
676
 
                         graph.find_difference('e', 'f'))
677
 
 
678
 
 
679
 
    def test_stacked_parents_provider(self):
680
 
        parents1 = _mod_graph.DictParentsProvider({'rev2': ['rev3']})
681
 
        parents2 = _mod_graph.DictParentsProvider({'rev1': ['rev4']})
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])
708
 
        self.assertEqual({'rev1':['rev4'], 'rev2':['rev3']},
709
 
                         stacked.get_parent_map(['rev1', 'rev2']))
710
 
        self.assertEqual({'rev2':['rev3'], 'rev1':['rev4']},
711
 
                         stacked.get_parent_map(['rev2', 'rev1']))
712
 
        self.assertEqual({'rev2':['rev3']},
713
 
                         stacked.get_parent_map(['rev2', 'rev2']))
714
 
        self.assertEqual({'rev1':['rev4']},
715
 
                         stacked.get_parent_map(['rev1', 'rev1']))
 
703
        self.assertEqual(({b'e'}, {b'f', b'g'}),
 
704
                         graph.find_difference(b'e', b'f'))
716
705
 
717
706
    def test_iter_topo_order(self):
718
707
        graph = self.make_graph(ancestry_1)
719
 
        args = ['rev2a', 'rev3', 'rev1']
 
708
        args = [b'rev2a', b'rev3', b'rev1']
720
709
        topo_args = list(graph.iter_topo_order(args))
721
710
        self.assertEqual(set(args), set(topo_args))
722
 
        self.assertTrue(topo_args.index('rev2a') > topo_args.index('rev1'))
723
 
        self.assertTrue(topo_args.index('rev2a') < topo_args.index('rev3'))
 
711
        self.assertTrue(topo_args.index(b'rev2a') > topo_args.index(b'rev1'))
 
712
        self.assertTrue(topo_args.index(b'rev2a') < topo_args.index(b'rev3'))
724
713
 
725
714
    def test_is_ancestor(self):
726
715
        graph = self.make_graph(ancestry_1)
727
 
        self.assertEqual(True, graph.is_ancestor('null:', 'null:'))
728
 
        self.assertEqual(True, graph.is_ancestor('null:', 'rev1'))
729
 
        self.assertEqual(False, graph.is_ancestor('rev1', 'null:'))
730
 
        self.assertEqual(True, graph.is_ancestor('null:', 'rev4'))
731
 
        self.assertEqual(False, graph.is_ancestor('rev4', 'null:'))
732
 
        self.assertEqual(False, graph.is_ancestor('rev4', 'rev2b'))
733
 
        self.assertEqual(True, graph.is_ancestor('rev2b', 'rev4'))
734
 
        self.assertEqual(False, graph.is_ancestor('rev2b', 'rev3'))
735
 
        self.assertEqual(False, graph.is_ancestor('rev3', 'rev2b'))
 
716
        self.assertEqual(True, graph.is_ancestor(b'null:', b'null:'))
 
717
        self.assertEqual(True, graph.is_ancestor(b'null:', b'rev1'))
 
718
        self.assertEqual(False, graph.is_ancestor(b'rev1', b'null:'))
 
719
        self.assertEqual(True, graph.is_ancestor(b'null:', b'rev4'))
 
720
        self.assertEqual(False, graph.is_ancestor(b'rev4', b'null:'))
 
721
        self.assertEqual(False, graph.is_ancestor(b'rev4', b'rev2b'))
 
722
        self.assertEqual(True, graph.is_ancestor(b'rev2b', b'rev4'))
 
723
        self.assertEqual(False, graph.is_ancestor(b'rev2b', b'rev3'))
 
724
        self.assertEqual(False, graph.is_ancestor(b'rev3', b'rev2b'))
736
725
        instrumented_provider = InstrumentedParentsProvider(graph)
737
726
        instrumented_graph = _mod_graph.Graph(instrumented_provider)
738
 
        instrumented_graph.is_ancestor('rev2a', 'rev2b')
739
 
        self.assertTrue('null:' not in instrumented_provider.calls)
 
727
        instrumented_graph.is_ancestor(b'rev2a', b'rev2b')
 
728
        self.assertTrue(b'null:' not in instrumented_provider.calls)
740
729
 
741
730
    def test_is_between(self):
742
731
        graph = self.make_graph(ancestry_1)
743
 
        self.assertEqual(True, graph.is_between('null:', 'null:', 'null:'))
744
 
        self.assertEqual(True, graph.is_between('rev1', 'null:', 'rev1'))
745
 
        self.assertEqual(True, graph.is_between('rev1', 'rev1', 'rev4'))
746
 
        self.assertEqual(True, graph.is_between('rev4', 'rev1', 'rev4'))
747
 
        self.assertEqual(True, graph.is_between('rev3', 'rev1', 'rev4'))
748
 
        self.assertEqual(False, graph.is_between('rev4', 'rev1', 'rev3'))
749
 
        self.assertEqual(False, graph.is_between('rev1', 'rev2a', 'rev4'))
750
 
        self.assertEqual(False, graph.is_between('null:', 'rev1', 'rev4'))
 
732
        self.assertEqual(True, graph.is_between(b'null:', b'null:', b'null:'))
 
733
        self.assertEqual(True, graph.is_between(b'rev1', b'null:', b'rev1'))
 
734
        self.assertEqual(True, graph.is_between(b'rev1', b'rev1', b'rev4'))
 
735
        self.assertEqual(True, graph.is_between(b'rev4', b'rev1', b'rev4'))
 
736
        self.assertEqual(True, graph.is_between(b'rev3', b'rev1', b'rev4'))
 
737
        self.assertEqual(False, graph.is_between(b'rev4', b'rev1', b'rev3'))
 
738
        self.assertEqual(False, graph.is_between(b'rev1', b'rev2a', b'rev4'))
 
739
        self.assertEqual(False, graph.is_between(b'null:', b'rev1', b'rev4'))
751
740
 
752
741
    def test_is_ancestor_boundary(self):
753
742
        """Ensure that we avoid searching the whole graph.
758
747
        graph = self.make_graph(boundary)
759
748
        instrumented_provider = InstrumentedParentsProvider(graph)
760
749
        graph = _mod_graph.Graph(instrumented_provider)
761
 
        self.assertFalse(graph.is_ancestor('a', 'c'))
762
 
        self.assertTrue('null:' not in instrumented_provider.calls)
 
750
        self.assertFalse(graph.is_ancestor(b'a', b'c'))
 
751
        self.assertTrue(b'null:' not in instrumented_provider.calls)
763
752
 
764
753
    def test_iter_ancestry(self):
765
754
        nodes = boundary.copy()
766
755
        nodes[NULL_REVISION] = ()
767
756
        graph = self.make_graph(nodes)
768
757
        expected = nodes.copy()
769
 
        expected.pop('a') # 'a' is not in the ancestry of 'c', all the
 
758
        expected.pop(b'a') # b'a' is not in the ancestry of b'c', all the
770
759
                          # other nodes are
771
 
        self.assertEqual(expected, dict(graph.iter_ancestry(['c'])))
772
 
        self.assertEqual(nodes, dict(graph.iter_ancestry(['a', 'c'])))
 
760
        self.assertEqual(expected, dict(graph.iter_ancestry([b'c'])))
 
761
        self.assertEqual(nodes, dict(graph.iter_ancestry([b'a', b'c'])))
773
762
 
774
763
    def test_iter_ancestry_with_ghost(self):
775
764
        graph = self.make_graph(with_ghost)
776
765
        expected = with_ghost.copy()
777
 
        # 'a' is not in the ancestry of 'c', and 'g' is a ghost
778
 
        expected['g'] = None
779
 
        self.assertEqual(expected, dict(graph.iter_ancestry(['a', 'c'])))
780
 
        expected.pop('a')
781
 
        self.assertEqual(expected, dict(graph.iter_ancestry(['c'])))
 
766
        # b'a' is not in the ancestry of b'c', and b'g' is a ghost
 
767
        expected[b'g'] = None
 
768
        self.assertEqual(expected, dict(graph.iter_ancestry([b'a', b'c'])))
 
769
        expected.pop(b'a')
 
770
        self.assertEqual(expected, dict(graph.iter_ancestry([b'c'])))
782
771
 
783
772
    def test_filter_candidate_lca(self):
784
773
        """Test filter_candidate_lca for a corner case
785
774
 
786
 
        This tests the case where we encounter the end of iteration for 'e'
787
 
        in the same pass as we discover that 'd' is an ancestor of 'e', and
788
 
        therefore 'e' can't be an lca.
 
775
        This tests the case where we encounter the end of iteration for b'e'
 
776
        in the same pass as we discover that b'd' is an ancestor of b'e', and
 
777
        therefore b'e' can't be an lca.
789
778
 
790
779
        To compensate for different dict orderings on other Python
791
 
        implementations, we mirror 'd' and 'e' with 'b' and 'a'.
 
780
        implementations, we mirror b'd' and b'e' with b'b' and b'a'.
792
781
        """
793
782
        # This test is sensitive to the iteration order of dicts.  It will
794
 
        # pass incorrectly if 'e' and 'a' sort before 'c'
 
783
        # pass incorrectly if b'e' and b'a' sort before b'c'
795
784
        #
796
785
        # NULL_REVISION
797
786
        #     / \
800
789
        #    b   d
801
790
        #     \ /
802
791
        #      c
803
 
        graph = self.make_graph({'c': ['b', 'd'], 'd': ['e'], 'b': ['a'],
804
 
                                 'a': [NULL_REVISION], 'e': [NULL_REVISION]})
805
 
        self.assertEqual(set(['c']), graph.heads(['a', 'c', 'e']))
 
792
        graph = self.make_graph({b'c': [b'b', b'd'], b'd': [b'e'], b'b': [b'a'],
 
793
                                 b'a': [NULL_REVISION], b'e': [NULL_REVISION]})
 
794
        self.assertEqual({b'c'}, graph.heads([b'a', b'c', b'e']))
806
795
 
807
796
    def test_heads_null(self):
808
797
        graph = self.make_graph(ancestry_1)
809
 
        self.assertEqual(set(['null:']), graph.heads(['null:']))
810
 
        self.assertEqual(set(['rev1']), graph.heads(['null:', 'rev1']))
811
 
        self.assertEqual(set(['rev1']), graph.heads(['rev1', 'null:']))
812
 
        self.assertEqual(set(['rev1']), graph.heads(set(['rev1', 'null:'])))
813
 
        self.assertEqual(set(['rev1']), graph.heads(('rev1', 'null:')))
 
798
        self.assertEqual({b'null:'}, graph.heads([b'null:']))
 
799
        self.assertEqual({b'rev1'}, graph.heads([b'null:', b'rev1']))
 
800
        self.assertEqual({b'rev1'}, graph.heads([b'rev1', b'null:']))
 
801
        self.assertEqual({b'rev1'}, graph.heads({b'rev1', b'null:'}))
 
802
        self.assertEqual({b'rev1'}, graph.heads((b'rev1', b'null:')))
814
803
 
815
804
    def test_heads_one(self):
816
805
        # A single node will always be a head
817
806
        graph = self.make_graph(ancestry_1)
818
 
        self.assertEqual(set(['null:']), graph.heads(['null:']))
819
 
        self.assertEqual(set(['rev1']), graph.heads(['rev1']))
820
 
        self.assertEqual(set(['rev2a']), graph.heads(['rev2a']))
821
 
        self.assertEqual(set(['rev2b']), graph.heads(['rev2b']))
822
 
        self.assertEqual(set(['rev3']), graph.heads(['rev3']))
823
 
        self.assertEqual(set(['rev4']), graph.heads(['rev4']))
 
807
        self.assertEqual({b'null:'}, graph.heads([b'null:']))
 
808
        self.assertEqual({b'rev1'}, graph.heads([b'rev1']))
 
809
        self.assertEqual({b'rev2a'}, graph.heads([b'rev2a']))
 
810
        self.assertEqual({b'rev2b'}, graph.heads([b'rev2b']))
 
811
        self.assertEqual({b'rev3'}, graph.heads([b'rev3']))
 
812
        self.assertEqual({b'rev4'}, graph.heads([b'rev4']))
824
813
 
825
814
    def test_heads_single(self):
826
815
        graph = self.make_graph(ancestry_1)
827
 
        self.assertEqual(set(['rev4']), graph.heads(['null:', 'rev4']))
828
 
        self.assertEqual(set(['rev2a']), graph.heads(['rev1', 'rev2a']))
829
 
        self.assertEqual(set(['rev2b']), graph.heads(['rev1', 'rev2b']))
830
 
        self.assertEqual(set(['rev3']), graph.heads(['rev1', 'rev3']))
831
 
        self.assertEqual(set(['rev4']), graph.heads(['rev1', 'rev4']))
832
 
        self.assertEqual(set(['rev4']), graph.heads(['rev2a', 'rev4']))
833
 
        self.assertEqual(set(['rev4']), graph.heads(['rev2b', 'rev4']))
834
 
        self.assertEqual(set(['rev4']), graph.heads(['rev3', 'rev4']))
 
816
        self.assertEqual({b'rev4'}, graph.heads([b'null:', b'rev4']))
 
817
        self.assertEqual({b'rev2a'}, graph.heads([b'rev1', b'rev2a']))
 
818
        self.assertEqual({b'rev2b'}, graph.heads([b'rev1', b'rev2b']))
 
819
        self.assertEqual({b'rev3'}, graph.heads([b'rev1', b'rev3']))
 
820
        self.assertEqual({b'rev4'}, graph.heads([b'rev1', b'rev4']))
 
821
        self.assertEqual({b'rev4'}, graph.heads([b'rev2a', b'rev4']))
 
822
        self.assertEqual({b'rev4'}, graph.heads([b'rev2b', b'rev4']))
 
823
        self.assertEqual({b'rev4'}, graph.heads([b'rev3', b'rev4']))
835
824
 
836
825
    def test_heads_two_heads(self):
837
826
        graph = self.make_graph(ancestry_1)
838
 
        self.assertEqual(set(['rev2a', 'rev2b']),
839
 
                         graph.heads(['rev2a', 'rev2b']))
840
 
        self.assertEqual(set(['rev3', 'rev2b']),
841
 
                         graph.heads(['rev3', 'rev2b']))
 
827
        self.assertEqual({b'rev2a', b'rev2b'},
 
828
                         graph.heads([b'rev2a', b'rev2b']))
 
829
        self.assertEqual({b'rev3', b'rev2b'},
 
830
                         graph.heads([b'rev3', b'rev2b']))
842
831
 
843
832
    def test_heads_criss_cross(self):
844
833
        graph = self.make_graph(criss_cross)
845
 
        self.assertEqual(set(['rev2a']),
846
 
                         graph.heads(['rev2a', 'rev1']))
847
 
        self.assertEqual(set(['rev2b']),
848
 
                         graph.heads(['rev2b', 'rev1']))
849
 
        self.assertEqual(set(['rev3a']),
850
 
                         graph.heads(['rev3a', 'rev1']))
851
 
        self.assertEqual(set(['rev3b']),
852
 
                         graph.heads(['rev3b', 'rev1']))
853
 
        self.assertEqual(set(['rev2a', 'rev2b']),
854
 
                         graph.heads(['rev2a', 'rev2b']))
855
 
        self.assertEqual(set(['rev3a']),
856
 
                         graph.heads(['rev3a', 'rev2a']))
857
 
        self.assertEqual(set(['rev3a']),
858
 
                         graph.heads(['rev3a', 'rev2b']))
859
 
        self.assertEqual(set(['rev3a']),
860
 
                         graph.heads(['rev3a', 'rev2a', 'rev2b']))
861
 
        self.assertEqual(set(['rev3b']),
862
 
                         graph.heads(['rev3b', 'rev2a']))
863
 
        self.assertEqual(set(['rev3b']),
864
 
                         graph.heads(['rev3b', 'rev2b']))
865
 
        self.assertEqual(set(['rev3b']),
866
 
                         graph.heads(['rev3b', 'rev2a', 'rev2b']))
867
 
        self.assertEqual(set(['rev3a', 'rev3b']),
868
 
                         graph.heads(['rev3a', 'rev3b']))
869
 
        self.assertEqual(set(['rev3a', 'rev3b']),
870
 
                         graph.heads(['rev3a', 'rev3b', 'rev2a', 'rev2b']))
 
834
        self.assertEqual({b'rev2a'},
 
835
                         graph.heads([b'rev2a', b'rev1']))
 
836
        self.assertEqual({b'rev2b'},
 
837
                         graph.heads([b'rev2b', b'rev1']))
 
838
        self.assertEqual({b'rev3a'},
 
839
                         graph.heads([b'rev3a', b'rev1']))
 
840
        self.assertEqual({b'rev3b'},
 
841
                         graph.heads([b'rev3b', b'rev1']))
 
842
        self.assertEqual({b'rev2a', b'rev2b'},
 
843
                         graph.heads([b'rev2a', b'rev2b']))
 
844
        self.assertEqual({b'rev3a'},
 
845
                         graph.heads([b'rev3a', b'rev2a']))
 
846
        self.assertEqual({b'rev3a'},
 
847
                         graph.heads([b'rev3a', b'rev2b']))
 
848
        self.assertEqual({b'rev3a'},
 
849
                         graph.heads([b'rev3a', b'rev2a', b'rev2b']))
 
850
        self.assertEqual({b'rev3b'},
 
851
                         graph.heads([b'rev3b', b'rev2a']))
 
852
        self.assertEqual({b'rev3b'},
 
853
                         graph.heads([b'rev3b', b'rev2b']))
 
854
        self.assertEqual({b'rev3b'},
 
855
                         graph.heads([b'rev3b', b'rev2a', b'rev2b']))
 
856
        self.assertEqual({b'rev3a', b'rev3b'},
 
857
                         graph.heads([b'rev3a', b'rev3b']))
 
858
        self.assertEqual({b'rev3a', b'rev3b'},
 
859
                         graph.heads([b'rev3a', b'rev3b', b'rev2a', b'rev2b']))
871
860
 
872
861
    def test_heads_shortcut(self):
873
862
        graph = self.make_graph(history_shortcut)
874
863
 
875
 
        self.assertEqual(set(['rev2a', 'rev2b', 'rev2c']),
876
 
                         graph.heads(['rev2a', 'rev2b', 'rev2c']))
877
 
        self.assertEqual(set(['rev3a', 'rev3b']),
878
 
                         graph.heads(['rev3a', 'rev3b']))
879
 
        self.assertEqual(set(['rev3a', 'rev3b']),
880
 
                         graph.heads(['rev2a', 'rev3a', 'rev3b']))
881
 
        self.assertEqual(set(['rev2a', 'rev3b']),
882
 
                         graph.heads(['rev2a', 'rev3b']))
883
 
        self.assertEqual(set(['rev2c', 'rev3a']),
884
 
                         graph.heads(['rev2c', 'rev3a']))
 
864
        self.assertEqual({b'rev2a', b'rev2b', b'rev2c'},
 
865
                         graph.heads([b'rev2a', b'rev2b', b'rev2c']))
 
866
        self.assertEqual({b'rev3a', b'rev3b'},
 
867
                         graph.heads([b'rev3a', b'rev3b']))
 
868
        self.assertEqual({b'rev3a', b'rev3b'},
 
869
                         graph.heads([b'rev2a', b'rev3a', b'rev3b']))
 
870
        self.assertEqual({b'rev2a', b'rev3b'},
 
871
                         graph.heads([b'rev2a', b'rev3b']))
 
872
        self.assertEqual({b'rev2c', b'rev3a'},
 
873
                         graph.heads([b'rev2c', b'rev3a']))
885
874
 
886
875
    def _run_heads_break_deeper(self, graph_dict, search):
887
876
        """Run heads on a graph-as-a-dict.
888
877
 
889
 
        If the search asks for the parents of 'deeper' the test will fail.
 
878
        If the search asks for the parents of b'deeper' the test will fail.
890
879
        """
891
880
        class stub(object):
892
881
            pass
893
882
        def get_parent_map(keys):
894
883
            result = {}
895
884
            for key in keys:
896
 
                if key == 'deeper':
 
885
                if key == b'deeper':
897
886
                    self.fail('key deeper was accessed')
898
887
                result[key] = graph_dict[key]
899
888
            return result
905
894
    def test_heads_limits_search(self):
906
895
        # test that a heads query does not search all of history
907
896
        graph_dict = {
908
 
            'left':['common'],
909
 
            'right':['common'],
910
 
            'common':['deeper'],
 
897
            b'left': [b'common'],
 
898
            b'right': [b'common'],
 
899
            b'common': [b'deeper'],
911
900
        }
912
 
        self.assertEqual(set(['left', 'right']),
913
 
            self._run_heads_break_deeper(graph_dict, ['left', 'right']))
 
901
        self.assertEqual({b'left', b'right'},
 
902
            self._run_heads_break_deeper(graph_dict, [b'left', b'right']))
914
903
 
915
904
    def test_heads_limits_search_assymetric(self):
916
905
        # test that a heads query does not search all of history
917
906
        graph_dict = {
918
 
            'left':['midleft'],
919
 
            'midleft':['common'],
920
 
            'right':['common'],
921
 
            'common':['aftercommon'],
922
 
            'aftercommon':['deeper'],
 
907
            b'left': [b'midleft'],
 
908
            b'midleft': [b'common'],
 
909
            b'right': [b'common'],
 
910
            b'common': [b'aftercommon'],
 
911
            b'aftercommon': [b'deeper'],
923
912
        }
924
 
        self.assertEqual(set(['left', 'right']),
925
 
            self._run_heads_break_deeper(graph_dict, ['left', 'right']))
 
913
        self.assertEqual({b'left', b'right'},
 
914
            self._run_heads_break_deeper(graph_dict, [b'left', b'right']))
926
915
 
927
916
    def test_heads_limits_search_common_search_must_continue(self):
928
917
        # test that common nodes are still queried, preventing
929
918
        # all-the-way-to-origin behaviour in the following graph:
930
919
        graph_dict = {
931
 
            'h1':['shortcut', 'common1'],
932
 
            'h2':['common1'],
933
 
            'shortcut':['common2'],
934
 
            'common1':['common2'],
935
 
            'common2':['deeper'],
 
920
            b'h1': [b'shortcut', b'common1'],
 
921
            b'h2': [b'common1'],
 
922
            b'shortcut': [b'common2'],
 
923
            b'common1': [b'common2'],
 
924
            b'common2': [b'deeper'],
936
925
        }
937
 
        self.assertEqual(set(['h1', 'h2']),
938
 
            self._run_heads_break_deeper(graph_dict, ['h1', 'h2']))
 
926
        self.assertEqual({b'h1', b'h2'},
 
927
            self._run_heads_break_deeper(graph_dict, [b'h1', b'h2']))
939
928
 
940
929
    def test_breadth_first_search_start_ghosts(self):
941
930
        graph = self.make_graph({})
942
931
        # with_ghosts reports the ghosts
943
 
        search = graph._make_breadth_first_searcher(['a-ghost'])
944
 
        self.assertEqual((set(), set(['a-ghost'])), search.next_with_ghosts())
 
932
        search = graph._make_breadth_first_searcher([b'a-ghost'])
 
933
        self.assertEqual((set(), {b'a-ghost'}), search.next_with_ghosts())
945
934
        self.assertRaises(StopIteration, search.next_with_ghosts)
946
935
        # next includes them
947
 
        search = graph._make_breadth_first_searcher(['a-ghost'])
948
 
        self.assertEqual(set(['a-ghost']), search.next())
949
 
        self.assertRaises(StopIteration, search.next)
 
936
        search = graph._make_breadth_first_searcher([b'a-ghost'])
 
937
        self.assertEqual({b'a-ghost'}, next(search))
 
938
        self.assertRaises(StopIteration, next, search)
950
939
 
951
940
    def test_breadth_first_search_deep_ghosts(self):
952
941
        graph = self.make_graph({
953
 
            'head':['present'],
954
 
            'present':['child', 'ghost'],
955
 
            'child':[],
 
942
            b'head': [b'present'],
 
943
            b'present': [b'child', b'ghost'],
 
944
            b'child': [],
956
945
            })
957
946
        # with_ghosts reports the ghosts
958
 
        search = graph._make_breadth_first_searcher(['head'])
959
 
        self.assertEqual((set(['head']), set()), search.next_with_ghosts())
960
 
        self.assertEqual((set(['present']), set()), search.next_with_ghosts())
961
 
        self.assertEqual((set(['child']), set(['ghost'])),
 
947
        search = graph._make_breadth_first_searcher([b'head'])
 
948
        self.assertEqual(({b'head'}, set()), search.next_with_ghosts())
 
949
        self.assertEqual(({b'present'}, set()), search.next_with_ghosts())
 
950
        self.assertEqual(({b'child'}, {b'ghost'}),
962
951
            search.next_with_ghosts())
963
952
        self.assertRaises(StopIteration, search.next_with_ghosts)
964
953
        # next includes them
965
 
        search = graph._make_breadth_first_searcher(['head'])
966
 
        self.assertEqual(set(['head']), search.next())
967
 
        self.assertEqual(set(['present']), search.next())
968
 
        self.assertEqual(set(['child', 'ghost']),
969
 
            search.next())
970
 
        self.assertRaises(StopIteration, search.next)
 
954
        search = graph._make_breadth_first_searcher([b'head'])
 
955
        self.assertEqual({b'head'}, next(search))
 
956
        self.assertEqual({b'present'}, next(search))
 
957
        self.assertEqual({b'child', b'ghost'},
 
958
            next(search))
 
959
        self.assertRaises(StopIteration, next, search)
971
960
 
972
961
    def test_breadth_first_search_change_next_to_next_with_ghosts(self):
973
962
        # To make the API robust, we allow calling both next() and
974
963
        # next_with_ghosts() on the same searcher.
975
964
        graph = self.make_graph({
976
 
            'head':['present'],
977
 
            'present':['child', 'ghost'],
978
 
            'child':[],
 
965
            b'head': [b'present'],
 
966
            b'present': [b'child', b'ghost'],
 
967
            b'child': [],
979
968
            })
980
969
        # start with next_with_ghosts
981
 
        search = graph._make_breadth_first_searcher(['head'])
982
 
        self.assertEqual((set(['head']), set()), search.next_with_ghosts())
983
 
        self.assertEqual(set(['present']), search.next())
984
 
        self.assertEqual((set(['child']), set(['ghost'])),
 
970
        search = graph._make_breadth_first_searcher([b'head'])
 
971
        self.assertEqual(({b'head'}, set()), search.next_with_ghosts())
 
972
        self.assertEqual({b'present'}, next(search))
 
973
        self.assertEqual(({b'child'}, {b'ghost'}),
985
974
            search.next_with_ghosts())
986
 
        self.assertRaises(StopIteration, search.next)
 
975
        self.assertRaises(StopIteration, next, search)
987
976
        # start with next
988
 
        search = graph._make_breadth_first_searcher(['head'])
989
 
        self.assertEqual(set(['head']), search.next())
990
 
        self.assertEqual((set(['present']), set()), search.next_with_ghosts())
991
 
        self.assertEqual(set(['child', 'ghost']),
992
 
            search.next())
 
977
        search = graph._make_breadth_first_searcher([b'head'])
 
978
        self.assertEqual({b'head'}, next(search))
 
979
        self.assertEqual(({b'present'}, set()), search.next_with_ghosts())
 
980
        self.assertEqual({b'child', b'ghost'},
 
981
            next(search))
993
982
        self.assertRaises(StopIteration, search.next_with_ghosts)
994
983
 
995
984
    def test_breadth_first_change_search(self):
996
985
        # Changing the search should work with both next and next_with_ghosts.
997
986
        graph = self.make_graph({
998
 
            'head':['present'],
999
 
            'present':['stopped'],
1000
 
            'other':['other_2'],
1001
 
            'other_2':[],
 
987
            b'head': [b'present'],
 
988
            b'present': [b'stopped'],
 
989
            b'other': [b'other_2'],
 
990
            b'other_2': [],
1002
991
            })
1003
 
        search = graph._make_breadth_first_searcher(['head'])
1004
 
        self.assertEqual((set(['head']), set()), search.next_with_ghosts())
1005
 
        self.assertEqual((set(['present']), set()), search.next_with_ghosts())
1006
 
        self.assertEqual(set(['present']),
1007
 
            search.stop_searching_any(['present']))
1008
 
        self.assertEqual((set(['other']), set(['other_ghost'])),
1009
 
            search.start_searching(['other', 'other_ghost']))
1010
 
        self.assertEqual((set(['other_2']), set()), search.next_with_ghosts())
 
992
        search = graph._make_breadth_first_searcher([b'head'])
 
993
        self.assertEqual(({b'head'}, set()), search.next_with_ghosts())
 
994
        self.assertEqual(({b'present'}, set()), search.next_with_ghosts())
 
995
        self.assertEqual({b'present'},
 
996
            search.stop_searching_any([b'present']))
 
997
        self.assertEqual(({b'other'}, {b'other_ghost'}),
 
998
            search.start_searching([b'other', b'other_ghost']))
 
999
        self.assertEqual(({b'other_2'}, set()), search.next_with_ghosts())
1011
1000
        self.assertRaises(StopIteration, search.next_with_ghosts)
1012
1001
        # next includes them
1013
 
        search = graph._make_breadth_first_searcher(['head'])
1014
 
        self.assertEqual(set(['head']), search.next())
1015
 
        self.assertEqual(set(['present']), search.next())
1016
 
        self.assertEqual(set(['present']),
1017
 
            search.stop_searching_any(['present']))
1018
 
        search.start_searching(['other', 'other_ghost'])
1019
 
        self.assertEqual(set(['other_2']), search.next())
1020
 
        self.assertRaises(StopIteration, search.next)
 
1002
        search = graph._make_breadth_first_searcher([b'head'])
 
1003
        self.assertEqual({b'head'}, next(search))
 
1004
        self.assertEqual({b'present'}, next(search))
 
1005
        self.assertEqual({b'present'},
 
1006
            search.stop_searching_any([b'present']))
 
1007
        search.start_searching([b'other', b'other_ghost'])
 
1008
        self.assertEqual({b'other_2'}, next(search))
 
1009
        self.assertRaises(StopIteration, next, search)
1021
1010
 
1022
1011
    def assertSeenAndResult(self, instructions, search, next):
1023
1012
        """Check the results of .seen and get_result() for a seach.
1040
1029
                search.start_searching(starts)
1041
1030
            if stops is not None:
1042
1031
                search.stop_searching_any(stops)
1043
 
            result = search.get_result()
1044
 
            self.assertEqual(recipe, result.get_recipe())
1045
 
            self.assertEqual(set(included_keys), result.get_keys())
 
1032
            state = search.get_state()
 
1033
            self.assertEqual(set(included_keys), state[2])
1046
1034
            self.assertEqual(seen, search.seen)
1047
1035
 
1048
1036
    def test_breadth_first_get_result_excludes_current_pending(self):
1049
1037
        graph = self.make_graph({
1050
 
            'head':['child'],
1051
 
            'child':[NULL_REVISION],
1052
 
            NULL_REVISION:[],
 
1038
            b'head': [b'child'],
 
1039
            b'child': [NULL_REVISION],
 
1040
            NULL_REVISION: [],
1053
1041
            })
1054
 
        search = graph._make_breadth_first_searcher(['head'])
 
1042
        search = graph._make_breadth_first_searcher([b'head'])
1055
1043
        # At the start, nothing has been seen, to its all excluded:
1056
 
        result = search.get_result()
1057
 
        self.assertEqual(('search', set(['head']), set(['head']), 0),
1058
 
            result.get_recipe())
1059
 
        self.assertEqual(set(), result.get_keys())
 
1044
        state = search.get_state()
 
1045
        self.assertEqual(({b'head'}, {b'head'}, set()),
 
1046
            state)
1060
1047
        self.assertEqual(set(), search.seen)
1061
1048
        # using next:
1062
1049
        expected = [
1063
 
            (set(['head']), (set(['head']), set(['child']), 1),
1064
 
             ['head'], None, None),
1065
 
            (set(['head', 'child']), (set(['head']), set([NULL_REVISION]), 2),
1066
 
             ['head', 'child'], None, None),
1067
 
            (set(['head', 'child', NULL_REVISION]), (set(['head']), set(), 3),
1068
 
             ['head', 'child', NULL_REVISION], None, None),
 
1050
            ({b'head'}, ({b'head'}, {b'child'}, 1),
 
1051
             [b'head'], None, None),
 
1052
            ({b'head', b'child'}, ({b'head'}, {NULL_REVISION}, 2),
 
1053
             [b'head', b'child'], None, None),
 
1054
            ({b'head', b'child', NULL_REVISION}, ({b'head'}, set(), 3),
 
1055
             [b'head', b'child', NULL_REVISION], None, None),
1069
1056
            ]
1070
 
        self.assertSeenAndResult(expected, search, search.next)
 
1057
        self.assertSeenAndResult(expected, search, search.__next__)
1071
1058
        # using next_with_ghosts:
1072
 
        search = graph._make_breadth_first_searcher(['head'])
 
1059
        search = graph._make_breadth_first_searcher([b'head'])
1073
1060
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1074
1061
 
1075
1062
    def test_breadth_first_get_result_starts_stops(self):
1076
1063
        graph = self.make_graph({
1077
 
            'head':['child'],
1078
 
            'child':[NULL_REVISION],
1079
 
            'otherhead':['otherchild'],
1080
 
            'otherchild':['excluded'],
1081
 
            'excluded':[NULL_REVISION],
 
1064
            b'head':[b'child'],
 
1065
            b'child':[NULL_REVISION],
 
1066
            b'otherhead':[b'otherchild'],
 
1067
            b'otherchild':[b'excluded'],
 
1068
            b'excluded':[NULL_REVISION],
1082
1069
            NULL_REVISION:[]
1083
1070
            })
1084
1071
        search = graph._make_breadth_first_searcher([])
1085
1072
        # Starting with nothing and adding a search works:
1086
 
        search.start_searching(['head'])
 
1073
        search.start_searching([b'head'])
1087
1074
        # head has been seen:
1088
 
        result = search.get_result()
1089
 
        self.assertEqual(('search', set(['head']), set(['child']), 1),
1090
 
            result.get_recipe())
1091
 
        self.assertEqual(set(['head']), result.get_keys())
1092
 
        self.assertEqual(set(['head']), search.seen)
 
1075
        state = search.get_state()
 
1076
        self.assertEqual(({b'head'}, {b'child'}, {b'head'}),
 
1077
            state)
 
1078
        self.assertEqual({b'head'}, search.seen)
1093
1079
        # using next:
1094
1080
        expected = [
1095
1081
            # stop at child, and start a new search at otherhead:
1096
1082
            # - otherhead counts as seen immediately when start_searching is
1097
1083
            # called.
1098
 
            (set(['head', 'child', 'otherhead']),
1099
 
             (set(['head', 'otherhead']), set(['child', 'otherchild']), 2),
1100
 
             ['head', 'otherhead'], ['otherhead'], ['child']),
1101
 
            (set(['head', 'child', 'otherhead', 'otherchild']),
1102
 
             (set(['head', 'otherhead']), set(['child', 'excluded']), 3),
1103
 
             ['head', 'otherhead', 'otherchild'], None, None),
 
1084
            ({b'head', b'child', b'otherhead'},
 
1085
             ({b'head', b'otherhead'}, {b'child', b'otherchild'}, 2),
 
1086
             [b'head', b'otherhead'], [b'otherhead'], [b'child']),
 
1087
            ({b'head', b'child', b'otherhead', b'otherchild'},
 
1088
             ({b'head', b'otherhead'}, {b'child', b'excluded'}, 3),
 
1089
             [b'head', b'otherhead', b'otherchild'], None, None),
1104
1090
            # stop searching excluded now
1105
 
            (set(['head', 'child', 'otherhead', 'otherchild', 'excluded']),
1106
 
             (set(['head', 'otherhead']), set(['child', 'excluded']), 3),
1107
 
             ['head', 'otherhead', 'otherchild'], None, ['excluded']),
 
1091
            ({b'head', b'child', b'otherhead', b'otherchild', b'excluded'},
 
1092
             ({b'head', b'otherhead'}, {b'child', b'excluded'}, 3),
 
1093
             [b'head', b'otherhead', b'otherchild'], None, [b'excluded']),
1108
1094
            ]
1109
 
        self.assertSeenAndResult(expected, search, search.next)
 
1095
        self.assertSeenAndResult(expected, search, search.__next__)
1110
1096
        # using next_with_ghosts:
1111
1097
        search = graph._make_breadth_first_searcher([])
1112
 
        search.start_searching(['head'])
 
1098
        search.start_searching([b'head'])
1113
1099
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1114
1100
 
1115
1101
    def test_breadth_first_stop_searching_not_queried(self):
1116
 
        # A client should be able to say 'stop node X' even if X has not been
 
1102
        # A client should be able to say b'stop node X' even if X has not been
1117
1103
        # returned to the client.
1118
1104
        graph = self.make_graph({
1119
 
            'head':['child', 'ghost1'],
1120
 
            'child':[NULL_REVISION],
1121
 
            NULL_REVISION:[],
 
1105
            b'head': [b'child', b'ghost1'],
 
1106
            b'child': [NULL_REVISION],
 
1107
            NULL_REVISION: [],
1122
1108
            })
1123
 
        search = graph._make_breadth_first_searcher(['head'])
 
1109
        search = graph._make_breadth_first_searcher([b'head'])
1124
1110
        expected = [
1125
1111
            # NULL_REVISION and ghost1 have not been returned
1126
 
            (set(['head']),
1127
 
             (set(['head']), set(['child', NULL_REVISION, 'ghost1']), 1),
1128
 
             ['head'], None, [NULL_REVISION, 'ghost1']),
 
1112
            ({b'head'},
 
1113
             ({b'head'}, {b'child', NULL_REVISION, b'ghost1'}, 1),
 
1114
             [b'head'], None, [NULL_REVISION, b'ghost1']),
1129
1115
            # ghost1 has been returned, NULL_REVISION is to be returned in the
1130
1116
            # next iteration.
1131
 
            (set(['head', 'child', 'ghost1']),
1132
 
             (set(['head']), set(['ghost1', NULL_REVISION]), 2),
1133
 
             ['head', 'child'], None, [NULL_REVISION, 'ghost1']),
 
1117
            ({b'head', b'child', b'ghost1'},
 
1118
             ({b'head'}, {b'ghost1', NULL_REVISION}, 2),
 
1119
             [b'head', b'child'], None, [NULL_REVISION, b'ghost1']),
1134
1120
            ]
1135
 
        self.assertSeenAndResult(expected, search, search.next)
 
1121
        self.assertSeenAndResult(expected, search, search.__next__)
1136
1122
        # using next_with_ghosts:
1137
 
        search = graph._make_breadth_first_searcher(['head'])
 
1123
        search = graph._make_breadth_first_searcher([b'head'])
1138
1124
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1139
1125
 
1140
1126
    def test_breadth_first_stop_searching_late(self):
1141
 
        # A client should be able to say 'stop node X' and have it excluded
 
1127
        # A client should be able to say b'stop node X' and have it excluded
1142
1128
        # from the result even if X was seen in an older iteration of the
1143
1129
        # search.
1144
1130
        graph = self.make_graph({
1145
 
            'head':['middle'],
1146
 
            'middle':['child'],
1147
 
            'child':[NULL_REVISION],
1148
 
            NULL_REVISION:[],
 
1131
            b'head': [b'middle'],
 
1132
            b'middle': [b'child'],
 
1133
            b'child': [NULL_REVISION],
 
1134
            NULL_REVISION: [],
1149
1135
            })
1150
 
        search = graph._make_breadth_first_searcher(['head'])
 
1136
        search = graph._make_breadth_first_searcher([b'head'])
1151
1137
        expected = [
1152
 
            (set(['head']), (set(['head']), set(['middle']), 1),
1153
 
             ['head'], None, None),
1154
 
            (set(['head', 'middle']), (set(['head']), set(['child']), 2),
1155
 
             ['head', 'middle'], None, None),
1156
 
            # 'middle' came from the previous iteration, but we don't stop
 
1138
            ({b'head'}, ({b'head'}, {b'middle'}, 1),
 
1139
             [b'head'], None, None),
 
1140
            ({b'head', b'middle'}, ({b'head'}, {b'child'}, 2),
 
1141
             [b'head', b'middle'], None, None),
 
1142
            # b'middle' came from the previous iteration, but we don't stop
1157
1143
            # searching it until *after* advancing the searcher.
1158
 
            (set(['head', 'middle', 'child']),
1159
 
             (set(['head']), set(['middle', 'child']), 1),
1160
 
             ['head'], None, ['middle', 'child']),
 
1144
            ({b'head', b'middle', b'child'},
 
1145
             ({b'head'}, {b'middle', b'child'}, 1),
 
1146
             [b'head'], None, [b'middle', b'child']),
1161
1147
            ]
1162
 
        self.assertSeenAndResult(expected, search, search.next)
 
1148
        self.assertSeenAndResult(expected, search, search.__next__)
1163
1149
        # using next_with_ghosts:
1164
 
        search = graph._make_breadth_first_searcher(['head'])
 
1150
        search = graph._make_breadth_first_searcher([b'head'])
1165
1151
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1166
1152
 
1167
1153
    def test_breadth_first_get_result_ghosts_are_excluded(self):
1168
1154
        graph = self.make_graph({
1169
 
            'head':['child', 'ghost'],
1170
 
            'child':[NULL_REVISION],
1171
 
            NULL_REVISION:[],
 
1155
            b'head': [b'child', b'ghost'],
 
1156
            b'child': [NULL_REVISION],
 
1157
            NULL_REVISION: [],
1172
1158
            })
1173
 
        search = graph._make_breadth_first_searcher(['head'])
 
1159
        search = graph._make_breadth_first_searcher([b'head'])
1174
1160
        # using next:
1175
1161
        expected = [
1176
 
            (set(['head']),
1177
 
             (set(['head']), set(['ghost', 'child']), 1),
1178
 
             ['head'], None, None),
1179
 
            (set(['head', 'child', 'ghost']),
1180
 
             (set(['head']), set([NULL_REVISION, 'ghost']), 2),
1181
 
             ['head', 'child'], None, None),
 
1162
            ({b'head'},
 
1163
             ({b'head'}, {b'ghost', b'child'}, 1),
 
1164
             [b'head'], None, None),
 
1165
            ({b'head', b'child', b'ghost'},
 
1166
             ({b'head'}, {NULL_REVISION, b'ghost'}, 2),
 
1167
             [b'head', b'child'], None, None),
1182
1168
            ]
1183
 
        self.assertSeenAndResult(expected, search, search.next)
 
1169
        self.assertSeenAndResult(expected, search, search.__next__)
1184
1170
        # using next_with_ghosts:
1185
 
        search = graph._make_breadth_first_searcher(['head'])
 
1171
        search = graph._make_breadth_first_searcher([b'head'])
1186
1172
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1187
1173
 
1188
1174
    def test_breadth_first_get_result_starting_a_ghost_ghost_is_excluded(self):
1189
1175
        graph = self.make_graph({
1190
 
            'head':['child'],
1191
 
            'child':[NULL_REVISION],
1192
 
            NULL_REVISION:[],
 
1176
            b'head': [b'child'],
 
1177
            b'child': [NULL_REVISION],
 
1178
            NULL_REVISION: [],
1193
1179
            })
1194
 
        search = graph._make_breadth_first_searcher(['head'])
 
1180
        search = graph._make_breadth_first_searcher([b'head'])
1195
1181
        # using next:
1196
1182
        expected = [
1197
 
            (set(['head', 'ghost']),
1198
 
             (set(['head', 'ghost']), set(['child', 'ghost']), 1),
1199
 
             ['head'], ['ghost'], None),
1200
 
            (set(['head', 'child', 'ghost']),
1201
 
             (set(['head', 'ghost']), set([NULL_REVISION, 'ghost']), 2),
1202
 
             ['head', 'child'], None, None),
 
1183
            ({b'head', b'ghost'},
 
1184
             ({b'head', b'ghost'}, {b'child', b'ghost'}, 1),
 
1185
             [b'head'], [b'ghost'], None),
 
1186
            ({b'head', b'child', b'ghost'},
 
1187
             ({b'head', b'ghost'}, {NULL_REVISION, b'ghost'}, 2),
 
1188
             [b'head', b'child'], None, None),
1203
1189
            ]
1204
 
        self.assertSeenAndResult(expected, search, search.next)
 
1190
        self.assertSeenAndResult(expected, search, search.__next__)
1205
1191
        # using next_with_ghosts:
1206
 
        search = graph._make_breadth_first_searcher(['head'])
 
1192
        search = graph._make_breadth_first_searcher([b'head'])
1207
1193
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1208
1194
 
1209
1195
    def test_breadth_first_revision_count_includes_NULL_REVISION(self):
1210
1196
        graph = self.make_graph({
1211
 
            'head':[NULL_REVISION],
1212
 
            NULL_REVISION:[],
 
1197
            b'head': [NULL_REVISION],
 
1198
            NULL_REVISION: [],
1213
1199
            })
1214
 
        search = graph._make_breadth_first_searcher(['head'])
 
1200
        search = graph._make_breadth_first_searcher([b'head'])
1215
1201
        # using next:
1216
1202
        expected = [
1217
 
            (set(['head']),
1218
 
             (set(['head']), set([NULL_REVISION]), 1),
1219
 
             ['head'], None, None),
1220
 
            (set(['head', NULL_REVISION]),
1221
 
             (set(['head']), set([]), 2),
1222
 
             ['head', NULL_REVISION], None, None),
 
1203
            ({b'head'},
 
1204
             ({b'head'}, {NULL_REVISION}, 1),
 
1205
             [b'head'], None, None),
 
1206
            ({b'head', NULL_REVISION},
 
1207
             ({b'head'}, set([]), 2),
 
1208
             [b'head', NULL_REVISION], None, None),
1223
1209
            ]
1224
 
        self.assertSeenAndResult(expected, search, search.next)
 
1210
        self.assertSeenAndResult(expected, search, search.__next__)
1225
1211
        # using next_with_ghosts:
1226
 
        search = graph._make_breadth_first_searcher(['head'])
 
1212
        search = graph._make_breadth_first_searcher([b'head'])
1227
1213
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1228
1214
 
1229
1215
    def test_breadth_first_search_get_result_after_StopIteration(self):
1230
1216
        # StopIteration should not invalid anything..
1231
1217
        graph = self.make_graph({
1232
 
            'head':[NULL_REVISION],
1233
 
            NULL_REVISION:[],
 
1218
            b'head': [NULL_REVISION],
 
1219
            NULL_REVISION: [],
1234
1220
            })
1235
 
        search = graph._make_breadth_first_searcher(['head'])
 
1221
        search = graph._make_breadth_first_searcher([b'head'])
1236
1222
        # using next:
1237
1223
        expected = [
1238
 
            (set(['head']),
1239
 
             (set(['head']), set([NULL_REVISION]), 1),
1240
 
             ['head'], None, None),
1241
 
            (set(['head', 'ghost', NULL_REVISION]),
1242
 
             (set(['head', 'ghost']), set(['ghost']), 2),
1243
 
             ['head', NULL_REVISION], ['ghost'], None),
 
1224
            ({b'head'},
 
1225
             ({b'head'}, {NULL_REVISION}, 1),
 
1226
             [b'head'], None, None),
 
1227
            ({b'head', b'ghost', NULL_REVISION},
 
1228
             ({b'head', b'ghost'}, {b'ghost'}, 2),
 
1229
             [b'head', NULL_REVISION], [b'ghost'], None),
1244
1230
            ]
1245
 
        self.assertSeenAndResult(expected, search, search.next)
1246
 
        self.assertRaises(StopIteration, search.next)
1247
 
        self.assertEqual(set(['head', 'ghost', NULL_REVISION]), search.seen)
1248
 
        result = search.get_result()
1249
 
        self.assertEqual(('search', set(['ghost', 'head']), set(['ghost']), 2),
1250
 
            result.get_recipe())
1251
 
        self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
 
1231
        self.assertSeenAndResult(expected, search, search.__next__)
 
1232
        self.assertRaises(StopIteration, next, search)
 
1233
        self.assertEqual({b'head', b'ghost', NULL_REVISION}, search.seen)
 
1234
        state = search.get_state()
 
1235
        self.assertEqual(
 
1236
            ({b'ghost', b'head'}, {b'ghost'},
 
1237
                {b'head', NULL_REVISION}),
 
1238
            state)
1252
1239
        # using next_with_ghosts:
1253
 
        search = graph._make_breadth_first_searcher(['head'])
 
1240
        search = graph._make_breadth_first_searcher([b'head'])
1254
1241
        self.assertSeenAndResult(expected, search, search.next_with_ghosts)
1255
 
        self.assertRaises(StopIteration, search.next)
1256
 
        self.assertEqual(set(['head', 'ghost', NULL_REVISION]), search.seen)
1257
 
        result = search.get_result()
1258
 
        self.assertEqual(('search', set(['ghost', 'head']), set(['ghost']), 2),
1259
 
            result.get_recipe())
1260
 
        self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
 
1242
        self.assertRaises(StopIteration, next, search)
 
1243
        self.assertEqual({b'head', b'ghost', NULL_REVISION}, search.seen)
 
1244
        state = search.get_state()
 
1245
        self.assertEqual(
 
1246
            ({b'ghost', b'head'}, {b'ghost'},
 
1247
                {b'head', NULL_REVISION}),
 
1248
            state)
1261
1249
 
1262
1250
 
1263
1251
class TestFindUniqueAncestors(TestGraphBase):
1268
1256
 
1269
1257
    def test_empty_set(self):
1270
1258
        graph = self.make_graph(ancestry_1)
1271
 
        self.assertFindUniqueAncestors(graph, [], 'rev1', ['rev1'])
1272
 
        self.assertFindUniqueAncestors(graph, [], 'rev2b', ['rev2b'])
1273
 
        self.assertFindUniqueAncestors(graph, [], 'rev3', ['rev1', 'rev3'])
 
1259
        self.assertFindUniqueAncestors(graph, [], b'rev1', [b'rev1'])
 
1260
        self.assertFindUniqueAncestors(graph, [], b'rev2b', [b'rev2b'])
 
1261
        self.assertFindUniqueAncestors(graph, [], b'rev3', [b'rev1', b'rev3'])
1274
1262
 
1275
1263
    def test_single_node(self):
1276
1264
        graph = self.make_graph(ancestry_1)
1277
 
        self.assertFindUniqueAncestors(graph, ['rev2a'], 'rev2a', ['rev1'])
1278
 
        self.assertFindUniqueAncestors(graph, ['rev2b'], 'rev2b', ['rev1'])
1279
 
        self.assertFindUniqueAncestors(graph, ['rev3'], 'rev3', ['rev2a'])
 
1265
        self.assertFindUniqueAncestors(graph, [b'rev2a'], b'rev2a', [b'rev1'])
 
1266
        self.assertFindUniqueAncestors(graph, [b'rev2b'], b'rev2b', [b'rev1'])
 
1267
        self.assertFindUniqueAncestors(graph, [b'rev3'], b'rev3', [b'rev2a'])
1280
1268
 
1281
1269
    def test_minimal_ancestry(self):
1282
1270
        graph = self.make_breaking_graph(extended_history_shortcut,
1283
 
                                         [NULL_REVISION, 'a', 'b'])
1284
 
        self.assertFindUniqueAncestors(graph, ['e'], 'e', ['d'])
 
1271
                                         [NULL_REVISION, b'a', b'b'])
 
1272
        self.assertFindUniqueAncestors(graph, [b'e'], b'e', [b'd'])
1285
1273
 
1286
1274
        graph = self.make_breaking_graph(extended_history_shortcut,
1287
 
                                         ['b'])
1288
 
        self.assertFindUniqueAncestors(graph, ['f'], 'f', ['a', 'd'])
 
1275
                                         [b'b'])
 
1276
        self.assertFindUniqueAncestors(graph, [b'f'], b'f', [b'a', b'd'])
1289
1277
 
1290
1278
        graph = self.make_breaking_graph(complex_shortcut,
1291
 
                                         ['a', 'b'])
1292
 
        self.assertFindUniqueAncestors(graph, ['h'], 'h', ['i'])
1293
 
        self.assertFindUniqueAncestors(graph, ['e', 'g', 'i'], 'i', ['h'])
1294
 
        self.assertFindUniqueAncestors(graph, ['h'], 'h', ['g'])
1295
 
        self.assertFindUniqueAncestors(graph, ['h'], 'h', ['j'])
 
1279
                                         [b'a', b'b'])
 
1280
        self.assertFindUniqueAncestors(graph, [b'h'], b'h', [b'i'])
 
1281
        self.assertFindUniqueAncestors(graph, [b'e', b'g', b'i'], b'i', [b'h'])
 
1282
        self.assertFindUniqueAncestors(graph, [b'h'], b'h', [b'g'])
 
1283
        self.assertFindUniqueAncestors(graph, [b'h'], b'h', [b'j'])
1296
1284
 
1297
1285
    def test_in_ancestry(self):
1298
1286
        graph = self.make_graph(ancestry_1)
1299
 
        self.assertFindUniqueAncestors(graph, [], 'rev1', ['rev3'])
1300
 
        self.assertFindUniqueAncestors(graph, [], 'rev2b', ['rev4'])
 
1287
        self.assertFindUniqueAncestors(graph, [], b'rev1', [b'rev3'])
 
1288
        self.assertFindUniqueAncestors(graph, [], b'rev2b', [b'rev4'])
1301
1289
 
1302
1290
    def test_multiple_revisions(self):
1303
1291
        graph = self.make_graph(ancestry_1)
1304
1292
        self.assertFindUniqueAncestors(graph,
1305
 
            ['rev4'], 'rev4', ['rev3', 'rev2b'])
 
1293
            [b'rev4'], b'rev4', [b'rev3', b'rev2b'])
1306
1294
        self.assertFindUniqueAncestors(graph,
1307
 
            ['rev2a', 'rev3', 'rev4'], 'rev4', ['rev2b'])
 
1295
            [b'rev2a', b'rev3', b'rev4'], b'rev4', [b'rev2b'])
1308
1296
 
1309
1297
    def test_complex_shortcut(self):
1310
1298
        graph = self.make_graph(complex_shortcut)
1311
1299
        self.assertFindUniqueAncestors(graph,
1312
 
            ['h', 'n'], 'n', ['m'])
 
1300
            [b'h', b'n'], b'n', [b'm'])
1313
1301
        self.assertFindUniqueAncestors(graph,
1314
 
            ['e', 'i', 'm'], 'm', ['n'])
 
1302
            [b'e', b'i', b'm'], b'm', [b'n'])
1315
1303
 
1316
1304
    def test_complex_shortcut2(self):
1317
1305
        graph = self.make_graph(complex_shortcut2)
1318
1306
        self.assertFindUniqueAncestors(graph,
1319
 
            ['j', 'u'], 'u', ['t'])
 
1307
            [b'j', b'u'], b'u', [b't'])
1320
1308
        self.assertFindUniqueAncestors(graph,
1321
 
            ['t'], 't', ['u'])
 
1309
            [b't'], b't', [b'u'])
1322
1310
 
1323
1311
    def test_multiple_interesting_unique(self):
1324
1312
        graph = self.make_graph(multiple_interesting_unique)
1325
1313
        self.assertFindUniqueAncestors(graph,
1326
 
            ['j', 'y'], 'y', ['z'])
 
1314
            [b'j', b'y'], b'y', [b'z'])
1327
1315
        self.assertFindUniqueAncestors(graph,
1328
 
            ['p', 'z'], 'z', ['y'])
 
1316
            [b'p', b'z'], b'z', [b'y'])
1329
1317
 
1330
1318
    def test_racing_shortcuts(self):
1331
1319
        graph = self.make_graph(racing_shortcuts)
1332
1320
        self.assertFindUniqueAncestors(graph,
1333
 
            ['p', 'q', 'z'], 'z', ['y'])
 
1321
            [b'p', b'q', b'z'], b'z', [b'y'])
1334
1322
        self.assertFindUniqueAncestors(graph,
1335
 
            ['h', 'i', 'j', 'y'], 'j', ['z'])
 
1323
            [b'h', b'i', b'j', b'y'], b'j', [b'z'])
1336
1324
 
1337
1325
 
1338
1326
class TestGraphFindDistanceToNull(TestGraphBase):
1346
1334
    def test_nothing_known(self):
1347
1335
        graph = self.make_graph(ancestry_1)
1348
1336
        self.assertFindDistance(0, graph, NULL_REVISION, [])
1349
 
        self.assertFindDistance(1, graph, 'rev1', [])
1350
 
        self.assertFindDistance(2, graph, 'rev2a', [])
1351
 
        self.assertFindDistance(2, graph, 'rev2b', [])
1352
 
        self.assertFindDistance(3, graph, 'rev3', [])
1353
 
        self.assertFindDistance(4, graph, 'rev4', [])
 
1337
        self.assertFindDistance(1, graph, b'rev1', [])
 
1338
        self.assertFindDistance(2, graph, b'rev2a', [])
 
1339
        self.assertFindDistance(2, graph, b'rev2b', [])
 
1340
        self.assertFindDistance(3, graph, b'rev3', [])
 
1341
        self.assertFindDistance(4, graph, b'rev4', [])
1354
1342
 
1355
1343
    def test_rev_is_ghost(self):
1356
1344
        graph = self.make_graph(ancestry_1)
1357
1345
        e = self.assertRaises(errors.GhostRevisionsHaveNoRevno,
1358
 
                              graph.find_distance_to_null, 'rev_missing', [])
1359
 
        self.assertEqual('rev_missing', e.revision_id)
1360
 
        self.assertEqual('rev_missing', e.ghost_revision_id)
 
1346
                              graph.find_distance_to_null, b'rev_missing', [])
 
1347
        self.assertEqual(b'rev_missing', e.revision_id)
 
1348
        self.assertEqual(b'rev_missing', e.ghost_revision_id)
1361
1349
 
1362
1350
    def test_ancestor_is_ghost(self):
1363
 
        graph = self.make_graph({'rev':['parent']})
 
1351
        graph = self.make_graph({b'rev': [b'parent']})
1364
1352
        e = self.assertRaises(errors.GhostRevisionsHaveNoRevno,
1365
 
                              graph.find_distance_to_null, 'rev', [])
1366
 
        self.assertEqual('rev', e.revision_id)
1367
 
        self.assertEqual('parent', e.ghost_revision_id)
 
1353
                              graph.find_distance_to_null, b'rev', [])
 
1354
        self.assertEqual(b'rev', e.revision_id)
 
1355
        self.assertEqual(b'parent', e.ghost_revision_id)
1368
1356
 
1369
1357
    def test_known_in_ancestry(self):
1370
1358
        graph = self.make_graph(ancestry_1)
1371
 
        self.assertFindDistance(2, graph, 'rev2a', [('rev1', 1)])
1372
 
        self.assertFindDistance(3, graph, 'rev3', [('rev2a', 2)])
 
1359
        self.assertFindDistance(2, graph, b'rev2a', [(b'rev1', 1)])
 
1360
        self.assertFindDistance(3, graph, b'rev3', [(b'rev2a', 2)])
1373
1361
 
1374
1362
    def test_known_in_ancestry_limits(self):
1375
 
        graph = self.make_breaking_graph(ancestry_1, ['rev1'])
1376
 
        self.assertFindDistance(4, graph, 'rev4', [('rev3', 3)])
 
1363
        graph = self.make_breaking_graph(ancestry_1, [b'rev1'])
 
1364
        self.assertFindDistance(4, graph, b'rev4', [(b'rev3', 3)])
1377
1365
 
1378
1366
    def test_target_is_ancestor(self):
1379
1367
        graph = self.make_graph(ancestry_1)
1380
 
        self.assertFindDistance(2, graph, 'rev2a', [('rev3', 3)])
 
1368
        self.assertFindDistance(2, graph, b'rev2a', [(b'rev3', 3)])
1381
1369
 
1382
1370
    def test_target_is_ancestor_limits(self):
1383
1371
        """We shouldn't search all history if we run into ourselves"""
1384
 
        graph = self.make_breaking_graph(ancestry_1, ['rev1'])
1385
 
        self.assertFindDistance(3, graph, 'rev3', [('rev4', 4)])
 
1372
        graph = self.make_breaking_graph(ancestry_1, [b'rev1'])
 
1373
        self.assertFindDistance(3, graph, b'rev3', [(b'rev4', 4)])
1386
1374
 
1387
1375
    def test_target_parallel_to_known_limits(self):
1388
1376
        # Even though the known revision isn't part of the other ancestry, they
1389
1377
        # eventually converge
1390
 
        graph = self.make_breaking_graph(with_tail, ['a'])
1391
 
        self.assertFindDistance(6, graph, 'f', [('g', 6)])
1392
 
        self.assertFindDistance(7, graph, 'h', [('g', 6)])
1393
 
        self.assertFindDistance(8, graph, 'i', [('g', 6)])
1394
 
        self.assertFindDistance(6, graph, 'g', [('i', 8)])
 
1378
        graph = self.make_breaking_graph(with_tail, [b'a'])
 
1379
        self.assertFindDistance(6, graph, b'f', [(b'g', 6)])
 
1380
        self.assertFindDistance(7, graph, b'h', [(b'g', 6)])
 
1381
        self.assertFindDistance(8, graph, b'i', [(b'g', 6)])
 
1382
        self.assertFindDistance(6, graph, b'g', [(b'i', 8)])
1395
1383
 
1396
1384
 
1397
1385
class TestFindMergeOrder(TestGraphBase):
1401
1389
 
1402
1390
    def test_parents(self):
1403
1391
        graph = self.make_graph(ancestry_1)
1404
 
        self.assertMergeOrder(['rev3', 'rev2b'], graph, 'rev4',
1405
 
                                                        ['rev3', 'rev2b'])
1406
 
        self.assertMergeOrder(['rev3', 'rev2b'], graph, 'rev4',
1407
 
                                                        ['rev2b', 'rev3'])
 
1392
        self.assertMergeOrder([b'rev3', b'rev2b'], graph, b'rev4',
 
1393
                                                        [b'rev3', b'rev2b'])
 
1394
        self.assertMergeOrder([b'rev3', b'rev2b'], graph, b'rev4',
 
1395
                                                        [b'rev2b', b'rev3'])
1408
1396
 
1409
1397
    def test_ancestors(self):
1410
1398
        graph = self.make_graph(ancestry_1)
1411
 
        self.assertMergeOrder(['rev1', 'rev2b'], graph, 'rev4',
1412
 
                                                        ['rev1', 'rev2b'])
1413
 
        self.assertMergeOrder(['rev1', 'rev2b'], graph, 'rev4',
1414
 
                                                        ['rev2b', 'rev1'])
 
1399
        self.assertMergeOrder([b'rev1', b'rev2b'], graph, b'rev4',
 
1400
                                                        [b'rev1', b'rev2b'])
 
1401
        self.assertMergeOrder([b'rev1', b'rev2b'], graph, b'rev4',
 
1402
                                                        [b'rev2b', b'rev1'])
1415
1403
 
1416
1404
    def test_shortcut_one_ancestor(self):
1417
1405
        # When we have enough info, we can stop searching
1418
 
        graph = self.make_breaking_graph(ancestry_1, ['rev3', 'rev2b', 'rev4'])
 
1406
        graph = self.make_breaking_graph(ancestry_1, [b'rev3', b'rev2b', b'rev4'])
1419
1407
        # Single ancestors shortcut right away
1420
 
        self.assertMergeOrder(['rev3'], graph, 'rev4', ['rev3'])
 
1408
        self.assertMergeOrder([b'rev3'], graph, b'rev4', [b'rev3'])
1421
1409
 
1422
1410
    def test_shortcut_after_one_ancestor(self):
1423
 
        graph = self.make_breaking_graph(ancestry_1, ['rev2a', 'rev2b'])
1424
 
        self.assertMergeOrder(['rev3', 'rev1'], graph, 'rev4', ['rev1', 'rev3'])
 
1411
        graph = self.make_breaking_graph(ancestry_1, [b'rev2a', b'rev2b'])
 
1412
        self.assertMergeOrder([b'rev3', b'rev1'], graph, b'rev4', [b'rev1', b'rev3'])
 
1413
 
 
1414
 
 
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(b'rev1', b'rev3')
 
1420
        self.assertEqual({b'rev1', b'rev2a', b'rev3'}, descendants)
 
1421
 
 
1422
    def test_find_descendants_rev1_rev4(self):
 
1423
        graph = self.make_graph(ancestry_1)
 
1424
        descendants = graph.find_descendants(b'rev1', b'rev4')
 
1425
        self.assertEqual({b'rev1', b'rev2a', b'rev2b', b'rev3', b'rev4'},
 
1426
                         descendants)
 
1427
 
 
1428
    def test_find_descendants_rev2a_rev4(self):
 
1429
        graph = self.make_graph(ancestry_1)
 
1430
        descendants = graph.find_descendants(b'rev2a', b'rev4')
 
1431
        self.assertEqual({b'rev2a', b'rev3', b'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(b'rev4', ancestry_1, b'rev2b', b'rev4')
 
1441
 
 
1442
    def test_find_lefthand_merger_rev2a(self):
 
1443
        self.check_merger(b'rev2a', ancestry_1, b'rev2a', b'rev4')
 
1444
 
 
1445
    def test_find_lefthand_merger_rev4(self):
 
1446
        self.check_merger(None, ancestry_1, b'rev4', b'rev2a')
 
1447
 
 
1448
    def test_find_lefthand_merger_f(self):
 
1449
        self.check_merger(b'i', complex_shortcut, b'f', b'm')
 
1450
 
 
1451
    def test_find_lefthand_merger_g(self):
 
1452
        self.check_merger(b'i', complex_shortcut, b'g', b'm')
 
1453
 
 
1454
    def test_find_lefthand_merger_h(self):
 
1455
        self.check_merger(b'n', complex_shortcut, b'h', b'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([b'rev4', b'rev3', b'rev2a', b'rev2b'])
 
1463
        self.assertEqual({b'rev1': [b'rev2a', b'rev2b'],
 
1464
                          b'rev2a': [b'rev3'],
 
1465
                          b'rev2b': [b'rev4'],
 
1466
                          b'rev3': [b'rev4']},
 
1467
                          child_map)
1425
1468
 
1426
1469
 
1427
1470
class TestCachingParentsProvider(tests.TestCase):
1434
1477
 
1435
1478
    def setUp(self):
1436
1479
        super(TestCachingParentsProvider, self).setUp()
1437
 
        dict_pp = _mod_graph.DictParentsProvider({'a':('b',)})
 
1480
        dict_pp = _mod_graph.DictParentsProvider({b'a': (b'b',)})
1438
1481
        self.inst_pp = InstrumentedParentsProvider(dict_pp)
1439
1482
        self.caching_pp = _mod_graph.CachingParentsProvider(self.inst_pp)
1440
1483
 
1441
1484
    def test_get_parent_map(self):
1442
1485
        """Requesting the same revision should be returned from cache"""
1443
1486
        self.assertEqual({}, self.caching_pp._cache)
1444
 
        self.assertEqual({'a':('b',)}, self.caching_pp.get_parent_map(['a']))
1445
 
        self.assertEqual(['a'], self.inst_pp.calls)
1446
 
        self.assertEqual({'a':('b',)}, self.caching_pp.get_parent_map(['a']))
 
1487
        self.assertEqual({b'a':(b'b',)}, self.caching_pp.get_parent_map([b'a']))
 
1488
        self.assertEqual([b'a'], self.inst_pp.calls)
 
1489
        self.assertEqual({b'a':(b'b',)}, self.caching_pp.get_parent_map([b'a']))
1447
1490
        # No new call, as it should have been returned from the cache
1448
 
        self.assertEqual(['a'], self.inst_pp.calls)
1449
 
        self.assertEqual({'a':('b',)}, self.caching_pp._cache)
 
1491
        self.assertEqual([b'a'], self.inst_pp.calls)
 
1492
        self.assertEqual({b'a':(b'b',)}, self.caching_pp._cache)
1450
1493
 
1451
1494
    def test_get_parent_map_not_present(self):
1452
1495
        """The cache should also track when a revision doesn't exist"""
1453
 
        self.assertEqual({}, self.caching_pp.get_parent_map(['b']))
1454
 
        self.assertEqual(['b'], self.inst_pp.calls)
1455
 
        self.assertEqual({}, self.caching_pp.get_parent_map(['b']))
 
1496
        self.assertEqual({}, self.caching_pp.get_parent_map([b'b']))
 
1497
        self.assertEqual([b'b'], self.inst_pp.calls)
 
1498
        self.assertEqual({}, self.caching_pp.get_parent_map([b'b']))
1456
1499
        # No new calls
1457
 
        self.assertEqual(['b'], self.inst_pp.calls)
 
1500
        self.assertEqual([b'b'], self.inst_pp.calls)
1458
1501
 
1459
1502
    def test_get_parent_map_mixed(self):
1460
1503
        """Anything that can be returned from cache, should be"""
1461
 
        self.assertEqual({}, self.caching_pp.get_parent_map(['b']))
1462
 
        self.assertEqual(['b'], self.inst_pp.calls)
1463
 
        self.assertEqual({'a':('b',)},
1464
 
                         self.caching_pp.get_parent_map(['a', 'b']))
1465
 
        self.assertEqual(['b', 'a'], self.inst_pp.calls)
 
1504
        self.assertEqual({}, self.caching_pp.get_parent_map([b'b']))
 
1505
        self.assertEqual([b'b'], self.inst_pp.calls)
 
1506
        self.assertEqual({b'a':(b'b',)},
 
1507
                         self.caching_pp.get_parent_map([b'a', b'b']))
 
1508
        self.assertEqual([b'b', b'a'], self.inst_pp.calls)
1466
1509
 
1467
1510
    def test_get_parent_map_repeated(self):
1468
1511
        """Asking for the same parent 2x will only forward 1 request."""
1469
 
        self.assertEqual({'a':('b',)},
1470
 
                         self.caching_pp.get_parent_map(['b', 'a', 'b']))
 
1512
        self.assertEqual({b'a':(b'b',)},
 
1513
                         self.caching_pp.get_parent_map([b'b', b'a', b'b']))
1471
1514
        # Use sorted because we don't care about the order, just that each is
1472
1515
        # only present 1 time.
1473
 
        self.assertEqual(['a', 'b'], sorted(self.inst_pp.calls))
 
1516
        self.assertEqual([b'a', b'b'], sorted(self.inst_pp.calls))
1474
1517
 
1475
1518
    def test_note_missing_key(self):
1476
1519
        """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)
 
1520
        self.caching_pp.note_missing_key(b'b')
 
1521
        self.assertEqual({}, self.caching_pp.get_parent_map([b'b']))
 
1522
        self.assertEqual([], self.inst_pp.calls)
 
1523
        self.assertEqual({b'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([b'a']))
 
1527
        self.assertEqual([], self.inst_pp.calls)
 
1528
        self.assertEqual({b'a': (b'b',)}, self.caching_pp.get_parent_map([b'a']))
 
1529
        self.assertEqual([b'a'], self.inst_pp.calls)
 
1530
        self.assertEqual({b'a': (b'b',)},
 
1531
                         self.caching_pp.get_cached_parent_map([b'a']))
1481
1532
 
1482
1533
 
1483
1534
class TestCachingParentsProviderExtras(tests.TestCaseWithTransport):
1488
1539
        class ExtraParentsProvider(object):
1489
1540
 
1490
1541
            def get_parent_map(self, keys):
1491
 
                return {'rev1': [], 'rev2': ['rev1',]}
 
1542
                return {b'rev1': [], b'rev2': [b'rev1',]}
1492
1543
 
1493
1544
        self.inst_pp = InstrumentedParentsProvider(ExtraParentsProvider())
1494
1545
        self.caching_pp = _mod_graph.CachingParentsProvider(
1496
1547
 
1497
1548
    def test_uncached(self):
1498
1549
        self.caching_pp.disable_cache()
1499
 
        self.assertEqual({'rev1': []},
1500
 
                         self.caching_pp.get_parent_map(['rev1']))
1501
 
        self.assertEqual(['rev1'], self.inst_pp.calls)
 
1550
        self.assertEqual({b'rev1': []},
 
1551
                         self.caching_pp.get_parent_map([b'rev1']))
 
1552
        self.assertEqual([b'rev1'], self.inst_pp.calls)
1502
1553
        self.assertIs(None, self.caching_pp._cache)
1503
1554
 
1504
1555
    def test_cache_initially_empty(self):
1505
1556
        self.assertEqual({}, self.caching_pp._cache)
1506
1557
 
1507
1558
    def test_cached(self):
1508
 
        self.assertEqual({'rev1': []},
1509
 
                         self.caching_pp.get_parent_map(['rev1']))
1510
 
        self.assertEqual(['rev1'], self.inst_pp.calls)
1511
 
        self.assertEqual({'rev1': [], 'rev2': ['rev1']},
 
1559
        self.assertEqual({b'rev1': []},
 
1560
                         self.caching_pp.get_parent_map([b'rev1']))
 
1561
        self.assertEqual([b'rev1'], self.inst_pp.calls)
 
1562
        self.assertEqual({b'rev1': [], b'rev2': [b'rev1']},
1512
1563
                         self.caching_pp._cache)
1513
 
        self.assertEqual({'rev1': []},
1514
 
                          self.caching_pp.get_parent_map(['rev1']))
1515
 
        self.assertEqual(['rev1'], self.inst_pp.calls)
 
1564
        self.assertEqual({b'rev1': []},
 
1565
                          self.caching_pp.get_parent_map([b'rev1']))
 
1566
        self.assertEqual([b'rev1'], self.inst_pp.calls)
1516
1567
 
1517
1568
    def test_disable_cache_clears_cache(self):
1518
1569
        # Put something in the cache
1519
 
        self.caching_pp.get_parent_map(['rev1'])
 
1570
        self.caching_pp.get_parent_map([b'rev1'])
1520
1571
        self.assertEqual(2, len(self.caching_pp._cache))
1521
1572
        self.caching_pp.disable_cache()
1522
1573
        self.assertIs(None, self.caching_pp._cache)
1526
1577
        self.assertEqual('Cache enabled when already enabled.', str(e))
1527
1578
 
1528
1579
    def test_cache_misses(self):
1529
 
        self.caching_pp.get_parent_map(['rev3'])
1530
 
        self.caching_pp.get_parent_map(['rev3'])
1531
 
        self.assertEqual(['rev3'], self.inst_pp.calls)
 
1580
        self.caching_pp.get_parent_map([b'rev3'])
 
1581
        self.caching_pp.get_parent_map([b'rev3'])
 
1582
        self.assertEqual([b'rev3'], self.inst_pp.calls)
1532
1583
 
1533
1584
    def test_no_cache_misses(self):
1534
1585
        self.caching_pp.disable_cache()
1535
1586
        self.caching_pp.enable_cache(cache_misses=False)
1536
 
        self.caching_pp.get_parent_map(['rev3'])
1537
 
        self.caching_pp.get_parent_map(['rev3'])
1538
 
        self.assertEqual(['rev3', 'rev3'], self.inst_pp.calls)
 
1587
        self.caching_pp.get_parent_map([b'rev3'])
 
1588
        self.caching_pp.get_parent_map([b'rev3'])
 
1589
        self.assertEqual([b'rev3', b'rev3'], self.inst_pp.calls)
1539
1590
 
1540
1591
    def test_cache_extras(self):
1541
 
        self.assertEqual({}, self.caching_pp.get_parent_map(['rev3']))
1542
 
        self.assertEqual({'rev2': ['rev1']},
1543
 
                         self.caching_pp.get_parent_map(['rev2']))
1544
 
        self.assertEqual(['rev3'], self.inst_pp.calls)
 
1592
        self.assertEqual({}, self.caching_pp.get_parent_map([b'rev3']))
 
1593
        self.assertEqual({b'rev2': [b'rev1']},
 
1594
                         self.caching_pp.get_parent_map([b'rev2']))
 
1595
        self.assertEqual([b'rev3'], self.inst_pp.calls)
 
1596
 
 
1597
    def test_extras_using_cached(self):
 
1598
        self.assertEqual({}, self.caching_pp.get_cached_parent_map([b'rev3']))
 
1599
        self.assertEqual({}, self.caching_pp.get_parent_map([b'rev3']))
 
1600
        self.assertEqual({b'rev2': [b'rev1']},
 
1601
                         self.caching_pp.get_cached_parent_map([b'rev2']))
 
1602
        self.assertEqual([b'rev3'], self.inst_pp.calls)
 
1603
 
1545
1604
 
1546
1605
 
1547
1606
class TestCollapseLinearRegions(tests.TestCase):
1590
1649
        # B C
1591
1650
        # |/
1592
1651
        # D
1593
 
        d = {('D',): [('B',), ('C',)], ('C',):[('A',)],
1594
 
             ('B',): [('A',)], ('A',): []}
 
1652
        d = {(b'D',): [(b'B',), (b'C',)], (b'C',):[(b'A',)],
 
1653
             (b'B',): [(b'A',)], (b'A',): []}
1595
1654
        g = _mod_graph.Graph(_mod_graph.DictParentsProvider(d))
1596
1655
        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())
 
1656
        self.assertEqual([b'D'], sorted(graph_thunk.heads([b'D', b'A'])))
 
1657
        self.assertEqual([b'D'], sorted(graph_thunk.heads([b'D', b'B'])))
 
1658
        self.assertEqual([b'D'], sorted(graph_thunk.heads([b'D', b'C'])))
 
1659
        self.assertEqual([b'B', b'C'], sorted(graph_thunk.heads([b'B', b'C'])))
 
1660
 
 
1661
    def test_add_node(self):
 
1662
        d = {(b'C',):[(b'A',)], (b'B',): [(b'A',)], (b'A',): []}
 
1663
        g = _mod_graph.KnownGraph(d)
 
1664
        graph_thunk = _mod_graph.GraphThunkIdsToKeys(g)
 
1665
        graph_thunk.add_node(b"D", [b"A", b"C"])
 
1666
        self.assertEqual([b'B', b'D'],
 
1667
            sorted(graph_thunk.heads([b'D', b'B', b'A'])))
 
1668
 
 
1669
    def test_merge_sort(self):
 
1670
        d = {(b'C',):[(b'A',)], (b'B',): [(b'A',)], (b'A',): []}
 
1671
        g = _mod_graph.KnownGraph(d)
 
1672
        graph_thunk = _mod_graph.GraphThunkIdsToKeys(g)
 
1673
        graph_thunk.add_node(b"D", [b"A", b"C"])
 
1674
        self.assertEqual([(b'C', 0, (2,), False), (b'A', 0, (1,), True)],
 
1675
            [(n.key, n.merge_depth, n.revno, n.end_of_merge)
 
1676
                 for n in graph_thunk.merge_sort(b'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({b'rev2': [b'rev3']})
 
1693
        parents2 = _mod_graph.DictParentsProvider({b'rev1': [b'rev4']})
 
1694
        stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
 
1695
        self.assertEqual({b'rev1':[b'rev4'], b'rev2':[b'rev3']},
 
1696
                         stacked.get_parent_map([b'rev1', b'rev2']))
 
1697
        self.assertEqual({b'rev2':[b'rev3'], b'rev1':[b'rev4']},
 
1698
                         stacked.get_parent_map([b'rev2', b'rev1']))
 
1699
        self.assertEqual({b'rev2':[b'rev3']},
 
1700
                         stacked.get_parent_map([b'rev2', b'rev2']))
 
1701
        self.assertEqual({b'rev1':[b'rev4']},
 
1702
                         stacked.get_parent_map([b'rev1', b'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({b'rev2': [b'rev1']})
 
1710
        parents2 = _mod_graph.DictParentsProvider({b'rev2': [b'rev1']})
 
1711
        stacked = _mod_graph.StackedParentsProvider([parents1, parents2])
 
1712
        self.assertEqual({b'rev2': [b'rev1']},
 
1713
                         stacked.get_parent_map([b'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(b'pp1', {b'rev2': (b'rev1',)},
 
1719
                                       has_cached=False)
 
1720
        pp2 = self.get_shared_provider(b'pp2', {b'rev2': (b'rev1',)},
 
1721
                                       has_cached=True)
 
1722
        stacked = _mod_graph.StackedParentsProvider([pp1, pp2])
 
1723
        self.assertEqual({b'rev2': (b'rev1',)}, stacked.get_parent_map([b'rev2']))
 
1724
        # No call on b'pp1' because it doesn't provide get_cached_parent_map
 
1725
        self.assertEqual([(b'pp2', 'cached', [b'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(b'pp1', {b'a': ()}, has_cached=True)
 
1732
        pp2 = self.get_shared_provider(b'pp2', {b'c': (b'b',)}, has_cached=False)
 
1733
        pp3 = self.get_shared_provider(b'pp3', {b'b': (b'a',)}, has_cached=True)
 
1734
        stacked = _mod_graph.StackedParentsProvider([pp1, pp2, pp3])
 
1735
        self.assertEqual({b'a': (), b'b': (b'a',), b'c': (b'b',)},
 
1736
                         stacked.get_parent_map([b'a', b'b', b'c', b'd']))
 
1737
        self.assertEqual([(b'pp1', 'cached', [b'a', b'b', b'c', b'd']),
 
1738
                          # No call to pp2, because it doesn't have cached
 
1739
                          (b'pp3', 'cached', [b'b', b'c', b'd']),
 
1740
                          (b'pp1', [b'c', b'd']),
 
1741
                          (b'pp2', [b'c', b'd']),
 
1742
                          (b'pp3', [b'd']),
 
1743
                         ], self.calls)