~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_knit.py

  • Committer: Mark Hammond
  • Date: 2009-01-12 01:55:34 UTC
  • mto: (3995.8.2 prepare-1.12)
  • mto: This revision was merged to the branch mainline in revision 4007.
  • Revision ID: mhammond@skippinet.com.au-20090112015534-yfxg50p7mpds9j4v
Include all .html files from the tortoise doc directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for Knit data structure"""
18
18
 
42
42
    KnitSequenceMatcher,
43
43
    KnitVersionedFiles,
44
44
    PlainKnitContent,
45
 
    _VFContentMapGenerator,
46
45
    _DirectPackAccess,
47
46
    _KndxIndex,
48
47
    _KnitGraphIndex,
64
63
from bzrlib.versionedfile import (
65
64
    AbsentContentFactory,
66
65
    ConstantMapper,
67
 
    network_bytes_to_kind_and_offset,
68
66
    RecordingVersionedFilesDecorator,
69
67
    )
70
68
 
299
297
        access = self.get_access()
300
298
        memos = access.add_raw_records([('key', 10)], '1234567890')
301
299
        self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
302
 
 
 
300
 
303
301
    def test_add_several_raw_records(self):
304
302
        """add_raw_records with many records and read some back."""
305
303
        access = self.get_access()
366
364
        :return: (versioned_file, reload_counter)
367
365
            versioned_file  a KnitVersionedFiles using the packs for access
368
366
        """
369
 
        builder = self.make_branch_builder('.')
370
 
        builder.start_series()
371
 
        builder.build_snapshot('rev-1', None, [
372
 
            ('add', ('', 'root-id', 'directory', None)),
373
 
            ('add', ('file', 'file-id', 'file', 'content\nrev 1\n')),
374
 
            ])
375
 
        builder.build_snapshot('rev-2', ['rev-1'], [
376
 
            ('modify', ('file-id', 'content\nrev 2\n')),
377
 
            ])
378
 
        builder.build_snapshot('rev-3', ['rev-2'], [
379
 
            ('modify', ('file-id', 'content\nrev 3\n')),
380
 
            ])
381
 
        builder.finish_series()
382
 
        b = builder.get_branch()
383
 
        b.lock_write()
384
 
        self.addCleanup(b.unlock)
385
 
        # Pack these three revisions into another pack file, but don't remove
386
 
        # the originals
387
 
        repo = b.repository
388
 
        collection = repo._pack_collection
389
 
        collection.ensure_loaded()
390
 
        orig_packs = collection.packs
391
 
        packer = pack_repo.Packer(collection, orig_packs, '.testpack')
392
 
        new_pack = packer.pack()
393
 
        # forget about the new pack
394
 
        collection.reset()
395
 
        repo.refresh_data()
396
 
        vf = repo.revisions
 
367
        tree = self.make_branch_and_memory_tree('tree')
 
368
        tree.lock_write()
 
369
        try:
 
370
            tree.add([''], ['root-id'])
 
371
            tree.commit('one', rev_id='rev-1')
 
372
            tree.commit('two', rev_id='rev-2')
 
373
            tree.commit('three', rev_id='rev-3')
 
374
            # Pack these two revisions into another pack file, but don't remove
 
375
            # the originials
 
376
            repo = tree.branch.repository
 
377
            collection = repo._pack_collection
 
378
            collection.ensure_loaded()
 
379
            orig_packs = collection.packs
 
380
            packer = pack_repo.Packer(collection, orig_packs, '.testpack')
 
381
            new_pack = packer.pack()
 
382
 
 
383
            vf = tree.branch.repository.revisions
 
384
        finally:
 
385
            tree.unlock()
 
386
        tree.branch.repository.lock_read()
 
387
        self.addCleanup(tree.branch.repository.unlock)
 
388
        del tree
397
389
        # Set up a reload() function that switches to using the new pack file
398
390
        new_index = new_pack.revision_index
399
391
        access_tuple = new_pack.access_tuple()
1099
1091
            call[1][1].getvalue())
1100
1092
        self.assertEqual({'create_parent_dir': True}, call[2])
1101
1093
 
1102
 
    def assertTotalBuildSize(self, size, keys, positions):
1103
 
        self.assertEqual(size,
1104
 
                         knit._get_total_build_size(None, keys, positions))
1105
 
 
1106
 
    def test__get_total_build_size(self):
1107
 
        positions = {
1108
 
            ('a',): (('fulltext', False), (('a',), 0, 100), None),
1109
 
            ('b',): (('line-delta', False), (('b',), 100, 21), ('a',)),
1110
 
            ('c',): (('line-delta', False), (('c',), 121, 35), ('b',)),
1111
 
            ('d',): (('line-delta', False), (('d',), 156, 12), ('b',)),
1112
 
            }
