/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_versionedfile.py

[merge] robertc's integration, updated tests to check for retcode=3

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 .. import (
20
 
    errors,
21
 
    multiparent,
22
 
    tests,
23
 
    )
24
 
from ..bzr import (
25
 
    groupcompress,
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((b'one',), (), [b'first\n'])
41
 
        vf.add_lines((b'two',), [(b'one',)], [b'first\n', b'second\n'])
42
 
        vf.add_lines((b'three',), [(b'one',), (b'two',)],
43
 
                     [b'first\n', b'second\n', b'third\n'])
44
 
        return vf
45
 
 
46
 
    def test_finds_parents(self):
47
 
        vf = self.make_three_vf()
48
 
        gen = versionedfile._MPDiffGenerator(vf, [(b'three',)])
49
 
        needed_keys, refcount = gen._find_needed_keys()
50
 
        self.assertEqual(sorted([(b'one',), (b'two',), (b'three',)]),
51
 
                         sorted(needed_keys))
52
 
        self.assertEqual({(b'one',): 1, (b'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((b'two',), [(b'one',)], [b'first\n', b'second\n'])
58
 
        gen = versionedfile._MPDiffGenerator(vf, [(b'two',)])
59
 
        needed_keys, refcount = gen._find_needed_keys()
60
 
        self.assertEqual(sorted([(b'two',)]), sorted(needed_keys))
61
 
        # It is returned, but we don't really care as we won't extract it
62
 
        self.assertEqual({(b'one',): 1}, refcount)
63
 
        self.assertEqual([(b'one',)], sorted(gen.ghost_parents))
64
 
        self.assertEqual([], sorted(gen.present_parents))
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, [(b'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, [(b'two',), (b'three',)])
76
 
        needed_keys, refcount = gen._find_needed_keys()
77
 
        self.assertEqual(sorted([(b'one',), (b'two',), (b'three',)]),
78
 
                         sorted(needed_keys))
79
 
        self.assertEqual({(b'one',): 2, (b'two',): 1}, refcount)
80
 
        self.assertEqual([(b'one',)], sorted(gen.present_parents))
81
 
 
82
 
    def test_process_contents(self):
83
 
        vf = self.make_three_vf()
84
 
        gen = versionedfile._MPDiffGenerator(vf, [(b'two',), (b'three',)])
85
 
        gen._find_needed_keys()
86
 
        self.assertEqual({(b'two',): ((b'one',),),
87
 
                          (b'three',): ((b'one',), (b'two',))},
88
 
                         gen.parent_map)
89
 
        self.assertEqual({(b'one',): 2, (b'two',): 1}, gen.refcounts)
90
 
        self.assertEqual(sorted([(b'one',), (b'two',), (b'three',)]),
91
 
                         sorted(gen.needed_keys))
92
 
        stream = vf.get_record_stream(gen.needed_keys, 'topological', True)
93
 
        record = next(stream)
94
 
        self.assertEqual((b'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
97
 
        gen._process_one_record(record.key, record.get_bytes_as('chunked'))
98
 
        # The chunks should be cached, the refcount untouched
99
 
        self.assertEqual({(b'one',)}, set(gen.chunks))
100
 
        self.assertEqual({(b'one',): 2, (b'two',): 1}, gen.refcounts)
101
 
        self.assertEqual(set(), set(gen.diffs))
102
 
        # Next we get 'two', which is something we output, but also needed for
103
 
        # three
104
 
        record = next(stream)
105
 
        self.assertEqual((b'two',), record.key)
106
 
        gen._process_one_record(record.key, record.get_bytes_as('chunked'))
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
110
 
        self.assertEqual({(b'one',), (b'two',)}, set(gen.chunks))
111
 
        self.assertEqual({(b'one',): 1, (b'two',): 1}, gen.refcounts)
112
 
        self.assertEqual({(b'two',)}, set(gen.diffs))
113
 
        self.assertEqual({(b'three',): ((b'one',), (b'two',))},
114
 
                         gen.parent_map)
115
 
        # Finally 'three', which allows us to remove all parents from the
116
 
        # caches
117
 
        record = next(stream)
118
 
        self.assertEqual((b'three',), record.key)
119
 
        gen._process_one_record(record.key, record.get_bytes_as('chunked'))
120
 
        # Both are now cached, and the diff for two has been extracted, and
121
 
        # one's refcount has been updated
122
 
        self.assertEqual(set(), set(gen.chunks))
123
 
        self.assertEqual({}, gen.refcounts)
124
 
        self.assertEqual({(b'two',), (b'three',)}, set(gen.diffs))
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, [(b'two',), (b'three',),
130
 
                                                  (b'one',)])
131
 
        diffs = gen.compute_diffs()
132
 
        expected_diffs = [
133
 
            multiparent.MultiParent([multiparent.ParentText(0, 0, 0, 1),
134
 
                                     multiparent.NewText([b'second\n'])]),
135
 
            multiparent.MultiParent([multiparent.ParentText(1, 0, 0, 2),
136
 
                                     multiparent.NewText([b'third\n'])]),
137
 
            multiparent.MultiParent([multiparent.NewText([b'first\n'])]),
138
 
            ]
139
 
        self.assertEqual(expected_diffs, diffs)