~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_index.py

  • Committer: Robert Collins
  • Date: 2007-07-13 13:10:30 UTC
  • mto: (2592.5.3 pack-repository)
  • mto: This revision was merged to the branch mainline in revision 2624.
  • Revision ID: robertc@robertcollins.net-20070713131030-xbo7bbks9zovdya4
Build a combined graph index to use multiple indices at once.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Tests for indices."""
18
18
 
19
19
from bzrlib import errors
20
 
from bzrlib.index import GraphIndexBuilder, GraphIndex
 
20
from bzrlib.index import CombinedGraphIndex, GraphIndexBuilder, GraphIndex
21
21
from bzrlib.tests import TestCaseWithMemoryTransport
22
22
 
23
23
 
258
258
            set(index.iter_entries(['name'])))
259
259
        self.assertRaises(errors.MissingKey, list, index.iter_entries(['ref']))
260
260
 
261
 
    def test_iter_nothing_empty(self):
 
261
    def test_iter_all_keys(self):
262
262
        index = self.make_index(1, nodes=[
263
263
            ('name', (['ref'], ), 'data'),
264
264
            ('ref', ([], ), 'refdata')])
266
266
            ('ref', ((), ), 'refdata')]),
267
267
            set(index.iter_entries(['name', 'ref'])))
268
268
 
269
 
    def test_iter_all_keys(self):
 
269
    def test_iter_nothing_empty(self):
270
270
        index = self.make_index()
271
271
        self.assertEqual([], list(index.iter_entries([])))
272
272
 
312
312
    def test_validate_no_refs_content(self):
313
313
        index = self.make_index(nodes=[('key', (), 'value')])
314
314
        index.validate()
 
315
 
 
316
 
 
317
class TestCombinedGraphIndex(TestCaseWithMemoryTransport):
 
318
 
 
319
    def make_index(self, name, ref_lists=0, nodes=[]):
 
320
        builder = GraphIndexBuilder(ref_lists)
 
321
        for node, references, value in nodes:
 
322
            builder.add_node(node, references, value)
 
323
        stream = builder.finish()
 
324
        trans = self.get_transport()
 
325
        trans.put_file(name, stream)
 
326
        return GraphIndex(trans, name)
 
327
 
 
328
    def test_open_missing_index_no_error(self):
 
329
        trans = self.get_transport()
 
330
        index1 = GraphIndex(trans, 'missing')
 
331
        index = CombinedGraphIndex([index1])
 
332
 
 
333
    def test_iter_all_entries_empty(self):
 
334
        index = CombinedGraphIndex([])
 
335
        self.assertEqual([], list(index.iter_all_entries()))
 
336
 
 
337
    def test_iter_all_entries_children_empty(self):
 
338
        index1 = self.make_index('name')
 
339
        index = CombinedGraphIndex([index1])
 
340
        self.assertEqual([], list(index.iter_all_entries()))
 
341
 
 
342
    def test_iter_all_entries_simple(self):
 
343
        index1 = self.make_index('name', nodes=[('name', (), 'data')])
 
344
        index = CombinedGraphIndex([index1])
 
345
        self.assertEqual([('name', (), 'data')],
 
346
            list(index.iter_all_entries()))
 
347
 
 
348
    def test_iter_all_entries_two_indices(self):
 
349
        index1 = self.make_index('name1', nodes=[('name', (), 'data')])
 
350
        index2 = self.make_index('name2', nodes=[('2', (), '')])
 
351
        index = CombinedGraphIndex([index1, index2])
 
352
        self.assertEqual([('name', (), 'data'),
 
353
            ('2', (), '')],
 
354
            list(index.iter_all_entries()))
 
355
 
 
356
    def test_iter_all_entries_two_indices_dup_key(self):
 
357
        index1 = self.make_index('name1', nodes=[('name', (), 'data')])
 
358
        index2 = self.make_index('name2', nodes=[('name', (), 'data')])
 
359
        index = CombinedGraphIndex([index1, index2])
 
360
        self.assertEqual([('name', (), 'data')],
 
361
            list(index.iter_all_entries()))
 
362
 
 
363
    def test_iter_nothing_empty(self):
 
364
        index = CombinedGraphIndex([])
 
365
        self.assertEqual([], list(index.iter_entries([])))
 
366
 
 
367
    def test_iter_nothing_children_empty(self):
 
368
        index1 = self.make_index('name')
 
369
        index = CombinedGraphIndex([index1])
 
370
        self.assertEqual([], list(index.iter_entries([])))
 
371
 
 
372
    def test_iter_all_keys(self):
 
373
        index1 = self.make_index('1', 1, nodes=[
 
374
            ('name', (['ref'], ), 'data')])
 
375
        index2 = self.make_index('2', 1, nodes=[
 
376
            ('ref', ([], ), 'refdata')])
 
377
        index = CombinedGraphIndex([index1, index2])
 
378
        self.assertEqual(set([('name', (('ref',),), 'data'),
 
379
            ('ref', ((), ), 'refdata')]),
 
380
            set(index.iter_entries(['name', 'ref'])))
 
381
 
 
382
    def test_iter_all_keys_dup_entry(self):
 
383
        index1 = self.make_index('1', 1, nodes=[
 
384
            ('name', (['ref'], ), 'data'),
 
385
            ('ref', ([], ), 'refdata')])
 
386
        index2 = self.make_index('2', 1, nodes=[
 
387
            ('ref', ([], ), 'refdata')])
 
388
        index = CombinedGraphIndex([index1, index2])
 
389
        self.assertEqual(set([('name', (('ref',),), 'data'),
 
390
            ('ref', ((), ), 'refdata')]),
 
391
            set(index.iter_entries(['name', 'ref'])))
 
392
 
 
393
    def test_iter_missing_entry_empty(self):
 
394
        index = CombinedGraphIndex([])
 
395
        self.assertRaises(errors.MissingKey, list, index.iter_entries(['a']))
 
396
 
 
397
    def test_iter_missing_entry_one_index(self):
 
398
        index1 = self.make_index('1')
 
399
        index = CombinedGraphIndex([index1])
 
400
        self.assertRaises(errors.MissingKey, list, index.iter_entries(['a']))
 
401
 
 
402
    def test_iter_missing_entry_two_index(self):
 
403
        index1 = self.make_index('1')
 
404
        index2 = self.make_index('2')
 
405
        index = CombinedGraphIndex([index1, index2])
 
406
        self.assertRaises(errors.MissingKey, list, index.iter_entries(['a']))
 
407
 
 
408
    def test_iter_entry_present_one_index_only(self):
 
409
        index1 = self.make_index('1', nodes=[('key', (), '')])
 
410
        index2 = self.make_index('2', nodes=[])
 
411
        index = CombinedGraphIndex([index1, index2])
 
412
        self.assertEqual([('key', (), '')],
 
413
            list(index.iter_entries(['key'])))
 
414
        # and in the other direction
 
415
        index = CombinedGraphIndex([index2, index1])
 
416
        self.assertEqual([('key', (), '')],
 
417
            list(index.iter_entries(['key'])))
 
418
 
 
419
    def test_validate_bad_child_index_errors(self):
 
420
        trans = self.get_transport()
 
421
        trans.put_bytes('name', "not an index\n")
 
422
        index1 = GraphIndex(trans, 'name')
 
423
        index = CombinedGraphIndex([index1])
 
424
        self.assertRaises(errors.BadIndexFormatSignature, index.validate)
 
425
 
 
426
    def test_validate_empty(self):
 
427
        index = CombinedGraphIndex([])
 
428
        index.validate()