/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
1
# Copyright (C) 2007-2011 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
17
from .. import (
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
18
    graph as _mod_graph,
19
    tests,
6670.4.1 by Jelmer Vernooij
Update imports.
20
    )
21
from ..bzr import (
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
22
    vf_search,
23
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
24
from ..revision import NULL_REVISION
25
from .test_graph import TestGraphBase
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
26
6341.1.4 by Jelmer Vernooij
Move more functionality to vf_search.
27
# Ancestry 1:
28
#
29
#  NULL_REVISION
30
#       |
31
#     rev1
32
#      /\
33
#  rev2a rev2b
34
#     |    |
35
#   rev3  /
36
#     |  /
37
#   rev4
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
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']}
6341.1.4 by Jelmer Vernooij
Move more functionality to vf_search.
43
44
# Ancestry 2:
45
#
46
#  NULL_REVISION
47
#    /    \
48
# rev1a  rev1b
49
#   |
50
# rev2a
51
#   |
52
# rev3a
53
#   |
54
# rev4a
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
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']}
6341.1.4 by Jelmer Vernooij
Move more functionality to vf_search.
60
61
62
# Extended history shortcut
63
#  NULL_REVISION
64
#       |
65
#       a
66
#       |\
67
#       b |
68
#       | |
69
#       c |
70
#       | |
71
#       d |
72
#       |\|
73
#       e f
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
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'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
80
                             }
6341.1.4 by Jelmer Vernooij
Move more functionality to vf_search.
81
82
83
class TestSearchResultRefine(tests.TestCase):
84
85
    def make_graph(self, ancestors):
86
        return _mod_graph.Graph(_mod_graph.DictParentsProvider(ancestors))
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
87
88
    def test_refine(self):
89
        # Used when pulling from a stacked repository, so test some revisions
90
        # being satisfied from the stacking branch.
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
91
        self.make_graph(
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
92
            {b"tip": [b"mid"], b"mid": [b"base"], b"tag": [b"base"],
7143.15.2 by Jelmer Vernooij
Run autopep8.
93
             b"base": [NULL_REVISION], NULL_REVISION: []})
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
94
        result = vf_search.SearchResult(
95
            {b'tip', b'tag'},
96
            {NULL_REVISION}, 4, {b'tip', b'mid', b'tag', b'base'})
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
97
        result = result.refine({b'tip'}, {b'mid'})
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
98
        recipe = result.get_recipe()
99
        # We should be starting from tag (original head) and mid (seen ref)
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
100
        self.assertEqual({b'mid', b'tag'}, recipe[1])
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
101
        # We should be stopping at NULL (original stop) and tip (seen head)
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
102
        self.assertEqual({NULL_REVISION, b'tip'}, recipe[2])
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
103
        self.assertEqual(3, recipe[3])
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
104
        result = result.refine({b'mid', b'tag', b'base'},
7143.15.2 by Jelmer Vernooij
Run autopep8.
105
                               {NULL_REVISION})
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
106
        recipe = result.get_recipe()
107
        # We should be starting from nothing (NULL was known as a cut point)
108
        self.assertEqual(set([]), recipe[1])
109
        # We should be stopping at NULL (original stop) and tip (seen head) and
110
        # tag (seen head) and mid(seen mid-point head). We could come back and
111
        # define this as not including mid, for minimal results, but it is
112
        # still 'correct' to include mid, and simpler/easier.
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
113
        self.assertEqual({NULL_REVISION, b'tip', b'tag', b'mid'}, recipe[2])
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
114
        self.assertEqual(0, recipe[3])
115
        self.assertTrue(result.is_empty())
116
117
118
class TestSearchResultFromParentMap(TestGraphBase):
119
120
    def assertSearchResult(self, start_keys, stop_keys, key_count, parent_map,
121
                           missing_keys=()):
6341.1.4 by Jelmer Vernooij
Move more functionality to vf_search.
122
        (start, stop, count) = vf_search.search_result_from_parent_map(
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
123
            parent_map, missing_keys)
124
        self.assertEqual((sorted(start_keys), sorted(stop_keys), key_count),
125
                         (sorted(start), sorted(stop), count))
126
127
    def test_no_parents(self):
128
        self.assertSearchResult([], [], 0, {})
129
        self.assertSearchResult([], [], 0, None)
130
131
    def test_ancestry_1(self):
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
132
        self.assertSearchResult([b'rev4'], [NULL_REVISION], len(ancestry_1),
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
133
                                ancestry_1)
134
135
    def test_ancestry_2(self):
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
136
        self.assertSearchResult([b'rev1b', b'rev4a'], [NULL_REVISION],
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
137
                                len(ancestry_2), ancestry_2)
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
138
        self.assertSearchResult([b'rev1b', b'rev4a'], [],
7143.15.2 by Jelmer Vernooij
Run autopep8.
139
                                len(ancestry_2) + 1, ancestry_2,
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
140
                                missing_keys=[NULL_REVISION])
141
142
    def test_partial_search(self):
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
143
        parent_map = dict((k, extended_history_shortcut[k])
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
144
                          for k in [b'e', b'f'])
145
        self.assertSearchResult([b'e', b'f'], [b'd', b'a'], 2,
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
146
                                parent_map)
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
147
        parent_map.update((k, extended_history_shortcut[k])
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
148
                          for k in [b'd', b'a'])
149
        self.assertSearchResult([b'e', b'f'], [b'c', NULL_REVISION], 4,
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
150
                                parent_map)
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
151
        parent_map[b'c'] = extended_history_shortcut[b'c']
