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(('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'])
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',)]),
52
self.assertEqual({('one',): 1, ('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(('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))
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, [('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, [('two',), ('three',)])
76
needed_keys, refcount = gen._find_needed_keys()
77
self.assertEqual(sorted([('one',), ('two',), ('three',)]),
79
self.assertEqual({('one',): 2, ('two',): 1}, refcount)
80
self.assertEqual([('one',)], sorted(gen.present_parents))
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',))},
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)
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
97
gen._process_one_record(record.key, record.get_bytes_as('chunked'))
98
# The chunks should be cached, the refcount untouched
99
self.assertEqual({('one',)}, set(gen.chunks))
100
self.assertEqual({('one',): 2, ('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(('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({('one',), ('two',)}, set(gen.chunks))
111
self.assertEqual({('one',): 1, ('two',): 1}, gen.refcounts)
112
self.assertEqual({('two',)}, set(gen.diffs))
113
self.assertEqual({('three',): (('one',), ('two',))},
115
# Finally 'three', which allows us to remove all parents from the
117
record = next(stream)
118
self.assertEqual(('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({('two',), ('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, [('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)