/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to breezy/tests/test_index.py

  • Committer: Jelmer Vernooij
  • Date: 2017-07-30 23:53:38 UTC
  • mfrom: (6734.1.22 move-errors-more)
  • Revision ID: jelmer@jelmer.uk-20170730235338-25jy0lgifkyx796l
Merge lp:~jelmer/brz/move-errors-more.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
    transport,
23
23
    )
24
24
from ..bzr import (
25
 
    index,
 
25
    index as _mod_index,
26
26
    )
27
27
 
28
28
 
 
29
class ErrorTests(tests.TestCase):
 
30
 
 
31
    def test_bad_index_format_signature(self):
 
32
        error = _mod_index.BadIndexFormatSignature("foo", "bar")
 
33
        self.assertEqual("foo is not an index of type bar.",
 
34
            str(error))
 
35
 
 
36
    def test_bad_index_data(self):
 
37
        error = _mod_index.BadIndexData("foo")
 
38
        self.assertEqual("Error in data for index foo.",
 
39
            str(error))
 
40
 
 
41
    def test_bad_index_duplicate_key(self):
 
42
        error = _mod_index.BadIndexDuplicateKey("foo", "bar")
 
43
        self.assertEqual("The key 'foo' is already in index 'bar'.",
 
44
            str(error))
 
45
 
 
46
    def test_bad_index_key(self):
 
47
        error = _mod_index.BadIndexKey("foo")
 
48
        self.assertEqual("The key 'foo' is not a valid key.",
 
49
            str(error))
 
50
 
 
51
    def test_bad_index_options(self):
 
52
        error = _mod_index.BadIndexOptions("foo")
 
53
        self.assertEqual("Could not parse options for index foo.",
 
54
            str(error))
 
55
 
 
56
    def test_bad_index_value(self):
 
57
        error = _mod_index.BadIndexValue("foo")
 
58
        self.assertEqual("The value 'foo' is not a valid value.",
 
59
            str(error))
 
60
 
 
61
 
29
62
class TestGraphIndexBuilder(tests.TestCaseWithMemoryTransport):
30
63
 
31
64
    def test_build_index_empty(self):
32
 
        builder = index.GraphIndexBuilder()
 
65
        builder = _mod_index.GraphIndexBuilder()
33
66
        stream = builder.finish()
34
67
        contents = stream.read()
35
68
        self.assertEqual(
37
70
            contents)
38
71
 
39
72
    def test_build_index_empty_two_element_keys(self):
40
 
        builder = index.GraphIndexBuilder(key_elements=2)
 
73
        builder = _mod_index.GraphIndexBuilder(key_elements=2)
41
74
        stream = builder.finish()
42
75
        contents = stream.read()
43
76
        self.assertEqual(
45
78
            contents)
46
79
 
47
80
    def test_build_index_one_reference_list_empty(self):
48
 
        builder = index.GraphIndexBuilder(reference_lists=1)
 
81
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
49
82
        stream = builder.finish()
50
83
        contents = stream.read()
51
84
        self.assertEqual(
53
86
            contents)
54
87
 
55
88
    def test_build_index_two_reference_list_empty(self):
56
 
        builder = index.GraphIndexBuilder(reference_lists=2)
 
89
        builder = _mod_index.GraphIndexBuilder(reference_lists=2)
57
90
        stream = builder.finish()
58
91
        contents = stream.read()
59
92
        self.assertEqual(
61
94
            contents)
62
95
 
63
96
    def test_build_index_one_node_no_refs(self):
64
 
        builder = index.GraphIndexBuilder()
 
97
        builder = _mod_index.GraphIndexBuilder()
65
98
        builder.add_node(('akey', ), 'data')
66
99
        stream = builder.finish()
67
100
        contents = stream.read()
70
103
            "akey\x00\x00\x00data\n\n", contents)
71
104
 
72
105
    def test_build_index_one_node_no_refs_accepts_empty_reflist(self):
73
 
        builder = index.GraphIndexBuilder()
 
106
        builder = _mod_index.GraphIndexBuilder()
74
107
        builder.add_node(('akey', ), 'data', ())
75
108
        stream = builder.finish()
76
109
        contents = stream.read()
82
115
        # multipart keys are separated by \x00 - because they are fixed length,
83
116
        # not variable this does not cause any issues, and seems clearer to the
84
117
        # author.
85
 
        builder = index.GraphIndexBuilder(key_elements=2)
 
118
        builder = _mod_index.GraphIndexBuilder(key_elements=2)
86
119
        builder.add_node(('akey', 'secondpart'), 'data')
87
120
        stream = builder.finish()
88
121
        contents = stream.read()
91
124
            "akey\x00secondpart\x00\x00\x00data\n\n", contents)
92
125
 
93
126
    def test_add_node_empty_value(self):
94
 
        builder = index.GraphIndexBuilder()
 
127
        builder = _mod_index.GraphIndexBuilder()
95
128
        builder.add_node(('akey', ), '')
96
129
        stream = builder.finish()
97
130
        contents = stream.read()
101
134
 
102
135
    def test_build_index_nodes_sorted(self):
103
136
        # the highest sorted node comes first.
104
 
        builder = index.GraphIndexBuilder()
 
137
        builder = _mod_index.GraphIndexBuilder()
