/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 brzlib/tests/test_vf_search.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 .. import (
 
17
from brzlib import (
18
18
    graph as _mod_graph,
19
19
    tests,
20
 
    )
21
 
from ..bzr import (
22
20
    vf_search,
23
21
    )
24
 
from ..revision import NULL_REVISION
25
 
from .test_graph import TestGraphBase
 
22
from brzlib.revision import NULL_REVISION
 
23
from brzlib.tests.test_graph import TestGraphBase
26
24
 
27
25
# Ancestry 1:
28
26
#
35
33
#   rev3  /
36
34
#     |  /
37
35
#   rev4
38
 
ancestry_1 = {b'rev1': [NULL_REVISION],
39
 
              b'rev2a': [b'rev1'],
40
 
              b'rev2b': [b'rev1'],
41
 
              b'rev3': [b'rev2a'],
42
 
              b'rev4': [b'rev3', b'rev2b']}
 
36
ancestry_1 = {'rev1': [NULL_REVISION], 'rev2a': ['rev1'], 'rev2b': ['rev1'],
 
37
              'rev3': ['rev2a'], 'rev4': ['rev3', 'rev2b']}
43
38
 
44
39
# Ancestry 2:
45
40
#
52
47
# rev3a
53
48
#   |
54
49
# rev4a
55
 
ancestry_2 = {b'rev1a': [NULL_REVISION],
56
 
              b'rev2a': [b'rev1a'],
57
 
              b'rev1b': [NULL_REVISION],
58
 
              b'rev3a': [b'rev2a'],
59
 
              b'rev4a': [b'rev3a']}
 
50
ancestry_2 = {'rev1a': [NULL_REVISION], 'rev2a': ['rev1a'],
 
51
              'rev1b': [NULL_REVISION], 'rev3a': ['rev2a'], 'rev4a': ['rev3a']}
60
52
 
61
53
 
62
54
# Extended history shortcut
71
63
#       d |
72
64
#       |\|
73
65
#       e f
74
 
extended_history_shortcut = {b'a': [NULL_REVISION],
75
 
                             b'b': [b'a'],
76
 
                             b'c': [b'b'],
77
 
                             b'd': [b'c'],
78
 
                             b'e': [b'd'],
79
 
                             b'f': [b'a', b'd'],
80
 
                             }
 
66
extended_history_shortcut = {'a': [NULL_REVISION],
 
67
                             'b': ['a'],
 
68
                             'c': ['b'],
 
69
                             'd': ['c'],
 
70
                             'e': ['d'],
 
71
                             'f': ['a', 'd'],
 
72
                            }
81
73
 
82
74
 
83
75
class TestSearchResultRefine(tests.TestCase):
88
80
    def test_refine(self):
89
81
        # Used when pulling from a stacked repository, so test some revisions
90
82
        # being satisfied from the stacking branch.
91
 
        self.make_graph(
92
 
            {b"tip": [b"mid"], b"mid": [b"base"], b"tag": [b"base"],
93
 
             b"base": [NULL_REVISION], NULL_REVISION: []})
94
 
        result = vf_search.SearchResult(
95
 
            {b'tip', b'tag'},
96
 
            {NULL_REVISION}, 4, {b'tip', b'mid', b'tag', b'base'})
97
 
        result = result.refine({b'tip'}, {b'mid'})
 
83
        g = self.make_graph(
 
84
            {"tip":["mid"], "mid":["base"], "tag":["base"],
 
85
             "base":[NULL_REVISION], NULL_REVISION:[]})
 
86
        result = vf_search.SearchResult(set(['tip', 'tag']),
 
87
            set([NULL_REVISION]), 4, set(['tip', 'mid', 'tag', 'base']))
 
88
        result = result.refine(set(['tip']), set(['mid']))
98
89
        recipe = result.get_recipe()
99
90
        # We should be starting from tag (original head) and mid (seen ref)
100
 
        self.assertEqual({b'mid', b'tag'}, recipe[1])
 
91
        self.assertEqual(set(['mid', 'tag']), recipe[1])
101
92
        # We should be stopping at NULL (original stop) and tip (seen head)
102
 
        self.assertEqual({NULL_REVISION, b'tip'}, recipe[2])
 
93
        self.assertEqual(set([NULL_REVISION, 'tip']), recipe[2])
103
94
        self.assertEqual(3, recipe[3])
104
 
        result = result.refine({b'mid', b'tag', b'base'},
105
 
                               {NULL_REVISION})
 
95
        result = result.refine(set(['mid', 'tag', 'base']),
 
96
            set([NULL_REVISION]))
106
97
        recipe = result.get_recipe()
107
98
        # We should be starting from nothing (NULL was known as a cut point)
108
99
        self.assertEqual(set([]), recipe[1])
110
101
        # tag (seen head) and mid(seen mid-point head). We could come back and
111
102
        # define this as not including mid, for minimal results, but it is
112
103
        # still 'correct' to include mid, and simpler/easier.
113
 
        self.assertEqual({NULL_REVISION, b'tip', b'tag', b'mid'}, recipe[2])
 
104
        self.assertEqual(set([NULL_REVISION, 'tip', 'tag', 'mid']), recipe[2])
114
105
        self.assertEqual(0, recipe[3])
115
106
        self.assertTrue(result.is_empty())
116
107
 
129
120
        self.assertSearchResult([], [], 0, None)
130
121
 
131
122
    def test_ancestry_1(self):
132
 
        self.assertSearchResult([b'rev4'], [NULL_REVISION], len(ancestry_1),
 
123
        self.assertSearchResult(['rev4'], [NULL_REVISION], len(ancestry_1),
133
124
                                ancestry_1)
134
125
 
135
126
    def test_ancestry_2(self):
136
 
        self.assertSearchResult([b'rev1b', b'rev4a'], [NULL_REVISION],
 
127
        self.assertSearchResult(['rev1b', 'rev4a'], [NULL_REVISION],
137
128
                                len(ancestry_2), ancestry_2)
138
 
        self.assertSearchResult([b'rev1b', b'rev4a'], [],
139
 
                                len(ancestry_2) + 1, ancestry_2,
 
129
        self.assertSearchResult(['rev1b', 'rev4a'], [],
 
130
                                len(ancestry_2)+1, ancestry_2,
140
131
                                missing_keys=[NULL_REVISION])
141
132
 
142
133
    def test_partial_search(self):
143
 
        parent_map = dict((k, extended_history_shortcut[k])
144
 
                          for k in [b'e', b'f'])
145
 
        self.assertSearchResult([b'e', b'f'], [b'd', b'a'], 2,
146
 
                                parent_map)
147
 
        parent_map.update((k, extended_history_shortcut[k])
148
 
                          for k in [b'd', b'a'])
149
 
        self.assertSearchResult([b'e', b'f'], [b'c', NULL_REVISION], 4,
150
 
                                parent_map)
151
 
        parent_map[b'c'] = extended_history_shortcut[b'c']
152
 
        self.assertSearchResult([b'e', b'f'], [b'b'], 6,
 
134
        parent_map = dict((k,extended_history_shortcut[k])
 
135
                          for k in ['e', 'f'])
 
136
        self.assertSearchResult(['e', 'f'], ['d', 'a'], 2,
 
137
                                parent_map)
 
138
        parent_map.update((k,extended_history_shortcut[k])
 
139
                          for k in ['d', 'a'])
 
140
        self.assertSearchResult(['e', 'f'], ['c', NULL_REVISION], 4,
 
141
                                parent_map)
 
142
        parent_map['c'] = extended_history_shortcut['c']
 
143
        self.assertSearchResult(['e', 'f'], ['b'], 6,
153
144
                                parent_map, missing_keys=[NULL_REVISION])
154
 
        parent_map[b'b'] = extended_history_shortcut[b'b']
155
 
        self.assertSearchResult([b'e', b'f'], [], 7,
 
145
        parent_map['b'] = extended_history_shortcut['b']
 
146
        self.assertSearchResult(['e', 'f'], [], 7,
156
147
                                parent_map, missing_keys=[NULL_REVISION])
157
148
 
158
149
 
166
157
                         (sorted(start), sorted(stop), count))
167
158
 
168
159
    def test_empty_ancestry(self):
169
 
        self.assertSearchResult([], [], 0, {}, (), [b'tip-rev-id'], 10)
 
160
        self.assertSearchResult([], [], 0, {}, (), ['tip-rev-id'], 10)
170
161
 
171
162
    def test_ancestry_1(self):
172
 
        self.assertSearchResult([b'rev4'], [b'rev1'], 4,
173
 
                                ancestry_1, (), [b'rev1'], 10)
174
 
        self.assertSearchResult([b'rev2a', b'rev2b'], [b'rev1'], 2,
175
 
                                ancestry_1, (), [b'rev1'], 1)
 
163
        self.assertSearchResult(['rev4'], ['rev1'], 4,
 
164
                                ancestry_1, (), ['rev1'], 10)
 
165
        self.assertSearchResult(['rev2a', 'rev2b'], ['rev1'], 2,
 
166
                                ancestry_1, (), ['rev1'], 1)
 
167
 
176
168
 
177
169
    def test_multiple_heads(self):
178
 
        self.assertSearchResult([b'e', b'f'], [b'a'], 5,
179
 
                                extended_history_shortcut, (), [b'a'], 10)
 
170
        self.assertSearchResult(['e', 'f'], ['a'], 5,
 
171
                                extended_history_shortcut, (), ['a'], 10)
180
172
        # Note that even though we only take 1 step back, we find 'f', which
181
173
        # means the described search will still find d and c.
182
 
        self.assertSearchResult([b'f'], [b'a'], 4,
183
 
                                extended_history_shortcut, (), [b'a'], 1)
184
 
        self.assertSearchResult([b'f'], [b'a'], 4,
185
 
                                extended_history_shortcut, (), [b'a'], 2)
 
174
        self.assertSearchResult(['f'], ['a'], 4,
 
175
                                extended_history_shortcut, (), ['a'], 1)
 
176
        self.assertSearchResult(['f'], ['a'], 4,
 
177
                                extended_history_shortcut, (), ['a'], 2)
186
178
 
187
179
 
188
180
class TestPendingAncestryResultRefine(tests.TestCase):
194
186
        # Used when pulling from a stacked repository, so test some revisions
195
187
        # being satisfied from the stacking branch.
196
188
        g = self.make_graph(
197
 
            {b"tip": [b"mid"], b"mid": [b"base"], b"tag": [b"base"],
198
 
             b"base": [NULL_REVISION], NULL_REVISION: []})
199
 
        result = vf_search.PendingAncestryResult([b'tip', b'tag'], None)
200
 
        result = result.refine({b'tip'}, {b'mid'})
201
 
        self.assertEqual({b'mid', b'tag'}, result.heads)
202
 
        result = result.refine({b'mid', b'tag', b'base'},
203
 
                               {NULL_REVISION})
204
 
        self.assertEqual({NULL_REVISION}, result.heads)
 
189
            {"tip":["mid"], "mid":["base"], "tag":["base"],
 
190
             "base":[NULL_REVISION], NULL_REVISION:[]})
 
191
        result = vf_search.PendingAncestryResult(['tip', 'tag'], None)
 
192
        result = result.refine(set(['tip']), set(['mid']))
 
193
        self.assertEqual(set(['mid', 'tag']), result.heads)
 
194
        result = result.refine(set(['mid', 'tag', 'base']),
 
195
            set([NULL_REVISION]))
 
196
        self.assertEqual(set([NULL_REVISION]), result.heads)
205
197
        self.assertTrue(result.is_empty())
206
198
 
207
199
 
208
200
class TestPendingAncestryResultGetKeys(tests.TestCaseWithMemoryTransport):
209
 
    """Tests for breezy.graph.PendingAncestryResult."""
 
201
    """Tests for brzlib.graph.PendingAncestryResult."""
210
202
 
211
203
    def test_get_keys(self):
212
204
        builder = self.make_branch_builder('b')
213
205
        builder.start_series()
214
 
        builder.build_snapshot(None, [
215
 
            ('add', ('', b'root-id', 'directory', ''))],
216
 
            revision_id=b'rev-1')
217
 
        builder.build_snapshot([b'rev-1'], [], revision_id=b'rev-2')
 
206
        builder.build_snapshot('rev-1', None, [
 
207
            ('add', ('', 'root-id', 'directory', ''))])
 
208
        builder.build_snapshot('rev-2', ['rev-1'], [])
218
209
        builder.finish_series()
219
210
        repo = builder.get_branch().repository
220
211
        repo.lock_read()
221
212
        self.addCleanup(repo.unlock)
222
 
        result = vf_search.PendingAncestryResult([b'rev-2'], repo)
223
 
        self.assertEqual({b'rev-1', b'rev-2'}, set(result.get_keys()))
 
213
        result = vf_search.PendingAncestryResult(['rev-2'], repo)
 
214
        self.assertEqual(set(['rev-1', 'rev-2']), set(result.get_keys()))
224
215
 
225
216
    def test_get_keys_excludes_ghosts(self):
226
217
        builder = self.make_branch_builder('b')
227
218
        builder.start_series()
228
 
        builder.build_snapshot(None, [
229
 
            ('add', ('', b'root-id', 'directory', ''))],
230
 
            revision_id=b'rev-1')
231
 
        builder.build_snapshot([b'rev-1', b'ghost'], [], revision_id=b'rev-2')
 
219
        builder.build_snapshot('rev-1', None, [
 
220
            ('add', ('', 'root-id', 'directory', ''))])
 
221
        builder.build_snapshot('rev-2', ['rev-1', 'ghost'], [])
232
222
        builder.finish_series()
233
223
        repo = builder.get_branch().repository
234
224
        repo.lock_read()
235
225
        self.addCleanup(repo.unlock)
236
 
        result = vf_search.PendingAncestryResult([b'rev-2'], repo)
237
 
        self.assertEqual(sorted([b'rev-1', b'rev-2']),
238
 
                         sorted(result.get_keys()))
 
226
        result = vf_search.PendingAncestryResult(['rev-2'], repo)
 
227
        self.assertEqual(sorted(['rev-1', 'rev-2']), sorted(result.get_keys()))
239
228
 
240
229
    def test_get_keys_excludes_null(self):
241
230
        # Make a 'graph' with an iter_ancestry that returns NULL_REVISION
243
232
        # ancestries.
244
233
        class StubGraph(object):
245
234
            def iter_ancestry(self, keys):
246
 
                return [(NULL_REVISION, ()), (b'foo', (NULL_REVISION,))]
247
 
        result = vf_search.PendingAncestryResult([b'rev-3'], None)
 
235
                return [(NULL_REVISION, ()), ('foo', (NULL_REVISION,))]
 
236
        result = vf_search.PendingAncestryResult(['rev-3'], None)
248
237
        result_keys = result._get_keys(StubGraph())
249
238
        # Only the non-null keys from the ancestry appear.
250
 
        self.assertEqual({b'foo'}, set(result_keys))
 
239
        self.assertEqual(set(['foo']), set(result_keys))