~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_knit.py

  • Committer: Matt Nordhoff
  • Date: 2009-04-04 02:50:01 UTC
  • mfrom: (4253 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4256.
  • Revision ID: mnordhoff@mattnordhoff.com-20090404025001-z1403k0tatmc8l91
Merge bzr.dev, fixing conflicts.

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for Knit data structure"""
18
18
 
299
299
        access = self.get_access()
300
300
        memos = access.add_raw_records([('key', 10)], '1234567890')
301
301
        self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
302
 
 
 
302
 
303
303
    def test_add_several_raw_records(self):
304
304
        """add_raw_records with many records and read some back."""
305
305
        access = self.get_access()
368
368
        """
369
369
        tree = self.make_branch_and_memory_tree('tree')
370
370
        tree.lock_write()
371
 
        try:
372
 
            tree.add([''], ['root-id'])
373
 
            tree.commit('one', rev_id='rev-1')
374
 
            tree.commit('two', rev_id='rev-2')
375
 
            tree.commit('three', rev_id='rev-3')
376
 
            # Pack these two revisions into another pack file, but don't remove
377
 
            # the originials
378
 
            repo = tree.branch.repository
379
 
            collection = repo._pack_collection
380
 
            collection.ensure_loaded()
381
 
            orig_packs = collection.packs
382
 
            packer = pack_repo.Packer(collection, orig_packs, '.testpack')
383
 
            new_pack = packer.pack()
384
 
 
385
 
            vf = tree.branch.repository.revisions
386
 
        finally:
387
 
            tree.unlock()
388
 
        tree.branch.repository.lock_read()
389
371
        self.addCleanup(tree.branch.repository.unlock)
 
372
        tree.add([''], ['root-id'])
 
373
        tree.commit('one', rev_id='rev-1')
 
374
        tree.commit('two', rev_id='rev-2')
 
375
        tree.commit('three', rev_id='rev-3')
 
376
        # Pack these three revisions into another pack file, but don't remove
 
377
        # the originals
 
378
        repo = tree.branch.repository
 
379
        collection = repo._pack_collection
 
380
        collection.ensure_loaded()
 
381
        orig_packs = collection.packs
 
382
        packer = pack_repo.Packer(collection, orig_packs, '.testpack')
 
383
        new_pack = packer.pack()
 
384
        # forget about the new pack
 
385
        collection.reset()
 
386
        repo.refresh_data()
 
387
        vf = tree.branch.repository.revisions
390
388
        del tree
391
389
        # Set up a reload() function that switches to using the new pack file
392
390
        new_index = new_pack.revision_index
1093
1091
            call[1][1].getvalue())
1094
1092
        self.assertEqual({'create_parent_dir': True}, call[2])
1095
1093
 
 
1094
    def assertTotalBuildSize(self, size, keys, positions):
 
1095
        self.assertEqual(size,
 
1096
                         knit._get_total_build_size(None, keys, positions))
 
1097
 
 
1098
    def test__get_total_build_size(self):
 
1099
        positions = {
 
1100
            ('a',): (('fulltext', False), (('a',), 0, 100), None),
 
1101
            ('b',): (('line-delta', False), (('b',), 100, 21), ('a',)),
 
1102
            ('c',): (('line-delta', False), (('c',), 121, 35), ('b',)),
 
1103
            ('d',): (('line-delta', False), (('d',), 156, 12), ('b',)),
 
1104
            }
 
1105
        self.assertTotalBuildSize(100, [('a',)], positions)
 
1106
        self.assertTotalBuildSize(121, [('b',)], positions)
 
1107
        # c needs both a & b
 
1108
        self.assertTotalBuildSize(156, [('c',)], positions)
 
1109
        # we shouldn't count 'b' twice
 
1110
        self.assertTotalBuildSize(156, [('b',), ('c',)], positions)
 
1111
        self.assertTotalBuildSize(133, [('d',)], positions)
 
1112
        self.assertTotalBuildSize(168, [('c',), ('d',)], positions)
 
1113
 
1096
1114
    def test_get_position(self):
1097
1115
        transport = MockTransport([
1098
1116
            _KndxIndex.HEADER,
1536
1554
            [('parent',)])])
1537
1555
        # but neither should have added data:
1538
1556
        self.assertEqual([[], [], [], []], self.caught_entries)
