/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
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
63
    def test_raises_on_ghost_keys(self):
64
        # If the requested key is a ghost, then we have a problem
65
        vf = self.make_vf()
66
        gen = versionedfile._MPDiffGenerator(vf, [('one',)])
67
        self.assertRaises(errors.RevisionNotPresent,
68
                          gen._find_needed_keys)
69
70
    def test_refcount_multiple_children(self):
71
        vf = self.make_three_vf()
72
        gen = versionedfile._MPDiffGenerator(vf, [('two',), ('three',)])
73
        needed_keys, refcount = gen._find_needed_keys()
74
        self.assertEqual(sorted([('one',), ('two',), ('three',)]),
75
                         sorted(needed_keys))
76
        self.assertEqual({('one',): 2, ('two',): 1}, refcount)
77
78
    def test_process_contents(self):
79
        vf = self.make_three_vf()
80
        gen = versionedfile._MPDiffGenerator(vf, [('two',), ('three',)])
81
        gen._find_needed_keys()
82
        self.assertEqual({('two',): (('one',),),
83
                          ('three',): (('one',), ('two',))},
84
                         gen.parent_map)
85
        self.assertEqual({('one',): 2, ('two',): 1}, gen.refcounts)
86
        self.assertEqual(sorted([('one',), ('two',), ('three',)]),
87
                         sorted(gen.needed_keys))
88
        stream = vf.get_record_stream(gen.needed_keys, 'topological', True)
89
        record = stream.next()
90
        self.assertEqual(('one',), record.key)
91
        # one is not needed in the output, but it is needed by children. As
92
        # such, it should end up in the various caches
93
        gen._process_one_record(record)
94
        # The chunks should be cached, the refcount untouched
95
        self.assertEqual([('one',)], gen.chunks.keys())
96
        self.assertEqual({('one',): 2, ('two',): 1}, gen.refcounts)
97
        self.assertEqual([], gen.diffs.keys())
98
        # Next we get 'two', which is something we output, but also needed for
99
        # three
100
        record = stream.next()
101
        self.assertEqual(('two',), record.key)
102
        gen._process_one_record(record)
103
        # Both are now cached, and the diff for two has been extracted, and
104
        # one's refcount has been updated. two has been removed from the
105
        # parent_map
106
        self.assertEqual(sorted([('one',), ('two',)]),
107
                         sorted(gen.chunks.keys()))
108
        self.assertEqual({('one',): 1, ('two',): 1}, gen.refcounts)
109
        self.assertEqual([('two',)], gen.diffs.keys())
110
        self.assertEqual({('three',): (('one',), ('two',))},
111
                         gen.parent_map)
112
        # Finally 'three', which allows us to remove all parents from the
113
        # caches
114
        record = stream.next()
115
        self.assertEqual(('three',), record.key)
116
        gen._process_one_record(record)
117
        # Both are now cached, and the diff for two has been extracted, and
118
        # one's refcount has been updated
119
        self.assertEqual([], gen.chunks.keys())
120
        self.assertEqual({}, gen.refcounts)
121
        self.assertEqual(sorted([('two',), ('three',)]),
122
                         sorted(gen.diffs.keys()))
123
124
    def test_compute_diffs(self):
125
        vf = self.make_three_vf()
126
        # The content is in the order requested, even if it isn't topological
127
        gen = versionedfile._MPDiffGenerator(vf, [('two',), ('three',),
128
                                                  ('one',)])
129
        diffs = gen.compute_diffs()
130
        expected_diffs = [
131
            multiparent.MultiParent([multiparent.ParentText(0, 0, 0, 1),
132
                                     multiparent.NewText(['second\n'])]),
133
            multiparent.MultiParent([multiparent.ParentText(1, 0, 0, 2),
134
                                     multiparent.NewText(['third\n'])]),
135
            multiparent.MultiParent([multiparent.NewText(['first\n'])]),
136
            ]
137
        self.assertEqual(expected_diffs, diffs)