1113
 
        self.assertTotalBuildSize(100, [('a',)], positions)
1114
 
        self.assertTotalBuildSize(121, [('b',)], positions)
1115
 
        # c needs both a & b
1116
 
        self.assertTotalBuildSize(156, [('c',)], positions)
1117
 
        # we shouldn't count 'b' twice
1118
 
        self.assertTotalBuildSize(156, [('b',), ('c',)], positions)
1119
 
        self.assertTotalBuildSize(133, [('d',)], positions)
1120
 
        self.assertTotalBuildSize(168, [('c',), ('d',)], positions)
1121
 
 
1122
1094
    def test_get_position(self):
1123
1095
        transport = MockTransport([
1124
1096
            _KndxIndex.HEADER,
1265
1237
            else:
1266
1238
                raise
1267
1239
 
1268
 
    def test_scan_unvalidated_index_not_implemented(self):
1269
 
        transport = MockTransport()
1270
 
        index = self.get_knit_index(transport, 'filename', 'r')
1271
 
        self.assertRaises(
1272
 
            NotImplementedError, index.scan_unvalidated_index,
1273
 
            'dummy graph_index')
1274
 
        self.assertRaises(
1275
 
            NotImplementedError, index.get_missing_compression_parents)
1276
 
 
1277
1240
    def test_short_line(self):
1278
1241
        transport = MockTransport([
1279
1242
            _KndxIndex.HEADER,
1322
1285
        return _KndxIndex(transport, mapper, lambda:None, allow_writes, lambda:True)
1323
1286
 
1324
1287
 
1325
 
class Test_KnitAnnotator(TestCaseWithMemoryTransport):
1326
 
 
1327
 
    def make_annotator(self):
1328
 
        factory = knit.make_pack_factory(True, True, 1)
1329
 
        vf = factory(self.get_transport())
1330
 
        return knit._KnitAnnotator(vf)
1331
 
 
1332
 
    def test__expand_fulltext(self):
1333
 
        ann = self.make_annotator()
1334
 
        rev_key = ('rev-id',)
1335
 
        ann._num_compression_children[rev_key] = 1
1336
 
        res = ann._expand_record(rev_key, (('parent-id',),), None,
1337
 
                           ['line1\n', 'line2\n'], ('fulltext', True))
1338
 
        # The content object and text lines should be cached appropriately
1339
 
        self.assertEqual(['line1\n', 'line2'], res)
1340
 
        content_obj = ann._content_objects[rev_key]
1341
 
        self.assertEqual(['line1\n', 'line2\n'], content_obj._lines)
1342
 
        self.assertEqual(res, content_obj.text())
1343
 
        self.assertEqual(res, ann._text_cache[rev_key])
1344
 
 
1345
 
    def test__expand_delta_comp_parent_not_available(self):
1346
 
        # Parent isn't available yet, so we return nothing, but queue up this
1347
 
        # node for later processing
1348
 
        ann = self.make_annotator()
1349
 
        rev_key = ('rev-id',)
1350
 
        parent_key = ('parent-id',)
1351
 
        record = ['0,1,1\n', 'new-line\n']
1352
 
        details = ('line-delta', False)
1353
 
        res = ann._expand_record(rev_key, (parent_key,), parent_key,
1354
 
                                 record, details)
1355
 
        self.assertEqual(None, res)
1356
 
        self.assertTrue(parent_key in ann._pending_deltas)
1357
 
        pending = ann._pending_deltas[parent_key]
1358
 
        self.assertEqual(1, len(pending))
1359
 
        self.assertEqual((rev_key, (parent_key,), record, details), pending[0])
1360
 
 
1361
 
    def test__expand_record_tracks_num_children(self):
1362
 
        ann = self.make_annotator()
1363
 
        rev_key = ('rev-id',)
1364
 
        rev2_key = ('rev2-id',)
1365
 
        parent_key = ('parent-id',)
1366
 
        record = ['0,1,1\n', 'new-line\n']
1367
 
        details = ('line-delta', False)
1368
 
        ann._num_compression_children[parent_key] = 2
1369
 
        ann._expand_record(parent_key, (), None, ['line1\n', 'line2\n'],
1370
 
                           ('fulltext', False))
1371
 
        res = ann._expand_record(rev_key, (parent_key,), parent_key,
1372
 
                                 record, details)
1373
 
        self.assertEqual({parent_key: 1}, ann._num_compression_children)
1374
 
        # Expanding the second child should remove the content object, and the
1375
 
        # num_compression_children entry
1376
 
        res = ann._expand_record(rev2_key, (parent_key,), parent_key,
1377
 
                                 record, details)
1378
 
        self.assertFalse(parent_key in ann._content_objects)
1379
 
        self.assertEqual({}, ann._num_compression_children)
1380
 
        # We should not cache the content_objects for rev2 and rev, because
1381
 
        # they do not have compression children of their own.
1382
 
        self.assertEqual({}, ann._content_objects)
1383
 
 
1384
 
    def test__expand_delta_records_blocks(self):
1385
 
        ann = self.make_annotator()
1386
 
        rev_key = ('rev-id',)
1387
 
        parent_key = ('parent-id',)
1388
 
        record = ['0,1,1\n', 'new-line\n']
1389
 
        details = ('line-delta', True)
1390
 
        ann._num_compression_children[parent_key] = 2
1391
 
        ann._expand_record(parent_key, (), None,
1392
 
                           ['line1\n', 'line2\n', 'line3\n'],
1393
 
                           ('fulltext', False))
1394
 
        ann._expand_record(rev_key, (parent_key,), parent_key, record, details)
1395
 
        self.assertEqual({(rev_key, parent_key): [(1, 1, 1), (3, 3, 0)]},
1396
 
                         ann._matching_blocks)
1397
 
        rev2_key = ('rev2-id',)
1398
 
        record = ['0,1,1\n', 'new-line\n']
1399
 
        details = ('line-delta', False)
1400
 
        ann._expand_record(rev2_key, (parent_key,), parent_key, record, details)
1401
 
        self.assertEqual([(1, 1, 2), (3, 3, 0)],
1402
 
                         ann._matching_blocks[(rev2_key, parent_key)])
1403
 
 
1404
 
    def test__get_parent_ann_uses_matching_blocks(self):
1405
 
        ann = self.make_annotator()
1406
 
        rev_key = ('rev-id',)
1407
 
        parent_key = ('parent-id',)
1408
 
        parent_ann = [(parent_key,)]*3
1409
 
        block_key = (rev_key, parent_key)
1410
 
        ann._annotations_cache[parent_key] = parent_ann
1411
 
        ann._matching_blocks[block_key] = [(0, 1, 1), (3, 3, 0)]
1412
 
        # We should not try to access any parent_lines content, because we know
1413
 
        # we already have the matching blocks
1414
 
        par_ann, blocks = ann._get_parent_annotations_and_matches(rev_key,
1415
 
                                        ['1\n', '2\n', '3\n'], parent_key)
1416
 
        self.assertEqual(parent_ann, par_ann)
1417
 
        self.assertEqual([(0, 1, 1), (3, 3, 0)], blocks)
1418
 
        self.assertEqual({}, ann._matching_blocks)
1419
 
 
1420
 
    def test__process_pending(self):
1421
 
        ann = self.make_annotator()
1422
 
        rev_key = ('rev-id',)
1423
 
        p1_key = ('p1-id',)
1424
 
        p2_key = ('p2-id',)
1425
 
        record = ['0,1,1\n', 'new-line\n']
1426
 
        details = ('line-delta', False)
1427
 
        p1_record = ['line1\n', 'line2\n']
1428
 
        ann._num_compression_children[p1_key] = 1
1429
 
        res = ann._expand_record(rev_key, (p1_key,p2_key), p1_key,
1430
 
                                 record, details)
1431
 
        self.assertEqual(None, res)
1432
 
        # self.assertTrue(p1_key in ann._pending_deltas)
1433
 
        self.assertEqual({}, ann._pending_annotation)
1434
 
        # Now insert p1, and we should be able to expand the delta
1435
 
        res = ann._expand_record(p1_key, (), None, p1_record,
1436
 
                                 ('fulltext', False))
1437
 
        self.assertEqual(p1_record, res)
1438
 
        ann._annotations_cache[p1_key] = [(p1_key,)]*2
1439
 
        res = ann._process_pending(p1_key)
1440
 
        self.assertEqual([], res)
1441
 
        self.assertFalse(p1_key in ann._pending_deltas)
1442
 
        self.assertTrue(p2_key in ann._pending_annotation)
1443
 
        self.assertEqual({p2_key: [(rev_key, (p1_key, p2_key))]},
1444
 
                         ann._pending_annotation)
1445
 
        # Now fill in parent 2, and pending annotation should be satisfied
1446
 
        res = ann._expand_record(p2_key, (), None, [], ('fulltext', False))
1447
 
        ann._annotations_cache[p2_key] = []
1448
 
        res = ann._process_pending(p2_key)
1449
 
        self.assertEqual([rev_key], res)
1450
 
        self.assertEqual({}, ann._pending_annotation)
1451
 
        self.assertEqual({}, ann._pending_deltas)
1452
 
 
1453
 
    def test_record_delta_removes_basis(self):
1454
 
        ann = self.make_annotator()
1455
 
        ann._expand_record(('parent-id',), (), None,
1456
 
                           ['line1\n', 'line2\n'], ('fulltext', False))
1457
 
        ann._num_compression_children['parent-id'] = 2
1458
 
 
1459
 
    def test_annotate_special_text(self):
1460
 
        ann = self.make_annotator()
1461
 
        vf = ann._vf
1462
 
        rev1_key = ('rev-1',)
1463
 
        rev2_key = ('rev-2',)
1464
 
        rev3_key = ('rev-3',)
1465
 
        spec_key = ('special:',)
1466
 
        vf.add_lines(rev1_key, [], ['initial content\n'])
1467
 
        vf.add_lines(rev2_key, [rev1_key], ['initial content\n',
1468
 
                                            'common content\n',
1469
 
                                            'content in 2\n'])
1470
 
        vf.add_lines(rev3_key, [rev1_key], ['initial content\n',
1471
 
                                            'common content\n',
1472
 
                                            'content in 3\n'])
1473
 
        spec_text = ('initial content\n'
1474
 
                     'common content\n'
1475
 
                     'content in 2\n'
1476
 
                     'content in 3\n')
1477
 
        ann.add_special_text(spec_key, [rev2_key, rev3_key], spec_text)
1478
 
        anns, lines = ann.annotate(spec_key)
1479
 
        self.assertEqual([(rev1_key,),
1480
 
                          (rev2_key, rev3_key),
1481
 
                          (rev2_key,),
1482
 
                          (rev3_key,),
1483
 
                         ], anns)
1484
 
        self.assertEqualDiff(spec_text, ''.join(lines))
1485
 
 
1486
 
 
1487
1288
class KnitTests(TestCaseWithTransport):
1488
1289
    """Class containing knit test helper routines."""
1489
1290
 
1495
1296
class TestBadShaError(KnitTests):
1496
1297
    """Tests for handling of sha errors."""
1497
1298
 
1498
 
    def test_sha_exception_has_text(self):
 
1299
    def test_exception_has_text(self):
1499
1300
        # having the failed text included in the error allows for recovery.
1500
1301
        source = self.make_test_knit()
1501
1302
        target = self.make_test_knit(name="target")
1512
1313
        target.insert_record_stream(
1513
1314
            source.get_record_stream([broken], 'unordered', False))
1514
1315
        err = self.assertRaises(errors.KnitCorrupt,
1515
 
            target.get_record_stream([broken], 'unordered', True
1516
 
            ).next().get_bytes_as, 'chunked')
 
1316
            target.get_record_stream([broken], 'unordered', True).next)
1517
1317
        self.assertEqual(['gam\n', 'bar\n'], err.content)
1518
1318
        # Test for formatting with live data
1519
1319
        self.assertStartsWith(str(err), "Knit ")
1724
1524
            [('parent',)])])
