~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_versionedfile.py

  • Committer: Robert Collins
  • Date: 2009-05-11 01:59:06 UTC
  • mto: This revision was merged to the branch mainline in revision 4593.
  • Revision ID: robertc@robertcollins.net-20090511015906-6zi6a9b8tuuhipc8
Less lock thrashing in check.py.

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,
30
29
    groupcompress,
31
30
    knit as _mod_knit,
32
31
    osutils,
33
32
    progress,
34
 
    ui,
35
33
    )
36
34
from bzrlib.errors import (
37
35
                           RevisionNotPresent,
1473
1471
            self.addCleanup(lambda:self.cleanup(files))
1474
1472
        return files
1475
1473
 
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
 
 
1523
1474
    def test_annotate(self):
1524
1475
        files = self.get_versionedfiles()
1525
1476
        self.get_diamond_files(files)
1559
1510
        self.assertRaises(RevisionNotPresent,
1560
1511
            files.annotate, prefix + ('missing-key',))
1561
1512
 
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
 
 
1584
1513
    def test_construct(self):
1585
1514
        """Each parameterised test can be constructed on a transport."""
1586
1515
        files = self.get_versionedfiles()
1591
1520
            trailing_eol=trailing_eol, nograph=not self.graph,
1592
1521
            left_only=left_only, nokeys=nokeys)
1593
1522
 
1594
 
    def _add_content_nostoresha(self, add_lines):
 
1523
    def test_add_lines_nostoresha(self):
1595
1524
        """When nostore_sha is supplied using old content raises."""
1596
1525
        vf = self.get_versionedfiles()
1597
1526
        empty_text = ('a', [])
1599
1528
        sample_text_no_nl = ('c', ["foo\n", "bar"])
1600
1529
        shas = []
1601
1530
        for version, lines in (empty_text, sample_text_nl, sample_text_no_nl):
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))
 
1531
            sha, _, _ = vf.add_lines(self.get_simple_key(version), [], lines)
1608
1532
            shas.append(sha)
1609
1533
        # we now have a copy of all the lines in the vf.
1610
1534
        for sha, (version, lines) in zip(
1613
1537
            self.assertRaises(errors.ExistingContent,
1614
1538
                vf.add_lines, new_key, [], lines,
1615
1539
                nostore_sha=sha)
1616
 
            self.assertRaises(errors.ExistingContent,
1617
 
                vf._add_text, new_key, [], ''.join(lines),
1618
 
                nostore_sha=sha)
1619
1540
            # and no new version should have been added.
1620
1541
            record = vf.get_record_stream([new_key], 'unordered', True).next()
1621
1542
            self.assertEqual('absent', record.storage_kind)
1622
1543
 
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
 
 
1629
1544
    def test_add_lines_return(self):
1630
1545
        files = self.get_versionedfiles()
1631
1546
        # save code by using the stock data insertion helper.
1738
1653
            f.get_record_stream([key_b], 'unordered', True
1739
1654
                ).next().get_bytes_as('fulltext'))
1740
1655
 
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
 
 
1783
1656
    def test_get_record_stream_empty(self):
1784
1657
        """An empty stream can be requested without error."""
1785
1658
        f = self.get_versionedfiles()
1796
1669
             'knit-delta-closure', 'knit-delta-closure-ref',
1797
1670
             'groupcompress-block', 'groupcompress-block-ref'])
1798
1671
 
1799
 
    def capture_stream(self, f, entries, on_seen, parents,
1800
 
        require_fulltext=False):
 
1672
    def capture_stream(self, f, entries, on_seen, parents):
1801
1673
        """Capture a stream for testing."""
1802
1674
        for factory in entries:
1803
1675
            on_seen(factory.key)
1808
1680
            self.assertEqual(parents[factory.key], factory.parents)
1809
1681
            self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
1810
1682
                str)
1811
 
            if require_fulltext:
1812
 
                factory.get_bytes_as('fulltext')
1813
1683
 
1814
1684
    def test_get_record_stream_interface(self):
1815
1685
        """each item in a stream has to provide a regular interface."""
1822
1692
        self.capture_stream(files, entries, seen.add, parent_map)
1823
1693
        self.assertEqual(set(keys), seen)
1824
1694
 
 
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
 
1825
1702
    def get_keys_and_sort_order(self):
1826
1703
        """Get diamond test keys list, and their sort ordering."""
1827
1704
        if self.key_length == 1:
2190
2067
        else:
2191
2068
            return None
2192
2069
 
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
 
 
2229
2070
    def test_get_parent_map(self):
2230
2071
        files = self.get_versionedfiles()
2231
2072
        if self.key_length == 1:
2706
2547
        self.assertRaises(NotImplementedError,
2707
2548
                self.texts.add_mpdiffs, [])
2708
2549
 
2709
 
    def test_check_noerrors(self):
2710
 
        self.texts.check()
 
2550
    def test_check(self):
 
2551
        self.assertTrue(self.texts.check())
2711
2552
 
2712
2553
    def test_insert_record_stream(self):
2713
2554
        self.assertRaises(NotImplementedError, self.texts.insert_record_stream,