~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_versionedfile.py

merge 2.0 branch rev 4647

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
from bzrlib import (
28
28
    errors,
 
29
    graph as _mod_graph,
29
30
    groupcompress,
30
31
    knit as _mod_knit,
31
32
    osutils,
32
33
    progress,
 
34
    ui,
33
35
    )
34
36
from bzrlib.errors import (
35
37
                           RevisionNotPresent,
1471
1473
            self.addCleanup(lambda:self.cleanup(files))
1472
1474
        return files
1473
1475
 
 
1476
    def get_simple_key(self, suffix):
 
1477
        """Return a key for the object under test."""
 
1478
        if self.key_length == 1:
 
1479
            return (suffix,)
 
1480
        else:
 
1481
            return ('FileA',) + (suffix,)
 
1482
 
 
1483
    def test_add_lines(self):
 
1484
        f = self.get_versionedfiles()
 
1485
        key0 = self.get_simple_key('r0')
 
1486
        key1 = self.get_simple_key('r1')
 
1487
        key2 = self.get_simple_key('r2')
 
1488
        keyf = self.get_simple_key('foo')
 
1489
        f.add_lines(key0, [], ['a\n', 'b\n'])
 
1490
        if self.graph:
 
1491
            f.add_lines(key1, [key0], ['b\n', 'c\n'])
 
1492
        else:
 
1493
            f.add_lines(key1, [], ['b\n', 'c\n'])
 
1494
        keys = f.keys()
 
1495
        self.assertTrue(key0 in keys)
 
1496
        self.assertTrue(key1 in keys)
 
1497
        records = []
 
1498
        for record in f.get_record_stream([key0, key1], 'unordered', True):
 
1499
            records.append((record.key, record.get_bytes_as('fulltext')))
 
1500
        records.sort()
 
1501
        self.assertEqual([(key0, 'a\nb\n'), (key1, 'b\nc\n')], records)
 
1502
 
 
1503
    def test__add_text(self):
 
1504
        f = self.get_versionedfiles()
 
1505
        key0 = self.get_simple_key('r0')
 
1506
        key1 = self.get_simple_key('r1')
 
1507
        key2 = self.get_simple_key('r2')
 
1508
        keyf = self.get_simple_key('foo')
 
1509
        f._add_text(key0, [], 'a\nb\n')
 
1510
        if self.graph:
 
1511
            f._add_text(key1, [key0], 'b\nc\n')
 
1512
        else:
 
1513
            f._add_text(key1, [], 'b\nc\n')
 
1514
        keys = f.keys()
 
1515
        self.assertTrue(key0 in keys)
 
1516
        self.assertTrue(key1 in keys)
 
1517
        records = []
 
1518
        for record in f.get_record_stream([key0, key1], 'unordered', True):
 
1519
            records.append((record.key, record.get_bytes_as('fulltext')))
 
1520
        records.sort()
 
1521
        self.assertEqual([(key0, 'a\nb\n'), (key1, 'b\nc\n')], records)
 
1522
 
1474
1523
    def test_annotate(self):
1475
1524
        files = self.get_versionedfiles()
1476
1525
        self.get_diamond_files(files)
1510
1559
        self.assertRaises(RevisionNotPresent,
1511
1560
            files.annotate, prefix + ('missing-key',))
1512
1561
 
 
1562
    def test_check_no_parameters(self):
 
1563
        files = self.get_versionedfiles()
 
1564
 
 
1565
    def test_check_progressbar_parameter(self):
 
1566
        """A progress bar can be supplied because check can be a generator."""
 
1567
        pb = ui.ui_factory.nested_progress_bar()
 
1568
        self.addCleanup(pb.finished)
 
1569
        files = self.get_versionedfiles()
 
1570
        files.check(progress_bar=pb)
 
1571
 
 
1572
    def test_check_with_keys_becomes_generator(self):
 
1573
        files = self.get_versionedfiles()
 
1574
        self.get_diamond_files(files)
 
1575
        keys = files.keys()
 
1576
        entries = files.check(keys=keys)
 
1577
        seen = set()
 
1578
        # Texts output should be fulltexts.
 
1579
        self.capture_stream(files, entries, seen.add,
 
1580
            files.get_parent_map(keys), require_fulltext=True)
 
1581
        # All texts should be output.
 
1582
        self.assertEqual(set(keys), seen)
 
1583
 
1513
1584
    def test_construct(self):
1514
1585
        """Each parameterised test can be constructed on a transport."""
1515
1586
        files = self.get_versionedfiles()
1520
1591
            trailing_eol=trailing_eol, nograph=not self.graph,
1521
1592
            left_only=left_only, nokeys=nokeys)