1725
1525
        # but neither should have added data:
1726
1526
        self.assertEqual([[], [], [], []], self.caught_entries)
1727
 
 
 
1527
        
1728
1528
    def test_add_version_different_dup(self):
1729
1529
        index = self.two_graph_index(deltas=True, catch_adds=True)
1730
1530
        # change options
1731
1531
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1732
 
            [(('tip',), 'line-delta', (None, 0, 100), [('parent',)])])
 
1532
            [(('tip',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
 
1533
        self.assertRaises(errors.KnitCorrupt, index.add_records,
 
1534
            [(('tip',), 'line-delta,no-eol', (None, 0, 100), [('parent',)])])
1733
1535
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1734
1536
            [(('tip',), 'fulltext', (None, 0, 100), [('parent',)])])
1735
1537
        # parents
1736
1538
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1737
1539
            [(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
1738
1540
        self.assertEqual([], self.caught_entries)
1739
 
 
 
1541
        
1740
1542
    def test_add_versions_nodeltas(self):
1741
1543
        index = self.two_graph_index(catch_adds=True)
1742
1544
        index.add_records([
1784
1586
            [('parent',)])])
1785
1587
        # but neither should have added data.
1786
1588
        self.assertEqual([[], [], [], []], self.caught_entries)
1787
 
 
 
1589
        
1788
1590
    def test_add_versions_different_dup(self):
1789
1591
        index = self.two_graph_index(deltas=True, catch_adds=True)
1790
1592
        # change options
1791
1593
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1792
 
            [(('tip',), 'line-delta', (None, 0, 100), [('parent',)])])
 
1594
            [(('tip',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
 
1595
        self.assertRaises(errors.KnitCorrupt, index.add_records,
 
1596
            [(('tip',), 'line-delta,no-eol', (None, 0, 100), [('parent',)])])
1793
1597
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1794
1598
            [(('tip',), 'fulltext', (None, 0, 100), [('parent',)])])
1795
1599
        # parents
1798
1602
        # change options in the second record
1799
1603
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1800
1604
            [(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)]),
1801
 
             (('tip',), 'line-delta', (None, 0, 100), [('parent',)])])
 
1605
             (('tip',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
1802
1606
        self.assertEqual([], self.caught_entries)
1803
1607
 
1804
 
    def make_g_index_missing_compression_parent(self):
1805
 
        graph_index = self.make_g_index('missing_comp', 2,
1806
 
            [(('tip', ), ' 100 78',
1807
 
              ([('missing-parent', ), ('ghost', )], [('missing-parent', )]))])
1808
 
        return graph_index
1809
 
 
1810
 
    def make_g_index_missing_parent(self):
1811
 
        graph_index = self.make_g_index('missing_parent', 2,
1812
 
            [(('parent', ), ' 100 78', ([], [])),
1813
 
             (('tip', ), ' 100 78',
1814
 
              ([('parent', ), ('missing-parent', )], [('parent', )])),
1815
 
              ])
1816
 
        return graph_index
1817
 
 
1818
 
    def make_g_index_no_external_refs(self):
1819
 
        graph_index = self.make_g_index('no_external_refs', 2,
1820
 
            [(('rev', ), ' 100 78',
1821
 
              ([('parent', ), ('ghost', )], []))])
1822
 
        return graph_index
1823
 
 
1824
 
    def test_add_good_unvalidated_index(self):
1825
 
        unvalidated = self.make_g_index_no_external_refs()
1826
 
        combined = CombinedGraphIndex([unvalidated])
1827
 
        index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1828
 
        index.scan_unvalidated_index(unvalidated)
1829
 
        self.assertEqual(frozenset(), index.get_missing_compression_parents())
1830
 
 
1831
 
    def test_add_missing_compression_parent_unvalidated_index(self):
1832
 
        unvalidated = self.make_g_index_missing_compression_parent()
1833
 
        combined = CombinedGraphIndex([unvalidated])
1834
 
        index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1835
 
        index.scan_unvalidated_index(unvalidated)
1836
 
        # This also checks that its only the compression parent that is
1837
 
        # examined, otherwise 'ghost' would also be reported as a missing
1838
 
        # parent.
1839
 
        self.assertEqual(
1840
 
            frozenset([('missing-parent',)]),
1841
 
            index.get_missing_compression_parents())
1842
 
 
1843
 
    def test_add_missing_noncompression_parent_unvalidated_index(self):
1844
 
        unvalidated = self.make_g_index_missing_parent()
1845
 
        combined = CombinedGraphIndex([unvalidated])
1846
 
        index = _KnitGraphIndex(combined, lambda: True, deltas=True,
1847
 
            track_external_parent_refs=True)
1848
 
        index.scan_unvalidated_index(unvalidated)
1849
 
        self.assertEqual(
1850
 
            frozenset([('missing-parent',)]), index.get_missing_parents())
1851
 
 
1852
 
    def test_track_external_parent_refs(self):
1853
 
        g_index = self.make_g_index('empty', 2, [])
1854
 
        combined = CombinedGraphIndex([g_index])
1855
 
        index = _KnitGraphIndex(combined, lambda: True, deltas=True,
1856
 
            add_callback=self.catch_add, track_external_parent_refs=True)
1857
 
        self.caught_entries = []
1858
 
        index.add_records([
1859
 
            (('new-key',), 'fulltext,no-eol', (None, 50, 60),
1860
 
             [('parent-1',), ('parent-2',)])])
1861
 
        self.assertEqual(
1862
 
            frozenset([('parent-1',), ('parent-2',)]),
1863
 
            index.get_missing_parents())
1864
 
 
1865
 
    def test_add_unvalidated_index_with_present_external_references(self):
1866
 
        index = self.two_graph_index(deltas=True)
1867
 
        # Ugly hack to get at one of the underlying GraphIndex objects that
1868
 
        # two_graph_index built.
1869
 
        unvalidated = index._graph_index._indices[1]
1870
 
        # 'parent' is an external ref of _indices[1] (unvalidated), but is
1871
 
        # present in _indices[0].
1872
 
        index.scan_unvalidated_index(unvalidated)
1873
 
        self.assertEqual(frozenset(), index.get_missing_compression_parents())
1874
 
 
1875
 
    def make_new_missing_parent_g_index(self, name):
1876
 
        missing_parent = name + '-missing-parent'
1877
 
        graph_index = self.make_g_index(name, 2,
1878
 
            [((name + 'tip', ), ' 100 78',
1879
 
              ([(missing_parent, ), ('ghost', )], [(missing_parent, )]))])
1880
 
        return graph_index
1881
 
 
1882
 
    def test_add_mulitiple_unvalidated_indices_with_missing_parents(self):
1883
 
        g_index_1 = self.make_new_missing_parent_g_index('one')
1884
 
        g_index_2 = self.make_new_missing_parent_g_index('two')
1885
 
        combined = CombinedGraphIndex([g_index_1, g_index_2])
1886
 
        index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1887
 
        index.scan_unvalidated_index(g_index_1)
1888
 
        index.scan_unvalidated_index(g_index_2)
1889
 
        self.assertEqual(
1890
 
            frozenset([('one-missing-parent',), ('two-missing-parent',)]),
1891
 
            index.get_missing_compression_parents())
1892
 
 
1893
 
    def test_add_mulitiple_unvalidated_indices_with_mutual_dependencies(self):
1894
 
        graph_index_a = self.make_g_index('one', 2,
1895
 
            [(('parent-one', ), ' 100 78', ([('non-compression-parent',)], [])),
1896
 
             (('child-of-two', ), ' 100 78',
1897
 
              ([('parent-two',)], [('parent-two',)]))])
1898
 
        graph_index_b = self.make_g_index('two', 2,
1899
 
            [(('parent-two', ), ' 100 78', ([('non-compression-parent',)], [])),
1900
 
             (('child-of-one', ), ' 100 78',
1901
 
              ([('parent-one',)], [('parent-one',)]))])
1902
 
        combined = CombinedGraphIndex([graph_index_a, graph_index_b])
1903
 
        index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1904
 
        index.scan_unvalidated_index(graph_index_a)
1905
 
        index.scan_unvalidated_index(graph_index_b)
1906
 
        self.assertEqual(
1907
 
            frozenset([]), index.get_missing_compression_parents())
1908
 
 
1909
1608
 
1910
1609
class TestNoParentsGraphIndexKnit(KnitTests):
1911
1610
    """Tests for knits using _KnitGraphIndex with no parents."""
1919
1618
        size = trans.put_file(name, stream)
1920
1619
        return GraphIndex(trans, name, size)
1921
1620
 
1922
 
    def test_add_good_unvalidated_index(self):
1923
 
        unvalidated = self.make_g_index('unvalidated')
1924
 
        combined = CombinedGraphIndex([unvalidated])
1925
 
        index = _KnitGraphIndex(combined, lambda: True, parents=False)
1926
 
        index.scan_unvalidated_index(unvalidated)
1927
 
        self.assertEqual(frozenset(),
1928
 
            index.get_missing_compression_parents())
1929
 
 
1930
1621
    def test_parents_deltas_incompatible(self):
1931
1622
        index = CombinedGraphIndex([])
1932
1623
        self.assertRaises(errors.KnitError, _KnitGraphIndex, lambda:True,
2013
1704
        index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000), [])])
