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))
455
class TestKnitCaching(KnitTests):
457
def create_knit(self, cache_add=False):
458
k = self.make_test_knit(True)
462
k.add_lines('text-1', [], split_lines(TEXT_1))
463
k.add_lines('text-2', [], split_lines(TEXT_2))
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)
471
def test_cache_add_and_clear(self):
472
k = self.create_knit(True)
474
self.assertEqual(['text-1', 'text-2'], sorted(k._data._cache.keys()))
477
self.assertEqual({}, k._data._cache)
479
def test_cache_data_read_raw(self):
480
k = self.create_knit()
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))
492
val = read_one_raw('text-1')
493
self.assertEqual({'text-1':val[1]}, k._data._cache)
496
# After clear, new reads are not cached
497
self.assertEqual({}, k._data._cache)
499
val2 = read_one_raw('text-1')
500
self.assertEqual(val, val2)
501
self.assertEqual({}, k._data._cache)
503
def test_cache_data_read(self):
504
k = self.create_knit()
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))
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])
525
self.assertEqual({}, k._data._cache)
527
val2 = read_one('text-2')
528
self.assertEqual(val, val2)
529
self.assertEqual({}, k._data._cache)
531
def test_cache_read(self):
532
k = self.create_knit()
535
text = k.get_text('text-1')
536
self.assertEqual(TEXT_1, text)
537
self.assertEqual(['text-1'], k._data._cache.keys())
540
self.assertEqual({}, k._data._cache)
542
text = k.get_text('text-1')
543
self.assertEqual(TEXT_1, text)
544
self.assertEqual({}, k._data._cache)