/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4103.3.4 by Martin Pool
Update test that depends on progress bar strings
1
# Copyright (C) 2005, 2009 Canonical Ltd
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
2
#
3
# Authors:
4
#   Johan Rydberg <jrydberg@gnu.org>
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
10
#
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
15
#
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
1704.2.15 by Martin Pool
Remove TODO about knit testing printed from test suite
21
# TODO: might be nice to create a versionedfile with some type of corruption
22
# considered typical and check that it can be detected/corrected.
23
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
24
from itertools import chain, izip
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
25
from StringIO import StringIO
26
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
27
from bzrlib import (
28
    errors,
3735.31.1 by John Arbash Meinel
Bring the groupcompress plugin into the brisbane-core branch.
29
    groupcompress,
30
    knit as _mod_knit,
2309.4.7 by John Arbash Meinel
Update VersionedFile tests to ensure that they can take Unicode,
31
    osutils,
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
32
    progress,
33
    )
1563.2.11 by Robert Collins
Consolidate reweave and join as we have no separate usage, make reweave tests apply to all versionedfile implementations and deprecate the old reweave apis.
34
from bzrlib.errors import (
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
35
                           RevisionNotPresent,
1563.2.11 by Robert Collins
Consolidate reweave and join as we have no separate usage, make reweave tests apply to all versionedfile implementations and deprecate the old reweave apis.
36
                           RevisionAlreadyPresent,
37
                           WeaveParentMismatch
38
                           )
2770.1.1 by Aaron Bentley
Initial implmentation of plain knit annotation
39
from bzrlib.knit import (
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
40
    cleanup_pack_knit,
41
    make_file_factory,
42
    make_pack_factory,
2770.1.1 by Aaron Bentley
Initial implmentation of plain knit annotation
43
    KnitAnnotateFactory,
2770.1.10 by Aaron Bentley
Merge bzr.dev
44
    KnitPlainFactory,
2770.1.1 by Aaron Bentley
Initial implmentation of plain knit annotation
45
    )
3350.3.14 by Robert Collins
Deprecate VersionedFile.join.
46
from bzrlib.symbol_versioning import one_four, one_five
3350.6.2 by Robert Collins
Prepare parameterised test environment.
47
from bzrlib.tests import (
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
48
    TestCase,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
49
    TestCaseWithMemoryTransport,
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
50
    TestNotApplicable,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
51
    TestSkipped,
52
    condition_isinstance,
53
    split_suite_by_condition,
4084.5.1 by Robert Collins
Bulk update all test adaptation into a single approach, using multiply_tests rather than test adapters.
54
    multiply_tests,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
55
    )
2929.3.8 by Vincent Ladeuil
Rename bzrlib/test/HTTPTestUtils.py to bzrlib/tests/http_utils.py and fix uses.
56
from bzrlib.tests.http_utils import TestCaseWithWebserver
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
57
from bzrlib.trace import mutter
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
58
from bzrlib.transport import get_transport
1563.2.13 by Robert Collins
InterVersionedFile implemented.
59
from bzrlib.transport.memory import MemoryTransport
1684.3.1 by Robert Collins
Fix versioned file joins with empty targets.
60
from bzrlib.tsort import topo_sort
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
61
from bzrlib.tuned_gzip import GzipFile
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
62
import bzrlib.versionedfile as versionedfile
3350.6.2 by Robert Collins
Prepare parameterised test environment.
63
from bzrlib.versionedfile import (
64
    ConstantMapper,
65
    HashEscapedPrefixMapper,
66
    PrefixMapper,
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
67
    VirtualVersionedFiles,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
68
    make_versioned_files_factory,
69
    )
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
70
from bzrlib.weave import WeaveFile
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
71
from bzrlib.weavefile import read_weave, write_weave
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
72
73
3350.6.2 by Robert Collins
Prepare parameterised test environment.
74
def load_tests(standard_tests, module, loader):
75
    """Parameterize VersionedFiles tests for different implementations."""
76
    to_adapt, result = split_suite_by_condition(
77
        standard_tests, condition_isinstance(TestVersionedFiles))
78
    # We want to be sure of behaviour for:
79
    # weaves prefix layout (weave texts)
80
    # individually named weaves (weave inventories)
81
    # annotated knits - prefix|hash|hash-escape layout, we test the third only
82
    #                   as it is the most complex mapper.
83
    # individually named knits
84
    # individual no-graph knits in packs (signatures)
85
    # individual graph knits in packs (inventories)
86
    # individual graph nocompression knits in packs (revisions)
87
    # plain text knits in packs (texts)
4084.5.1 by Robert Collins
Bulk update all test adaptation into a single approach, using multiply_tests rather than test adapters.
88
    len_one_scenarios = [
3350.6.2 by Robert Collins
Prepare parameterised test environment.
89
        ('weave-named', {
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
90
            'cleanup':None,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
91
            'factory':make_versioned_files_factory(WeaveFile,
92
                ConstantMapper('inventory')),
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
93
            'graph':True,
94
            'key_length':1,
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
95
            'support_partial_insertion': False,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
96
            }),
97
        ('named-knit', {
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
98
            'cleanup':None,
99
            'factory':make_file_factory(False, ConstantMapper('revisions')),
100
            'graph':True,
101
            'key_length':1,
4009.3.7 by Andrew Bennetts
Most tests passing.
102
            'support_partial_insertion': False,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
103
            }),
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
104
        ('named-nograph-nodelta-knit-pack', {
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
105
            'cleanup':cleanup_pack_knit,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
106
            'factory':make_pack_factory(False, False, 1),
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
107
            'graph':False,
108
            'key_length':1,
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
109
            'support_partial_insertion': False,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
110
            }),
111
        ('named-graph-knit-pack', {
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
112
            'cleanup':cleanup_pack_knit,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
113
            'factory':make_pack_factory(True, True, 1),
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
114
            'graph':True,
115
            'key_length':1,
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
116
            'support_partial_insertion': True,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
117
            }),
118
        ('named-graph-nodelta-knit-pack', {
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
119
            'cleanup':cleanup_pack_knit,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
120
            'factory':make_pack_factory(True, False, 1),
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
121
            'graph':True,
122
            'key_length':1,
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
123
            'support_partial_insertion': False,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
124
            }),
3735.31.1 by John Arbash Meinel
Bring the groupcompress plugin into the brisbane-core branch.
125
        ('groupcompress-nograph', {
126
            'cleanup':groupcompress.cleanup_pack_group,
127
            'factory':groupcompress.make_pack_factory(False, False, 1),
128
            'graph': False,
129
            'key_length':1,
130
            'support_partial_insertion':False,
131
            }),
3350.6.2 by Robert Collins
Prepare parameterised test environment.
132
        ]
4084.5.1 by Robert Collins
Bulk update all test adaptation into a single approach, using multiply_tests rather than test adapters.
133
    len_two_scenarios = [
3350.6.2 by Robert Collins
Prepare parameterised test environment.
134
        ('weave-prefix', {
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
135
            'cleanup':None,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
136
            'factory':make_versioned_files_factory(WeaveFile,
137
                PrefixMapper()),
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
138
            'graph':True,
139
            'key_length':2,
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
140
            'support_partial_insertion': False,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
141
            }),
142
        ('annotated-knit-escape', {
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
143
            'cleanup':None,
144
            'factory':make_file_factory(True, HashEscapedPrefixMapper()),
145
            'graph':True,
146
            'key_length':2,
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
147
            'support_partial_insertion': False,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
148
            }),
149
        ('plain-knit-pack', {
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
150
            'cleanup':cleanup_pack_knit,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
151
            'factory':make_pack_factory(True, True, 2),
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
152
            'graph':True,
153
            'key_length':2,
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
154
            'support_partial_insertion': True,
3350.6.2 by Robert Collins
Prepare parameterised test environment.
155
            }),
3735.31.10 by John Arbash Meinel
Add groupcompress with multiple parents.
156
        ('groupcompress', {
157
            'cleanup':groupcompress.cleanup_pack_group,
158
            'factory':groupcompress.make_pack_factory(True, False, 1),
159
            'graph': True,
160
            'key_length':1,
161
            'support_partial_insertion':False,
162
            }),
3350.6.2 by Robert Collins
Prepare parameterised test environment.
163
        ]
4084.5.1 by Robert Collins
Bulk update all test adaptation into a single approach, using multiply_tests rather than test adapters.
164
    scenarios = len_one_scenarios + len_two_scenarios
165
    return multiply_tests(to_adapt, scenarios, result)
3350.6.2 by Robert Collins
Prepare parameterised test environment.
166
167
3350.3.11 by Robert Collins
Test inserting a stream that overlaps the current content of a knit does not error.
168
def get_diamond_vf(f, trailing_eol=True, left_only=False):
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
169
    """Get a diamond graph to exercise deltas and merges.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
170
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
171
    :param trailing_eol: If True end the last line with \n.
172
    """
173
    parents = {
174
        'origin': (),
175
        'base': (('origin',),),
176
        'left': (('base',),),
177
        'right': (('base',),),
178
        'merged': (('left',), ('right',)),
179
        }
180
    # insert a diamond graph to exercise deltas and merges.
181
    if trailing_eol:
182
        last_char = '\n'
183
    else:
184
        last_char = ''
185
    f.add_lines('origin', [], ['origin' + last_char])
186
    f.add_lines('base', ['origin'], ['base' + last_char])
187
    f.add_lines('left', ['base'], ['base\n', 'left' + last_char])
3350.3.11 by Robert Collins
Test inserting a stream that overlaps the current content of a knit does not error.
188
    if not left_only:
189
        f.add_lines('right', ['base'],
190
            ['base\n', 'right' + last_char])
191
        f.add_lines('merged', ['left', 'right'],
192
            ['base\n', 'left\n', 'right\n', 'merged' + last_char])
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
193
    return f, parents
194
195
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
196
def get_diamond_files(files, key_length, trailing_eol=True, left_only=False,
3735.2.5 by Robert Collins
Teach VersionedFiles how to allocate keys based on content hashes.
197
    nograph=False, nokeys=False):
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
198
    """Get a diamond graph to exercise deltas and merges.
199
200
    This creates a 5-node graph in files. If files supports 2-length keys two
201
    graphs are made to exercise the support for multiple ids.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
202
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
203
    :param trailing_eol: If True end the last line with \n.
204
    :param key_length: The length of keys in files. Currently supports length 1
205
        and 2 keys.
206
    :param left_only: If True do not add the right and merged nodes.
207
    :param nograph: If True, do not provide parents to the add_lines calls;
208
        this is useful for tests that need inserted data but have graphless
209
        stores.
3735.2.5 by Robert Collins
Teach VersionedFiles how to allocate keys based on content hashes.
210
    :param nokeys: If True, pass None is as the key for all insertions.
211
        Currently implies nograph.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
212
    :return: The results of the add_lines calls.
213
    """
3735.2.5 by Robert Collins
Teach VersionedFiles how to allocate keys based on content hashes.
214
    if nokeys:
215
        nograph = True
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
216
    if key_length == 1:
217
        prefixes = [()]
218
    else:
219
        prefixes = [('FileA',), ('FileB',)]
220
    # insert a diamond graph to exercise deltas and merges.
221
    if trailing_eol:
222
        last_char = '\n'
223
    else:
224
        last_char = ''
225
    result = []
226
    def get_parents(suffix_list):
227
        if nograph:
228
            return ()
229
        else:
230
            result = [prefix + suffix for suffix in suffix_list]
231
            return result
3735.2.5 by Robert Collins
Teach VersionedFiles how to allocate keys based on content hashes.
232
    def get_key(suffix):
233
        if nokeys:
234
            return (None, )
235
        else:
236
            return (suffix,)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
237
    # we loop over each key because that spreads the inserts across prefixes,
238
    # which is how commit operates.
239
    for prefix in prefixes:
3735.2.5 by Robert Collins
Teach VersionedFiles how to allocate keys based on content hashes.
240
        result.append(files.add_lines(prefix + get_key('origin'), (),
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
241
            ['origin' + last_char]))
242
    for prefix in prefixes:
3735.2.5 by Robert Collins
Teach VersionedFiles how to allocate keys based on content hashes.
243
        result.append(files.add_lines(prefix + get_key('base'),
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
244
            get_parents([('origin',)]), ['base' + last_char]))
245
    for prefix in prefixes:
3735.2.5 by Robert Collins
Teach VersionedFiles how to allocate keys based on content hashes.
246
        result.append(files.add_lines(prefix + get_key('left'),
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
247
            get_parents([('base',)]),
248
            ['base\n', 'left' + last_char]))
249
    if not left_only:
250
        for prefix in prefixes:
3735.2.5 by Robert Collins
Teach VersionedFiles how to allocate keys based on content hashes.
251
            result.append(files.add_lines(prefix + get_key('right'),
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
252
                get_parents([('base',)]),
253
                ['base\n', 'right' + last_char]))
254
        for prefix in prefixes:
3735.2.5 by Robert Collins
Teach VersionedFiles how to allocate keys based on content hashes.
255
            result.append(files.add_lines(prefix + get_key('merged'),
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
256
                get_parents([('left',), ('right',)]),
257
                ['base\n', 'left\n', 'right\n', 'merged' + last_char]))
258
    return result
259
260
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
261
class VersionedFileTestMixIn(object):
262
    """A mixin test class for testing VersionedFiles.
263
264
    This is not an adaptor-style test at this point because
265
    theres no dynamic substitution of versioned file implementations,
266
    they are strictly controlled by their owning repositories.
267
    """
268
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
269
    def get_transaction(self):
270
        if not hasattr(self, '_transaction'):
271
            self._transaction = None
272
        return self._transaction
273
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
274
    def test_add(self):
275
        f = self.get_file()
276
        f.add_lines('r0', [], ['a\n', 'b\n'])
277
        f.add_lines('r1', ['r0'], ['b\n', 'c\n'])
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
278
        def verify_file(f):
279
            versions = f.versions()
280
            self.assertTrue('r0' in versions)
281
            self.assertTrue('r1' in versions)
282
            self.assertEquals(f.get_lines('r0'), ['a\n', 'b\n'])
283
            self.assertEquals(f.get_text('r0'), 'a\nb\n')
284
            self.assertEquals(f.get_lines('r1'), ['b\n', 'c\n'])
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
285
            self.assertEqual(2, len(f))
286
            self.assertEqual(2, f.num_versions())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
287
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
288
            self.assertRaises(RevisionNotPresent,
289
                f.add_lines, 'r2', ['foo'], [])
290
            self.assertRaises(RevisionAlreadyPresent,
291
                f.add_lines, 'r1', [], [])
292
        verify_file(f)
1666.1.6 by Robert Collins
Make knit the default format.
293
        # this checks that reopen with create=True does not break anything.
294
        f = self.reopen_file(create=True)
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
295
        verify_file(f)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
296
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
297
    def test_adds_with_parent_texts(self):
298
        f = self.get_file()
299
        parent_texts = {}
2776.1.1 by Robert Collins
* The ``add_lines`` methods on ``VersionedFile`` implementations has changed
300
        _, _, parent_texts['r0'] = f.add_lines('r0', [], ['a\n', 'b\n'])
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
301
        try:
2776.1.1 by Robert Collins
* The ``add_lines`` methods on ``VersionedFile`` implementations has changed
302
            _, _, parent_texts['r1'] = f.add_lines_with_ghosts('r1',
303
                ['r0', 'ghost'], ['b\n', 'c\n'], parent_texts=parent_texts)
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
304
        except NotImplementedError:
305
            # if the format doesn't support ghosts, just add normally.
2776.1.1 by Robert Collins
* The ``add_lines`` methods on ``VersionedFile`` implementations has changed
306
            _, _, parent_texts['r1'] = f.add_lines('r1',
307
                ['r0'], ['b\n', 'c\n'], parent_texts=parent_texts)
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
308
        f.add_lines('r2', ['r1'], ['c\n', 'd\n'], parent_texts=parent_texts)
309
        self.assertNotEqual(None, parent_texts['r0'])
310
        self.assertNotEqual(None, parent_texts['r1'])
311
        def verify_file(f):
312
            versions = f.versions()
313
            self.assertTrue('r0' in versions)
314
            self.assertTrue('r1' in versions)
315
            self.assertTrue('r2' in versions)
316
            self.assertEquals(f.get_lines('r0'), ['a\n', 'b\n'])
317
            self.assertEquals(f.get_lines('r1'), ['b\n', 'c\n'])
318
            self.assertEquals(f.get_lines('r2'), ['c\n', 'd\n'])
319
            self.assertEqual(3, f.num_versions())
320
            origins = f.annotate('r1')
321
            self.assertEquals(origins[0][0], 'r0')
322
            self.assertEquals(origins[1][0], 'r1')
323
            origins = f.annotate('r2')
324
            self.assertEquals(origins[0][0], 'r1')
325
            self.assertEquals(origins[1][0], 'r2')
326
327
        verify_file(f)
328
        f = self.reopen_file()
329
        verify_file(f)