1522
1593
 
1523
 
    def test_add_lines_nostoresha(self):
 
1594
    def _add_content_nostoresha(self, add_lines):
1524
1595
        """When nostore_sha is supplied using old content raises."""
1525
1596
        vf = self.get_versionedfiles()
1526
1597
        empty_text = ('a', [])
1528
1599
        sample_text_no_nl = ('c', ["foo\n", "bar"])
1529
1600
        shas = []
1530
1601
        for version, lines in (empty_text, sample_text_nl, sample_text_no_nl):
1531
 
            sha, _, _ = vf.add_lines(self.get_simple_key(version), [], lines)
 
1602
            if add_lines:
 
1603
                sha, _, _ = vf.add_lines(self.get_simple_key(version), [],
 
1604
                                         lines)
 
1605
            else:
 
1606
                sha, _, _ = vf._add_text(self.get_simple_key(version), [],
 
1607
                                         ''.join(lines))
1532
1608
            shas.append(sha)
1533
1609
        # we now have a copy of all the lines in the vf.
1534
1610
        for sha, (version, lines) in zip(
1537
1613
            self.assertRaises(errors.ExistingContent,
1538
1614
                vf.add_lines, new_key, [], lines,
1539
1615
                nostore_sha=sha)
 
1616
            self.assertRaises(errors.ExistingContent,
 
1617
                vf._add_text, new_key, [], ''.join(lines),
 
1618
                nostore_sha=sha)
1540
1619
            # and no new version should have been added.
1541
1620
            record = vf.get_record_stream([new_key], 'unordered', True).next()
1542
1621
            self.assertEqual('absent', record.storage_kind)
1543
1622
 
 
1623
    def test_add_lines_nostoresha(self):
 
1624
        self._add_content_nostoresha(add_lines=True)
 
1625
 
 
1626
    def test__add_text_nostoresha(self):
 
1627
        self._add_content_nostoresha(add_lines=False)
 
1628
 
1544
1629
    def test_add_lines_return(self):
1545
1630
        files = self.get_versionedfiles()
1546
1631
        # save code by using the stock data insertion helper.
1653
1738
            f.get_record_stream([key_b], 'unordered', True
1654
1739
                ).next().get_bytes_as('fulltext'))
1655
1740
 
 
1741
    def test_get_known_graph_ancestry(self):
 
1742
        f = self.get_versionedfiles()
 
1743
        if not self.graph:
 
1744
            raise TestNotApplicable('ancestry info only relevant with graph.')
 
1745
        key_a = self.get_simple_key('a')
 
1746
        key_b = self.get_simple_key('b')
 
1747
        key_c = self.get_simple_key('c')
 
1748
        # A
 
1749
        # |\
 
1750
        # | B
 
1751
        # |/
 
1752
        # C
 
1753
        f.add_lines(key_a, [], ['\n'])
 
1754
        f.add_lines(key_b, [key_a], ['\n'])
 
1755
        f.add_lines(key_c, [key_a, key_b], ['\n'])
 
1756
        kg = f.get_known_graph_ancestry([key_c])
 
1757
        self.assertIsInstance(kg, _mod_graph.KnownGraph)
 
1758
        self.assertEqual([key_a, key_b, key_c], list(kg.topo_sort()))
 
1759
 
 
1760
    def test_known_graph_with_fallbacks(self):
 
1761
        f = self.get_versionedfiles('files')
 
1762
        if not self.graph:
 
1763
            raise TestNotApplicable('ancestry info only relevant with graph.')
 
1764
        if getattr(f, 'add_fallback_versioned_files', None) is None:
 
1765
            raise TestNotApplicable("%s doesn't support fallbacks"
 
1766
                                    % (f.__class__.__name__,))
 
1767
        key_a = self.get_simple_key('a')
 
1768
        key_b = self.get_simple_key('b')
 
1769
        key_c = self.get_simple_key('c')
 
1770
        # A     only in fallback
 
1771
        # |\
 
1772
        # | B
 
1773
        # |/
 
1774
        # C
 
1775
        g = self.get_versionedfiles('fallback')
 
1776
        g.add_lines(key_a, [], ['\n'])
 