152
        self.assertSearchResult([b'e', b'f'], [b'b'], 6,
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
153
                                parent_map, missing_keys=[NULL_REVISION])
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
154
        parent_map[b'b'] = extended_history_shortcut[b'b']
155
        self.assertSearchResult([b'e', b'f'], [], 7,
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
156
                                parent_map, missing_keys=[NULL_REVISION])
157
158
159
class TestLimitedSearchResultFromParentMap(TestGraphBase):
160
161
    def assertSearchResult(self, start_keys, stop_keys, key_count, parent_map,
162
                           missing_keys, tip_keys, depth):
6341.1.4 by Jelmer Vernooij
Move more functionality to vf_search.
163
        (start, stop, count) = vf_search.limited_search_result_from_parent_map(
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
164
            parent_map, missing_keys, tip_keys, depth)
165
        self.assertEqual((sorted(start_keys), sorted(stop_keys), key_count),
166
                         (sorted(start), sorted(stop), count))
167
168
    def test_empty_ancestry(self):
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
169
        self.assertSearchResult([], [], 0, {}, (), [b'tip-rev-id'], 10)
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
170
171
    def test_ancestry_1(self):
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
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)
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
176
177
    def test_multiple_heads(self):
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
178
        self.assertSearchResult([b'e', b'f'], [b'a'], 5,
179
                                extended_history_shortcut, (), [b'a'], 10)
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
180
        # Note that even though we only take 1 step back, we find 'f', which
181
        # means the described search will still find d and c.
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
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)
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
186
187
6341.1.4 by Jelmer Vernooij
Move more functionality to vf_search.
188
class TestPendingAncestryResultRefine(tests.TestCase):
189
190
    def make_graph(self, ancestors):
191
        return _mod_graph.Graph(_mod_graph.DictParentsProvider(ancestors))
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
192
193
    def test_refine(self):
194
        # Used when pulling from a stacked repository, so test some revisions
195
        # being satisfied from the stacking branch.
196
        g = self.make_graph(
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
197
            {b"tip": [b"mid"], b"mid": [b"base"], b"tag": [b"base"],
7143.15.2 by Jelmer Vernooij
Run autopep8.
198
             b"base": [NULL_REVISION], NULL_REVISION: []})
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
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'},
7143.15.2 by Jelmer Vernooij
Run autopep8.
203
                               {NULL_REVISION})
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
204
        self.assertEqual({NULL_REVISION}, result.heads)
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
205
        self.assertTrue(result.is_empty())
206
207
208
class TestPendingAncestryResultGetKeys(tests.TestCaseWithMemoryTransport):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
209
    """Tests for breezy.graph.PendingAncestryResult."""
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
210
211
    def test_get_keys(self):
212
        builder = self.make_branch_builder('b')
213
        builder.start_series()
6816.2.3 by Jelmer Vernooij
Port over last uses of build_snapshot.
214
        builder.build_snapshot(None, [
6973.13.2 by Jelmer Vernooij
Fix some more tests.
215
            ('add', ('', b'root-id', 'directory', ''))],
6973.5.2 by Jelmer Vernooij
Add more bees.
216
            revision_id=b'rev-1')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
217
        builder.build_snapshot([b'rev-1'], [], revision_id=b'rev-2')
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
218
        builder.finish_series()
219
        repo = builder.get_branch().repository
220
        repo.lock_read()
221
        self.addCleanup(repo.unlock)
6973.13.2 by Jelmer Vernooij
Fix some more tests.
222
        result = vf_search.PendingAncestryResult([b'rev-2'], repo)
223
        self.assertEqual({b'rev-1', b'rev-2'}, set(result.get_keys()))
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
224
225
    def test_get_keys_excludes_ghosts(self):
226
        builder = self.make_branch_builder('b')
227
        builder.start_series()
6816.2.3 by Jelmer Vernooij
Port over last uses of build_snapshot.
228
        builder.build_snapshot(None, [
6973.13.2 by Jelmer Vernooij
Fix some more tests.
229
            ('add', ('', b'root-id', 'directory', ''))],
6973.5.2 by Jelmer Vernooij
Add more bees.
230
            revision_id=b'rev-1')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
231
        builder.build_snapshot([b'rev-1', b'ghost'], [], revision_id=b'rev-2')
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
232
        builder.finish_series()
233
        repo = builder.get_branch().repository
234
        repo.lock_read()
235
        self.addCleanup(repo.unlock)
6973.13.2 by Jelmer Vernooij
Fix some more tests.
236
        result = vf_search.PendingAncestryResult([b'rev-2'], repo)
7143.15.2 by Jelmer Vernooij
Run autopep8.
237
        self.assertEqual(sorted([b'rev-1', b'rev-2']),
238
                         sorted(result.get_keys()))
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
239
240
    def test_get_keys_excludes_null(self):
241
        # Make a 'graph' with an iter_ancestry that returns NULL_REVISION
242
        # somewhere other than the last element, which can happen in real
243
        # ancestries.
244
        class StubGraph(object):
245
            def iter_ancestry(self, keys):
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
246
                return [(NULL_REVISION, ()), (b'foo', (NULL_REVISION,))]
247
        result = vf_search.PendingAncestryResult([b'rev-3'], None)
6341.1.3 by Jelmer Vernooij
Move search result code to vf_search module.
248
        result_keys = result._get_keys(StubGraph())
249
        # Only the non-null keys from the ancestry appear.
7058.4.1 by Jelmer Vernooij
Fix another 40 tests.
250
        self.assertEqual({b'foo'}, set(result_keys))