272
272
self.assertEquals(origins[0], ('text-c', 'z\n'))
273
273
self.assertEquals(origins[1], ('text-b', 'c\n'))
275
def test_extraction_reads_components_once(self):
276
t = MemoryTransport()
277
instrumented_t = TransportLogger(t)
278
k1 = KnitVersionedFile('id', instrumented_t, create=True, delta=True)
279
# should read the index
280
self.assertEqual([('id.kndx',)], instrumented_t._calls)
281
instrumented_t._calls = []
283
k1.add_lines('base', [], ['text\n'])
284
# should not have read at all
285
self.assertEqual([], instrumented_t._calls)
288
k1.add_lines('sub', ['base'], ['text\n', 'text2\n'])
289
# should not have read at all
290
self.assertEqual([], instrumented_t._calls)
294
# should not have read at all
295
self.assertEqual([], instrumented_t._calls)
302
# should have read a component
303
# should not have read the first component only
304
self.assertEqual([('id.knit', [(0, 87)])], instrumented_t._calls)
305
instrumented_t._calls = []
308
# should not have read at all
309
self.assertEqual([], instrumented_t._calls)
310
# and now read the other component
312
# should have read the second component
313
self.assertEqual([('id.knit', [(87, 93)])], instrumented_t._calls)
314
instrumented_t._calls = []
319
k1.add_lines('sub2', ['base'], ['text\n', 'text3\n'])
320
# should read the first component only
321
self.assertEqual([('id.knit', [(0, 87)])], instrumented_t._calls)
275
def test_get_line_delta_texts(self):
276
"""Make sure we can call get_texts on text with reused line deltas"""
277
k1 = KnitVersionedFile('test1', get_transport('.'),
278
factory=KnitPlainFactory(), create=True)
283
parents = ['%d' % (t-1)]
284
k1.add_lines('%d' % t, parents, ['hello\n'] * t)
285
k1.get_texts(('%d' % t) for t in range(3))
323
287
def test_iter_lines_reads_in_order(self):
324
288
t = MemoryTransport()
486
450
self.failIf(WeaveToKnit.is_compatible(k, w))
487
451
self.failIf(WeaveToKnit.is_compatible(w, w))
488
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)