1
# Copyright (C) 2010 Canonical Ltd
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.
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.
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
17
"""Tests for VersionedFile classes"""
30
class Test_MPDiffGenerator(tests.TestCaseWithMemoryTransport):
31
# Should this be a per vf test?
34
t = self.get_transport('')
35
factory = groupcompress.make_pack_factory(True, True, 1)
38
def make_three_vf(self):
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'])
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',)]),
52
self.assertEqual({(b'one',): 1, (b'two',): 1}, refcount)
54
def test_ignores_ghost_parents(self):
55
# If a parent is a ghost, it is just ignored
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))
66
def test_raises_on_ghost_keys(self):
67
# If the requested key is a ghost, then we have a problem
69
gen = versionedfile._MPDiffGenerator(vf, [(b'one',)])
70
self.assertRaises(errors.RevisionNotPresent,
71
gen._find_needed_keys)
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',)]),
79
self.assertEqual({(b'one',): 2, (b'two',): 1}, refcount)
80
self.assertEqual([(b'one',)], sorted(gen.present_parents))
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',))},
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)
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
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
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',))},
115
# Finally 'three', which allows us to remove all parents from the
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))
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',),
131
diffs = gen.compute_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'])]),
139
self.assertEqual(expected_diffs, diffs)