2014
1705
        # but neither should have added data.
2015
1706
        self.assertEqual([[], [], [], []], self.caught_entries)
2016
 
 
 
1707
        
2017
1708
    def test_add_version_different_dup(self):
2018
1709
        index = self.two_graph_index(catch_adds=True)
2019
1710
        # change options
2027
1718
        self.assertRaises(errors.KnitCorrupt, index.add_records,
2028
1719
            [(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)])])
2029
1720
        self.assertEqual([], self.caught_entries)
2030
 
 
 
1721
        
2031
1722
    def test_add_versions(self):
2032
1723
        index = self.two_graph_index(catch_adds=True)
2033
1724
        index.add_records([
2065
1756
        index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000), [])])
2066
1757
        # but neither should have added data.
2067
1758
        self.assertEqual([[], [], [], []], self.caught_entries)
2068
 
 
 
1759
        
2069
1760
    def test_add_versions_different_dup(self):
2070
1761
        index = self.two_graph_index(catch_adds=True)
2071
1762
        # change options
2085
1776
        self.assertEqual([], self.caught_entries)
2086
1777
 
2087
1778
 
2088
 
class TestKnitVersionedFiles(KnitTests):
2089
 
 
2090
 
    def assertGroupKeysForIo(self, exp_groups, keys, non_local_keys,
2091
 
                             positions, _min_buffer_size=None):