105
138
        # use three to have a good chance of glitching dictionary hash
106
139
        # lookups etc. Insert in randomish order that is not correct
107
140
        # and not the reverse of the correct order.
119
152
 
120
153
    def test_build_index_2_element_key_nodes_sorted(self):
121
154
        # multiple element keys are sorted first-key, second-key.
122
 
        builder = index.GraphIndexBuilder(key_elements=2)
 
155
        builder = _mod_index.GraphIndexBuilder(key_elements=2)
123
156
        # use three values of each key element, to have a good chance of
124
157
        # glitching dictionary hash lookups etc. Insert in randomish order that
125
158
        # is not correct and not the reverse of the correct order.
148
181
            "\n", contents)
149
182
 
150
183
    def test_build_index_reference_lists_are_included_one(self):
151
 
        builder = index.GraphIndexBuilder(reference_lists=1)
 
184
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
152
185
        builder.add_node(('key', ), 'data', ([], ))
153
186
        stream = builder.finish()
154
187
        contents = stream.read()
158
191
            "\n", contents)
159
192
 
160
193
    def test_build_index_reference_lists_with_2_element_keys(self):
161
 
        builder = index.GraphIndexBuilder(reference_lists=1, key_elements=2)
 
194
        builder = _mod_index.GraphIndexBuilder(reference_lists=1, key_elements=2)
162
195
        builder.add_node(('key', 'key2'), 'data', ([], ))
163
196
        stream = builder.finish()
164
197
        contents = stream.read()
168
201
            "\n", contents)
169
202
 
170
203
    def test_build_index_reference_lists_are_included_two(self):
171
 
        builder = index.GraphIndexBuilder(reference_lists=2)
 
204
        builder = _mod_index.GraphIndexBuilder(reference_lists=2)
172
205
        builder.add_node(('key', ), 'data', ([], []))
173
206
        stream = builder.finish()
174
207
        contents = stream.read()
178
211
            "\n", contents)
179
212
 
180
213
    def test_clear_cache(self):
181
 
        builder = index.GraphIndexBuilder(reference_lists=2)
 
214
        builder = _mod_index.GraphIndexBuilder(reference_lists=2)
182
215
        # This is a no-op, but the api should exist
183
216
        builder.clear_cache()
184
217
 
185
218
    def test_node_references_are_byte_offsets(self):
186
 
        builder = index.GraphIndexBuilder(reference_lists=1)
 
219
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
187
220
        builder.add_node(('reference', ), 'data', ([], ))
188
221
        builder.add_node(('key', ), 'data', ([('reference', )], ))
189
222
        stream = builder.finish()
195
228
            "\n", contents)
196
229
 
197
230
    def test_node_references_are_cr_delimited(self):
198
 
        builder = index.GraphIndexBuilder(reference_lists=1)
 
231
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
199
232
        builder.add_node(('reference', ), 'data', ([], ))
200
233
        builder.add_node(('reference2', ), 'data', ([], ))
201
234
        builder.add_node(('key', ), 'data',
210
243
            "\n", contents)
211
244
 
212
245
    def test_multiple_reference_lists_are_tab_delimited(self):
213
 
        builder = index.GraphIndexBuilder(reference_lists=2)
 
246
        builder = _mod_index.GraphIndexBuilder(reference_lists=2)
214
247
        builder.add_node(('keference', ), 'data', ([], []))
215
248
        builder.add_node(('rey', ), 'data',
216
249
                         ([('keference', )], [('keference', )]))
223
256
            "\n", contents)
224
257
 
225
258
    def test_add_node_referencing_missing_key_makes_absent(self):
226
 
        builder = index.GraphIndexBuilder(reference_lists=1)
 
259
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
227
260
        builder.add_node(('rey', ), 'data',
228
261
                         ([('beference', ), ('aeference2', )], ))
229
262
        stream = builder.finish()
237
270
 
238
271
    def test_node_references_three_digits(self):
239
272
        # test the node digit expands as needed.
240
 
        builder = index.GraphIndexBuilder(reference_lists=1)
 
273
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
241
274
        references = [(str(val), ) for val in range(8, -1, -1)]
242
275
        builder.add_node(('2-key', ), '', (references, ))
243
276
        stream = builder.finish()
259
292
    def test_absent_has_no_reference_overhead(self):
260
293
        # the offsets after an absent record should be correct when there are
261
294
        # >1 reference lists.
262
 
        builder = index.GraphIndexBuilder(reference_lists=2)
 
295
        builder = _mod_index.GraphIndexBuilder(reference_lists=2)
263
296
        builder.add_node(('parent', ), '', ([('aail', ), ('zther', )], []))
264
297
        stream = builder.finish()
265
298
        contents = stream.read()
271
304
            "\n", contents)
272
305
 
273
306
    def test_add_node_bad_key(self):
274
 
        builder = index.GraphIndexBuilder()
 
307
        builder = _mod_index.GraphIndexBuilder()
275
308
        for bad_char in '\t\n\x0b\x0c\r\x00 ':
