~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_versionedfile.py

(gz) Backslash escape selftest output when printing to non-unicode consoles
 (Martin [gz])

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
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
 
16
 
 
17
"""Tests for VersionedFile classes"""
 
18
 
 
19
from bzrlib import (
 
20
    errors,
 
21
    groupcompress,
 
22
    multiparent,
 
23
    tests,
 
24
    versionedfile,
 
25
    )
 
26
 
 
27
 
 
28
class Test_MPDiffGenerator(tests.TestCaseWithMemoryTransport):
 
29
    # Should this be a per vf test?
 
30
 
 
31
    def make_vf(self):
 
32
        t = self.get_transport('')
 
33
        factory = groupcompress.make_pack_factory(True, True, 1)
 
34
        return factory(t)
 
35
 
 
36
    def make_three_vf(self):
 
37
        vf = self.make_vf()
 
38
        vf.add_lines(('one',), (), ['first\n'])
 
39
        vf.add_lines(('two',), [('one',)], ['first\n', 'second\n'])
 
40
        vf.add_lines(('three',), [('one',), ('two',)],
 
41
                    ['first\n', 'second\n', 'third\n'])
 
42
        return vf
 
43
 
 
44
    def test_finds_parents(self):
 
45
        vf = self.make_three_vf()
 
46
        gen = versionedfile._MPDiffGenerator(vf, [('three',)])
 
47
        needed_keys, refcount = gen._find_needed_keys()
 
48
        self.assertEqual(sorted([('one',), ('two',), ('three',)]),
 
49
                         sorted(needed_keys))
 
50
        self.assertEqual({('one',): 1, ('two',): 1}, refcount)
 
51
 
 
52
    def test_ignores_ghost_parents(self):
 
53
        # If a parent is a ghost, it is just ignored
 
54
        vf = self.make_vf()
 
55
        vf.add_lines(('two',), [('one',)], ['first\n', 'second\n'])
 
56
        gen = versionedfile._MPDiffGenerator(vf, [('two',)])
 
57
        needed_keys, refcount = gen._find_needed_keys()
 
58
        self.assertEqual(sorted([('two',)]), sorted(needed_keys))
 
59
        # It is returned, but we don't really care as we won't extract it
 
60
        self.assertEqual({('one',): 1}, refcount)
 
61
        self.assertEqual([('one',)], sorted(gen.ghost_parents))
 
62
        self.assertEqual([], sorted(gen.present_parents))
 
63
 
 
64
    def test_raises_on_ghost_keys(self):
 
65
        # If the requested key is a ghost, then we have a problem
 
66
        vf = self.make_vf()
 
67
        gen = versionedfile._MPDiffGenerator(vf, [('one',)])
 
68
        self.assertRaises(errors.RevisionNotPresent,
 
69
                          gen._find_needed_keys)
 
70
 
 
71
    def test_refcount_multiple_children(self):
 
72
        vf = self.make_three_vf()
 
73
        gen = versionedfile._MPDiffGenerator(vf, [('two',), ('three',)])
 
74
        needed_keys, refcount = gen._find_needed_keys()
 
75
        self.assertEqual(sorted([('one',), ('two',), ('three',)]),
 
76
                         sorted(needed_keys))
 
77
        self.assertEqual({('one',): 2, ('two',): 1}, refcount)
 
78
        self.assertEqual([('one',)], sorted(gen.present_parents))
 
79
 
 
80
    def test_process_contents(self):
 
81
        vf = self.make_three_vf()
 
82
        gen = versionedfile._MPDiffGenerator(vf, [('two',), ('three',)])
 
83
        gen._find_needed_keys()
 
84
        self.assertEqual({('two',): (('one',),),
 
85
                          ('three',): (('one',), ('two',))},
 
86
                         gen.parent_map)
 
87
        self.assertEqual({('one',): 2, ('two',): 1}, gen.refcounts)
 
88
        self.assertEqual(sorted([('one',), ('two',), ('three',)]),
 
89
                         sorted(gen.needed_keys))
 
90
        stream = vf.get_record_stream(gen.needed_keys, 'topological', True)
 
91
        record = stream.next()
 
92
        self.assertEqual(('one',), record.key)
 
93
        # one is not needed in the output, but it is needed by children. As
 
94
        # such, it should end up in the various caches
 
95
        gen._process_one_record(record.key, record.get_bytes_as('chunked'))
 
96
        # The chunks should be cached, the refcount untouched
 
97
        self.assertEqual([('one',)], gen.chunks.keys())
 
98
        self.assertEqual({('one',): 2, ('two',): 1}, gen.refcounts)
 
99
        self.assertEqual([], gen.diffs.keys())
 
100
        # Next we get 'two', which is something we output, but also needed for
 
101
        # three
 
102
        record = stream.next()
 
103
        self.assertEqual(('two',), record.key)
 
104
        gen._process_one_record(record.key, record.get_bytes_as('chunked'))
 
105
        # Both are now cached, and the diff for two has been extracted, and
 
106
        # one's refcount has been updated. two has been removed from the
 
107
        # parent_map
 
108
        self.assertEqual(sorted([('one',), ('two',)]),
 
109
                         sorted(gen.chunks.keys()))
 
110
        self.assertEqual({('one',): 1, ('two',): 1}, gen.refcounts)
 
111
        self.assertEqual([('two',)], gen.diffs.keys())
 
112
        self.assertEqual({('three',): (('one',), ('two',))},
 
113
                         gen.parent_map)
 
114
        # Finally 'three', which allows us to remove all parents from the
 
115
        # caches
 
116
        record = stream.next()
 
117
        self.assertEqual(('three',), record.key)
 
118
        gen._process_one_record(record.key, record.get_bytes_as('chunked'))
 
119
        # Both are now cached, and the diff for two has been extracted, and
 
120
        # one's refcount has been updated
 
121
        self.assertEqual([], gen.chunks.keys())
 
122
        self.assertEqual({}, gen.refcounts)
 
123
        self.assertEqual(sorted([('two',), ('three',)]),
 
124
                         sorted(gen.diffs.keys()))
 
125
 
 
126
    def test_compute_diffs(self):
 
127
        vf = self.make_three_vf()
 
128
        # The content is in the order requested, even if it isn't topological
 
129
        gen = versionedfile._MPDiffGenerator(vf, [('two',), ('three',),
 
130
                                                  ('one',)])
 
131
        diffs = gen.compute_diffs()
 
132
        expected_diffs = [
 
133
            multiparent.MultiParent([multiparent.ParentText(0, 0, 0, 1),
 
134
                                     multiparent.NewText(['second\n'])]),
 
135
            multiparent.MultiParent([multiparent.ParentText(1, 0, 0, 2),
 
136
                                     multiparent.NewText(['third\n'])]),
 
137
            multiparent.MultiParent([multiparent.NewText(['first\n'])]),
 
138
            ]
 
139
        self.assertEqual(expected_diffs, diffs)