1777
        f.add_fallback_versioned_files(g)
 
1778
        f.add_lines(key_b, [key_a], ['\n'])
 
1779
        f.add_lines(key_c, [key_a, key_b], ['\n'])
 
1780
        kg = f.get_known_graph_ancestry([key_c])
 
1781
        self.assertEqual([key_a, key_b, key_c], list(kg.topo_sort()))
 
1782
 
1656
1783
    def test_get_record_stream_empty(self):
1657
1784
        """An empty stream can be requested without error."""
1658
1785
        f = self.get_versionedfiles()
1669
1796
             'knit-delta-closure', 'knit-delta-closure-ref',
1670
1797
             'groupcompress-block', 'groupcompress-block-ref'])
1671
1798
 
1672
 
    def capture_stream(self, f, entries, on_seen, parents):
 
1799
    def capture_stream(self, f, entries, on_seen, parents,
 
1800
        require_fulltext=False):
1673
1801
        """Capture a stream for testing."""
1674
1802
        for factory in entries:
1675
1803
            on_seen(factory.key)
1680
1808
            self.assertEqual(parents[factory.key], factory.parents)
1681
1809
            self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
1682
1810
                str)
 
1811
            if require_fulltext:
 
1812
                factory.get_bytes_as('fulltext')
1683
1813
 
1684
1814
    def test_get_record_stream_interface(self):
1685
1815
        """each item in a stream has to provide a regular interface."""
1692
1822
        self.capture_stream(files, entries, seen.add, parent_map)
1693
1823
        self.assertEqual(set(keys), seen)
1694
1824
 
1695
 
    def get_simple_key(self, suffix):
1696
 
        """Return a key for the object under test."""
1697
 
        if self.key_length == 1:
1698
 
            return (suffix,)
1699
 
        else:
1700
 
            return ('FileA',) + (suffix,)
1701
 
 
1702
1825
    def get_keys_and_sort_order(self):
1703
1826
        """Get diamond test keys list, and their sort ordering."""
1704
1827
        if self.key_length == 1:
2067
2190
        else:
2068
2191
            return None
2069
2192
 
 
2193
    def test_get_annotator(self):
 
2194
        files = self.get_versionedfiles()
 
2195
        self.get_diamond_files(files)
 
2196
        origin_key = self.get_simple_key('origin')
 
2197
        base_key = self.get_simple_key('base')
 
2198
        left_key = self.get_simple_key('left')
 
2199
        right_key = self.get_simple_key('right')
 
2200
        merged_key = self.get_simple_key('merged')
 
2201
        # annotator = files.get_annotator()
 
2202
        # introduced full text
 
2203
        origins, lines = files.get_annotator().annotate(origin_key)
 
2204
        self.assertEqual([(origin_key,)], origins)
 
2205
        self.assertEqual(['origin\n'], lines)
 
2206
        # a delta
 
2207
        origins, lines = files.get_annotator().annotate(base_key)
 
2208
        self.assertEqual([(base_key,)], origins)
 
2209
        # a merge
 
2210
        origins, lines = files.get_annotator().annotate(merged_key)
 
2211
        if self.graph:
 
2212
            self.assertEqual([
 
2213
                (base_key,),
 
2214
                (left_key,),
 
2215
                (right_key,),
 
2216
                (merged_key,),
 
2217
                ], origins)
 
2218
        else:
 
2219
            # Without a graph everything is new.
 
2220
            self.assertEqual([
 
2221
                (merged_key,),
 
2222
                (merged_key,),
 
2223
                (merged_key,),
 
2224
                (merged_key,),
 
2225
                ], origins)
 
2226
        self.assertRaises(RevisionNotPresent,
 
2227
            files.get_annotator().annotate, self.get_simple_key('missing-key'))
 
2228
 
2070
2229
    def test_get_parent_map(self):
2071
2230
        files = self.get_versionedfiles()
2072
2231
        if self.key_length == 1:
2547
2706
        self.assertRaises(NotImplementedError,
2548
2707
                self.texts.add_mpdiffs, [])
2549
2708
 
2550
 
    def test_check(self):
2551
 
        self.assertTrue(self.texts.check())
 
2709
    def test_check_noerrors(self):
 
2710
        self.texts.check()
2552
2711
 
2553
2712
    def test_insert_record_stream(self):
2554
2713
        self.assertRaises(NotImplementedError, self.texts.insert_record_stream,