276
 
            self.assertRaises(errors.BadIndexKey, builder.add_node,
 
309
            self.assertRaises(_mod_index.BadIndexKey, builder.add_node,
277
310
                ('a%skey' % bad_char, ), 'data')
278
 
        self.assertRaises(errors.BadIndexKey, builder.add_node,
 
311
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node,
279
312
                ('', ), 'data')
280
 
        self.assertRaises(errors.BadIndexKey, builder.add_node,
 
313
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node,
281
314
                'not-a-tuple', 'data')
282
315
        # not enough length
283
 
        self.assertRaises(errors.BadIndexKey, builder.add_node,
 
316
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node,
284
317
                (), 'data')
285
318
        # too long
286
 
        self.assertRaises(errors.BadIndexKey, builder.add_node,
 
319
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node,
287
320
                ('primary', 'secondary'), 'data')
288
321
        # secondary key elements get checked too:
289
 
        builder = index.GraphIndexBuilder(key_elements=2)
 
322
        builder = _mod_index.GraphIndexBuilder(key_elements=2)
290
323
        for bad_char in '\t\n\x0b\x0c\r\x00 ':
291
 
            self.assertRaises(errors.BadIndexKey, builder.add_node,
 
324
            self.assertRaises(_mod_index.BadIndexKey, builder.add_node,
292
325
                ('prefix', 'a%skey' % bad_char), 'data')
293
326
 
294
327
    def test_add_node_bad_data(self):
295
 
        builder = index.GraphIndexBuilder()
296
 
        self.assertRaises(errors.BadIndexValue, builder.add_node, ('akey', ),
 
328
        builder = _mod_index.GraphIndexBuilder()
 
329
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, ('akey', ),
297
330
            'data\naa')
298
 
        self.assertRaises(errors.BadIndexValue, builder.add_node, ('akey', ),
 
331
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, ('akey', ),
299
332
            'data\x00aa')
300
333
 
301
334
    def test_add_node_bad_mismatched_ref_lists_length(self):
302
 
        builder = index.GraphIndexBuilder()
303
 
        self.assertRaises(errors.BadIndexValue, builder.add_node, ('akey', ),
 
335
        builder = _mod_index.GraphIndexBuilder()
 
336
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, ('akey', ),
304
337
            'data aa', ([], ))
305
 
        builder = index.GraphIndexBuilder(reference_lists=1)
306
 
        self.assertRaises(errors.BadIndexValue, builder.add_node, ('akey', ),
 
338
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
 
339
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, ('akey', ),
307
340
            'data aa')
308
 
        self.assertRaises(errors.BadIndexValue, builder.add_node, ('akey', ),
 
341
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, ('akey', ),
309
342
            'data aa', (), )
310
 
        self.assertRaises(errors.BadIndexValue, builder.add_node, ('akey', ),
 
343
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, ('akey', ),
311
344
            'data aa', ([], []))
312
 
        builder = index.GraphIndexBuilder(reference_lists=2)
313
 
        self.assertRaises(errors.BadIndexValue, builder.add_node, ('akey', ),
 
345
        builder = _mod_index.GraphIndexBuilder(reference_lists=2)
 
346
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, ('akey', ),
314
347
            'data aa')
315
 
        self.assertRaises(errors.BadIndexValue, builder.add_node, ('akey', ),
 
348
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, ('akey', ),
316
349
            'data aa', ([], ))
317
 
        self.assertRaises(errors.BadIndexValue, builder.add_node, ('akey', ),
 
350
        self.assertRaises(_mod_index.BadIndexValue, builder.add_node, ('akey', ),
318
351
            'data aa', ([], [], []))
319
352
 
320
353
    def test_add_node_bad_key_in_reference_lists(self):
321
354
        # first list, first key - trivial
322
 
        builder = index.GraphIndexBuilder(reference_lists=1)
323
 
        self.assertRaises(errors.BadIndexKey, builder.add_node, ('akey', ),
 
355
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
 
356
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node, ('akey', ),
324
357
            'data aa', ([('a key', )], ))
325
358
        # references keys must be tuples too
326
 
        self.assertRaises(errors.BadIndexKey, builder.add_node, ('akey', ),
 
359
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node, ('akey', ),
327
360
            'data aa', (['not-a-tuple'], ))
328
361
        # not enough length
329
 
        self.assertRaises(errors.BadIndexKey, builder.add_node, ('akey', ),
 
362
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node, ('akey', ),
330
363
            'data aa', ([()], ))
331
364
        # too long
332
 
        self.assertRaises(errors.BadIndexKey, builder.add_node, ('akey', ),
 
365
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node, ('akey', ),
333
366
            'data aa', ([('primary', 'secondary')], ))
334
367
        # need to check more than the first key in the list
335
 
        self.assertRaises(errors.BadIndexKey, builder.add_node, ('akey', ),
 
368
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node, ('akey', ),
336
369
            'data aa', ([('agoodkey', ), ('that is a bad key', )], ))
337
370
        # and if there is more than one list it should be getting checked
338
371
        # too
339
 
        builder = index.GraphIndexBuilder(reference_lists=2)
340
 
        self.assertRaises(errors.BadIndexKey, builder.add_node, ('akey', ),
 
372
        builder = _mod_index.GraphIndexBuilder(reference_lists=2)
 
373
        self.assertRaises(_mod_index.BadIndexKey, builder.add_node, ('akey', ),
341
374
            'data aa', ([], ['a bad key']))
342
375
 
343
376
    def test_add_duplicate_key(self):
344
 
        builder = index.GraphIndexBuilder()
 
377
        builder = _mod_index.GraphIndexBuilder()
345
378
        builder.add_node(('key', ), 'data')
346
 
        self.assertRaises(errors.BadIndexDuplicateKey,
 
379
        self.assertRaises(_mod_index.BadIndexDuplicateKey,
347
380
                          builder.add_node, ('key', ), 'data')
348
381
 
349
382
    def test_add_duplicate_key_2_elements(self):
350
 
        builder = index.GraphIndexBuilder(key_elements=2)
 
383
        builder = _mod_index.GraphIndexBuilder(key_elements=2)
351
384
        builder.add_node(('key', 'key'), 'data')
352
 
        self.assertRaises(errors.BadIndexDuplicateKey, builder.add_node,
 
385
        self.assertRaises(_mod_index.BadIndexDuplicateKey, builder.add_node,
353
386
            ('key', 'key'), 'data')
354
387
 
355
388
    def test_add_key_after_referencing_key(self):
356
 
        builder = index.GraphIndexBuilder(reference_lists=1)
 
389
        builder = _mod_index.GraphIndexBuilder(reference_lists=1)
357
390
        builder.add_node(('key', ), 'data', ([('reference', )], ))
358
391
        builder.add_node(('reference', ), 'data', ([],))
359
392
 
360
393
    def test_add_key_after_referencing_key_2_elements(self):
361
 
        builder = index.GraphIndexBuilder(reference_lists=1, key_elements=2)
 
394
        builder = _mod_index.GraphIndexBuilder(reference_lists=1, key_elements=2)
362
395
        builder.add_node(('k', 'ey'), 'data', ([('reference', 'tokey')], ))
363
396
        builder.add_node(('reference', 'tokey'), 'data', ([],))
364
397
 
365
398
    def test_set_optimize(self):
366
 
        builder = index.GraphIndexBuilder(reference_lists=1, key_elements=2)
 
399
        builder = _mod_index.GraphIndexBuilder(reference_lists=1, key_elements=2)
367
400
        builder.set_optimize(for_size=True)
368
401
        self.assertTrue(builder._optimize_for_size)
369
402
        builder.set_optimize(for_size=False)
387
420
        return nodes
388
421
 
389
422
    def make_index(self, ref_lists=0, key_elements=1, nodes=[]):
390
 
        builder = index.GraphIndexBuilder(ref_lists, key_elements=key_elements)
 
423
        builder = _mod_index.GraphIndexBuilder(ref_lists, key_elements=key_elements)
391
424
        for key, value, references in nodes:
392
425
            builder.add_node(key, value, references)
393
426
        stream = builder.finish()
394
427
        trans = transport.get_transport_from_url('trace+' + self.get_url())
395
428
        size = trans.put_file('index', stream)
396
 
        return index.GraphIndex(trans, 'index', size)
 
429
        return _mod_index.GraphIndex(trans, 'index', size)
397
430
 
398
431
    def make_index_with_offset(self, ref_lists=0, key_elements=1, nodes=[],
399
432
                               offset=0):
400
 
        builder = index.GraphIndexBuilder(ref_lists, key_elements=key_elements)
 
433
        builder = _mod_index.GraphIndexBuilder(ref_lists, key_elements=key_elements)
401
434
        for key, value, references in nodes:
402
435
            builder.add_node(key, value, references)
403
436
        content = builder.finish().read()
404
437
        size = len(content)
405
438
        trans = self.get_transport()
406
439
        trans.put_bytes('index', (' '*offset) + content)
407
 
        return index.GraphIndex(trans, 'index', size, offset=offset)
 
440
        return _mod_index.GraphIndex(trans, 'index', size, offset=offset)
408
441
 
409
442
    def test_clear_cache(self):
410
443
        index = self.make_index()
415
448
    def test_open_bad_index_no_error(self):
416
449
        trans = self.get_transport()
417
450
        trans.put_bytes('name', "not an index\n")
418
 
        idx = index.GraphIndex(trans, 'name', 13)
 
451
        idx = _mod_index.GraphIndex(trans, 'name', 13)
419
452
 
420
453
    def test_with_offset(self):
421
454
        nodes = self.make_nodes(200)
838
871
 
839
872
    def test_iter_missing_entry_empty_no_size(self):
840
873
        idx = self.make_index()
841
 
        idx = index.GraphIndex(idx._transport, 'index', None)
 
874
        idx = _mod_index.GraphIndex(idx._transport, 'index', None)
842
875
        self.assertEqual([], list(idx.iter_entries([('a', )])))
843
876
 
844
877
    def test_iter_key_prefix_1_element_key_None(self):
845
878
        index = self.make_index()
846
 
        self.assertRaises(errors.BadIndexKey, list,
 
879
        self.assertRaises(_mod_index.BadIndexKey, list,
847
880
            index.iter_entries_prefix([(None, )]))
848
881
 
849
882
    def test_iter_key_prefix_wrong_length(self):
850
883
        index = self.make_index()
851
 
        self.assertRaises(errors.BadIndexKey, list,
 
884
        self.assertRaises(_mod_index.BadIndexKey, list,
852
885
            index.iter_entries_prefix([('foo', None)]))
853
886
        index = self.make_index(key_elements=2)
854
 
        self.assertRaises(errors.BadIndexKey, list,
 
887
        self.assertRaises(_mod_index.BadIndexKey, list,
855
888
            index.iter_entries_prefix([('foo', )]))
856
 
        self.assertRaises(errors.BadIndexKey, list,
 
889
        self.assertRaises(_mod_index.BadIndexKey, list,
857
890
            index.iter_entries_prefix([('foo', None, None)]))
858
891
 
859
892
    def test_iter_key_prefix_1_key_element_no_refs(self):
935
968
    def test_validate_bad_index_errors(self):
936
969
        trans = self.get_transport()
937
970
        trans.put_bytes('name', "not an index\n")
938
 
        idx = index.GraphIndex(trans, 'name', 13)
939
 
        self.assertRaises(errors.BadIndexFormatSignature, idx.validate)
 
971
        idx = _mod_index.GraphIndex(trans, 'name', 13)
 
972
        self.assertRaises(_mod_index.BadIndexFormatSignature, idx.validate)
940
973
 
941
974
    def test_validate_bad_node_refs(self):
942
975
        idx = self.make_index(2)
945
978
        # change the options line to end with a rather than a parseable number
946
979
        new_content = content[:-2] + 'a\n\n'
947
980
        trans.put_bytes('index', new_content)
948
 
        self.assertRaises(errors.BadIndexOptions, idx.validate)
 
981
        self.assertRaises(_mod_index.BadIndexOptions, idx.validate)
949
982
 
950
983
    def test_validate_missing_end_line_empty(self):
951
984
        index = self.make_index(2)
953
986
        content = trans.get_bytes('index')
954
987
        # truncate the last byte
955
988
        trans.put_bytes('index', content[:-1])
956
 
        self.assertRaises(errors.BadIndexData, index.validate)
 
989
        self.assertRaises(_mod_index.BadIndexData, index.validate)
957
990
 
958
991
    def test_validate_missing_end_line_nonempty(self):
959
992
        index = self.make_index(2, nodes=[(('key', ), '', ([], []))])
961
994
        content = trans.get_bytes('index')
962
995
        # truncate the last byte
963
996
        trans.put_bytes('index', content[:-1])
964
 
        self.assertRaises(errors.BadIndexData, index.validate)
 
997
        self.assertRaises(_mod_index.BadIndexData, index.validate)
965
998
 
966
999
    def test_validate_empty(self):
967
1000
        index = self.make_index()
1056
1089
        self.assertEqual(set(), search_keys)
1057
1090
 
1058
1091
    def test_supports_unlimited_cache(self):
1059
 
        builder = index.GraphIndexBuilder(0, key_elements=1)
 
1092
        builder = _mod_index.GraphIndexBuilder(0, key_elements=1)
1060
1093
        stream = builder.finish()
1061
1094
        trans = self.get_transport()
1062
1095
        size = trans.put_file('index', stream)
1063
1096
        # It doesn't matter what unlimited_cache does here, just that it can be
1064
1097
        # passed
1065
 
        idx = index.GraphIndex(trans, 'index', size, unlimited_cache=True)
 
1098
        idx = _mod_index.GraphIndex(trans, 'index', size, unlimited_cache=True)
1066
1099
 
1067
1100
 
1068
1101
class TestCombinedGraphIndex(tests.TestCaseWithMemoryTransport):
1069
1102
 
1070
1103
    def make_index(self, name, ref_lists=0, key_elements=1, nodes=[]):
1071
 
        builder = index.GraphIndexBuilder(ref_lists, key_elements=key_elements)
 
1104
        builder = _mod_index.GraphIndexBuilder(ref_lists, key_elements=key_elements)
1072
1105
        for key, value, references in nodes:
1073
1106
            builder.add_node(key, value, references)
1074
1107
        stream = builder.finish()
1075
1108
        trans = self.get_transport()
1076
1109
        size = trans.put_file(name, stream)
1077
 
        return index.GraphIndex(trans, name, size)
 
1110
        return _mod_index.GraphIndex(trans, name, size)
1078
1111
 
1079
1112
    def make_combined_index_with_missing(self, missing=['1', '2']):
1080
1113
        """Create a CombinedGraphIndex which will have missing indexes.
1103
1136
            reload_counter[1] += 1
1104
1137
            idx._indices[:] = new_indices
1105
1138
            return True
1106
 
        idx = index.CombinedGraphIndex([idx1, idx2], reload_func=reload)
 
1139
        idx = _mod_index.CombinedGraphIndex([idx1, idx2], reload_func=reload)
1107
1140
        trans = self.get_transport()
1108
1141
        for fname in missing:
1109
1142
            trans.delete(fname)
1111
1144
 
1112
1145
    def test_open_missing_index_no_error(self):
1113
1146
        trans = self.get_transport()
1114
 
        idx1 = index.GraphIndex(trans, 'missing', 100)
1115
 
        idx = index.CombinedGraphIndex([idx1])
 
1147
        idx1 = _mod_index.GraphIndex(trans, 'missing', 100)
 
1148
        idx = _mod_index.CombinedGraphIndex([idx1])
1116
1149
 
1117
1150
    def test_add_index(self):
1118
 
        idx = index.CombinedGraphIndex([])
 
1151
        idx = _mod_index.CombinedGraphIndex([])
1119
1152
        idx1 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
1120
1153
        idx.insert_index(0, idx1)
1121
1154
        self.assertEqual([(idx1, ('key', ), '')],
1136
1169
                log.append(self._index)
1137
1170
                return self._index.clear_cache()
1138
1171
 
1139
 
        idx = index.CombinedGraphIndex([])
 
1172
        idx = _mod_index.CombinedGraphIndex([])
1140
1173
        idx1 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
1141
1174
        idx.insert_index(0, ClearCacheProxy(idx1))
1142
1175
        idx2 = self.make_index('name', 0, nodes=[(('key', ), '', ())])
1146
1179
        self.assertEqual(sorted([idx1, idx2]), sorted(log))
1147
1180
 
1148
1181
    def test_iter_all_entries_empty(self):
1149
 
        idx = index.CombinedGraphIndex([])
 
1182
        idx = _mod_index.CombinedGraphIndex([])
1150
1183
        self.assertEqual([], list(idx.iter_all_entries()))
1151
1184
 
1152
1185
    def test_iter_all_entries_children_empty(self):
1153
1186
        idx1 = self.make_index('name')
1154
 
        idx = index.CombinedGraphIndex([idx1])
 
1187
        idx = _mod_index.CombinedGraphIndex([idx1])
1155
1188
        self.assertEqual([], list(idx.iter_all_entries()))
1156
1189
 
1157
1190
    def test_iter_all_entries_simple(self):
1158
1191
        idx1 = self.make_index('name', nodes=[(('name', ), 'data', ())])
1159
 
        idx = index.CombinedGraphIndex([idx1])
 
1192
        idx = _mod_index.CombinedGraphIndex([idx1])
1160
1193
        self.assertEqual([(idx1, ('name', ), 'data')],
1161
1194
            list(idx.iter_all_entries()))
1162
1195
 
1163
1196
    def test_iter_all_entries_two_indices(self):
1164
1197
        idx1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1165
1198
        idx2 = self.make_index('name2', nodes=[(('2', ), '', ())])
1166
 
        idx = index.CombinedGraphIndex([idx1, idx2])
 
1199
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
1167
1200
        self.assertEqual([(idx1, ('name', ), 'data'),
1168
1201
                          (idx2, ('2', ), '')],
1169
1202
                         list(idx.iter_all_entries()))
1171
1204
    def test_iter_entries_two_indices_dup_key(self):
1172
1205
        idx1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1173
1206
        idx2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
1174
 
        idx = index.CombinedGraphIndex([idx1, idx2])
 
1207
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
1175
1208
        self.assertEqual([(idx1, ('name', ), 'data')],
1176
1209
                         list(idx.iter_entries([('name', )])))
1177
1210
 
1178
1211
    def test_iter_all_entries_two_indices_dup_key(self):
1179
1212
        idx1 = self.make_index('name1', nodes=[(('name', ), 'data', ())])
1180
1213
        idx2 = self.make_index('name2', nodes=[(('name', ), 'data', ())])
1181
 
        idx = index.CombinedGraphIndex([idx1, idx2])
 
1214
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
1182
1215
        self.assertEqual([(idx1, ('name', ), 'data')],
1183
1216
                         list(idx.iter_all_entries()))
1184
1217
 
1188
1221
        idx2 = self.make_index('2', 1, key_elements=2, nodes=[
1189
1222
                (('name', 'fin2'), 'beta', ([], )),
1190
1223
                (('ref', 'erence'), 'refdata', ([], ))])
1191
 
        idx = index.CombinedGraphIndex([idx1, idx2])
 
1224
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
1192
1225
        self.assertEqual({(idx1, ('name', 'fin1'), 'data',
1193
1226
                               ((('ref', 'erence'),),)),
1194
1227
                              (idx2, ('ref', 'erence'), 'refdata', ((), ))},
1200
1233
                         set(idx.iter_entries_prefix([('name', None)])))
1201
1234
 
1202
1235
    def test_iter_nothing_empty(self):
1203
 
        idx = index.CombinedGraphIndex([])
 
1236
        idx = _mod_index.CombinedGraphIndex([])
1204
1237
        self.assertEqual([], list(idx.iter_entries([])))
1205
1238
 
1206
1239
    def test_iter_nothing_children_empty(self):
1207
1240
        idx1 = self.make_index('name')
1208
 
        idx = index.CombinedGraphIndex([idx1])
 
1241
        idx = _mod_index.CombinedGraphIndex([idx1])
1209
1242
        self.assertEqual([], list(idx.iter_entries([])))
1210
1243
 
1211
1244
    def test_iter_all_keys(self):
1212
1245
        idx1 = self.make_index('1', 1, nodes=[(('name', ), 'data',
1213
1246
                                               ([('ref', )], ))])
1214
1247
        idx2 = self.make_index('2', 1, nodes=[(('ref', ), 'refdata', ((), ))])
1215
 
        idx = index.CombinedGraphIndex([idx1, idx2])
 
1248
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
1216
1249
        self.assertEqual({(idx1, ('name', ), 'data', ((('ref', ), ), )),
1217
1250
                              (idx2, ('ref', ), 'refdata', ((), ))},
1218
1251
                         set(idx.iter_entries([('name', ), ('ref', )])))
1222
1255
                                                 ([('ref', )], )),
1223
1256
                                                (('ref', ), 'refdata', ([], ))])
1224
1257
        idx2 = self.make_index('2', 1, nodes=[(('ref', ), 'refdata', ([], ))])
1225
 
        idx = index.CombinedGraphIndex([idx1, idx2])
 
1258
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
1226
1259
        self.assertEqual({(idx1, ('name', ), 'data', ((('ref',),),)),
1227
1260
                              (idx1, ('ref', ), 'refdata', ((), ))},
1228
1261
                         set(idx.iter_entries([('name', ), ('ref', )])))
1229
1262
 
1230
1263
    def test_iter_missing_entry_empty(self):
1231
 
        idx = index.CombinedGraphIndex([])
 
1264
        idx = _mod_index.CombinedGraphIndex([])
1232
1265
        self.assertEqual([], list(idx.iter_entries([('a', )])))
1233
1266
 
1234
1267
    def test_iter_missing_entry_one_index(self):
1235
1268
        idx1 = self.make_index('1')
1236
 
        idx = index.CombinedGraphIndex([idx1])
 
1269
        idx = _mod_index.CombinedGraphIndex([idx1])
1237
1270
        self.assertEqual([], list(idx.iter_entries([('a', )])))
1238
1271
 
1239
1272
    def test_iter_missing_entry_two_index(self):
1240
1273
        idx1 = self.make_index('1')
1241
1274
        idx2 = self.make_index('2')
1242
 
        idx = index.CombinedGraphIndex([idx1, idx2])
 
1275
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
1243
1276
        self.assertEqual([], list(idx.iter_entries([('a', )])))
1244
1277
 
1245
1278
    def test_iter_entry_present_one_index_only(self):
1246
1279
        idx1 = self.make_index('1', nodes=[(('key', ), '', ())])
1247
1280
        idx2 = self.make_index('2', nodes=[])
1248
 
        idx = index.CombinedGraphIndex([idx1, idx2])
 
1281
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
1249
1282
        self.assertEqual([(idx1, ('key', ), '')],
1250
1283
                         list(idx.iter_entries([('key', )])))
1251
1284
        # and in the other direction
1252
 
        idx = index.CombinedGraphIndex([idx2, idx1])
 
1285
        idx = _mod_index.CombinedGraphIndex([idx2, idx1])
1253
1286
        self.assertEqual([(idx1, ('key', ), '')],
1254
1287
                         list(idx.iter_entries([('key', )])))
1255
1288
 
1256
1289
    def test_key_count_empty(self):
1257
1290
        idx1 = self.make_index('1', nodes=[])
1258
1291
        idx2 = self.make_index('2', nodes=[])
1259
 
        idx = index.CombinedGraphIndex([idx1, idx2])
 
1292
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
1260
1293
        self.assertEqual(0, idx.key_count())
1261
1294
 
1262
1295
    def test_key_count_sums_index_keys(self):
1264
1297
            (('1',), '', ()),
1265
1298
            (('2',), '', ())])
1266
1299
        idx2 = self.make_index('2', nodes=[(('1',), '', ())])
1267
 
        idx = index.CombinedGraphIndex([idx1, idx2])
 
1300
        idx = _mod_index.CombinedGraphIndex([idx1, idx2])
1268
1301
        self.assertEqual(3, idx.key_count())
1269
1302
 
1270
1303
    def test_validate_bad_child_index_errors(self):
1271
1304
        trans = self.get_transport()
1272
1305
        trans.put_bytes('name', "not an index\n")
1273
 
        idx1 = index.GraphIndex(trans, 'name', 13)
1274
 
        idx = index.CombinedGraphIndex([idx1])
1275
 
        self.assertRaises(errors.BadIndexFormatSignature, idx.validate)
 
1306
        idx1 = _mod_index.GraphIndex(trans, 'name', 13)
 
1307
        idx = _mod_index.CombinedGraphIndex([idx1])
 
1308
        self.assertRaises(_mod_index.BadIndexFormatSignature, idx.validate)
1276
1309
 
1277
1310
    def test_validate_empty(self):
1278
 
        idx = index.CombinedGraphIndex([])
 
1311
        idx = _mod_index.CombinedGraphIndex([])
1279
1312
        idx.validate()
1280
1313
 
1281
1314
    def test_key_count_reloads(self):
1403
1436
    def test_reorder_after_iter_entries(self):
1404
1437
        # Four indices: [key1] in idx1, [key2,key3] in idx2, [] in idx3,
1405
1438
        # [key4] in idx4.
1406
 
        idx = index.CombinedGraphIndex([])
 
1439
        idx = _mod_index.CombinedGraphIndex([])
1407
1440
        idx.insert_index(0, self.make_index_with_simple_nodes('1'), '1')
1408
1441
        idx.insert_index(1, self.make_index_with_simple_nodes('2'), '2')
1409
1442
        idx.insert_index(2, self.make_index_with_simple_nodes('3'), '3')
1420
1453
    def test_reorder_propagates_to_siblings(self):
1421
1454
        # Two CombinedGraphIndex objects, with the same number of indicies with
1422
1455
        # matching names.
1423
 
        cgi1 = index.CombinedGraphIndex([])
1424
 
        cgi2 = index.CombinedGraphIndex([])
 
1456
        cgi1 = _mod_index.CombinedGraphIndex([])
 
1457
        cgi2 = _mod_index.CombinedGraphIndex([])
1425
1458
        cgi1.insert_index(0, self.make_index_with_simple_nodes('1-1'), 'one')
1426
1459
        cgi1.insert_index(1, self.make_index_with_simple_nodes('1-2'), 'two')
1427
1460
        cgi2.insert_index(0, self.make_index_with_simple_nodes('2-1'), 'one')
1465
1498
            (key3, 'value', ([key2],)),
1466
1499
            (key4, 'value', ([key3],)),
1467
1500
            ])
1468
 
        c_index = index.CombinedGraphIndex([index1, index2])
 
1501
        c_index = _mod_index.CombinedGraphIndex([index1, index2])
1469
1502
        parent_map, missing_keys = c_index.find_ancestry([key1], 0)
1470
1503
        self.assertEqual({key1: ()}, parent_map)
1471
1504
        self.assertEqual(set(), missing_keys)
1488
1521
        index2 = self.make_index('34', ref_lists=1, nodes=[
1489
1522
            (key3, 'value', ([key2],)),
1490
1523
            ])
1491
 
        c_index = index.CombinedGraphIndex([index1, index2])
 
1524
        c_index = _mod_index.CombinedGraphIndex([index1, index2])
1492
1525
        # Searching for a key which is actually not present at all should
1493
1526
        # eventually converge
1494
1527
        parent_map, missing_keys = c_index.find_ancestry([key4], 0)
1496
1529
        self.assertEqual({key4}, missing_keys)
1497
1530
 
1498
1531
    def test_find_ancestors_no_indexes(self):
1499
 
        c_index = index.CombinedGraphIndex([])
 
1532
        c_index = _mod_index.CombinedGraphIndex([])
1500
1533
        key1 = ('key-1',)
1501
1534
        parent_map, missing_keys = c_index.find_ancestry([key1], 0)
1502
1535
        self.assertEqual({}, parent_map)
1514
1547
        index2 = self.make_index('34', ref_lists=1, nodes=[
1515
1548
            (key4, 'value', ([key2, key3],)),
1516
1549
            ])
1517
 
        c_index = index.CombinedGraphIndex([index1, index2])
 
1550
        c_index = _mod_index.CombinedGraphIndex([index1, index2])
1518
1551
        # Searching for a key which is actually not present at all should
1519
1552
        # eventually converge
1520
1553
        parent_map, missing_keys = c_index.find_ancestry([key4], 0)
1536
1569
class TestInMemoryGraphIndex(tests.TestCaseWithMemoryTransport):
1537
1570
 
1538
1571
    def make_index(self, ref_lists=0, key_elements=1, nodes=[]):
1539
 
        result = index.InMemoryGraphIndex(ref_lists, key_elements=key_elements)
 
1572
        result = _mod_index.InMemoryGraphIndex(ref_lists, key_elements=key_elements)
1540
1573
        result.add_nodes(nodes)
1541
1574
        return result
1542
1575
 
1667
1700
 
1668
1701
    def make_index(self, ref_lists=1, key_elements=2, nodes=[],
1669
1702
                   add_callback=False):
1670
 
        result = index.InMemoryGraphIndex(ref_lists, key_elements=key_elements)
 
1703
        result = _mod_index.InMemoryGraphIndex(ref_lists, key_elements=key_elements)
1671
1704
        result.add_nodes(nodes)
1672
1705
        if add_callback:
1673
1706
            add_nodes_callback = result.add_nodes
1674
1707
        else:
1675
1708
            add_nodes_callback = None
1676
 
        adapter = index.GraphIndexPrefixAdapter(
 
1709
        adapter = _mod_index.GraphIndexPrefixAdapter(
1677
1710
            result, ('prefix', ), key_elements - 1,
1678
1711
            add_nodes_callback=add_nodes_callback)
1679
1712
        return result, adapter
1698
1731
            set(index.iter_all_entries()))
1699
1732
 
1700
1733
    def test_construct(self):
1701
 
        idx = index.InMemoryGraphIndex()
1702
 
        adapter = index.GraphIndexPrefixAdapter(idx, ('prefix', ), 1)
 
1734
        idx = _mod_index.InMemoryGraphIndex()
 
1735
        adapter = _mod_index.GraphIndexPrefixAdapter(idx, ('prefix', ), 1)
1703
1736
 
1704
1737
    def test_construct_with_callback(self):
1705
 
        idx = index.InMemoryGraphIndex()
1706
 
        adapter = index.GraphIndexPrefixAdapter(idx, ('prefix', ), 1,
 
1738
        idx = _mod_index.InMemoryGraphIndex()
 
1739
        adapter = _mod_index.GraphIndexPrefixAdapter(idx, ('prefix', ), 1,
1707
1740
                                                idx.add_nodes)
1708
1741
 
1709
1742
    def test_iter_all_entries_cross_prefix_map_errors(self):
1710
1743
        index, adapter = self.make_index(nodes=[
1711
1744
            (('prefix', 'key1'), 'data1', ((('prefixaltered', 'key2'),),))])
1712
 
        self.assertRaises(errors.BadIndexData, list, adapter.iter_all_entries())
 
1745
        self.assertRaises(_mod_index.BadIndexData, list, adapter.iter_all_entries())
1713
1746
 
1714
1747
    def test_iter_all_entries(self):
1715
1748
        index, adapter = self.make_index(nodes=[