330
2805.6.7 by Robert Collins
Review feedback.
331
    def test_add_unicode_content(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
332
        # unicode content is not permitted in versioned files.
2805.6.7 by Robert Collins
Review feedback.
333
        # versioned files version sequences of bytes only.
334
        vf = self.get_file()
335
        self.assertRaises(errors.BzrBadParameterUnicode,
336
            vf.add_lines, 'a', [], ['a\n', u'b\n', 'c\n'])
337
        self.assertRaises(
338
            (errors.BzrBadParameterUnicode, NotImplementedError),
339
            vf.add_lines_with_ghosts, 'a', [], ['a\n', u'b\n', 'c\n'])
340
2520.4.150 by Aaron Bentley
Test that non-Weave uses left_matching_blocks for add_lines
341
    def test_add_follows_left_matching_blocks(self):
342
        """If we change left_matching_blocks, delta changes
343
344
        Note: There are multiple correct deltas in this case, because
345
        we start with 1 "a" and we get 3.
346
        """
347
        vf = self.get_file()
348
        if isinstance(vf, WeaveFile):
349
            raise TestSkipped("WeaveFile ignores left_matching_blocks")
350
        vf.add_lines('1', [], ['a\n'])
351
        vf.add_lines('2', ['1'], ['a\n', 'a\n', 'a\n'],
352
                     left_matching_blocks=[(0, 0, 1), (1, 3, 0)])
2794.1.2 by Robert Collins
Nuke versioned file add/get delta support, allowing easy simplification of unannotated Content, reducing memory copies and friction during commit on unannotated texts.
353
        self.assertEqual(['a\n', 'a\n', 'a\n'], vf.get_lines('2'))
2520.4.150 by Aaron Bentley
Test that non-Weave uses left_matching_blocks for add_lines
354
        vf.add_lines('3', ['1'], ['a\n', 'a\n', 'a\n'],
355
                     left_matching_blocks=[(0, 2, 1), (1, 3, 0)])
2794.1.2 by Robert Collins
Nuke versioned file add/get delta support, allowing easy simplification of unannotated Content, reducing memory copies and friction during commit on unannotated texts.
356
        self.assertEqual(['a\n', 'a\n', 'a\n'], vf.get_lines('3'))
2520.4.150 by Aaron Bentley
Test that non-Weave uses left_matching_blocks for add_lines
357
2805.6.7 by Robert Collins
Review feedback.
358
    def test_inline_newline_throws(self):
359
        # \r characters are not permitted in lines being added
360
        vf = self.get_file()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
361
        self.assertRaises(errors.BzrBadParameterContainsNewline,
2805.6.7 by Robert Collins
Review feedback.
362
            vf.add_lines, 'a', [], ['a\n\n'])
363
        self.assertRaises(
364
            (errors.BzrBadParameterContainsNewline, NotImplementedError),
365
            vf.add_lines_with_ghosts, 'a', [], ['a\n\n'])
366
        # but inline CR's are allowed
367
        vf.add_lines('a', [], ['a\r\n'])
368
        try:
369
            vf.add_lines_with_ghosts('b', [], ['a\r\n'])
370
        except NotImplementedError:
371
            pass
372
2229.2.1 by Aaron Bentley
Reject reserved ids in versiondfile, tree, branch and repository
373
    def test_add_reserved(self):
374
        vf = self.get_file()
375
        self.assertRaises(errors.ReservedId,
376
            vf.add_lines, 'a:', [], ['a\n', 'b\n', 'c\n'])
377
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
378
    def test_add_lines_nostoresha(self):
379
        """When nostore_sha is supplied using old content raises."""
380
        vf = self.get_file()
381
        empty_text = ('a', [])
382
        sample_text_nl = ('b', ["foo\n", "bar\n"])
383
        sample_text_no_nl = ('c', ["foo\n", "bar"])
384
        shas = []
385
        for version, lines in (empty_text, sample_text_nl, sample_text_no_nl):
386
            sha, _, _ = vf.add_lines(version, [], lines)
387
            shas.append(sha)
388
        # we now have a copy of all the lines in the vf.
389
        for sha, (version, lines) in zip(
390
            shas, (empty_text, sample_text_nl, sample_text_no_nl)):
391
            self.assertRaises(errors.ExistingContent,
392
                vf.add_lines, version + "2", [], lines,
393
                nostore_sha=sha)
394
            # and no new version should have been added.
395
            self.assertRaises(errors.RevisionNotPresent, vf.get_lines,
396
                version + "2")
397
2803.1.1 by Robert Collins
Fix typo in ghosts version of test_add_lines_nostoresha.
398
    def test_add_lines_with_ghosts_nostoresha(self):
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
399
        """When nostore_sha is supplied using old content raises."""
400
        vf = self.get_file()
401
        empty_text = ('a', [])
402
        sample_text_nl = ('b', ["foo\n", "bar\n"])
403
        sample_text_no_nl = ('c', ["foo\n", "bar"])
404
        shas = []
405
        for version, lines in (empty_text, sample_text_nl, sample_text_no_nl):
406
            sha, _, _ = vf.add_lines(version, [], lines)
407
            shas.append(sha)
408
        # we now have a copy of all the lines in the vf.
409
        # is the test applicable to this vf implementation?
410
        try:
411
            vf.add_lines_with_ghosts('d', [], [])
412
        except NotImplementedError:
413
            raise TestSkipped("add_lines_with_ghosts is optional")
414
        for sha, (version, lines) in zip(
415
            shas, (empty_text, sample_text_nl, sample_text_no_nl)):
416
            self.assertRaises(errors.ExistingContent,
417
                vf.add_lines_with_ghosts, version + "2", [], lines,
418
                nostore_sha=sha)
419
            # and no new version should have been added.
420
            self.assertRaises(errors.RevisionNotPresent, vf.get_lines,
421
                version + "2")
422
2776.1.1 by Robert Collins
* The ``add_lines`` methods on ``VersionedFile`` implementations has changed
423
    def test_add_lines_return_value(self):
424
        # add_lines should return the sha1 and the text size.
425
        vf = self.get_file()
426
        empty_text = ('a', [])
427
        sample_text_nl = ('b', ["foo\n", "bar\n"])
428
        sample_text_no_nl = ('c', ["foo\n", "bar"])
429
        # check results for the three cases:
430
        for version, lines in (empty_text, sample_text_nl, sample_text_no_nl):
431
            # the first two elements are the same for all versioned files:
432
            # - the digest and the size of the text. For some versioned files
433
            #   additional data is returned in additional tuple elements.
434
            result = vf.add_lines(version, [], lines)
435
            self.assertEqual(3, len(result))
436
            self.assertEqual((osutils.sha_strings(lines), sum(map(len, lines))),
437
                result[0:2])
438
        # parents should not affect the result:
439
        lines = sample_text_nl[1]
440
        self.assertEqual((osutils.sha_strings(lines), sum(map(len, lines))),
441
            vf.add_lines('d', ['b', 'c'], lines)[0:2])
442
2229.2.1 by Aaron Bentley
Reject reserved ids in versiondfile, tree, branch and repository
443
    def test_get_reserved(self):
444
        vf = self.get_file()
445
        self.assertRaises(errors.ReservedId, vf.get_texts, ['b:'])
446
        self.assertRaises(errors.ReservedId, vf.get_lines, 'b:')
447
        self.assertRaises(errors.ReservedId, vf.get_text, 'b:')
448
3468.2.4 by Martin Pool
Test and fix #234748 problems in trailing newline diffs
449
    def test_add_unchanged_last_line_noeol_snapshot(self):
450
        """Add a text with an unchanged last line with no eol should work."""
451
        # Test adding this in a number of chain lengths; because the interface
452
        # for VersionedFile does not allow forcing a specific chain length, we
453
        # just use a small base to get the first snapshot, then a much longer
454
        # first line for the next add (which will make the third add snapshot)
455
        # and so on. 20 has been chosen as an aribtrary figure - knits use 200
456
        # as a capped delta length, but ideally we would have some way of
457
        # tuning the test to the store (e.g. keep going until a snapshot
458
        # happens).
459
        for length in range(20):
460
            version_lines = {}
461
            vf = self.get_file('case-%d' % length)
462
            prefix = 'step-%d'
463
            parents = []
464
            for step in range(length):
465
                version = prefix % step
466
                lines = (['prelude \n'] * step) + ['line']
467
                vf.add_lines(version, parents, lines)
468
                version_lines[version] = lines
469
                parents = [version]
470
            vf.add_lines('no-eol', parents, ['line'])
471
            vf.get_texts(version_lines.keys())
472
            self.assertEqualDiff('line', vf.get_text('no-eol'))
473
474
    def test_get_texts_eol_variation(self):
475
        # similar to the failure in <http://bugs.launchpad.net/234748>
476
        vf = self.get_file()
477
        sample_text_nl = ["line\n"]
478
        sample_text_no_nl = ["line"]
479
        versions = []
480
        version_lines = {}
481
        parents = []
482
        for i in range(4):
483
            version = 'v%d' % i
484
            if i % 2:
485
                lines = sample_text_nl
486
            else:
487
                lines = sample_text_no_nl
488
            # left_matching blocks is an internal api; it operates on the
489
            # *internal* representation for a knit, which is with *all* lines
490
            # being normalised to end with \n - even the final line in a no_nl
491
            # file. Using it here ensures that a broken internal implementation
492
            # (which is what this test tests) will generate a correct line
493
            # delta (which is to say, an empty delta).
494
            vf.add_lines(version, parents, lines,
495
                left_matching_blocks=[(0, 0, 1)])
496
            parents = [version]
497
            versions.append(version)
498
            version_lines[version] = lines
499
        vf.check()
500
        vf.get_texts(versions)
501
        vf.get_texts(reversed(versions))
502
3460.2.1 by Robert Collins
* Inserting a bundle which changes the contents of a file with no trailing
503
    def test_add_lines_with_matching_blocks_noeol_last_line(self):
504
        """Add a text with an unchanged last line with no eol should work."""
505
        from bzrlib import multiparent
506
        # Hand verified sha1 of the text we're adding.
507
        sha1 = '6a1d115ec7b60afb664dc14890b5af5ce3c827a4'
508
        # Create a mpdiff which adds a new line before the trailing line, and
509
        # reuse the last line unaltered (which can cause annotation reuse).
510
        # Test adding this in two situations:
511
        # On top of a new insertion
512
        vf = self.get_file('fulltext')
513
        vf.add_lines('noeol', [], ['line'])
514
        vf.add_lines('noeol2', ['noeol'], ['newline\n', 'line'],
515
            left_matching_blocks=[(0, 1, 1)])
516
        self.assertEqualDiff('newline\nline', vf.get_text('noeol2'))
517
        # On top of a delta
518
        vf = self.get_file('delta')
519
        vf.add_lines('base', [], ['line'])
520
        vf.add_lines('noeol', ['base'], ['prelude\n', 'line'])
521
        vf.add_lines('noeol2', ['noeol'], ['newline\n', 'line'],
522
            left_matching_blocks=[(1, 1, 1)])
523
        self.assertEqualDiff('newline\nline', vf.get_text('noeol2'))
524
2520.4.85 by Aaron Bentley
Get all test passing (which just proves there aren't enough tests!)
525
    def test_make_mpdiffs(self):
2520.4.3 by Aaron Bentley
Implement plain strategy for extracting and installing multiparent diffs
526
        from bzrlib import multiparent
527
        vf = self.get_file('foo')
528
        sha1s = self._setup_for_deltas(vf)
529
        new_vf = self.get_file('bar')
530
        for version in multiparent.topo_iter(vf):
2520.4.85 by Aaron Bentley
Get all test passing (which just proves there aren't enough tests!)
531
            mpdiff = vf.make_mpdiffs([version])[0]
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
532
            new_vf.add_mpdiffs([(version, vf.get_parent_map([version])[version],
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
533
                                 vf.get_sha1s([version])[version], mpdiff)])
2520.4.3 by Aaron Bentley
Implement plain strategy for extracting and installing multiparent diffs
534
            self.assertEqualDiff(vf.get_text(version),
535
                                 new_vf.get_text(version))
536
3453.3.2 by John Arbash Meinel
Add a test case for the first loop, unable to find a way to trigger the second loop
537
    def test_make_mpdiffs_with_ghosts(self):
538
        vf = self.get_file('foo')
3453.3.4 by John Arbash Meinel
Skip the new test for old weave formats that don't support ghosts
539
        try:
540
            vf.add_lines_with_ghosts('text', ['ghost'], ['line\n'])
541
        except NotImplementedError:
542
            # old Weave formats do not allow ghosts
543
            return
3453.3.2 by John Arbash Meinel
Add a test case for the first loop, unable to find a way to trigger the second loop
544
        self.assertRaises(errors.RevisionNotPresent, vf.make_mpdiffs, ['ghost'])
545
1596.2.38 by Robert Collins
rollback from using deltas to using fulltexts - deltas need more work to be ready.
546
    def _setup_for_deltas(self, f):
2794.1.2 by Robert Collins
Nuke versioned file add/get delta support, allowing easy simplification of unannotated Content, reducing memory copies and friction during commit on unannotated texts.
547
        self.assertFalse(f.has_version('base'))
1596.2.36 by Robert Collins
add a get_delta api to versioned_file.
548
        # add texts that should trip the knit maximum delta chain threshold
549
        # as well as doing parallel chains of data in knits.
550
        # this is done by two chains of 25 insertions
551
        f.add_lines('base', [], ['line\n'])
1596.2.38 by Robert Collins
rollback from using deltas to using fulltexts - deltas need more work to be ready.
552
        f.add_lines('noeol', ['base'], ['line'])
553
        # detailed eol tests:
554
        # shared last line with parent no-eol
555
        f.add_lines('noeolsecond', ['noeol'], ['line\n', 'line'])
556
        # differing last line with parent, both no-eol
557
        f.add_lines('noeolnotshared', ['noeolsecond'], ['line\n', 'phone'])
558
        # add eol following a noneol parent, change content
559
        f.add_lines('eol', ['noeol'], ['phone\n'])
560
        # add eol following a noneol parent, no change content
561
        f.add_lines('eolline', ['noeol'], ['line\n'])
562
        # noeol with no parents:
563
        f.add_lines('noeolbase', [], ['line'])
564
        # noeol preceeding its leftmost parent in the output:
565
        # this is done by making it a merge of two parents with no common
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
566
        # anestry: noeolbase and noeol with the
1596.2.38 by Robert Collins
rollback from using deltas to using fulltexts - deltas need more work to be ready.
567
        # later-inserted parent the leftmost.
568
        f.add_lines('eolbeforefirstparent', ['noeolbase', 'noeol'], ['line'])
569
        # two identical eol texts
570
        f.add_lines('noeoldup', ['noeol'], ['line'])
1596.2.36 by Robert Collins
add a get_delta api to versioned_file.
571
        next_parent = 'base'
572
        text_name = 'chain1-'
573
        text = ['line\n']
574
        sha1s = {0 :'da6d3141cb4a5e6f464bf6e0518042ddc7bfd079',
575
                 1 :'45e21ea146a81ea44a821737acdb4f9791c8abe7',
576
                 2 :'e1f11570edf3e2a070052366c582837a4fe4e9fa',
577
                 3 :'26b4b8626da827088c514b8f9bbe4ebf181edda1',
578
                 4 :'e28a5510be25ba84d31121cff00956f9970ae6f6',
579
                 5 :'d63ec0ce22e11dcf65a931b69255d3ac747a318d',
580
                 6 :'2c2888d288cb5e1d98009d822fedfe6019c6a4ea',
581
                 7 :'95c14da9cafbf828e3e74a6f016d87926ba234ab',
582
                 8 :'779e9a0b28f9f832528d4b21e17e168c67697272',
583
                 9 :'1f8ff4e5c6ff78ac106fcfe6b1e8cb8740ff9a8f',
584
                 10:'131a2ae712cf51ed62f143e3fbac3d4206c25a05',
585
                 11:'c5a9d6f520d2515e1ec401a8f8a67e6c3c89f199',
586
                 12:'31a2286267f24d8bedaa43355f8ad7129509ea85',
587
                 13:'dc2a7fe80e8ec5cae920973973a8ee28b2da5e0a',
588
                 14:'2c4b1736566b8ca6051e668de68650686a3922f2',
589
                 15:'5912e4ecd9b0c07be4d013e7e2bdcf9323276cde',
590
                 16:'b0d2e18d3559a00580f6b49804c23fea500feab3',
591
                 17:'8e1d43ad72f7562d7cb8f57ee584e20eb1a69fc7',
592
                 18:'5cf64a3459ae28efa60239e44b20312d25b253f3',
593
                 19:'1ebed371807ba5935958ad0884595126e8c4e823',
594
                 20:'2aa62a8b06fb3b3b892a3292a068ade69d5ee0d3',
595
                 21:'01edc447978004f6e4e962b417a4ae1955b6fe5d',
596
                 22:'d8d8dc49c4bf0bab401e0298bb5ad827768618bb',
597
                 23:'c21f62b1c482862983a8ffb2b0c64b3451876e3f',
598
                 24:'c0593fe795e00dff6b3c0fe857a074364d5f04fc',
599
                 25:'dd1a1cf2ba9cc225c3aff729953e6364bf1d1855',
600
                 }
601
        for depth in range(26):
602
            new_version = text_name + '%s' % depth
603
            text = text + ['line\n']
604
            f.add_lines(new_version, [next_parent], text)
605
            next_parent = new_version
606
        next_parent = 'base'
607
        text_name = 'chain2-'
608
        text = ['line\n']
609
        for depth in range(26):
610
            new_version = text_name + '%s' % depth
611
            text = text + ['line\n']
612
            f.add_lines(new_version, [next_parent], text)
613
            next_parent = new_version
1596.2.38 by Robert Collins
rollback from using deltas to using fulltexts - deltas need more work to be ready.
614
        return sha1s
1596.2.37 by Robert Collins
Switch to delta based content copying in the generic versioned file copier.
615
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
616
    def test_ancestry(self):
617
        f = self.get_file()
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
618
        self.assertEqual([], f.get_ancestry([]))
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
619
        f.add_lines('r0', [], ['a\n', 'b\n'])
620
        f.add_lines('r1', ['r0'], ['b\n', 'c\n'])
621
        f.add_lines('r2', ['r0'], ['b\n', 'c\n'])
622
        f.add_lines('r3', ['r2'], ['b\n', 'c\n'])
623
        f.add_lines('rM', ['r1', 'r2'], ['b\n', 'c\n'])
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
624
        self.assertEqual([], f.get_ancestry([]))
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
625
        versions = f.get_ancestry(['rM'])
626
        # there are some possibilities:
627
        # r0 r1 r2 rM r3
628
        # r0 r1 r2 r3 rM
629
        # etc
630
        # so we check indexes
631
        r0 = versions.index('r0')
632
        r1 = versions.index('r1')
633
        r2 = versions.index('r2')
634
        self.assertFalse('r3' in versions)
635
        rM = versions.index('rM')
636
        self.assertTrue(r0 < r1)
637
        self.assertTrue(r0 < r2)
638
        self.assertTrue(r1 < rM)
639
        self.assertTrue(r2 < rM)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
640
641
        self.assertRaises(RevisionNotPresent,
642
            f.get_ancestry, ['rM', 'rX'])
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
643
2530.1.1 by Aaron Bentley
Make topological sorting optional for get_ancestry
644
        self.assertEqual(set(f.get_ancestry('rM')),
645
            set(f.get_ancestry('rM', topo_sorted=False)))
646
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
647
    def test_mutate_after_finish(self):
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
648
        self._transaction = 'before'
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
649
        f = self.get_file()
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
650
        self._transaction = 'after'
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
651
        self.assertRaises(errors.OutSideTransaction, f.add_lines, '', [], [])
652
        self.assertRaises(errors.OutSideTransaction, f.add_lines_with_ghosts, '', [], [])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
653
1563.2.15 by Robert Collins
remove the weavestore assumptions about the number and nature of files it manages.
654
    def test_copy_to(self):
655
        f = self.get_file()
656
        f.add_lines('0', [], ['a\n'])
657
        t = MemoryTransport()
658
        f.copy_to('foo', t)
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
659
        for suffix in self.get_factory().get_suffixes():
1563.2.15 by Robert Collins
remove the weavestore assumptions about the number and nature of files it manages.
660
            self.assertTrue(t.has('foo' + suffix))
661
662
    def test_get_suffixes(self):
663
        f = self.get_file()
664
        # and should be a list
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
665
        self.assertTrue(isinstance(self.get_factory().get_suffixes(), list))
1563.2.15 by Robert Collins
remove the weavestore assumptions about the number and nature of files it manages.
666
3287.5.1 by Robert Collins
Add VersionedFile.get_parent_map.
667
    def test_get_parent_map(self):
668
        f = self.get_file()
669
        f.add_lines('r0', [], ['a\n', 'b\n'])
670
        self.assertEqual(
671
            {'r0':()}, f.get_parent_map(['r0']))
672
        f.add_lines('r1', ['r0'], ['a\n', 'b\n'])
673
        self.assertEqual(
674
            {'r1':('r0',)}, f.get_parent_map(['r1']))
675
        self.assertEqual(
676
            {'r0':(),
677
             'r1':('r0',)},
678
            f.get_parent_map(['r0', 'r1']))
679
        f.add_lines('r2', [], ['a\n', 'b\n'])
680
        f.add_lines('r3', [], ['a\n', 'b\n'])
681
        f.add_lines('m', ['r0', 'r1', 'r2', 'r3'], ['a\n', 'b\n'])
682
        self.assertEqual(
683
            {'m':('r0', 'r1', 'r2', 'r3')}, f.get_parent_map(['m']))
684
        self.assertEqual({}, f.get_parent_map('y'))
685
        self.assertEqual(
686
            {'r0':(),
687
             'r1':('r0',)},
688
            f.get_parent_map(['r0', 'y', 'r1']))
689
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
690
    def test_annotate(self):
691
        f = self.get_file()
692
        f.add_lines('r0', [], ['a\n', 'b\n'])
693
        f.add_lines('r1', ['r0'], ['c\n', 'b\n'])
694
        origins = f.annotate('r1')
695
        self.assertEquals(origins[0][0], 'r1')
696
        self.assertEquals(origins[1][0], 'r0')
697
698
        self.assertRaises(RevisionNotPresent,
699
            f.annotate, 'foo')
700
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
701
    def test_detection(self):
702
        # Test weaves detect corruption.
703
        #
704
        # Weaves contain a checksum of their texts.
705
        # When a text is extracted, this checksum should be
706
        # verified.
707
708
        w = self.get_file_corrupted_text()
709
710
        self.assertEqual('hello\n', w.get_text('v1'))
711
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
712
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
713
        self.assertRaises(errors.WeaveInvalidChecksum, w.check)
714
715
        w = self.get_file_corrupted_checksum()
716
717
        self.assertEqual('hello\n', w.get_text('v1'))
718
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
719
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
720
        self.assertRaises(errors.WeaveInvalidChecksum, w.check)
721
722
    def get_file_corrupted_text(self):
723
        """Return a versioned file with corrupt text but valid metadata."""
724
        raise NotImplementedError(self.get_file_corrupted_text)
725
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
726
    def reopen_file(self, name='foo'):
727
        """Open the versioned file from disk again."""
728
        raise NotImplementedError(self.reopen_file)
729
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
730
    def test_iter_lines_added_or_present_in_versions(self):
731
        # test that we get at least an equalset of the lines added by
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
732
        # versions in the weave
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
733
        # the ordering here is to make a tree so that dumb searches have
734
        # more changes to muck up.
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
735
736
        class InstrumentedProgress(progress.DummyProgress):
737
738
            def __init__(self):
739
740
                progress.DummyProgress.__init__(self)
741
                self.updates = []
742
743
            def update(self, msg=None, current=None, total=None):
744
                self.updates.append((msg, current, total))
745
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
746
        vf = self.get_file()
747
        # add a base to get included
748
        vf.add_lines('base', [], ['base\n'])
749
        # add a ancestor to be included on one side
750
        vf.add_lines('lancestor', [], ['lancestor\n'])
751
        # add a ancestor to be included on the other side
752
        vf.add_lines('rancestor', ['base'], ['rancestor\n'])
753
        # add a child of rancestor with no eofile-nl
754
        vf.add_lines('child', ['rancestor'], ['base\n', 'child\n'])
755
        # add a child of lancestor and base to join the two roots
756
        vf.add_lines('otherchild',
757
                     ['lancestor', 'base'],
758
                     ['base\n', 'lancestor\n', 'otherchild\n'])
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
759
        def iter_with_versions(versions, expected):
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
760
            # now we need to see what lines are returned, and how often.
2975.3.1 by Robert Collins
Change (without backwards compatibility) the
761
            lines = {}
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
762
            progress = InstrumentedProgress()
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
763
            # iterate over the lines
2975.3.1 by Robert Collins
Change (without backwards compatibility) the
764
            for line in vf.iter_lines_added_or_present_in_versions(versions,
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
765
                pb=progress):
2975.3.1 by Robert Collins
Change (without backwards compatibility) the
766
                lines.setdefault(line, 0)
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
767
                lines[line] += 1
2975.3.1 by Robert Collins
Change (without backwards compatibility) the
768
            if []!= progress.updates:
2039.1.2 by Aaron Bentley
Tweak test to avoid catching assert
769
                self.assertEqual(expected, progress.updates)
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
770
            return lines
2147.1.3 by John Arbash Meinel
In knit.py we were re-using a variable in 2 loops, causing bogus progress messages to be generated.
771
        lines = iter_with_versions(['child', 'otherchild'],
4103.3.4 by Martin Pool
Update test that depends on progress bar strings
772
                                   [('Walking content', 0, 2),
773
                                    ('Walking content', 1, 2),
774
                                    ('Walking content', 2, 2)])
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
775
        # we must see child and otherchild
2975.3.1 by Robert Collins
Change (without backwards compatibility) the
776
        self.assertTrue(lines[('child\n', 'child')] > 0)
777
        self.assertTrue(lines[('otherchild\n', 'otherchild')] > 0)
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
778
        # we dont care if we got more than that.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
779
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
780
        # test all lines
4103.3.4 by Martin Pool
Update test that depends on progress bar strings
781
        lines = iter_with_versions(None, [('Walking content', 0, 5),
782
                                          ('Walking content', 1, 5),
783
                                          ('Walking content', 2, 5),
784
                                          ('Walking content', 3, 5),
785
                                          ('Walking content', 4, 5),
786
                                          ('Walking content', 5, 5)])
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
787
        # all lines must be seen at least once
2975.3.1 by Robert Collins
Change (without backwards compatibility) the
788
        self.assertTrue(lines[('base\n', 'base')] > 0)
789
        self.assertTrue(lines[('lancestor\n', 'lancestor')] > 0)
790
        self.assertTrue(lines[('rancestor\n', 'rancestor')] > 0)
791
        self.assertTrue(lines[('child\n', 'child')] > 0)
792
        self.assertTrue(lines[('otherchild\n', 'otherchild')] > 0)
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
793
1594.2.8 by Robert Collins
add ghost aware apis to knits.
794
    def test_add_lines_with_ghosts(self):
795
        # some versioned file formats allow lines to be added with parent
796
        # information that is > than that in the format. Formats that do
797
        # not support this need to raise NotImplementedError on the
798
        # add_lines_with_ghosts api.
799
        vf = self.get_file()
800
        # add a revision with ghost parents
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
801
        # The preferred form is utf8, but we should translate when needed
802
        parent_id_unicode = u'b\xbfse'
803
        parent_id_utf8 = parent_id_unicode.encode('utf8')
1594.2.8 by Robert Collins
add ghost aware apis to knits.
804
        try:
2309.4.7 by John Arbash Meinel
Update VersionedFile tests to ensure that they can take Unicode,
805
            vf.add_lines_with_ghosts('notbxbfse', [parent_id_utf8], [])
1594.2.8 by Robert Collins
add ghost aware apis to knits.
806
        except NotImplementedError:
807
            # check the other ghost apis are also not implemented
808
            self.assertRaises(NotImplementedError, vf.get_ancestry_with_ghosts, ['foo'])
809
            self.assertRaises(NotImplementedError, vf.get_parents_with_ghosts, 'foo')
810
            return
2150.2.1 by Robert Collins
Correctly decode utf8 revision ids from knits when parsing, fixes a regression where a unicode revision id is stored correctly, but then indexed by the utf8 value on the next invocation of bzr, rather than the unicode value.
811
        vf = self.reopen_file()
1594.2.8 by Robert Collins
add ghost aware apis to knits.
812
        # test key graph related apis: getncestry, _graph, get_parents
813
        # has_version
814
        # - these are ghost unaware and must not be reflect ghosts
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
815
        self.assertEqual(['notbxbfse'], vf.get_ancestry('notbxbfse'))
816
        self.assertFalse(vf.has_version(parent_id_utf8))
1594.2.8 by Robert Collins
add ghost aware apis to knits.
817
        # we have _with_ghost apis to give us ghost information.
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
818
        self.assertEqual([parent_id_utf8, 'notbxbfse'], vf.get_ancestry_with_ghosts(['notbxbfse']))
819
        self.assertEqual([parent_id_utf8], vf.get_parents_with_ghosts('notbxbfse'))
1594.2.8 by Robert Collins
add ghost aware apis to knits.
820
        # if we add something that is a ghost of another, it should correct the
821
        # results of the prior apis
2858.2.1 by Martin Pool
Remove most calls to safe_file_id and safe_revision_id.
822
        vf.add_lines(parent_id_utf8, [], [])
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
823
        self.assertEqual([parent_id_utf8, 'notbxbfse'], vf.get_ancestry(['notbxbfse']))
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
824
        self.assertEqual({'notbxbfse':(parent_id_utf8,)},
825
            vf.get_parent_map(['notbxbfse']))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
826
        self.assertTrue(vf.has_version(parent_id_utf8))
1594.2.8 by Robert Collins
add ghost aware apis to knits.
827
        # we have _with_ghost apis to give us ghost information.
2858.2.1 by Martin Pool
Remove most calls to safe_file_id and safe_revision_id.
828
        self.assertEqual([parent_id_utf8, 'notbxbfse'],
829
            vf.get_ancestry_with_ghosts(['notbxbfse']))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
830
        self.assertEqual([parent_id_utf8], vf.get_parents_with_ghosts('notbxbfse'))
1594.2.8 by Robert Collins
add ghost aware apis to knits.
831
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
832
    def test_add_lines_with_ghosts_after_normal_revs(self):
833
        # some versioned file formats allow lines to be added with parent
834
        # information that is > than that in the format. Formats that do
835
        # not support this need to raise NotImplementedError on the
836
        # add_lines_with_ghosts api.
837
        vf = self.get_file()
838
        # probe for ghost support
839
        try:
3287.6.5 by Robert Collins
Deprecate VersionedFile.has_ghost.
840
            vf.add_lines_with_ghosts('base', [], ['line\n', 'line_b\n'])
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
841
        except NotImplementedError:
842
            return
843
        vf.add_lines_with_ghosts('references_ghost',
844
                                 ['base', 'a_ghost'],
845
                                 ['line\n', 'line_b\n', 'line_c\n'])
846
        origins = vf.annotate('references_ghost')
847
        self.assertEquals(('base', 'line\n'), origins[0])
848
        self.assertEquals(('base', 'line_b\n'), origins[1])
849
        self.assertEquals(('references_ghost', 'line_c\n'), origins[2])
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
850
851
    def test_readonly_mode(self):
852
        transport = get_transport(self.get_url('.'))
853
        factory = self.get_factory()
854
        vf = factory('id', transport, 0777, create=True, access_mode='w')
855
        vf = factory('id', transport, access_mode='r')
856
        self.assertRaises(errors.ReadOnlyError, vf.add_lines, 'base', [], [])
857
        self.assertRaises(errors.ReadOnlyError,
858
                          vf.add_lines_with_ghosts,
859
                          'base',
860
                          [],
861
                          [])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
862
3316.2.9 by Robert Collins
* ``VersionedFile.get_sha1`` is deprecated, please use
863
    def test_get_sha1s(self):
1666.1.6 by Robert Collins
Make knit the default format.
864
        # check the sha1 data is available
865
        vf = self.get_file()
866
        # a simple file
867
        vf.add_lines('a', [], ['a\n'])
868
        # the same file, different metadata
869
        vf.add_lines('b', ['a'], ['a\n'])
870
        # a file differing only in last newline.
871
        vf.add_lines('c', [], ['a'])
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
872
        self.assertEqual({
873
            'a': '3f786850e387550fdab836ed7e6dc881de23001b',
874
            'c': '86f7e437faa5a7fce15d1ddcb9eaeaea377667b8',
875
            'b': '3f786850e387550fdab836ed7e6dc881de23001b',
876
            },
877
            vf.get_sha1s(['a', 'c', 'b']))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
878
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
879
2535.3.1 by Andrew Bennetts
Add get_format_signature to VersionedFile
880
class TestWeave(TestCaseWithMemoryTransport, VersionedFileTestMixIn):
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
881
882
    def get_file(self, name='foo'):
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
883
        return WeaveFile(name, get_transport(self.get_url('.')), create=True,
884
            get_scope=self.get_transaction)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
885
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
886
    def get_file_corrupted_text(self):
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
887
        w = WeaveFile('foo', get_transport(self.get_url('.')), create=True,
888
            get_scope=self.get_transaction)
1563.2.13 by Robert Collins
InterVersionedFile implemented.
889
        w.add_lines('v1', [], ['hello\n'])
890
        w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
891
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
892
        # We are going to invasively corrupt the text
893
        # Make sure the internals of weave are the same
894
        self.assertEqual([('{', 0)
895
                        , 'hello\n'
896
                        , ('}', None)
897
                        , ('{', 1)
898
                        , 'there\n'
899
                        , ('}', None)
900
                        ], w._weave)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
901
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
902
        self.assertEqual(['f572d396fae9206628714fb2ce00f72e94f2258f'
903
                        , '90f265c6e75f1c8f9ab76dcf85528352c5f215ef'
904
                        ], w._sha1s)
905
        w.check()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
906
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
907
        # Corrupted
908
        w._weave[4] = 'There\n'
909
        return w
910
911
    def get_file_corrupted_checksum(self):
912
        w = self.get_file_corrupted_text()
913
        # Corrected
914
        w._weave[4] = 'there\n'
915
        self.assertEqual('hello\nthere\n', w.get_text('v2'))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
916
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
917
        #Invalid checksum, first digit changed
918
        w._sha1s[1] =  'f0f265c6e75f1c8f9ab76dcf85528352c5f215ef'
919
        return w
920
1666.1.6 by Robert Collins
Make knit the default format.
921
    def reopen_file(self, name='foo', create=False):
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
922
        return WeaveFile(name, get_transport(self.get_url('.')), create=create,
923
            get_scope=self.get_transaction)
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
924
1563.2.25 by Robert Collins
Merge in upstream.
925
    def test_no_implicit_create(self):
926
        self.assertRaises(errors.NoSuchFile,
927
                          WeaveFile,
928
                          'foo',
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
929
                          get_transport(self.get_url('.')),
930
                          get_scope=self.get_transaction)
1563.2.25 by Robert Collins
Merge in upstream.
931
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
932
    def get_factory(self):
933
        return WeaveFile
934
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
935
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
936
class TestPlanMergeVersionedFile(TestCaseWithMemoryTransport):
937
938
    def setUp(self):
939
        TestCaseWithMemoryTransport.setUp(self)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
940
        mapper = PrefixMapper()
941
        factory = make_file_factory(True, mapper)
942
        self.vf1 = factory(self.get_transport('root-1'))
943
        self.vf2 = factory(self.get_transport('root-2'))
944
        self.plan_merge_vf = versionedfile._PlanMergeVersionedFile('root')
945
        self.plan_merge_vf.fallback_versionedfiles.extend([self.vf1, self.vf2])
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
946
947
    def test_add_lines(self):
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
948
        self.plan_merge_vf.add_lines(('root', 'a:'), [], [])
949
        self.assertRaises(ValueError, self.plan_merge_vf.add_lines,
950
            ('root', 'a'), [], [])
951
        self.assertRaises(ValueError, self.plan_merge_vf.add_lines,
952
            ('root', 'a:'), None, [])
953
        self.assertRaises(ValueError, self.plan_merge_vf.add_lines,
954
            ('root', 'a:'), [], None)
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
955
956
    def setup_abcde(self):
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
957
        self.vf1.add_lines(('root', 'A'), [], ['a'])
958
        self.vf1.add_lines(('root', 'B'), [('root', 'A')], ['b'])
959
        self.vf2.add_lines(('root', 'C'), [], ['c'])
960
        self.vf2.add_lines(('root', 'D'), [('root', 'C')], ['d'])
961
        self.plan_merge_vf.add_lines(('root', 'E:'),
962
            [('root', 'B'), ('root', 'D')], ['e'])
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
963
964
    def test_get_parents(self):
965
        self.setup_abcde()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
966
        self.assertEqual({('root', 'B'):(('root', 'A'),)},
967
            self.plan_merge_vf.get_parent_map([('root', 'B')]))
968
        self.assertEqual({('root', 'D'):(('root', 'C'),)},
969
            self.plan_merge_vf.get_parent_map([('root', 'D')]))
970
        self.assertEqual({('root', 'E:'):(('root', 'B'),('root', 'D'))},
971
            self.plan_merge_vf.get_parent_map([('root', 'E:')]))
972
        self.assertEqual({},
973
            self.plan_merge_vf.get_parent_map([('root', 'F')]))
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
974
        self.assertEqual({
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
975
                ('root', 'B'):(('root', 'A'),),
976
                ('root', 'D'):(('root', 'C'),),
977
                ('root', 'E:'):(('root', 'B'),('root', 'D')),
978
                },
979
            self.plan_merge_vf.get_parent_map(
980
                [('root', 'B'), ('root', 'D'), ('root', 'E:'), ('root', 'F')]))
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
981
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
982
    def test_get_record_stream(self):
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
983
        self.setup_abcde()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
984
        def get_record(suffix):
985
            return self.plan_merge_vf.get_record_stream(
986
                [('root', suffix)], 'unordered', True).next()
987
        self.assertEqual('a', get_record('A').get_bytes_as('fulltext'))
988
        self.assertEqual('c', get_record('C').get_bytes_as('fulltext'))
989
        self.assertEqual('e', get_record('E:').get_bytes_as('fulltext'))
990
        self.assertEqual('absent', get_record('F').storage_kind)
1666.1.1 by Robert Collins
Add trivial http-using test for versioned files.
991
992
993
class TestReadonlyHttpMixin(object):
994
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
995
    def get_transaction(self):
996
        return 1
997
1666.1.1 by Robert Collins
Add trivial http-using test for versioned files.
998
    def test_readonly_http_works(self):
999
        # we should be able to read from http with a versioned file.
1000
        vf = self.get_file()
1666.1.6 by Robert Collins
Make knit the default format.
1001
        # try an empty file access
1002
        readonly_vf = self.get_factory()('foo', get_transport(self.get_readonly_url('.')))
1003
        self.assertEqual([], readonly_vf.versions())
1004
        # now with feeling.
1666.1.1 by Robert Collins
Add trivial http-using test for versioned files.
1005
        vf.add_lines('1', [], ['a\n'])
1006
        vf.add_lines('2', ['1'], ['b\n', 'a\n'])
1007
        readonly_vf = self.get_factory()('foo', get_transport(self.get_readonly_url('.')))
1666.1.6 by Robert Collins
Make knit the default format.
1008
        self.assertEqual(['1', '2'], vf.versions())
1666.1.1 by Robert Collins
Add trivial http-using test for versioned files.
1009
        for version in readonly_vf.versions():
1010
            readonly_vf.get_lines(version)
1011
1012
1013
class TestWeaveHTTP(TestCaseWithWebserver, TestReadonlyHttpMixin):
1014
1015
    def get_file(self):
3316.2.3 by Robert Collins
Remove manual notification of transaction finishing on versioned files.
1016
        return WeaveFile('foo', get_transport(self.get_url('.')), create=True,
1017
            get_scope=self.get_transaction)
1666.1.1 by Robert Collins
Add trivial http-using test for versioned files.
1018
1019
    def get_factory(self):
1020
        return WeaveFile
1021
1022
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1023
class MergeCasesMixin(object):
1024
1025
    def doMerge(self, base, a, b, mp):
1026
        from cStringIO import StringIO
1027
        from textwrap import dedent
1028
1029
        def addcrlf(x):
1030
            return x + '\n'
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1031
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1032
        w = self.get_file()
1033
        w.add_lines('text0', [], map(addcrlf, base))
1034
        w.add_lines('text1', ['text0'], map(addcrlf, a))
1035
        w.add_lines('text2', ['text0'], map(addcrlf, b))
1036
1037
        self.log_contents(w)
1038
1039
        self.log('merge plan:')
1040
        p = list(w.plan_merge('text1', 'text2'))
1041
        for state, line in p:
1042
            if line:
1043
                self.log('%12s | %s' % (state, line[:-1]))
1044
1045
        self.log('merge:')
1046
        mt = StringIO()
1047
        mt.writelines(w.weave_merge(p))
1048
        mt.seek(0)
1049
        self.log(mt.getvalue())
1050
1051
        mp = map(addcrlf, mp)
1052
        self.assertEqual(mt.readlines(), mp)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1053
1054
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1055
    def testOneInsert(self):
1056
        self.doMerge([],
1057
                     ['aa'],
1058
                     [],
1059
                     ['aa'])
1060
1061
    def testSeparateInserts(self):
1062
        self.doMerge(['aaa', 'bbb', 'ccc'],
1063
                     ['aaa', 'xxx', 'bbb', 'ccc'],
1064
                     ['aaa', 'bbb', 'yyy', 'ccc'],
1065
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
1066
1067
    def testSameInsert(self):
1068
        self.doMerge(['aaa', 'bbb', 'ccc'],
1069
                     ['aaa', 'xxx', 'bbb', 'ccc'],
1070
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'],
1071
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
1072
    overlappedInsertExpected = ['aaa', 'xxx', 'yyy', 'bbb']
1073
    def testOverlappedInsert(self):
1074
        self.doMerge(['aaa', 'bbb'],
1075
                     ['aaa', 'xxx', 'yyy', 'bbb'],
1076
                     ['aaa', 'xxx', 'bbb'], self.overlappedInsertExpected)
1077
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1078
        # really it ought to reduce this to
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1079
        # ['aaa', 'xxx', 'yyy', 'bbb']
1080
1081
1082
    def testClashReplace(self):
1083
        self.doMerge(['aaa'],
1084
                     ['xxx'],
1085
                     ['yyy', 'zzz'],
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1086
                     ['<<<<<<< ', 'xxx', '=======', 'yyy', 'zzz',
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1087
                      '>>>>>>> '])
1088
1089
    def testNonClashInsert1(self):
1090
        self.doMerge(['aaa'],
1091
                     ['xxx', 'aaa'],
1092
                     ['yyy', 'zzz'],
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1093
                     ['<<<<<<< ', 'xxx', 'aaa', '=======', 'yyy', 'zzz',
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1094
                      '>>>>>>> '])
1095
1096
    def testNonClashInsert2(self):
1097
        self.doMerge(['aaa'],
1098
                     ['aaa'],
1099
                     ['yyy', 'zzz'],
1100
                     ['yyy', 'zzz'])
1101
1102
1103
    def testDeleteAndModify(self):
1104
        """Clashing delete and modification.
1105
1106
        If one side modifies a region and the other deletes it then
1107
        there should be a conflict with one side blank.
1108
        """
1109
1110
        #######################################
1111
        # skippd, not working yet
1112
        return
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1113
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1114
        self.doMerge(['aaa', 'bbb', 'ccc'],
1115
                     ['aaa', 'ddd', 'ccc'],
1116
                     ['aaa', 'ccc'],
1117
                     ['<<<<<<<< ', 'aaa', '=======', '>>>>>>> ', 'ccc'])
1118
1119
    def _test_merge_from_strings(self, base, a, b, expected):
1120
        w = self.get_file()
1121
        w.add_lines('text0', [], base.splitlines(True))
1122
        w.add_lines('text1', ['text0'], a.splitlines(True))
1123
        w.add_lines('text2', ['text0'], b.splitlines(True))
1124
        self.log('merge plan:')
1125
        p = list(w.plan_merge('text1', 'text2'))
1126
        for state, line in p:
1127
            if line:
1128
                self.log('%12s | %s' % (state, line[:-1]))
1129
        self.log('merge result:')
1130
        result_text = ''.join(w.weave_merge(p))
1131
        self.log(result_text)
1132
        self.assertEqualDiff(result_text, expected)
1133
1134
    def test_weave_merge_conflicts(self):
1135
        # does weave merge properly handle plans that end with unchanged?
1136
        result = ''.join(self.get_file().weave_merge([('new-a', 'hello\n')]))
1137
        self.assertEqual(result, 'hello\n')
1138
1139
    def test_deletion_extended(self):
1140
        """One side deletes, the other deletes more.
1141
        """
1142
        base = """\
1143
            line 1
1144
            line 2
1145
            line 3
1146
            """
1147
        a = """\
1148
            line 1
1149
            line 2
1150
            """
1151
        b = """\
1152
            line 1
1153
            """
1154
        result = """\
1155
            line 1
1156
            """
1157
        self._test_merge_from_strings(base, a, b, result)
1158
1159
    def test_deletion_overlap(self):
1160
        """Delete overlapping regions with no other conflict.
1161
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1162
        Arguably it'd be better to treat these as agreement, rather than
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1163
        conflict, but for now conflict is safer.
1164
        """
1165
        base = """\
1166
            start context
1167
            int a() {}
1168
            int b() {}
1169
            int c() {}
1170
            end context
1171
            """
1172
        a = """\
1173
            start context
1174
            int a() {}
1175
            end context
1176
            """
1177
        b = """\
1178
            start context
1179
            int c() {}
1180
            end context
1181
            """
1182
        result = """\
1183
            start context
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
1184
<<<<<<<\x20
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1185
            int a() {}
1186
=======
1187
            int c() {}
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
1188
>>>>>>>\x20
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1189
            end context
1190
            """
1191
        self._test_merge_from_strings(base, a, b, result)
1192
1193
    def test_agreement_deletion(self):
1194
        """Agree to delete some lines, without conflicts."""
1195
        base = """\
1196
            start context
1197
            base line 1
1198
            base line 2
1199
            end context
1200
            """
1201
        a = """\
1202
            start context
1203
            base line 1
1204
            end context
1205
            """
1206
        b = """\
1207
            start context
1208
            base line 1
1209
            end context
1210
            """
1211
        result = """\
1212
            start context
1213
            base line 1
1214
            end context
1215
            """
1216
        self._test_merge_from_strings(base, a, b, result)
1217
1218
    def test_sync_on_deletion(self):
1219
        """Specific case of merge where we can synchronize incorrectly.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1220
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1221
        A previous version of the weave merge concluded that the two versions
1222
        agreed on deleting line 2, and this could be a synchronization point.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1223
        Line 1 was then considered in isolation, and thought to be deleted on
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1224
        both sides.
1225
1226
        It's better to consider the whole thing as a disagreement region.
1227
        """
1228
        base = """\
1229
            start context
1230
            base line 1
1231
            base line 2
1232
            end context
1233
            """
1234
        a = """\
1235
            start context
1236
            base line 1
1237
            a's replacement line 2
1238
            end context
1239
            """
1240
        b = """\
1241
            start context
1242
            b replaces
1243
            both lines
1244
            end context
1245
            """
1246
        result = """\
1247
            start context
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
1248
<<<<<<<\x20
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1249
            base line 1
1250
            a's replacement line 2
1251
=======
1252
            b replaces
1253
            both lines
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
1254
>>>>>>>\x20
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1255
            end context
1256
            """
1257
        self._test_merge_from_strings(base, a, b, result)
1258
1259
2535.3.1 by Andrew Bennetts
Add get_format_signature to VersionedFile
1260
class TestWeaveMerge(TestCaseWithMemoryTransport, MergeCasesMixin):
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1261
1262
    def get_file(self, name='foo'):
1263
        return WeaveFile(name, get_transport(self.get_url('.')), create=True)
1264
1265
    def log_contents(self, w):
1266
        self.log('weave is:')
1267
        tmpf = StringIO()
1268
        write_weave(w, tmpf)
1269
        self.log(tmpf.getvalue())
1270
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1271
    overlappedInsertExpected = ['aaa', '<<<<<<< ', 'xxx', 'yyy', '=======',
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1272
                                'xxx', '>>>>>>> ', 'bbb']
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1273
1274
1275
class TestContentFactoryAdaption(TestCaseWithMemoryTransport):
1276
1277
    def test_select_adaptor(self):
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
1278
        """Test expected adapters exist."""
1279
        # One scenario for each lookup combination we expect to use.
1280
        # Each is source_kind, requested_kind, adapter class
1281
        scenarios = [
1282
            ('knit-delta-gz', 'fulltext', _mod_knit.DeltaPlainToFullText),
1283
            ('knit-ft-gz', 'fulltext', _mod_knit.FTPlainToFullText),
1284
            ('knit-annotated-delta-gz', 'knit-delta-gz',
1285
                _mod_knit.DeltaAnnotatedToUnannotated),
1286
            ('knit-annotated-delta-gz', 'fulltext',
1287
                _mod_knit.DeltaAnnotatedToFullText),
1288
            ('knit-annotated-ft-gz', 'knit-ft-gz',
1289
                _mod_knit.FTAnnotatedToUnannotated),
1290
            ('knit-annotated-ft-gz', 'fulltext',
1291
                _mod_knit.FTAnnotatedToFullText),
1292
            ]
1293
        for source, requested, klass in scenarios:
1294
            adapter_factory = versionedfile.adapter_registry.get(
1295
                (source, requested))
1296
            adapter = adapter_factory(None)
1297
            self.assertIsInstance(adapter, klass)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1298
3350.3.5 by Robert Collins
Create adapters from plain compressed knit content.
1299
    def get_knit(self, annotated=True):
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1300
        mapper = ConstantMapper('knit')
1301
        transport = self.get_transport()
1302
        return make_file_factory(annotated, mapper)(transport)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1303
1304
    def helpGetBytes(self, f, ft_adapter, delta_adapter):
3350.3.22 by Robert Collins
Review feedback.
1305
        """Grab the interested adapted texts for tests."""
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1306
        # origin is a fulltext
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1307
        entries = f.get_record_stream([('origin',)], 'unordered', False)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1308
        base = entries.next()
4005.3.1 by Robert Collins
Change the signature on VersionedFiles adapters to allow less typing and more flexability inside adapters.
1309
        ft_data = ft_adapter.get_bytes(base)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1310
        # merged is both a delta and multiple parents.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1311
        entries = f.get_record_stream([('merged',)], 'unordered', False)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1312
        merged = entries.next()
4005.3.1 by Robert Collins
Change the signature on VersionedFiles adapters to allow less typing and more flexability inside adapters.
1313
        delta_data = delta_adapter.get_bytes(merged)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1314
        return ft_data, delta_data
1315
1316
    def test_deannotation_noeol(self):
1317
        """Test converting annotated knits to unannotated knits."""
1318
        # we need a full text, and a delta
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1319
        f = self.get_knit()
1320
        get_diamond_files(f, 1, trailing_eol=False)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1321
        ft_data, delta_data = self.helpGetBytes(f,
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
1322
            _mod_knit.FTAnnotatedToUnannotated(None),
1323
            _mod_knit.DeltaAnnotatedToUnannotated(None))
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1324
        self.assertEqual(
1325
            'version origin 1 b284f94827db1fa2970d9e2014f080413b547a7e\n'
1326
            'origin\n'
1327
            'end origin\n',
1328
            GzipFile(mode='rb', fileobj=StringIO(ft_data)).read())
1329
        self.assertEqual(
1330
            'version merged 4 32c2e79763b3f90e8ccde37f9710b6629c25a796\n'
1331
            '1,2,3\nleft\nright\nmerged\nend merged\n',
1332
            GzipFile(mode='rb', fileobj=StringIO(delta_data)).read())
1333
1334
    def test_deannotation(self):
1335
        """Test converting annotated knits to unannotated knits."""
1336
        # we need a full text, and a delta
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1337
        f = self.get_knit()
1338
        get_diamond_files(f, 1)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1339
        ft_data, delta_data = self.helpGetBytes(f,
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
1340
            _mod_knit.FTAnnotatedToUnannotated(None),
1341
            _mod_knit.DeltaAnnotatedToUnannotated(None))
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1342
        self.assertEqual(
1343
            'version origin 1 00e364d235126be43292ab09cb4686cf703ddc17\n'
1344
            'origin\n'
1345
            'end origin\n',
1346
            GzipFile(mode='rb', fileobj=StringIO(ft_data)).read())
1347
        self.assertEqual(
1348
            'version merged 3 ed8bce375198ea62444dc71952b22cfc2b09226d\n'
1349
            '2,2,2\nright\nmerged\nend merged\n',
1350
            GzipFile(mode='rb', fileobj=StringIO(delta_data)).read())
1351
1352
    def test_annotated_to_fulltext_no_eol(self):
1353
        """Test adapting annotated knits to full texts (for -> weaves)."""
1354
        # we need a full text, and a delta
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1355
        f = self.get_knit()
1356
        get_diamond_files(f, 1, trailing_eol=False)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1357
        # Reconstructing a full text requires a backing versioned file, and it
1358
        # must have the base lines requested from it.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1359
        logged_vf = versionedfile.RecordingVersionedFilesDecorator(f)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1360
        ft_data, delta_data = self.helpGetBytes(f,
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
1361
            _mod_knit.FTAnnotatedToFullText(None),
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1362
            _mod_knit.DeltaAnnotatedToFullText(logged_vf))
1363
        self.assertEqual('origin', ft_data)
1364
        self.assertEqual('base\nleft\nright\nmerged', delta_data)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1365
        self.assertEqual([('get_record_stream', [('left',)], 'unordered',
1366
            True)], logged_vf.calls)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1367
1368
    def test_annotated_to_fulltext(self):
1369
        """Test adapting annotated knits to full texts (for -> weaves)."""
1370
        # we need a full text, and a delta
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1371
        f = self.get_knit()
1372
        get_diamond_files(f, 1)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1373
        # Reconstructing a full text requires a backing versioned file, and it
1374
        # must have the base lines requested from it.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1375
        logged_vf = versionedfile.RecordingVersionedFilesDecorator(f)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1376
        ft_data, delta_data = self.helpGetBytes(f,
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
1377
            _mod_knit.FTAnnotatedToFullText(None),
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1378
            _mod_knit.DeltaAnnotatedToFullText(logged_vf))
1379
        self.assertEqual('origin\n', ft_data)
1380
        self.assertEqual('base\nleft\nright\nmerged\n', delta_data)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1381
        self.assertEqual([('get_record_stream', [('left',)], 'unordered',
1382
            True)], logged_vf.calls)
3350.3.5 by Robert Collins
Create adapters from plain compressed knit content.
1383
1384
    def test_unannotated_to_fulltext(self):
1385
        """Test adapting unannotated knits to full texts.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1386
3350.3.5 by Robert Collins
Create adapters from plain compressed knit content.
1387
        This is used for -> weaves, and for -> annotated knits.
1388
        """
1389
        # we need a full text, and a delta
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1390
        f = self.get_knit(annotated=False)
1391
        get_diamond_files(f, 1)
3350.3.5 by Robert Collins
Create adapters from plain compressed knit content.
1392
        # Reconstructing a full text requires a backing versioned file, and it
1393
        # must have the base lines requested from it.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1394
        logged_vf = versionedfile.RecordingVersionedFilesDecorator(f)
3350.3.5 by Robert Collins
Create adapters from plain compressed knit content.
1395
        ft_data, delta_data = self.helpGetBytes(f,
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
1396
            _mod_knit.FTPlainToFullText(None),
3350.3.5 by Robert Collins
Create adapters from plain compressed knit content.
1397
            _mod_knit.DeltaPlainToFullText(logged_vf))
1398
        self.assertEqual('origin\n', ft_data)
1399
        self.assertEqual('base\nleft\nright\nmerged\n', delta_data)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1400
        self.assertEqual([('get_record_stream', [('left',)], 'unordered',
1401
            True)], logged_vf.calls)
3350.3.5 by Robert Collins
Create adapters from plain compressed knit content.
1402
3350.3.6 by Robert Collins
Test EOL behaviour of plain knit record adapters.
1403
    def test_unannotated_to_fulltext_no_eol(self):
1404
        """Test adapting unannotated knits to full texts.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1405
3350.3.6 by Robert Collins
Test EOL behaviour of plain knit record adapters.
1406
        This is used for -> weaves, and for -> annotated knits.
1407
        """
1408
        # we need a full text, and a delta
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1409
        f = self.get_knit(annotated=False)
1410
        get_diamond_files(f, 1, trailing_eol=False)
3350.3.6 by Robert Collins
Test EOL behaviour of plain knit record adapters.
1411
        # Reconstructing a full text requires a backing versioned file, and it
1412
        # must have the base lines requested from it.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1413
        logged_vf = versionedfile.RecordingVersionedFilesDecorator(f)
3350.3.6 by Robert Collins
Test EOL behaviour of plain knit record adapters.
1414
        ft_data, delta_data = self.helpGetBytes(f,
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
1415
            _mod_knit.FTPlainToFullText(None),
3350.3.6 by Robert Collins
Test EOL behaviour of plain knit record adapters.
1416
            _mod_knit.DeltaPlainToFullText(logged_vf))
1417
        self.assertEqual('origin', ft_data)
1418
        self.assertEqual('base\nleft\nright\nmerged', delta_data)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1419
        self.assertEqual([('get_record_stream', [('left',)], 'unordered',
1420
            True)], logged_vf.calls)
3350.3.6 by Robert Collins
Test EOL behaviour of plain knit record adapters.
1421
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
1422
1423
class TestKeyMapper(TestCaseWithMemoryTransport):
1424
    """Tests for various key mapping logic."""
1425
1426
    def test_identity_mapper(self):
1427
        mapper = versionedfile.ConstantMapper("inventory")
1428
        self.assertEqual("inventory", mapper.map(('foo@ar',)))
1429
        self.assertEqual("inventory", mapper.map(('quux',)))
1430
1431
    def test_prefix_mapper(self):
1432
        #format5: plain
1433
        mapper = versionedfile.PrefixMapper()
1434
        self.assertEqual("file-id", mapper.map(("file-id", "revision-id")))
1435
        self.assertEqual("new-id", mapper.map(("new-id", "revision-id")))
1436
        self.assertEqual(('file-id',), mapper.unmap("file-id"))
1437
        self.assertEqual(('new-id',), mapper.unmap("new-id"))
1438
1439
    def test_hash_prefix_mapper(self):
1440
        #format6: hash + plain
1441
        mapper = versionedfile.HashPrefixMapper()
1442
        self.assertEqual("9b/file-id", mapper.map(("file-id", "revision-id")))
1443
        self.assertEqual("45/new-id", mapper.map(("new-id", "revision-id")))
1444
        self.assertEqual(('file-id',), mapper.unmap("9b/file-id"))
1445
        self.assertEqual(('new-id',), mapper.unmap("45/new-id"))
1446
1447
    def test_hash_escaped_mapper(self):
1448
        #knit1: hash + escaped
1449
        mapper = versionedfile.HashEscapedPrefixMapper()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1450
        self.assertEqual("88/%2520", mapper.map((" ", "revision-id")))
1451
        self.assertEqual("ed/fil%2545-%2549d", mapper.map(("filE-Id",
1452
            "revision-id")))
1453
        self.assertEqual("88/ne%2557-%2549d", mapper.map(("neW-Id",
1454
            "revision-id")))
1455
        self.assertEqual(('filE-Id',), mapper.unmap("ed/fil%2545-%2549d"))
1456
        self.assertEqual(('neW-Id',), mapper.unmap("88/ne%2557-%2549d"))
3350.6.2 by Robert Collins
Prepare parameterised test environment.
1457
1458
1459
class TestVersionedFiles(TestCaseWithMemoryTransport):
1460
    """Tests for the multiple-file variant of VersionedFile."""
1461
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1462
    def get_versionedfiles(self, relpath='files'):
1463
        transport = self.get_transport(relpath)
1464
        if relpath != '.':
1465
            transport.mkdir('.')
1466
        files = self.factory(transport)
1467
        if self.cleanup is not None:
1468
            self.addCleanup(lambda:self.cleanup(files))
1469
        return files
1470
1471
    def test_annotate(self):
1472
        files = self.get_versionedfiles()
1473
        self.get_diamond_files(files)
1474
        if self.key_length == 1:
1475
            prefix = ()
1476
        else:
1477
            prefix = ('FileA',)
1478
        # introduced full text
1479
        origins = files.annotate(prefix + ('origin',))
1480
        self.assertEqual([
1481
            (prefix + ('origin',), 'origin\n')],
1482
            origins)
1483
        # a delta
1484
        origins = files.annotate(prefix + ('base',))
1485
        self.assertEqual([
1486
            (prefix + ('base',), 'base\n')],
1487
            origins)
1488
        # a merge
1489
        origins = files.annotate(prefix + ('merged',))
1490
        if self.graph:
1491
            self.assertEqual([
1492
                (prefix + ('base',), 'base\n'),
1493
                (prefix + ('left',), 'left\n'),
1494
                (prefix + ('right',), 'right\n'),
1495
                (prefix + ('merged',), 'merged\n')
1496
                ],
1497
                origins)
1498
        else:
1499
            # Without a graph everything is new.
1500
            self.assertEqual([
1501
                (prefix + ('merged',), 'base\n'),
1502
                (prefix + ('merged',), 'left\n'),
1503
                (prefix + ('merged',), 'right\n'),
1504
                (prefix + ('merged',), 'merged\n')
1505
                ],
1506
                origins)
1507
        self.assertRaises(RevisionNotPresent,
1508
            files.annotate, prefix + ('missing-key',))
1509
3350.6.2 by Robert Collins
Prepare parameterised test environment.
1510
    def test_construct(self):
1511
        """Each parameterised test can be constructed on a transport."""
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1512
        files = self.get_versionedfiles()
1513
3735.2.5 by Robert Collins
Teach VersionedFiles how to allocate keys based on content hashes.
1514
    def get_diamond_files(self, files, trailing_eol=True, left_only=False,
1515
        nokeys=False):
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1516
        return get_diamond_files(files, self.key_length,
1517
            trailing_eol=trailing_eol, nograph=not self.graph,
3735.2.5 by Robert Collins
Teach VersionedFiles how to allocate keys based on content hashes.
1518
            left_only=left_only, nokeys=nokeys)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1519
4119.1.1 by John Arbash Meinel
Move the 'add_lines_nostoresha' to being tested against all VF implementations.
1520
    def test_add_lines_nostoresha(self):
1521
        """When nostore_sha is supplied using old content raises."""
1522
        vf = self.get_versionedfiles()
1523
        empty_text = ('a', [])
1524
        sample_text_nl = ('b', ["foo\n", "bar\n"])
1525
        sample_text_no_nl = ('c', ["foo\n", "bar"])
1526
        shas = []
1527
        for version, lines in (empty_text, sample_text_nl, sample_text_no_nl):
1528
            sha, _, _ = vf.add_lines(self.get_simple_key(version), [], lines)
1529
            shas.append(sha)
1530
        # we now have a copy of all the lines in the vf.
1531
        for sha, (version, lines) in zip(
1532
            shas, (empty_text, sample_text_nl, sample_text_no_nl)):
1533
            new_key = self.get_simple_key(version + "2")
1534
            self.assertRaises(errors.ExistingContent,
1535
                vf.add_lines, new_key, [], lines,
1536
                nostore_sha=sha)
1537
            # and no new version should have been added.
1538
            record = vf.get_record_stream([new_key], 'unordered', True).next()
1539
            self.assertEqual('absent', record.storage_kind)
1540
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1541
    def test_add_lines_return(self):
1542
        files = self.get_versionedfiles()
1543
        # save code by using the stock data insertion helper.
1544
        adds = self.get_diamond_files(files)
1545
        results = []
1546
        # We can only validate the first 2 elements returned from add_lines.
1547
        for add in adds:
1548
            self.assertEqual(3, len(add))
1549
            results.append(add[:2])
1550
        if self.key_length == 1:
1551
            self.assertEqual([
1552
                ('00e364d235126be43292ab09cb4686cf703ddc17', 7),
1553
                ('51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1554
                ('a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1555
                ('9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1556
                ('ed8bce375198ea62444dc71952b22cfc2b09226d', 23)],
1557
                results)
1558
        elif self.key_length == 2:
1559
            self.assertEqual([
1560
                ('00e364d235126be43292ab09cb4686cf703ddc17', 7),
1561
                ('00e364d235126be43292ab09cb4686cf703ddc17', 7),
1562
                ('51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1563
                ('51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1564
                ('a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1565
                ('a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1566
                ('9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1567
                ('9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1568
                ('ed8bce375198ea62444dc71952b22cfc2b09226d', 23),
1569
                ('ed8bce375198ea62444dc71952b22cfc2b09226d', 23)],
1570
                results)
1571
3735.2.5 by Robert Collins
Teach VersionedFiles how to allocate keys based on content hashes.
1572
    def test_add_lines_no_key_generates_chk_key(self):
1573
        files = self.get_versionedfiles()
1574
        # save code by using the stock data insertion helper.
1575
        adds = self.get_diamond_files(files, nokeys=True)
1576
        results = []
1577
        # We can only validate the first 2 elements returned from add_lines.
1578
        for add in adds:
1579
            self.assertEqual(3, len(add))
1580
            results.append(add[:2])
1581
        if self.key_length == 1:
1582
            self.assertEqual([
1583
                ('00e364d235126be43292ab09cb4686cf703ddc17', 7),
1584
                ('51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1585
                ('a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1586
                ('9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1587
                ('ed8bce375198ea62444dc71952b22cfc2b09226d', 23)],
1588
                results)
1589
            # Check the added items got CHK keys.
1590
            self.assertEqual(set([
1591
                ('sha1:00e364d235126be43292ab09cb4686cf703ddc17',),
1592
                ('sha1:51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44',),
1593
                ('sha1:9ef09dfa9d86780bdec9219a22560c6ece8e0ef1',),
1594
                ('sha1:a8478686da38e370e32e42e8a0c220e33ee9132f',),
1595
                ('sha1:ed8bce375198ea62444dc71952b22cfc2b09226d',),
1596
                ]),
1597
                files.keys())
1598
        elif self.key_length == 2:
1599
            self.assertEqual([
1600
                ('00e364d235126be43292ab09cb4686cf703ddc17', 7),
1601
                ('00e364d235126be43292ab09cb4686cf703ddc17', 7),
1602
                ('51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1603
                ('51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1604
                ('a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1605
                ('a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1606
                ('9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1607
                ('9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1608
                ('ed8bce375198ea62444dc71952b22cfc2b09226d', 23),
1609
                ('ed8bce375198ea62444dc71952b22cfc2b09226d', 23)],
1610
                results)
1611
            # Check the added items got CHK keys.
1612
            self.assertEqual(set([
1613
                ('FileA', 'sha1:00e364d235126be43292ab09cb4686cf703ddc17'),
1614
                ('FileA', 'sha1:51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44'),
1615
                ('FileA', 'sha1:9ef09dfa9d86780bdec9219a22560c6ece8e0ef1'),
1616
                ('FileA', 'sha1:a8478686da38e370e32e42e8a0c220e33ee9132f'),
1617
                ('FileA', 'sha1:ed8bce375198ea62444dc71952b22cfc2b09226d'),
1618
                ('FileB', 'sha1:00e364d235126be43292ab09cb4686cf703ddc17'),
1619
                ('FileB', 'sha1:51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44'),
1620
                ('FileB', 'sha1:9ef09dfa9d86780bdec9219a22560c6ece8e0ef1'),
1621
                ('FileB', 'sha1:a8478686da38e370e32e42e8a0c220e33ee9132f'),
1622
                ('FileB', 'sha1:ed8bce375198ea62444dc71952b22cfc2b09226d'),
1623
                ]),
1624
                files.keys())
1625
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1626
    def test_empty_lines(self):
1627
        """Empty files can be stored."""
1628
        f = self.get_versionedfiles()
1629
        key_a = self.get_simple_key('a')
1630
        f.add_lines(key_a, [], [])
1631
        self.assertEqual('',
1632
            f.get_record_stream([key_a], 'unordered', True
1633
                ).next().get_bytes_as('fulltext'))
1634
        key_b = self.get_simple_key('b')
1635
        f.add_lines(key_b, self.get_parents([key_a]), [])
1636
        self.assertEqual('',
1637
            f.get_record_stream([key_b], 'unordered', True
1638
                ).next().get_bytes_as('fulltext'))
1639
1640
    def test_newline_only(self):
1641
        f = self.get_versionedfiles()
1642
        key_a = self.get_simple_key('a')
1643
        f.add_lines(key_a, [], ['\n'])
1644
        self.assertEqual('\n',
1645
            f.get_record_stream([key_a], 'unordered', True
1646
                ).next().get_bytes_as('fulltext'))
1647
        key_b = self.get_simple_key('b')
1648
        f.add_lines(key_b, self.get_parents([key_a]), ['\n'])
1649
        self.assertEqual('\n',
1650
            f.get_record_stream([key_b], 'unordered', True
1651
                ).next().get_bytes_as('fulltext'))
1652
1653
    def test_get_record_stream_empty(self):
1654
        """An empty stream can be requested without error."""
1655
        f = self.get_versionedfiles()
1656
        entries = f.get_record_stream([], 'unordered', False)
1657
        self.assertEqual([], list(entries))
1658
1659
    def assertValidStorageKind(self, storage_kind):
1660
        """Assert that storage_kind is a valid storage_kind."""
1661
        self.assertSubset([storage_kind],
1662
            ['mpdiff', 'knit-annotated-ft', 'knit-annotated-delta',
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
1663
             'knit-ft', 'knit-delta', 'chunked', 'fulltext',
1664
             'knit-annotated-ft-gz', 'knit-annotated-delta-gz', 'knit-ft-gz',
4005.3.6 by Robert Collins
Support delta_closure=True with NetworkRecordStream to transmit deltas over the wire when full text extraction is required on the far end.
1665
             'knit-delta-gz',
3735.32.12 by John Arbash Meinel
Add groupcompress-block[-ref] as valid stream types.
1666
             'knit-delta-closure', 'knit-delta-closure-ref',
1667
             'groupcompress-block', 'groupcompress-block-ref'])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1668
1669
    def capture_stream(self, f, entries, on_seen, parents):
1670
        """Capture a stream for testing."""
1671
        for factory in entries:
1672
            on_seen(factory.key)
1673
            self.assertValidStorageKind(factory.storage_kind)
3735.32.13 by John Arbash Meinel
Allow the sha1 to be None for a factory.
1674
            if factory.sha1 is not None:
1675
                self.assertEqual(f.get_sha1s([factory.key])[factory.key],
1676
                    factory.sha1)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1677
            self.assertEqual(parents[factory.key], factory.parents)
1678
            self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
1679
                str)
1680
1681
    def test_get_record_stream_interface(self):
1682
        """each item in a stream has to provide a regular interface."""
1683
        files = self.get_versionedfiles()
1684
        self.get_diamond_files(files)
1685
        keys, _ = self.get_keys_and_sort_order()
1686
        parent_map = files.get_parent_map(keys)
1687
        entries = files.get_record_stream(keys, 'unordered', False)
1688
        seen = set()
1689
        self.capture_stream(files, entries, seen.add, parent_map)
1690
        self.assertEqual(set(keys), seen)
1691
1692
    def get_simple_key(self, suffix):
1693
        """Return a key for the object under test."""
1694
        if self.key_length == 1:
1695
            return (suffix,)
1696
        else:
1697
            return ('FileA',) + (suffix,)
1698
1699
    def get_keys_and_sort_order(self):
1700
        """Get diamond test keys list, and their sort ordering."""
1701
        if self.key_length == 1:
1702
            keys = [('merged',), ('left',), ('right',), ('base',)]
1703
            sort_order = {('merged',):2, ('left',):1, ('right',):1, ('base',):0}
1704
        else:
1705
            keys = [
1706
                ('FileA', 'merged'), ('FileA', 'left'), ('FileA', 'right'),
1707
                ('FileA', 'base'),
1708
                ('FileB', 'merged'), ('FileB', 'left'), ('FileB', 'right'),
1709
                ('FileB', 'base'),
1710
                ]
1711
            sort_order = {
1712
                ('FileA', 'merged'):2, ('FileA', 'left'):1, ('FileA', 'right'):1,
1713
                ('FileA', 'base'):0,
1714
                ('FileB', 'merged'):2, ('FileB', 'left'):1, ('FileB', 'right'):1,
1715
                ('FileB', 'base'):0,
1716
                }
1717
        return keys, sort_order
1718
4111.1.1 by Robert Collins
Add a groupcompress sort order.
1719
    def get_keys_and_groupcompress_sort_order(self):
1720
        """Get diamond test keys list, and their groupcompress sort ordering."""
1721
        if self.key_length == 1:
1722
            keys = [('merged',), ('left',), ('right',), ('base',)]
1723
            sort_order = {('merged',):0, ('left',):1, ('right',):1, ('base',):2}
1724
        else:
1725
            keys = [
1726
                ('FileA', 'merged'), ('FileA', 'left'), ('FileA', 'right'),
1727
                ('FileA', 'base'),
1728
                ('FileB', 'merged'), ('FileB', 'left'), ('FileB', 'right'),
1729
                ('FileB', 'base'),
1730
                ]
1731
            sort_order = {
1732
                ('FileA', 'merged'):0, ('FileA', 'left'):1, ('FileA', 'right'):1,
1733
                ('FileA', 'base'):2,
1734
                ('FileB', 'merged'):3, ('FileB', 'left'):4, ('FileB', 'right'):4,
1735
                ('FileB', 'base'):5,
1736
                }
1737
        return keys, sort_order
1738
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1739
    def test_get_record_stream_interface_ordered(self):
1740
        """each item in a stream has to provide a regular interface."""
1741
        files = self.get_versionedfiles()
1742
        self.get_diamond_files(files)
1743
        keys, sort_order = self.get_keys_and_sort_order()
1744
        parent_map = files.get_parent_map(keys)
1745
        entries = files.get_record_stream(keys, 'topological', False)
1746
        seen = []
1747
        self.capture_stream(files, entries, seen.append, parent_map)
1748
        self.assertStreamOrder(sort_order, seen, keys)
1749
1750
    def test_get_record_stream_interface_ordered_with_delta_closure(self):
1751
        """each item must be accessible as a fulltext."""
1752
        files = self.get_versionedfiles()
1753
        self.get_diamond_files(files)
1754
        keys, sort_order = self.get_keys_and_sort_order()
1755
        parent_map = files.get_parent_map(keys)
1756
        entries = files.get_record_stream(keys, 'topological', True)
1757
        seen = []
1758
        for factory in entries:
1759
            seen.append(factory.key)
1760
            self.assertValidStorageKind(factory.storage_kind)
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
1761
            self.assertSubset([factory.sha1],
1762
                [None, files.get_sha1s([factory.key])[factory.key]])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1763
            self.assertEqual(parent_map[factory.key], factory.parents)
1764
            # self.assertEqual(files.get_text(factory.key),
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
1765
            ft_bytes = factory.get_bytes_as('fulltext')
1766
            self.assertIsInstance(ft_bytes, str)
1767
            chunked_bytes = factory.get_bytes_as('chunked')
1768
            self.assertEqualDiff(ft_bytes, ''.join(chunked_bytes))
1769
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1770
        self.assertStreamOrder(sort_order, seen, keys)
1771
4111.1.1 by Robert Collins
Add a groupcompress sort order.
1772
    def test_get_record_stream_interface_groupcompress(self):
1773
        """each item in a stream has to provide a regular interface."""
1774
        files = self.get_versionedfiles()
1775
        self.get_diamond_files(files)
1776
        keys, sort_order = self.get_keys_and_groupcompress_sort_order()
1777
        parent_map = files.get_parent_map(keys)
1778
        entries = files.get_record_stream(keys, 'groupcompress', False)
1779
        seen = []
1780
        self.capture_stream(files, entries, seen.append, parent_map)
1781
        self.assertStreamOrder(sort_order, seen, keys)
1782
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1783
    def assertStreamOrder(self, sort_order, seen, keys):
1784
        self.assertEqual(len(set(seen)), len(keys))
1785
        if self.key_length == 1:
1786
            lows = {():0}
1787
        else:
1788
            lows = {('FileA',):0, ('FileB',):0}
1789
        if not self.graph:
1790
            self.assertEqual(set(keys), set(seen))
1791
        else:
1792
            for key in seen:
1793
                sort_pos = sort_order[key]
1794
                self.assertTrue(sort_pos >= lows[key[:-1]],
1795
                    "Out of order in sorted stream: %r, %r" % (key, seen))
1796
                lows[key[:-1]] = sort_pos
1797
1798
    def test_get_record_stream_unknown_storage_kind_raises(self):
1799
        """Asking for a storage kind that the stream cannot supply raises."""
1800
        files = self.get_versionedfiles()
1801
        self.get_diamond_files(files)
1802
        if self.key_length == 1:
1803
            keys = [('merged',), ('left',), ('right',), ('base',)]
1804
        else:
1805
            keys = [
1806
                ('FileA', 'merged'), ('FileA', 'left'), ('FileA', 'right'),
1807
                ('FileA', 'base'),
1808
                ('FileB', 'merged'), ('FileB', 'left'), ('FileB', 'right'),
1809
                ('FileB', 'base'),
1810
                ]
1811
        parent_map = files.get_parent_map(keys)
1812
        entries = files.get_record_stream(keys, 'unordered', False)
1813
        # We track the contents because we should be able to try, fail a
1814
        # particular kind and then ask for one that works and continue.
1815
        seen = set()
1816
        for factory in entries:
1817
            seen.add(factory.key)
1818
            self.assertValidStorageKind(factory.storage_kind)
3735.32.13 by John Arbash Meinel
Allow the sha1 to be None for a factory.
1819
            if factory.sha1 is not None:
1820
                self.assertEqual(files.get_sha1s([factory.key])[factory.key],
1821
                                 factory.sha1)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1822
            self.assertEqual(parent_map[factory.key], factory.parents)
1823
            # currently no stream emits mpdiff
1824
            self.assertRaises(errors.UnavailableRepresentation,
1825
                factory.get_bytes_as, 'mpdiff')
1826
            self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
1827
                str)
1828
        self.assertEqual(set(keys), seen)
1829
1830
    def test_get_record_stream_missing_records_are_absent(self):
1831
        files = self.get_versionedfiles()
1832
        self.get_diamond_files(files)
1833
        if self.key_length == 1:
1834
            keys = [('merged',), ('left',), ('right',), ('absent',), ('base',)]
1835
        else:
1836
            keys = [
1837
                ('FileA', 'merged'), ('FileA', 'left'), ('FileA', 'right'),
1838
                ('FileA', 'absent'), ('FileA', 'base'),
1839
                ('FileB', 'merged'), ('FileB', 'left'), ('FileB', 'right'),
1840
                ('FileB', 'absent'), ('FileB', 'base'),
1841
                ('absent', 'absent'),
1842
                ]
1843
        parent_map = files.get_parent_map(keys)
1844
        entries = files.get_record_stream(keys, 'unordered', False)
1845
        self.assertAbsentRecord(files, keys, parent_map, entries)
1846
        entries = files.get_record_stream(keys, 'topological', False)
1847
        self.assertAbsentRecord(files, keys, parent_map, entries)
1848
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
1849
    def assertRecordHasContent(self, record, bytes):
1850
        """Assert that record has the bytes bytes."""
1851
        self.assertEqual(bytes, record.get_bytes_as('fulltext'))
4005.3.7 by Robert Collins
Review feedback.
1852
        self.assertEqual(bytes, ''.join(record.get_bytes_as('chunked')))
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
1853
4005.3.2 by Robert Collins
First passing NetworkRecordStream test - a fulltext from any record type which isn't a chunked or fulltext can be serialised and deserialised successfully.
1854
    def test_get_record_stream_native_formats_are_wire_ready_one_ft(self):
1855
        files = self.get_versionedfiles()
1856
        key = self.get_simple_key('foo')
1857
        files.add_lines(key, (), ['my text\n', 'content'])
1858
        stream = files.get_record_stream([key], 'unordered', False)
1859
        record = stream.next()
1860
        if record.storage_kind in ('chunked', 'fulltext'):
1861
            # chunked and fulltext representations are for direct use not wire
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
1862
            # serialisation: check they are able to be used directly. To send
1863
            # such records over the wire translation will be needed.
1864
            self.assertRecordHasContent(record, "my text\ncontent")
4005.3.2 by Robert Collins
First passing NetworkRecordStream test - a fulltext from any record type which isn't a chunked or fulltext can be serialised and deserialised successfully.
1865
        else:
1866
            bytes = [record.get_bytes_as(record.storage_kind)]
1867
            network_stream = versionedfile.NetworkRecordStream(bytes).read()
1868
            source_record = record
1869
            records = []
1870
            for record in network_stream:
1871
                records.append(record)
1872
                self.assertEqual(source_record.storage_kind,
1873
                    record.storage_kind)
1874
                self.assertEqual(source_record.parents, record.parents)
1875
                self.assertEqual(
1876
                    source_record.get_bytes_as(source_record.storage_kind),
1877
                    record.get_bytes_as(record.storage_kind))
1878
            self.assertEqual(1, len(records))
1879
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
1880
    def assertStreamMetaEqual(self, records, expected, stream):
1881
        """Assert that streams expected and stream have the same records.
4032.1.2 by John Arbash Meinel
Track down a few more files that have trailing whitespace.
1882
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
1883
        :param records: A list to collect the seen records.
1884
        :return: A generator of the records in stream.
1885
        """
1886
        # We make assertions during copying to catch things early for
1887
        # easier debugging.
1888
        for record, ref_record in izip(stream, expected):
1889
            records.append(record)
1890
            self.assertEqual(ref_record.key, record.key)
1891
            self.assertEqual(ref_record.storage_kind, record.storage_kind)
1892
            self.assertEqual(ref_record.parents, record.parents)
1893
            yield record
1894
1895
    def stream_to_bytes_or_skip_counter(self, skipped_records, full_texts,
1896
        stream):
1897
        """Convert a stream to a bytes iterator.
1898
1899
        :param skipped_records: A list with one element to increment when a
1900
            record is skipped.
4032.1.2 by John Arbash Meinel
Track down a few more files that have trailing whitespace.
1901
        :param full_texts: A dict from key->fulltext representation, for
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
1902
            checking chunked or fulltext stored records.
1903
        :param stream: A record_stream.
1904
        :return: An iterator over the bytes of each record.
1905
        """
1906
        for record in stream:
1907
            if record.storage_kind in ('chunked', 'fulltext'):
1908
                skipped_records[0] += 1
1909
                # check the content is correct for direct use.
1910
                self.assertRecordHasContent(record, full_texts[record.key])
1911
            else:
1912
                yield record.get_bytes_as(record.storage_kind)
1913
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
1914
    def test_get_record_stream_native_formats_are_wire_ready_ft_delta(self):
1915
        files = self.get_versionedfiles()
1916
        target_files = self.get_versionedfiles('target')
1917
        key = self.get_simple_key('ft')
1918
        key_delta = self.get_simple_key('delta')
1919
        files.add_lines(key, (), ['my text\n', 'content'])
1920
        if self.graph:
1921
            delta_parents = (key,)
1922
        else:
1923
            delta_parents = ()
1924
        files.add_lines(key_delta, delta_parents, ['different\n', 'content\n'])
1925
        local = files.get_record_stream([key, key_delta], 'unordered', False)
1926
        ref = files.get_record_stream([key, key_delta], 'unordered', False)
1927
        skipped_records = [0]
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
1928
        full_texts = {
1929
            key: "my text\ncontent",
1930
            key_delta: "different\ncontent\n",
1931
            }
1932
        byte_stream = self.stream_to_bytes_or_skip_counter(
1933
            skipped_records, full_texts, local)
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
1934
        network_stream = versionedfile.NetworkRecordStream(byte_stream).read()
1935
        records = []
1936
        # insert the stream from the network into a versioned files object so we can
1937
        # check the content was carried across correctly without doing delta
1938
        # inspection.
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
1939
        target_files.insert_record_stream(
1940
            self.assertStreamMetaEqual(records, ref, network_stream))
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
1941
        # No duplicates on the wire thank you!
1942
        self.assertEqual(2, len(records) + skipped_records[0])
1943
        if len(records):
4005.3.4 by Robert Collins
Test copying just a delta over the wire.
1944
            # if any content was copied it all must have all been.
1945
            self.assertIdenticalVersionedFile(files, target_files)
1946
1947
    def test_get_record_stream_native_formats_are_wire_ready_delta(self):
1948
        # copy a delta over the wire
1949
        files = self.get_versionedfiles()
1950
        target_files = self.get_versionedfiles('target')
1951
        key = self.get_simple_key('ft')
1952
        key_delta = self.get_simple_key('delta')
1953
        files.add_lines(key, (), ['my text\n', 'content'])
1954
        if self.graph:
1955
            delta_parents = (key,)
1956
        else:
1957
            delta_parents = ()
1958
        files.add_lines(key_delta, delta_parents, ['different\n', 'content\n'])
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
1959
        # Copy the basis text across so we can reconstruct the delta during
1960
        # insertion into target.
4005.3.4 by Robert Collins
Test copying just a delta over the wire.
1961
        target_files.insert_record_stream(files.get_record_stream([key],
1962
            'unordered', False))
1963
        local = files.get_record_stream([key_delta], 'unordered', False)
1964
        ref = files.get_record_stream([key_delta], 'unordered', False)
1965
        skipped_records = [0]
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
1966
        full_texts = {
1967
            key_delta: "different\ncontent\n",
1968
            }
1969
        byte_stream = self.stream_to_bytes_or_skip_counter(
1970
            skipped_records, full_texts, local)
4005.3.4 by Robert Collins
Test copying just a delta over the wire.
1971
        network_stream = versionedfile.NetworkRecordStream(byte_stream).read()
1972
        records = []
1973
        # insert the stream from the network into a versioned files object so we can
1974
        # check the content was carried across correctly without doing delta
1975
        # inspection during check_stream.
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
1976
        target_files.insert_record_stream(
1977
            self.assertStreamMetaEqual(records, ref, network_stream))
4005.3.4 by Robert Collins
Test copying just a delta over the wire.
1978
        # No duplicates on the wire thank you!
1979
        self.assertEqual(1, len(records) + skipped_records[0])
1980
        if len(records):
1981
            # if any content was copied it all must have all been
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
1982
            self.assertIdenticalVersionedFile(files, target_files)
1983
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
1984
    def test_get_record_stream_wire_ready_delta_closure_included(self):
1985
        # copy a delta over the wire with the ability to get its full text.
1986
        files = self.get_versionedfiles()
1987
        key = self.get_simple_key('ft')
1988
        key_delta = self.get_simple_key('delta')
1989
        files.add_lines(key, (), ['my text\n', 'content'])
1990
        if self.graph:
1991
            delta_parents = (key,)
1992
        else:
1993
            delta_parents = ()
1994
        files.add_lines(key_delta, delta_parents, ['different\n', 'content\n'])
1995
        local = files.get_record_stream([key_delta], 'unordered', True)
1996
        ref = files.get_record_stream([key_delta], 'unordered', True)
1997
        skipped_records = [0]
1998
        full_texts = {
1999
            key_delta: "different\ncontent\n",
2000
            }
2001
        byte_stream = self.stream_to_bytes_or_skip_counter(
2002
            skipped_records, full_texts, local)
2003
        network_stream = versionedfile.NetworkRecordStream(byte_stream).read()
2004
        records = []
2005
        # insert the stream from the network into a versioned files object so we can
2006
        # check the content was carried across correctly without doing delta
2007
        # inspection during check_stream.
2008
        for record in self.assertStreamMetaEqual(records, ref, network_stream):
2009
            # we have to be able to get the full text out:
2010
            self.assertRecordHasContent(record, full_texts[record.key])
2011
        # No duplicates on the wire thank you!
2012
        self.assertEqual(1, len(records) + skipped_records[0])
2013
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2014
    def assertAbsentRecord(self, files, keys, parents, entries):
2015
        """Helper for test_get_record_stream_missing_records_are_absent."""
2016
        seen = set()
2017
        for factory in entries:
2018
            seen.add(factory.key)
2019
            if factory.key[-1] == 'absent':
2020
                self.assertEqual('absent', factory.storage_kind)
2021
                self.assertEqual(None, factory.sha1)
2022
                self.assertEqual(None, factory.parents)
2023
            else:
2024
                self.assertValidStorageKind(factory.storage_kind)
3735.32.13 by John Arbash Meinel
Allow the sha1 to be None for a factory.
2025
                if factory.sha1 is not None:
2026
                    sha1 = files.get_sha1s([factory.key])[factory.key]
2027
                    self.assertEqual(sha1, factory.sha1)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2028
                self.assertEqual(parents[factory.key], factory.parents)
2029
                self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
2030
                    str)
2031
        self.assertEqual(set(keys), seen)
2032
2033
    def test_filter_absent_records(self):
2034
        """Requested missing records can be filter trivially."""
2035
        files = self.get_versionedfiles()
2036
        self.get_diamond_files(files)
2037
        keys, _ = self.get_keys_and_sort_order()
2038
        parent_map = files.get_parent_map(keys)
2039
        # Add an absent record in the middle of the present keys. (We don't ask
2040
        # for just absent keys to ensure that content before and after the
2041
        # absent keys is still delivered).
2042
        present_keys = list(keys)
2043
        if self.key_length == 1:
2044
            keys.insert(2, ('extra',))
2045
        else:
2046
            keys.insert(2, ('extra', 'extra'))
2047
        entries = files.get_record_stream(keys, 'unordered', False)
2048
        seen = set()
2049
        self.capture_stream(files, versionedfile.filter_absent(entries), seen.add,
2050
            parent_map)
2051
        self.assertEqual(set(present_keys), seen)
2052
2053
    def get_mapper(self):
2054
        """Get a mapper suitable for the key length of the test interface."""
2055
        if self.key_length == 1:
2056
            return ConstantMapper('source')
2057
        else:
2058
            return HashEscapedPrefixMapper()
2059
2060
    def get_parents(self, parents):
2061
        """Get parents, taking self.graph into consideration."""
2062
        if self.graph:
2063
            return parents
2064
        else:
2065
            return None
2066
2067
    def test_get_parent_map(self):
2068
        files = self.get_versionedfiles()
2069
        if self.key_length == 1:
2070
            parent_details = [
2071
                (('r0',), self.get_parents(())),
2072
                (('r1',), self.get_parents((('r0',),))),
2073
                (('r2',), self.get_parents(())),
2074
                (('r3',), self.get_parents(())),
2075
                (('m',), self.get_parents((('r0',),('r1',),('r2',),('r3',)))),
2076
                ]
2077
        else:
2078
            parent_details = [
2079
                (('FileA', 'r0'), self.get_parents(())),
2080
                (('FileA', 'r1'), self.get_parents((('FileA', 'r0'),))),
2081
                (('FileA', 'r2'), self.get_parents(())),
2082
                (('FileA', 'r3'), self.get_parents(())),
2083
                (('FileA', 'm'), self.get_parents((('FileA', 'r0'),
2084
                    ('FileA', 'r1'), ('FileA', 'r2'), ('FileA', 'r3')))),
2085
                ]
2086
        for key, parents in parent_details:
2087
            files.add_lines(key, parents, [])
2088
            # immediately after adding it should be queryable.
2089
            self.assertEqual({key:parents}, files.get_parent_map([key]))
2090
        # We can ask for an empty set
2091
        self.assertEqual({}, files.get_parent_map([]))
2092
        # We can ask for many keys
2093
        all_parents = dict(parent_details)
2094
        self.assertEqual(all_parents, files.get_parent_map(all_parents.keys()))
2095
        # Absent keys are just not included in the result.
2096
        keys = all_parents.keys()
2097
        if self.key_length == 1:
2098
            keys.insert(1, ('missing',))
2099
        else:
2100
            keys.insert(1, ('missing', 'missing'))
2101
        # Absent keys are just ignored
2102
        self.assertEqual(all_parents, files.get_parent_map(keys))
2103
2104
    def test_get_sha1s(self):
2105
        files = self.get_versionedfiles()
2106
        self.get_diamond_files(files)
2107
        if self.key_length == 1:
2108
            keys = [('base',), ('origin',), ('left',), ('merged',), ('right',)]
2109
        else:
2110
            # ask for shas from different prefixes.
2111
            keys = [
2112
                ('FileA', 'base'), ('FileB', 'origin'), ('FileA', 'left'),
2113
                ('FileA', 'merged'), ('FileB', 'right'),
2114
                ]
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
2115
        self.assertEqual({
2116
            keys[0]: '51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44',
2117
            keys[1]: '00e364d235126be43292ab09cb4686cf703ddc17',
2118
            keys[2]: 'a8478686da38e370e32e42e8a0c220e33ee9132f',
2119
            keys[3]: 'ed8bce375198ea62444dc71952b22cfc2b09226d',
2120
            keys[4]: '9ef09dfa9d86780bdec9219a22560c6ece8e0ef1',
2121
            },
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2122
            files.get_sha1s(keys))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2123
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2124
    def test_insert_record_stream_empty(self):
2125
        """Inserting an empty record stream should work."""
2126
        files = self.get_versionedfiles()
2127
        files.insert_record_stream([])
2128
2129
    def assertIdenticalVersionedFile(self, expected, actual):
2130
        """Assert that left and right have the same contents."""
2131
        self.assertEqual(set(actual.keys()), set(expected.keys()))
2132
        actual_parents = actual.get_parent_map(actual.keys())
2133
        if self.graph:
2134
            self.assertEqual(actual_parents, expected.get_parent_map(expected.keys()))
2135
        else:
2136
            for key, parents in actual_parents.items():
2137
                self.assertEqual(None, parents)
2138
        for key in actual.keys():
2139
            actual_text = actual.get_record_stream(
2140
                [key], 'unordered', True).next().get_bytes_as('fulltext')
2141
            expected_text = expected.get_record_stream(
2142
                [key], 'unordered', True).next().get_bytes_as('fulltext')
2143
            self.assertEqual(actual_text, expected_text)
2144
2145
    def test_insert_record_stream_fulltexts(self):
2146
        """Any file should accept a stream of fulltexts."""
2147
        files = self.get_versionedfiles()
2148
        mapper = self.get_mapper()
2149
        source_transport = self.get_transport('source')
2150
        source_transport.mkdir('.')
2151
        # weaves always output fulltexts.
2152
        source = make_versioned_files_factory(WeaveFile, mapper)(
2153
            source_transport)
2154
        self.get_diamond_files(source, trailing_eol=False)
2155
        stream = source.get_record_stream(source.keys(), 'topological',
2156
            False)
2157
        files.insert_record_stream(stream)
2158
        self.assertIdenticalVersionedFile(source, files)
2159
2160
    def test_insert_record_stream_fulltexts_noeol(self):
2161
        """Any file should accept a stream of fulltexts."""
2162
        files = self.get_versionedfiles()
2163
        mapper = self.get_mapper()
2164
        source_transport = self.get_transport('source')
2165
        source_transport.mkdir('.')
2166
        # weaves always output fulltexts.
2167
        source = make_versioned_files_factory(WeaveFile, mapper)(
2168
            source_transport)
2169
        self.get_diamond_files(source, trailing_eol=False)
2170
        stream = source.get_record_stream(source.keys(), 'topological',
2171
            False)
2172
        files.insert_record_stream(stream)
2173
        self.assertIdenticalVersionedFile(source, files)
2174
2175
    def test_insert_record_stream_annotated_knits(self):
2176
        """Any file should accept a stream from plain knits."""
2177
        files = self.get_versionedfiles()
2178
        mapper = self.get_mapper()
2179
        source_transport = self.get_transport('source')
2180
        source_transport.mkdir('.')
2181
        source = make_file_factory(True, mapper)(source_transport)
2182
        self.get_diamond_files(source)
2183
        stream = source.get_record_stream(source.keys(), 'topological',
2184
            False)
2185
        files.insert_record_stream(stream)
2186
        self.assertIdenticalVersionedFile(source, files)
2187
2188
    def test_insert_record_stream_annotated_knits_noeol(self):
2189
        """Any file should accept a stream from plain knits."""
2190
        files = self.get_versionedfiles()
2191
        mapper = self.get_mapper()
2192
        source_transport = self.get_transport('source')
2193
        source_transport.mkdir('.')
2194
        source = make_file_factory(True, mapper)(source_transport)
2195
        self.get_diamond_files(source, trailing_eol=False)
2196
        stream = source.get_record_stream(source.keys(), 'topological',
2197
            False)
2198
        files.insert_record_stream(stream)
2199
        self.assertIdenticalVersionedFile(source, files)
2200
2201
    def test_insert_record_stream_plain_knits(self):
2202
        """Any file should accept a stream from plain knits."""
2203
        files = self.get_versionedfiles()
2204
        mapper = self.get_mapper()
2205
        source_transport = self.get_transport('source')
2206
        source_transport.mkdir('.')
2207
        source = make_file_factory(False, mapper)(source_transport)
2208
        self.get_diamond_files(source)
2209
        stream = source.get_record_stream(source.keys(), 'topological',
2210
            False)
2211
        files.insert_record_stream(stream)
2212
        self.assertIdenticalVersionedFile(source, files)
2213
2214
    def test_insert_record_stream_plain_knits_noeol(self):
2215
        """Any file should accept a stream from plain knits."""
2216
        files = self.get_versionedfiles()
2217
        mapper = self.get_mapper()
2218
        source_transport = self.get_transport('source')
2219
        source_transport.mkdir('.')
2220
        source = make_file_factory(False, mapper)(source_transport)
2221
        self.get_diamond_files(source, trailing_eol=False)
2222
        stream = source.get_record_stream(source.keys(), 'topological',
2223
            False)
2224
        files.insert_record_stream(stream)
2225
        self.assertIdenticalVersionedFile(source, files)
2226
2227
    def test_insert_record_stream_existing_keys(self):
2228
        """Inserting keys already in a file should not error."""
2229
        files = self.get_versionedfiles()
2230
        source = self.get_versionedfiles('source')
2231
        self.get_diamond_files(source)
2232
        # insert some keys into f.
2233
        self.get_diamond_files(files, left_only=True)
2234
        stream = source.get_record_stream(source.keys(), 'topological',
2235
            False)
2236
        files.insert_record_stream(stream)
2237
        self.assertIdenticalVersionedFile(source, files)
2238
2239
    def test_insert_record_stream_missing_keys(self):
2240
        """Inserting a stream with absent keys should raise an error."""
2241
        files = self.get_versionedfiles()
2242
        source = self.get_versionedfiles('source')
2243
        stream = source.get_record_stream([('missing',) * self.key_length],
2244
            'topological', False)
2245
        self.assertRaises(errors.RevisionNotPresent, files.insert_record_stream,
2246
            stream)
2247
2248
    def test_insert_record_stream_out_of_order(self):
2249
        """An out of order stream can either error or work."""
2250
        files = self.get_versionedfiles()
2251
        source = self.get_versionedfiles('source')
2252
        self.get_diamond_files(source)
2253
        if self.key_length == 1:
2254
            origin_keys = [('origin',)]
2255
            end_keys = [('merged',), ('left',)]
2256
            start_keys = [('right',), ('base',)]
2257
        else:
2258
            origin_keys = [('FileA', 'origin'), ('FileB', 'origin')]
2259
            end_keys = [('FileA', 'merged',), ('FileA', 'left',),
2260
                ('FileB', 'merged',), ('FileB', 'left',)]
2261
            start_keys = [('FileA', 'right',), ('FileA', 'base',),
2262
                ('FileB', 'right',), ('FileB', 'base',)]
2263
        origin_entries = source.get_record_stream(origin_keys, 'unordered', False)
2264
        end_entries = source.get_record_stream(end_keys, 'topological', False)
2265
        start_entries = source.get_record_stream(start_keys, 'topological', False)
2266
        entries = chain(origin_entries, end_entries, start_entries)
2267
        try:
2268
            files.insert_record_stream(entries)
2269
        except RevisionNotPresent:
2270
            # Must not have corrupted the file.
2271
            files.check()
2272
        else:
2273
            self.assertIdenticalVersionedFile(source, files)
2274
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
2275
    def get_knit_delta_source(self):
2276
        """Get a source that can produce a stream with knit delta records,
2277
        regardless of this test's scenario.
2278
        """
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2279
        mapper = self.get_mapper()
2280
        source_transport = self.get_transport('source')
2281
        source_transport.mkdir('.')
2282
        source = make_file_factory(False, mapper)(source_transport)
4009.3.1 by Andrew Bennetts
Fix test_insert_record_stream_delta_missing_basis_no_corruption to test what it claims to, and fix KnitVersionedFiles.get_record_stream to match the expected exception.
2283
        get_diamond_files(source, self.key_length, trailing_eol=True,
2284
            nograph=False, left_only=False)
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
2285
        return source
2286
2287
    def test_insert_record_stream_delta_missing_basis_no_corruption(self):
2288
        """Insertion where a needed basis is not included notifies the caller
2289
        of the missing basis.  In the meantime a record missing its basis is
2290
        not added.
2291
        """
2292
        source = self.get_knit_delta_source()
4009.3.7 by Andrew Bennetts
Most tests passing.
2293
        keys = [self.get_simple_key('origin'), self.get_simple_key('merged')]
2294
        entries = source.get_record_stream(keys, 'unordered', False)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2295
        files = self.get_versionedfiles()
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
2296
        if self.support_partial_insertion:
4009.3.12 by Robert Collins
Polish on inserting record streams with missing compression parents.
2297
            self.assertEqual([],
2298
                list(files.get_missing_compression_parent_keys()))
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
2299
            files.insert_record_stream(entries)
2300
            missing_bases = files.get_missing_compression_parent_keys()
2301
            self.assertEqual(set([self.get_simple_key('left')]),
2302
                set(missing_bases))
4009.3.7 by Andrew Bennetts
Most tests passing.
2303
            self.assertEqual(set(keys), set(files.get_parent_map(keys)))
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
2304
        else:
2305
            self.assertRaises(
2306
                errors.RevisionNotPresent, files.insert_record_stream, entries)
4009.3.7 by Andrew Bennetts
Most tests passing.
2307
            files.check()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2308
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
2309
    def test_insert_record_stream_delta_missing_basis_can_be_added_later(self):
2310
        """Insertion where a needed basis is not included notifies the caller
2311
        of the missing basis.  That basis can be added in a second
2312
        insert_record_stream call that does not need to repeat records present
4009.3.3 by Andrew Bennetts
Add docstrings.
2313
        in the previous stream.  The record(s) that required that basis are
2314
        fully inserted once their basis is no longer missing.
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
2315
        """
2316
        if not self.support_partial_insertion:
2317
            raise TestNotApplicable(
2318
                'versioned file scenario does not support partial insertion')
2319
        source = self.get_knit_delta_source()
2320
        entries = source.get_record_stream([self.get_simple_key('origin'),
2321
            self.get_simple_key('merged')], 'unordered', False)
2322
        files = self.get_versionedfiles()
2323
        files.insert_record_stream(entries)
2324
        missing_bases = files.get_missing_compression_parent_keys()
2325
        self.assertEqual(set([self.get_simple_key('left')]),
2326
            set(missing_bases))
4009.3.7 by Andrew Bennetts
Most tests passing.
2327
        # 'merged' is inserted (although a commit of a write group involving
2328
        # this versionedfiles would fail).
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
2329
        merged_key = self.get_simple_key('merged')
4009.3.7 by Andrew Bennetts
Most tests passing.
2330
        self.assertEqual(
2331
            [merged_key], files.get_parent_map([merged_key]).keys())
2332
        # Add the full delta closure of the missing records
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
2333
        missing_entries = source.get_record_stream(
4009.3.7 by Andrew Bennetts
Most tests passing.
2334
            missing_bases, 'unordered', True)
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
2335
        files.insert_record_stream(missing_entries)
4009.3.7 by Andrew Bennetts
Most tests passing.
2336
        # Now 'merged' is fully inserted (and a commit would succeed).
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
2337
        self.assertEqual([], list(files.get_missing_compression_parent_keys()))
2338
        self.assertEqual(
2339
            [merged_key], files.get_parent_map([merged_key]).keys())
4009.3.7 by Andrew Bennetts
Most tests passing.
2340
        files.check()
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
2341
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2342
    def test_iter_lines_added_or_present_in_keys(self):
2343
        # test that we get at least an equalset of the lines added by
2344
        # versions in the store.
2345
        # the ordering here is to make a tree so that dumb searches have
2346
        # more changes to muck up.
2347
2348
        class InstrumentedProgress(progress.DummyProgress):
2349
2350
            def __init__(self):
2351
2352
                progress.DummyProgress.__init__(self)
2353
                self.updates = []
2354
2355
            def update(self, msg=None, current=None, total=None):
2356
                self.updates.append((msg, current, total))
2357
2358
        files = self.get_versionedfiles()
2359
        # add a base to get included
2360
        files.add_lines(self.get_simple_key('base'), (), ['base\n'])
2361
        # add a ancestor to be included on one side
2362
        files.add_lines(self.get_simple_key('lancestor'), (), ['lancestor\n'])
2363
        # add a ancestor to be included on the other side
2364
        files.add_lines(self.get_simple_key('rancestor'),
2365
            self.get_parents([self.get_simple_key('base')]), ['rancestor\n'])
2366
        # add a child of rancestor with no eofile-nl
2367
        files.add_lines(self.get_simple_key('child'),
2368
            self.get_parents([self.get_simple_key('rancestor')]),
2369
            ['base\n', 'child\n'])
2370
        # add a child of lancestor and base to join the two roots
2371
        files.add_lines(self.get_simple_key('otherchild'),
2372
            self.get_parents([self.get_simple_key('lancestor'),
2373
                self.get_simple_key('base')]),
2374
            ['base\n', 'lancestor\n', 'otherchild\n'])
2375
        def iter_with_keys(keys, expected):
2376
            # now we need to see what lines are returned, and how often.
2377
            lines = {}
2378
            progress = InstrumentedProgress()
2379
            # iterate over the lines
2380
            for line in files.iter_lines_added_or_present_in_keys(keys,
2381
                pb=progress):
2382
                lines.setdefault(line, 0)
2383
                lines[line] += 1
2384
            if []!= progress.updates:
2385
                self.assertEqual(expected, progress.updates)
2386
            return lines
2387
        lines = iter_with_keys(
2388
            [self.get_simple_key('child'), self.get_simple_key('otherchild')],
4103.3.4 by Martin Pool
Update test that depends on progress bar strings
2389
            [('Walking content', 0, 2),
2390
             ('Walking content', 1, 2),
2391
             ('Walking content', 2, 2)])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2392
        # we must see child and otherchild
2393
        self.assertTrue(lines[('child\n', self.get_simple_key('child'))] > 0)
2394
        self.assertTrue(
2395
            lines[('otherchild\n', self.get_simple_key('otherchild'))] > 0)
2396
        # we dont care if we got more than that.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2397
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2398
        # test all lines
2399
        lines = iter_with_keys(files.keys(),
4103.3.4 by Martin Pool
Update test that depends on progress bar strings
2400
            [('Walking content', 0, 5),
2401
             ('Walking content', 1, 5),
2402
             ('Walking content', 2, 5),
2403
             ('Walking content', 3, 5),
2404
             ('Walking content', 4, 5),
2405
             ('Walking content', 5, 5)])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2406
        # all lines must be seen at least once
2407
        self.assertTrue(lines[('base\n', self.get_simple_key('base'))] > 0)
2408
        self.assertTrue(
2409
            lines[('lancestor\n', self.get_simple_key('lancestor'))] > 0)
2410
        self.assertTrue(
2411
            lines[('rancestor\n', self.get_simple_key('rancestor'))] > 0)
2412
        self.assertTrue(lines[('child\n', self.get_simple_key('child'))] > 0)
2413
        self.assertTrue(
2414
            lines[('otherchild\n', self.get_simple_key('otherchild'))] > 0)
2415
2416
    def test_make_mpdiffs(self):
2417
        from bzrlib import multiparent
2418
        files = self.get_versionedfiles('source')
2419
        # add texts that should trip the knit maximum delta chain threshold
2420
        # as well as doing parallel chains of data in knits.
2421
        # this is done by two chains of 25 insertions
2422
        files.add_lines(self.get_simple_key('base'), [], ['line\n'])
2423
        files.add_lines(self.get_simple_key('noeol'),
2424
            self.get_parents([self.get_simple_key('base')]), ['line'])
2425
        # detailed eol tests:
2426
        # shared last line with parent no-eol
2427
        files.add_lines(self.get_simple_key('noeolsecond'),
2428
            self.get_parents([self.get_simple_key('noeol')]),
2429
                ['line\n', 'line'])
2430
        # differing last line with parent, both no-eol
2431
        files.add_lines(self.get_simple_key('noeolnotshared'),
2432
            self.get_parents([self.get_simple_key('noeolsecond')]),
2433
                ['line\n', 'phone'])
2434
        # add eol following a noneol parent, change content
2435
        files.add_lines(self.get_simple_key('eol'),
2436
            self.get_parents([self.get_simple_key('noeol')]), ['phone\n'])
2437
        # add eol following a noneol parent, no change content
2438
        files.add_lines(self.get_simple_key('eolline'),
2439
            self.get_parents([self.get_simple_key('noeol')]), ['line\n'])
2440
        # noeol with no parents:
2441
        files.add_lines(self.get_simple_key('noeolbase'), [], ['line'])
2442
        # noeol preceeding its leftmost parent in the output:
2443
        # this is done by making it a merge of two parents with no common
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2444
        # anestry: noeolbase and noeol with the
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2445
        # later-inserted parent the leftmost.
2446
        files.add_lines(self.get_simple_key('eolbeforefirstparent'),
2447
            self.get_parents([self.get_simple_key('noeolbase'),
2448
                self.get_simple_key('noeol')]),
2449
            ['line'])
2450
        # two identical eol texts
2451
        files.add_lines(self.get_simple_key('noeoldup'),
2452
            self.get_parents([self.get_simple_key('noeol')]), ['line'])
2453
        next_parent = self.get_simple_key('base')
2454
        text_name = 'chain1-'
2455
        text = ['line\n']
2456
        sha1s = {0 :'da6d3141cb4a5e6f464bf6e0518042ddc7bfd079',
2457
                 1 :'45e21ea146a81ea44a821737acdb4f9791c8abe7',
2458
                 2 :'e1f11570edf3e2a070052366c582837a4fe4e9fa',
2459
                 3 :'26b4b8626da827088c514b8f9bbe4ebf181edda1',
2460
                 4 :'e28a5510be25ba84d31121cff00956f9970ae6f6',
2461
                 5 :'d63ec0ce22e11dcf65a931b69255d3ac747a318d',
2462
                 6 :'2c2888d288cb5e1d98009d822fedfe6019c6a4ea',
2463
                 7 :'95c14da9cafbf828e3e74a6f016d87926ba234ab',
2464
                 8 :'779e9a0b28f9f832528d4b21e17e168c67697272',
2465
                 9 :'1f8ff4e5c6ff78ac106fcfe6b1e8cb8740ff9a8f',
2466
                 10:'131a2ae712cf51ed62f143e3fbac3d4206c25a05',
2467
                 11:'c5a9d6f520d2515e1ec401a8f8a67e6c3c89f199',
2468
                 12:'31a2286267f24d8bedaa43355f8ad7129509ea85',
2469
                 13:'dc2a7fe80e8ec5cae920973973a8ee28b2da5e0a',
2470
                 14:'2c4b1736566b8ca6051e668de68650686a3922f2',
2471
                 15:'5912e4ecd9b0c07be4d013e7e2bdcf9323276cde',
2472
                 16:'b0d2e18d3559a00580f6b49804c23fea500feab3',
2473
                 17:'8e1d43ad72f7562d7cb8f57ee584e20eb1a69fc7',
2474
                 18:'5cf64a3459ae28efa60239e44b20312d25b253f3',
2475
                 19:'1ebed371807ba5935958ad0884595126e8c4e823',
2476
                 20:'2aa62a8b06fb3b3b892a3292a068ade69d5ee0d3',
2477
                 21:'01edc447978004f6e4e962b417a4ae1955b6fe5d',
2478
                 22:'d8d8dc49c4bf0bab401e0298bb5ad827768618bb',
2479
                 23:'c21f62b1c482862983a8ffb2b0c64b3451876e3f',
2480
                 24:'c0593fe795e00dff6b3c0fe857a074364d5f04fc',
2481
                 25:'dd1a1cf2ba9cc225c3aff729953e6364bf1d1855',
2482
                 }
2483
        for depth in range(26):
2484
            new_version = self.get_simple_key(text_name + '%s' % depth)
2485
            text = text + ['line\n']
2486
            files.add_lines(new_version, self.get_parents([next_parent]), text)
2487
            next_parent = new_version
2488
        next_parent = self.get_simple_key('base')
2489
        text_name = 'chain2-'
2490
        text = ['line\n']
2491
        for depth in range(26):
2492
            new_version = self.get_simple_key(text_name + '%s' % depth)
2493
            text = text + ['line\n']
2494
            files.add_lines(new_version, self.get_parents([next_parent]), text)
2495
            next_parent = new_version
2496
        target = self.get_versionedfiles('target')
2497
        for key in multiparent.topo_iter_keys(files, files.keys()):
2498
            mpdiff = files.make_mpdiffs([key])[0]
2499
            parents = files.get_parent_map([key])[key] or []
2500
            target.add_mpdiffs(
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
2501
                [(key, parents, files.get_sha1s([key])[key], mpdiff)])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2502
            self.assertEqualDiff(
2503
                files.get_record_stream([key], 'unordered',
2504
                    True).next().get_bytes_as('fulltext'),
2505
                target.get_record_stream([key], 'unordered',
2506
                    True).next().get_bytes_as('fulltext')
2507
                )
2508
2509
    def test_keys(self):
2510
        # While use is discouraged, versions() is still needed by aspects of
2511
        # bzr.
2512
        files = self.get_versionedfiles()
2513
        self.assertEqual(set(), set(files.keys()))
2514
        if self.key_length == 1:
2515
            key = ('foo',)
2516
        else:
2517
            key = ('foo', 'bar',)
2518
        files.add_lines(key, (), [])
2519
        self.assertEqual(set([key]), set(files.keys()))
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
2520
2521
2522
class VirtualVersionedFilesTests(TestCase):
2523
    """Basic tests for the VirtualVersionedFiles implementations."""
2524
2525
    def _get_parent_map(self, keys):
2526
        ret = {}
2527
        for k in keys:
2528
            if k in self._parent_map:
2529
                ret[k] = self._parent_map[k]
2530
        return ret
2531
2532
    def setUp(self):
2533
        TestCase.setUp(self)
2534
        self._lines = {}
2535
        self._parent_map = {}
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2536
        self.texts = VirtualVersionedFiles(self._get_parent_map,
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
2537
                                           self._lines.get)
2538
2539
    def test_add_lines(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2540
        self.assertRaises(NotImplementedError,
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
2541
                self.texts.add_lines, "foo", [], [])
2542
2543
    def test_add_mpdiffs(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2544
        self.assertRaises(NotImplementedError,
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
2545
                self.texts.add_mpdiffs, [])
2546
2547
    def test_check(self):
2548
        self.assertTrue(self.texts.check())
2549
2550
    def test_insert_record_stream(self):
2551
        self.assertRaises(NotImplementedError, self.texts.insert_record_stream,
2552
                          [])
2553
3518.1.2 by Jelmer Vernooij
Fix some stylistic issues pointed out by Ian.
2554
    def test_get_sha1s_nonexistent(self):
2555
        self.assertEquals({}, self.texts.get_sha1s([("NONEXISTENT",)]))
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
2556
2557
    def test_get_sha1s(self):
2558
        self._lines["key"] = ["dataline1", "dataline2"]
2559
        self.assertEquals({("key",): osutils.sha_strings(self._lines["key"])},
2560
                           self.texts.get_sha1s([("key",)]))
2561
2562
    def test_get_parent_map(self):
2563
        self._parent_map = {"G": ("A", "B")}
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2564
        self.assertEquals({("G",): (("A",),("B",))},
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
2565
                          self.texts.get_parent_map([("G",), ("L",)]))
2566
2567
    def test_get_record_stream(self):
2568
        self._lines["A"] = ["FOO", "BAR"]
2569
        it = self.texts.get_record_stream([("A",)], "unordered", True)
2570
        record = it.next()
3890.2.2 by John Arbash Meinel
Change the signature to report the storage kind as 'chunked'
2571
        self.assertEquals("chunked", record.storage_kind)
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
2572
        self.assertEquals("FOOBAR", record.get_bytes_as("fulltext"))
3890.2.2 by John Arbash Meinel
Change the signature to report the storage kind as 'chunked'
2573
        self.assertEquals(["FOO", "BAR"], record.get_bytes_as("chunked"))
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
2574
2575
    def test_get_record_stream_absent(self):
2576
        it = self.texts.get_record_stream([("A",)], "unordered", True)
2577
        record = it.next()
2578
        self.assertEquals("absent", record.storage_kind)
2579
3949.4.1 by Jelmer Vernooij
Implement VirtualVersionedFiles.iter_lines_added_or_present_in_keys.
2580
    def test_iter_lines_added_or_present_in_keys(self):
2581
        self._lines["A"] = ["FOO", "BAR"]
2582
        self._lines["B"] = ["HEY"]
2583
        self._lines["C"] = ["Alberta"]
2584
        it = self.texts.iter_lines_added_or_present_in_keys([("A",), ("B",)])
4032.1.2 by John Arbash Meinel
Track down a few more files that have trailing whitespace.
2585
        self.assertEquals(sorted([("FOO", "A"), ("BAR", "A"), ("HEY", "B")]),
3949.4.1 by Jelmer Vernooij
Implement VirtualVersionedFiles.iter_lines_added_or_present_in_keys.
2586
            sorted(list(it)))
2587
3871.4.1 by John Arbash Meinel
Add a VFDecorator that can yield records in a specified order
2588
2589
class TestOrderingVersionedFilesDecorator(TestCaseWithMemoryTransport):
2590
2591
    def get_ordering_vf(self, key_priority):
2592
        builder = self.make_branch_builder('test')
2593
        builder.start_series()
2594
        builder.build_snapshot('A', None, [
2595
            ('add', ('', 'TREE_ROOT', 'directory', None))])
2596
        builder.build_snapshot('B', ['A'], [])
2597
        builder.build_snapshot('C', ['B'], [])
2598
        builder.build_snapshot('D', ['C'], [])
2599
        builder.finish_series()
2600
        b = builder.get_branch()
2601
        b.lock_read()
2602
        self.addCleanup(b.unlock)
2603
        vf = b.repository.inventories
2604
        return versionedfile.OrderingVersionedFilesDecorator(vf, key_priority)
2605
2606
    def test_get_empty(self):
2607
        vf = self.get_ordering_vf({})
2608
        self.assertEqual([], vf.calls)
2609
2610
    def test_get_record_stream_topological(self):
2611
        vf = self.get_ordering_vf({('A',): 3, ('B',): 2, ('C',): 4, ('D',): 1})
2612
        request_keys = [('B',), ('C',), ('D',), ('A',)]
2613
        keys = [r.key for r in vf.get_record_stream(request_keys,
2614
                                    'topological', False)]
2615
        # We should have gotten the keys in topological order
2616
        self.assertEqual([('A',), ('B',), ('C',), ('D',)], keys)
2617
        # And recorded that the request was made
2618
        self.assertEqual([('get_record_stream', request_keys, 'topological',
2619
                           False)], vf.calls)
2620
2621
    def test_get_record_stream_ordered(self):
2622
        vf = self.get_ordering_vf({('A',): 3, ('B',): 2, ('C',): 4, ('D',): 1})
2623
        request_keys = [('B',), ('C',), ('D',), ('A',)]
2624
        keys = [r.key for r in vf.get_record_stream(request_keys,
2625
                                   'unordered', False)]
2626
        # They should be returned based on their priority
2627
        self.assertEqual([('D',), ('B',), ('A',), ('C',)], keys)
2628
        # And the request recorded
2629
        self.assertEqual([('get_record_stream', request_keys, 'unordered',
2630
                           False)], vf.calls)
2631
2632
    def test_get_record_stream_implicit_order(self):
2633
        vf = self.get_ordering_vf({('B',): 2, ('D',): 1})
2634
        request_keys = [('B',), ('C',), ('D',), ('A',)]
2635
        keys = [r.key for r in vf.get_record_stream(request_keys,
2636
                                   'unordered', False)]
2637
        # A and C are not in the map, so they get sorted to the front. A comes
2638
        # before C alphabetically, so it comes back first
2639
        self.assertEqual([('A',), ('C',), ('D',), ('B',)], keys)
2640
        # And the request recorded
2641
        self.assertEqual([('get_record_stream', request_keys, 'unordered',
2642
                           False)], vf.calls)