1539
 
        
 
1557
 
1540
1558
    def test_add_version_different_dup(self):
1541
1559
        index = self.two_graph_index(deltas=True, catch_adds=True)
1542
1560
        # change options
1548
1566
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1549
1567
            [(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
1550
1568
        self.assertEqual([], self.caught_entries)
1551
 
        
 
1569
 
1552
1570
    def test_add_versions_nodeltas(self):
1553
1571
        index = self.two_graph_index(catch_adds=True)
1554
1572
        index.add_records([
1596
1614
            [('parent',)])])
1597
1615
        # but neither should have added data.
1598
1616
        self.assertEqual([[], [], [], []], self.caught_entries)
1599
 
        
 
1617
 
1600
1618
    def test_add_versions_different_dup(self):
1601
1619
        index = self.two_graph_index(deltas=True, catch_adds=True)
1602
1620
        # change options
1618
1636
            [(('tip', ), ' 100 78',
1619
1637
              ([('missing-parent', ), ('ghost', )], [('missing-parent', )]))])
1620
1638
        return graph_index
1621
 
    
 
1639
 
1622
1640
    def make_g_index_no_external_refs(self):
1623
1641
        graph_index = self.make_g_index('no_external_refs', 2,
1624
1642
            [(('rev', ), ' 100 78',
1687
1705
        index.scan_unvalidated_index(graph_index_b)
1688
1706
        self.assertEqual(
1689
1707
            frozenset([]), index.get_missing_compression_parents())
1690
 
        
 
1708
 
1691
1709
 
1692
1710
class TestNoParentsGraphIndexKnit(KnitTests):
1693
1711
    """Tests for knits using _KnitGraphIndex with no parents."""
1795
1813
        index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000), [])])
1796
1814
        # but neither should have added data.
1797
1815
        self.assertEqual([[], [], [], []], self.caught_entries)
1798
 
        
 
1816
 
1799
1817
    def test_add_version_different_dup(self):
1800
1818
        index = self.two_graph_index(catch_adds=True)
1801
1819
        # change options
1809
1827
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1810
1828
            [(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)])])
1811
1829
        self.assertEqual([], self.caught_entries)
1812
 
        
 
1830
 
1813
1831
    def test_add_versions(self):
1814
1832
        index = self.two_graph_index(catch_adds=True)
1815
1833
        index.add_records([
1847
1865
        index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000), [])])
1848
1866
        # but neither should have added data.
1849
1867
        self.assertEqual([[], [], [], []], self.caught_entries)
1850
 
        
 
1868
 
1851
1869
    def test_add_versions_different_dup(self):
1852
1870
        index = self.two_graph_index(catch_adds=True)
1853
1871
        # change options
1867
1885
        self.assertEqual([], self.caught_entries)
1868
1886
 
1869
1887
 
 
1888
class TestKnitVersionedFiles(KnitTests):
 
1889
 
 
1890
    def assertGroupKeysForIo(self, exp_groups, keys, non_local_keys,
 
1891
                             positions, _min_buffer_size=None):
 
1892
        kvf = self.make_test_knit()
 
1893
        if _min_buffer_size is None:
 
1894
            _min_buffer_size = knit._STREAM_MIN_BUFFER_SIZE
 
1895
        self.assertEqual(exp_groups, kvf._group_keys_for_io(keys,
 
1896
                                        non_local_keys, positions,
 
1897
                                        _min_buffer_size=_min_buffer_size))
 
1898
 
 
1899
    def assertSplitByPrefix(self, expected_map, expected_prefix_order,
 
1900
                            keys):
 
1901
        split, prefix_order = KnitVersionedFiles._split_by_prefix(keys)
 
1902
        self.assertEqual(expected_map, split)
 
1903
        self.assertEqual(expected_prefix_order, prefix_order)
 
1904
 
 
1905
    def test__group_keys_for_io(self):
 
1906
        ft_detail = ('fulltext', False)
 
1907
        ld_detail = ('line-delta', False)
 
1908
        f_a = ('f', 'a')
 
1909
        f_b = ('f', 'b')
 
1910
        f_c = ('f', 'c')
 
1911
        g_a = ('g', 'a')
 
1912
        g_b = ('g', 'b')
 
1913
        g_c = ('g', 'c')
 