2092
 
        kvf = self.make_test_knit()
2093
 
        if _min_buffer_size is None:
2094
 
            _min_buffer_size = knit._STREAM_MIN_BUFFER_SIZE
2095
 
        self.assertEqual(exp_groups, kvf._group_keys_for_io(keys,
2096
 
                                        non_local_keys, positions,
2097
 
                                        _min_buffer_size=_min_buffer_size))
2098
 
 
2099
 
    def assertSplitByPrefix(self, expected_map, expected_prefix_order,
2100
 
                            keys):
2101
 
        split, prefix_order = KnitVersionedFiles._split_by_prefix(keys)
2102
 
        self.assertEqual(expected_map, split)
2103
 
        self.assertEqual(expected_prefix_order, prefix_order)
2104
 
 
2105
 
    def test__group_keys_for_io(self):
2106
 
        ft_detail = ('fulltext', False)
2107
 
        ld_detail = ('line-delta', False)
2108
 
        f_a = ('f', 'a')
2109
 
        f_b = ('f', 'b')
2110
 
        f_c = ('f', 'c')
2111
 
        g_a = ('g', 'a')
2112
 
        g_b = ('g', 'b')
2113
 
        g_c = ('g', 'c')
2114
 
        positions = {
2115
 
            f_a: (ft_detail, (f_a, 0, 100), None),
2116
 
            f_b: (ld_detail, (f_b, 100, 21), f_a),
2117
 
            f_c: (ld_detail, (f_c, 180, 15), f_b),
2118
 
            g_a: (ft_detail, (g_a, 121, 35), None),
2119
 
            g_b: (ld_detail, (g_b, 156, 12), g_a),
2120
 
            g_c: (ld_detail, (g_c, 195, 13), g_a),
2121
 
            }
