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"""
28
class Test_MPDiffGenerator(tests.TestCaseWithMemoryTransport):
29
# Should this be a per vf test?
32
t = self.get_transport('')
33
factory = groupcompress.make_pack_factory(True, True, 1)
36
def make_three_vf(self):
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'])
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',)]),
50
self.assertEqual({('one',): 1, ('two',): 1}, refcount)
52
def test_ignores_ghost_parents(self):
53
# If a parent is a ghost, it is just ignored
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))
63
def test_raises_on_ghost_keys(self):
64
# If the requested key is a ghost, then we have a problem
66
gen = versionedfile._MPDiffGenerator(vf, [('one',)])
67
self.assertRaises(errors.RevisionNotPresent,
68
gen._find_needed_keys)
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',)]),
76
self.assertEqual({('one',): 2, ('two',): 1}, refcount)
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',))},
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
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
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',))},
112
# Finally 'three', which allows us to remove all parents from the
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()))
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',),
129
diffs = gen.compute_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'])]),
137
self.assertEqual(expected_diffs, diffs)