/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
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
19
from .. import (
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
20
    errors,
21
    multiparent,
22
    tests,
6670.4.1 by Jelmer Vernooij
Update imports.
23
    )
24
from ..bzr import (
25
    groupcompress,
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
26
    versionedfile,
27
    )
28
29
30
class Test_MPDiffGenerator(tests.TestCaseWithMemoryTransport):
31
    # Should this be a per vf test?
32
33
    def make_vf(self):
34
        t = self.get_transport('')
35
        factory = groupcompress.make_pack_factory(True, True, 1)
36
        return factory(t)
37
38
    def make_three_vf(self):
39
        vf = self.make_vf()
40
        vf.add_lines(('one',), (), ['first\n'])
41
        vf.add_lines(('two',), [('one',)], ['first\n', 'second\n'])
42
        vf.add_lines(('three',), [('one',), ('two',)],
43
                    ['first\n', 'second\n', 'third\n'])
44
        return vf
45
46
    def test_finds_parents(self):
47
        vf = self.make_three_vf()
48
        gen = versionedfile._MPDiffGenerator(vf, [('three',)])
49
        needed_keys, refcount = gen._find_needed_keys()
50
        self.assertEqual(sorted([('one',), ('two',), ('three',)]),
51
                         sorted(needed_keys))
52
        self.assertEqual({('one',): 1, ('two',): 1}, refcount)
53
54
    def test_ignores_ghost_parents(self):
55
        # If a parent is a ghost, it is just ignored
56
        vf = self.make_vf()
57
        vf.add_lines(('two',), [('one',)], ['first\n', 'second\n'])
58
        gen = versionedfile._MPDiffGenerator(vf, [('two',)])
59
        needed_keys, refcount = gen._find_needed_keys()
60
        self.assertEqual(sorted([('two',)]), sorted(needed_keys))
61
        # It is returned, but we don't really care as we won't extract it
62
        self.assertEqual({('one',): 1}, refcount)
63
        self.assertEqual([('one',)], sorted(gen.ghost_parents))
5374.2.5 by John Arbash Meinel
Rework things a bit so the logic can be shared.
64
        self.assertEqual([], sorted(gen.present_parents))
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
65
66
    def test_raises_on_ghost_keys(self):
67
        # If the requested key is a ghost, then we have a problem
68
        vf = self.make_vf()
69
        gen = versionedfile._MPDiffGenerator(vf, [('one',)])
70
        self.assertRaises(errors.RevisionNotPresent,
71
                          gen._find_needed_keys)
72
73
    def test_refcount_multiple_children(self):
74
        vf = self.make_three_vf()
75
        gen = versionedfile._MPDiffGenerator(vf, [('two',), ('three',)])
76
        needed_keys, refcount = gen._find_needed_keys()
77
        self.assertEqual(sorted([('one',), ('two',), ('three',)]),
78
                         sorted(needed_keys))
79
        self.assertEqual({('one',): 2, ('two',): 1}, refcount)
5374.2.5 by John Arbash Meinel
Rework things a bit so the logic can be shared.
80
        self.assertEqual([('one',)], sorted(gen.present_parents))
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
81
82
    def test_process_contents(self):
83
        vf = self.make_three_vf()
84
        gen = versionedfile._MPDiffGenerator(vf, [('two',), ('three',)])
85
        gen._find_needed_keys()
86
        self.assertEqual({('two',): (('one',),),
87
                          ('three',): (('one',), ('two',))},
88
                         gen.parent_map)
89
        self.assertEqual({('one',): 2, ('two',): 1}, gen.refcounts)
90
        self.assertEqual(sorted([('one',), ('two',), ('three',)]),
91
                         sorted(gen.needed_keys))
92
        stream = vf.get_record_stream(gen.needed_keys, 'topological', True)
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
93
        record = next(stream)
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
94
        self.assertEqual(('one',), record.key)
95
        # one is not needed in the output, but it is needed by children. As
96
        # such, it should end up in the various caches
5374.2.5 by John Arbash Meinel
Rework things a bit so the logic can be shared.
97
        gen._process_one_record(record.key, record.get_bytes_as('chunked'))
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
98
        # The chunks should be cached, the refcount untouched
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
99
        self.assertEqual({('one',)}, set(gen.chunks))
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
100
        self.assertEqual({('one',): 2, ('two',): 1}, gen.refcounts)
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
101
        self.assertEqual(set(), set(gen.diffs))
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
102
        # Next we get 'two', which is something we output, but also needed for
103
        # three
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
104
        record = next(stream)
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
105
        self.assertEqual(('two',), record.key)
5374.2.5 by John Arbash Meinel
Rework things a bit so the logic can be shared.
106
        gen._process_one_record(record.key, record.get_bytes_as('chunked'))
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
107
        # Both are now cached, and the diff for two has been extracted, and
108
        # one's refcount has been updated. two has been removed from the
109
        # parent_map
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
110
        self.assertEqual({('one',), ('two',)}, set(gen.chunks))
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
111
        self.assertEqual({('one',): 1, ('two',): 1}, gen.refcounts)
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
112
        self.assertEqual({('two',)}, set(gen.diffs))
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
113
        self.assertEqual({('three',): (('one',), ('two',))},
114
                         gen.parent_map)
115
        # Finally 'three', which allows us to remove all parents from the
116
        # caches
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
117
        record = next(stream)
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
118
        self.assertEqual(('three',), record.key)
5374.2.5 by John Arbash Meinel
Rework things a bit so the logic can be shared.
119
        gen._process_one_record(record.key, record.get_bytes_as('chunked'))
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
120
        # Both are now cached, and the diff for two has been extracted, and
121
        # one's refcount has been updated
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
122
        self.assertEqual(set(), set(gen.chunks))
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
123
        self.assertEqual({}, gen.refcounts)
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
124
        self.assertEqual({('two',), ('three',)}, set(gen.diffs))
5374.2.2 by John Arbash Meinel
Create a multi-parent diff generator class.
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)