2122
 
        self.assertGroupKeysForIo([([f_a], set())],
2123
 
                                  [f_a], [], positions)
2124
 
        self.assertGroupKeysForIo([([f_a], set([f_a]))],
2125
 
                                  [f_a], [f_a], positions)
2126
 
        self.assertGroupKeysForIo([([f_a, f_b], set([]))],
2127
 
                                  [f_a, f_b], [], positions)
2128
 
        self.assertGroupKeysForIo([([f_a, f_b], set([f_b]))],
2129
 
                                  [f_a, f_b], [f_b], positions)
2130
 
        self.assertGroupKeysForIo([([f_a, f_b, g_a, g_b], set())],
2131
 
                                  [f_a, g_a, f_b, g_b], [], positions)
2132
 
        self.assertGroupKeysForIo([([f_a, f_b, g_a, g_b], set())],
2133
 
                                  [f_a, g_a, f_b, g_b], [], positions,
2134
 
                                  _min_buffer_size=150)
2135
 
        self.assertGroupKeysForIo([([f_a, f_b], set()), ([g_a, g_b], set())],
2136
 
                                  [f_a, g_a, f_b, g_b], [], positions,
2137
 
                                  _min_buffer_size=100)
2138
 
        self.assertGroupKeysForIo([([f_c], set()), ([g_b], set())],
2139
 
                                  [f_c, g_b], [], positions,
2140
 
                                  _min_buffer_size=125)
2141
 
        self.assertGroupKeysForIo([([g_b, f_c], set())],
2142
 
                                  [g_b, f_c], [], positions,
2143
 
                                  _min_buffer_size=125)
2144
 
 
2145
 
    def test__split_by_prefix(self):
2146
 
        self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2147
 
                                  'g': [('g', 'b'), ('g', 'a')],
2148
 
                                 }, ['f', 'g'],
2149
 
                                 [('f', 'a'), ('g', 'b'),
2150
 
                                  ('g', 'a'), ('f', 'b')])
2151
 
 
2152
 
        self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2153
 
                                  'g': [('g', 'b'), ('g', 'a')],
2154
 
                                 }, ['f', 'g'],
2155
 
                                 [('f', 'a'), ('f', 'b'),
2156
 
                                  ('g', 'b'), ('g', 'a')])
2157
 
 
2158
 
        self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2159
 
                                  'g': [('g', 'b'), ('g', 'a')],
2160
 
                                 }, ['f', 'g'],
2161
 
                                 [('f', 'a'), ('f', 'b'),
2162
 
                                  ('g', 'b'), ('g', 'a')])
2163
 
 
2164
 
        self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2165
 
                                  'g': [('g', 'b'), ('g', 'a')],
2166
 
                                  '': [('a',), ('b',)]
2167
 
                                 }, ['f', 'g', ''],
2168
 
                                 [('f', 'a'), ('g', 'b'),
2169
 
                                  ('a',), ('b',),
2170
 
                                  ('g', 'a'), ('f', 'b')])
2171
 
 
2172
 
 
2173
1779
class TestStacking(KnitTests):
2174
1780
 
2175
1781
    def get_basis_and_test_knit(self):
2230
1836
        # self.assertEqual([("annotate", key_basis)], basis.calls)
2231
1837
        self.assertEqual([('get_parent_map', set([key_basis])),
2232
1838
            ('get_parent_map', set([key_basis])),
 
1839
            ('get_parent_map', set([key_basis])),
2233
1840
            ('get_record_stream', [key_basis], 'unordered', True)],
2234
1841
            basis.calls)
2235
1842
 
2236
1843
    def test_check(self):
2237
1844
        # At the moment checking a stacked knit does implicitly check the
2238
 
        # fallback files.
 
1845
        # fallback files.  
2239
1846
        basis, test = self.get_basis_and_test_knit()
2240
1847
        test.check()
2241
1848
 
2333
1940
                True).next()
2334
1941
            self.assertEqual(record.key, result[0])
2335
1942
            self.assertEqual(record.sha1, result[1])
2336
 
            # We used to check that the storage kind matched, but actually it
