~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_knit.py

merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
450
450
        self.failIf(WeaveToKnit.is_compatible(k, w))
451
451
        self.failIf(WeaveToKnit.is_compatible(w, w))
452
452
        self.failIf(WeaveToKnit.is_compatible(k, k))
 
453
 
 
454
 
 
455
class TestKnitCaching(KnitTests):
 
456
    
 
457
    def create_knit(self, cache_add=False):
 
458
        k = self.make_test_knit(True)
 
459
        if cache_add:
 
460
            k.enable_cache()
 
461
 
 
462
        k.add_lines('text-1', [], split_lines(TEXT_1))
 
463
        k.add_lines('text-2', [], split_lines(TEXT_2))
 
464
        return k
 
465
 
 
466
    def test_no_caching(self):
 
467
        k = self.create_knit()
 
468
        # Nothing should be cached without setting 'enable_cache'
 
469
        self.assertEqual({}, k._data._cache)
 
470
 
 
471
    def test_cache_add_and_clear(self):
 
472
        k = self.create_knit(True)
 
473
 
 
474
        self.assertEqual(['text-1', 'text-2'], sorted(k._data._cache.keys()))
 
475
 
 
476
        k.clear_cache()
 
477
        self.assertEqual({}, k._data._cache)
 
478
 
 
479
    def test_cache_data_read_raw(self):
 
480
        k = self.create_knit()
 
481
 
 
482
        # Now cache and read
 
483
        k.enable_cache()
 
484
 
 
485
        def read_one_raw(version):
 
486
            pos_map = k._get_components_positions([version])
 
487
            method, pos, size, next = pos_map[version]
 
488
            lst = list(k._data.read_records_iter_raw([(version, pos, size)]))
 
489
            self.assertEqual(1, len(lst))
 
490
            return lst[0]
 
491
 
 
492
        val = read_one_raw('text-1')
 
493
        self.assertEqual({'text-1':val[1]}, k._data._cache)
 
494
 
 
495
        k.clear_cache()
 
496
        # After clear, new reads are not cached
 
497
        self.assertEqual({}, k._data._cache)
 
498
 
 
499
        val2 = read_one_raw('text-1')
 
500
        self.assertEqual(val, val2)
 
501
        self.assertEqual({}, k._data._cache)
 
502
 
 
503
    def test_cache_data_read(self):
 
504
        k = self.create_knit()
 
505
 
 
506
        def read_one(version):
 
507
            pos_map = k._get_components_positions([version])
 
508
            method, pos, size, next = pos_map[version]
 
509
            lst = list(k._data.read_records_iter([(version, pos, size)]))
 
510
            self.assertEqual(1, len(lst))
 
511
            return lst[0]
 
512
 
 
513
        # Now cache and read
 
514
        k.enable_cache()
 
515
 
 
516
        val = read_one('text-2')
 
517
        self.assertEqual(['text-2'], k._data._cache.keys())
 
518
        self.assertEqual('text-2', val[0])
 
519
        content, digest = k._data._parse_record('text-2',
 
520
                                                k._data._cache['text-2'])
 
521
        self.assertEqual(content, val[1])
 
522
        self.assertEqual(digest, val[2])
 
523
 
 
524
        k.clear_cache()
 
525
        self.assertEqual({}, k._data._cache)
 
526
 
 
527
        val2 = read_one('text-2')
 
528
        self.assertEqual(val, val2)
 
529
        self.assertEqual({}, k._data._cache)
 
530
 
 
531
    def test_cache_read(self):
 
532
        k = self.create_knit()
 
533
        k.enable_cache()
 
534
 
 
535
        text = k.get_text('text-1')
 
536
        self.assertEqual(TEXT_1, text)
 
537
        self.assertEqual(['text-1'], k._data._cache.keys())
 
538
 
 
539
        k.clear_cache()
 
540
        self.assertEqual({}, k._data._cache)
 
541
 
 
542
        text = k.get_text('text-1')
 
543
        self.assertEqual(TEXT_1, text)
 
544
        self.assertEqual({}, k._data._cache)