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))
62
self.assertEqual([], sorted(gen.present_parents))
64
def test_raises_on_ghost_keys(self):
65
# If the requested key is a ghost, then we have a problem
67
gen = versionedfile._MPDiffGenerator(vf, [('one',)])
68
self.assertRaises(errors.RevisionNotPresent,
69
gen._find_needed_keys)
71
def test_refcount_multiple_children(self):
72
vf = self.make_three_vf()
73
gen = versionedfile._MPDiffGenerator(vf, [('two',), ('three',)])
74
needed_keys, refcount = gen._find_needed_keys()
75
self.assertEqual(sorted([('one',), ('two',), ('three',)]),
77
self.assertEqual({('one',): 2, ('two',): 1}, refcount)
78
self.assertEqual([('one',)], sorted(gen.present_parents))
80
def test_process_contents(self):
81
vf = self.make_three_vf()
82
gen = versionedfile._MPDiffGenerator(vf, [('two',), ('three',)])
83
gen._find_needed_keys()
84
self.assertEqual({('two',): (('one',),),
85
('three',): (('one',), ('two',))},
87
self.assertEqual({('one',): 2, ('two',): 1}, gen.refcounts)
88
self.assertEqual(sorted([('one',), ('two',), ('three',)]),
89
sorted(gen.needed_keys))
90
stream = vf.get_record_stream(gen.needed_keys, 'topological', True)
91
record = stream.next()
92
self.assertEqual(('one',), record.key)
93
# one is not needed in the output, but it is needed by children. As
94
# such, it should end up in the various caches
95
gen._process_one_record(record.key, record.get_bytes_as('chunked'))
96
# The chunks should be cached, the refcount untouched
97
self.assertEqual([('one',)], gen.chunks.keys())
98
self.assertEqual({('one',): 2, ('two',): 1}, gen.refcounts)
99
self.assertEqual([], gen.diffs.keys())
100
# Next we get 'two', which is something we output, but also needed for
102
record = stream.next()
103
self.assertEqual(('two',), record.key)
104
gen._process_one_record(record.key, record.get_bytes_as('chunked'))
105
# Both are now cached, and the diff for two has been extracted, and
106
# one's refcount has been updated. two has been removed from the
108
self.assertEqual(sorted([('one',), ('two',)]),
109
sorted(gen.chunks.keys()))
110
self.assertEqual({('one',): 1, ('two',): 1}, gen.refcounts)
111
self.assertEqual([('two',)], gen.diffs.keys())
112
self.assertEqual({('three',): (('one',), ('two',))},
114
# Finally 'three', which allows us to remove all parents from the
116
record = stream.next()
117
self.assertEqual(('three',), record.key)
118
gen._process_one_record(record.key, record.get_bytes_as('chunked'))
119
# Both are now cached, and the diff for two has been extracted, and
120
# one's refcount has been updated
121
self.assertEqual([], gen.chunks.keys())
122
self.assertEqual({}, gen.refcounts)
123
self.assertEqual(sorted([('two',), ('three',)]),
124
sorted(gen.diffs.keys()))
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',),
131
diffs = gen.compute_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'])]),
139
self.assertEqual(expected_diffs, diffs)