2337
 
            # depends on whether it was sourced from the basis, or in a single
2338
 
            # group, because asking for full texts returns proxy objects to a
2339
 
            # _ContentMapGenerator object; so checking the kind is unneeded.
 
1943
            self.assertEqual(record.storage_kind, result[2])
2340
1944
            self.assertEqual(record.get_bytes_as('fulltext'), result[3])
2341
1945
        # It's not strictly minimal, but it seems reasonable for now for it to
2342
1946
        # ask which fallbacks have which parents.
2479
2083
 
2480
2084
    def test_iter_lines_added_or_present_in_keys(self):
2481
2085
        # Lines from the basis are returned, and lines for a given key are only
2482
 
        # returned once.
 
2086
        # returned once. 
2483
2087
        key1 = ('foo1',)
2484
2088
        key2 = ('foo2',)
2485
2089
        # all sources are asked for keys:
2573
2177
        self.assertEqual(set([key_left, key_right]), set(last_call[1]))
2574
2178
        self.assertEqual('unordered', last_call[2])
2575
2179
        self.assertEqual(True, last_call[3])
2576
 
 
2577
 
 
2578
 
class TestNetworkBehaviour(KnitTests):
2579
 
    """Tests for getting data out of/into knits over the network."""
2580
 
 
2581
 
    def test_include_delta_closure_generates_a_knit_delta_closure(self):
2582
 
        vf = self.make_test_knit(name='test')
2583
 
        # put in three texts, giving ft, delta, delta
2584
 
        vf.add_lines(('base',), (), ['base\n', 'content\n'])
2585
 
        vf.add_lines(('d1',), (('base',),), ['d1\n'])
2586
 
        vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2587
 
        # But heuristics could interfere, so check what happened:
2588
 
        self.assertEqual(['knit-ft-gz', 'knit-delta-gz', 'knit-delta-gz'],
2589
 
            [record.storage_kind for record in
2590
 
             vf.get_record_stream([('base',), ('d1',), ('d2',)],
2591
 
                'topological', False)])
2592
 
        # generate a stream of just the deltas include_delta_closure=True,
2593
 
        # serialise to the network, and check that we get a delta closure on the wire.
2594
 
        stream = vf.get_record_stream([('d1',), ('d2',)], 'topological', True)
2595
 
        netb = [record.get_bytes_as(record.storage_kind) for record in stream]
2596
 
        # The first bytes should be a memo from _ContentMapGenerator, and the
2597
 
        # second bytes should be empty (because its a API proxy not something
2598
 
        # for wire serialisation.
2599
 
        self.assertEqual('', netb[1])
2600
 
        bytes = netb[0]
2601
 
        kind, line_end = network_bytes_to_kind_and_offset(bytes)
2602
 
        self.assertEqual('knit-delta-closure', kind)
2603
 
 
2604
 
 
2605
 
class TestContentMapGenerator(KnitTests):
2606
 
    """Tests for ContentMapGenerator"""
2607
 
 
2608
 
    def test_get_record_stream_gives_records(self):
2609
 
        vf = self.make_test_knit(name='test')
2610
 
        # put in three texts, giving ft, delta, delta
2611
 
        vf.add_lines(('base',), (), ['base\n', 'content\n'])
2612
 
        vf.add_lines(('d1',), (('base',),), ['d1\n'])
2613
 
        vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2614
 
        keys = [('d1',), ('d2',)]
2615
 
        generator = _VFContentMapGenerator(vf, keys,
2616
 
            global_map=vf.get_parent_map(keys))
2617
 
        for record in generator.get_record_stream():
2618
 
            if record.key == ('d1',):
2619
 
                self.assertEqual('d1\n', record.get_bytes_as('fulltext'))
2620
 
            else:
2621
 
                self.assertEqual('d2\n', record.get_bytes_as('fulltext'))
2622
 
 
2623
 
    def test_get_record_stream_kinds_are_raw(self):
2624
 
        vf = self.make_test_knit(name='test')
2625
 
        # put in three texts, giving ft, delta, delta
2626
 
        vf.add_lines(('base',), (), ['base\n', 'content\n'])
2627
 
        vf.add_lines(('d1',), (('base',),), ['d1\n'])
2628
 
        vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2629
 
        keys = [('base',), ('d1',), ('d2',)]
2630
 
        generator = _VFContentMapGenerator(vf, keys,
2631
 
            global_map=vf.get_parent_map(keys))
2632
 
        kinds = {('base',): 'knit-delta-closure',
2633
 
            ('d1',): 'knit-delta-closure-ref',
2634
 
            ('d2',): 'knit-delta-closure-ref',
2635
 
            }
2636
 
        for record in generator.get_record_stream():
2637
 
            self.assertEqual(kinds[record.key], record.storage_kind)