1914
        positions = {
 
1915
            f_a: (ft_detail, (f_a, 0, 100), None),
 
1916
            f_b: (ld_detail, (f_b, 100, 21), f_a),
 
1917
            f_c: (ld_detail, (f_c, 180, 15), f_b),
 
1918
            g_a: (ft_detail, (g_a, 121, 35), None),
 
1919
            g_b: (ld_detail, (g_b, 156, 12), g_a),
 
1920
            g_c: (ld_detail, (g_c, 195, 13), g_a),
 
1921
            }
 
1922
        self.assertGroupKeysForIo([([f_a], set())],
 
1923
                                  [f_a], [], positions)
 
1924
        self.assertGroupKeysForIo([([f_a], set([f_a]))],
 
1925
                                  [f_a], [f_a], positions)
 
1926
        self.assertGroupKeysForIo([([f_a, f_b], set([]))],
 
1927
                                  [f_a, f_b], [], positions)
 
1928
        self.assertGroupKeysForIo([([f_a, f_b], set([f_b]))],
 
1929
                                  [f_a, f_b], [f_b], positions)
 
1930
        self.assertGroupKeysForIo([([f_a, f_b, g_a, g_b], set())],
 
1931
                                  [f_a, g_a, f_b, g_b], [], positions)
 
1932
        self.assertGroupKeysForIo([([f_a, f_b, g_a, g_b], set())],
 
1933
                                  [f_a, g_a, f_b, g_b], [], positions,
 
1934
                                  _min_buffer_size=150)
 
1935
        self.assertGroupKeysForIo([([f_a, f_b], set()), ([g_a, g_b], set())],
 
1936
                                  [f_a, g_a, f_b, g_b], [], positions,
 
1937
                                  _min_buffer_size=100)
 
1938
        self.assertGroupKeysForIo([([f_c], set()), ([g_b], set())],
 
1939
                                  [f_c, g_b], [], positions,
 
1940
                                  _min_buffer_size=125)
 
1941
        self.assertGroupKeysForIo([([g_b, f_c], set())],
 
1942
                                  [g_b, f_c], [], positions,
 
1943
                                  _min_buffer_size=125)
 
1944
 
 
1945
    def test__split_by_prefix(self):
 
1946
        self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
 
1947
                                  'g': [('g', 'b'), ('g', 'a')],
 
1948
                                 }, ['f', 'g'],
 
1949
                                 [('f', 'a'), ('g', 'b'),
 
1950
                                  ('g', 'a'), ('f', 'b')])
 
1951
 
 
1952
        self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
 
1953
                                  'g': [('g', 'b'), ('g', 'a')],
 
1954
                                 }, ['f', 'g'],
 
1955
                                 [('f', 'a'), ('f', 'b'),
 
1956
                                  ('g', 'b'), ('g', 'a')])
 
1957
 
 
1958
        self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
 
1959
                                  'g': [('g', 'b'), ('g', 'a')],
 
1960
                                 }, ['f', 'g'],
 
1961
                                 [('f', 'a'), ('f', 'b'),
 
1962
                                  ('g', 'b'), ('g', 'a')])
 
1963
 
 
1964
        self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
 
1965
                                  'g': [('g', 'b'), ('g', 'a')],
 
1966
                                  '': [('a',), ('b',)]
 
1967
                                 }, ['f', 'g', ''],
 
1968
                                 [('f', 'a'), ('g', 'b'),
 
1969
                                  ('a',), ('b',),
 
1970
                                  ('g', 'a'), ('f', 'b')])
 
1971
 
 
1972
 
1870
1973
class TestStacking(KnitTests):
1871
1974
 
1872
1975
    def get_basis_and_test_knit(self):
1933
2036
 
1934
2037
    def test_check(self):
1935
2038
        # At the moment checking a stacked knit does implicitly check the
1936
 
        # fallback files.  
 
2039
        # fallback files.
1937
2040
        basis, test = self.get_basis_and_test_knit()
1938
2041
        test.check()
1939
2042
 
2177
2280
 
2178
2281
    def test_iter_lines_added_or_present_in_keys(self):
2179
2282
        # Lines from the basis are returned, and lines for a given key are only
2180
 
        # returned once. 
 
2283
        # returned once.
2181
2284
        key1 = ('foo1',)
2182
2285
        key2 = ('foo2',)
2183
2286
        # all sources are asked for keys: