~bzr/loggerhead/daily-ppa

« back to all changes in this revision

Viewing changes to loggerhead/tests/test_history.py

  • Committer: Max Bowsher
  • Date: 2011-03-17 08:33:16 UTC
  • mfrom: (182.150.20 trunk-rich)
  • Revision ID: maxb@f2s.com-20110317083316-n5nq18tsaaxkskut
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
#
 
17
 
 
18
"""Direct tests of the loggerhead/history.py module"""
 
19
 
 
20
from datetime import datetime
 
21
from bzrlib import tests
 
22
 
 
23
from loggerhead import history as _mod_history
 
24
 
 
25
 
 
26
class TestCaseWithExamples(tests.TestCaseWithMemoryTransport):
 
27
 
 
28
    def make_linear_ancestry(self):
 
29
        # Time goes up
 
30
        # rev-3
 
31
        #  |
 
32
        # rev-2
 
33
        #  |
 
34
        # rev-1
 
35
        builder = self.make_branch_builder('branch')
 
36
        builder.start_series()
 
37
        builder.build_snapshot('rev-1', None, [
 
38
            ('add', ('', 'root-id', 'directory', None))])
 
39
        builder.build_snapshot('rev-2', ['rev-1'], [])
 
40
        builder.build_snapshot('rev-3', ['rev-2'], [])
 
41
        builder.finish_series()
 
42
        b = builder.get_branch()
 
43
        self.addCleanup(b.lock_read().unlock)
 
44
        return _mod_history.History(b, {})
 
45
 
 
46
    def make_long_linear_ancestry(self):
 
47
        builder = self.make_branch_builder('branch')
 
48
        builder.start_series()
 
49
        builder.build_snapshot('A', None, [
 
50
            ('add', ('', 'root-id', 'directory', None))])
 
51
        for r in "BCDEFGHIJKLMNOPQRSTUVWXYZ":
 
52
            builder.build_snapshot(r, None, [])
 
53
        builder.finish_series()
 
54
        b = builder.get_branch()
 
55
        self.addCleanup(b.lock_read().unlock)
 
56
        return _mod_history.History(b, {})
 
57
 
 
58
    def make_merged_ancestry(self):
 
59
        # Time goes up
 
60
        # rev-3
 
61
        #  |  \
 
62
        #  |  rev-2
 
63
        #  |  /
 
64
        # rev-1
 
65
        builder = self.make_branch_builder('branch')
 
66
        builder.start_series()
 
67
        builder.build_snapshot('rev-1', None, [
 
68
            ('add', ('', 'root-id', 'directory', None))])
 
69
        builder.build_snapshot('rev-2', ['rev-1'], [])
 
70
        builder.build_snapshot('rev-3', ['rev-1', 'rev-2'], [])
 
71
        builder.finish_series()
 
72
        b = builder.get_branch()
 
73
        self.addCleanup(b.lock_read().unlock)
 
74
        return _mod_history.History(b, {})
 
75
 
 
76
    def make_deep_merged_ancestry(self):
 
77
        # Time goes up
 
78
        # F
 
79
        # |\
 
80
        # | E
 
81
        # | |\
 
82
        # | | D
 
83
        # | |/
 
84
        # B C
 
85
        # |/
 
86
        # A
 
87
        builder = self.make_branch_builder('branch')
 
88
        builder.start_series()
 
89
        builder.build_snapshot('A', None, [
 
90
            ('add', ('', 'root-id', 'directory', None))])
 
91
        builder.build_snapshot('B', ['A'], [])
 
92
        builder.build_snapshot('C', ['A'], [])
 
93
        builder.build_snapshot('D', ['C'], [])
 
94
        builder.build_snapshot('E', ['C', 'D'], [])
 
95
        builder.build_snapshot('F', ['B', 'E'], [])
 
96
        builder.finish_series()
 
97
        b = builder.get_branch()
 
98
        self.addCleanup(b.lock_read().unlock)
 
99
        return _mod_history.History(b, {})
 
100
 
 
101
    def assertRevidsFrom(self, expected, history, search_revs, tip_rev):
 
102
        self.assertEqual(expected,
 
103
                         list(history.get_revids_from(search_revs, tip_rev)))
 
104
 
 
105
 
 
106
class _DictProxy(object):
 
107
 
 
108
    def __init__(self, d):
 
109
        self._d = d
 
110
        self._accessed = set()
 
111
        self.__setitem__ = d.__setitem__
 
112
 
 
113
    def __getitem__(self, name):
 
114
        self._accessed.add(name)
 
115
        return self._d[name]
 
116
 
 
117
    def __len__(self):
 
118
        return len(self._d)
 
119
 
 
120
 
 
121
def track_rev_info_accesses(h):
 
122
    """Track __getitem__ access to History._rev_info,
 
123
 
 
124
    :return: set of items accessed
 
125
    """
 
126
    h._rev_info = _DictProxy(h._rev_info)
 
127
    return h._rev_info._accessed
 
128
 
 
129
 
 
130
class TestHistoryGetRevidsFrom(TestCaseWithExamples):
 
131
 
 
132
    def test_get_revids_from_simple_mainline(self):
 
133
        history = self.make_linear_ancestry()
 
134
        self.assertRevidsFrom(['rev-3', 'rev-2', 'rev-1'],
 
135
                              history, None, 'rev-3')
 
136
 
 
137
    def test_get_revids_from_merged_mainline(self):
 
138
        history = self.make_merged_ancestry()
 
139
        self.assertRevidsFrom(['rev-3', 'rev-1'],
 
140
                              history, None, 'rev-3')
 
141
 
 
142
    def test_get_revids_given_one_rev(self):
 
143
        history = self.make_merged_ancestry()
 
144
        # rev-3 was the first mainline revision to see rev-2.
 
145
        self.assertRevidsFrom(['rev-3'], history, ['rev-2'], 'rev-3')
 
146
 
 
147
    def test_get_revids_deep_ancestry(self):
 
148
        history = self.make_deep_merged_ancestry()
 
149
        self.assertRevidsFrom(['F'], history, ['F'], 'F')
 
150
        self.assertRevidsFrom(['F'], history, ['E'], 'F')
 
151
        self.assertRevidsFrom(['F'], history, ['D'], 'F')
 
152
        self.assertRevidsFrom(['F'], history, ['C'], 'F')
 
153
        self.assertRevidsFrom(['B'], history, ['B'], 'F')
 
154
        self.assertRevidsFrom(['A'], history, ['A'], 'F')
 
155
 
 
156
    def test_get_revids_doesnt_over_produce_simple_mainline(self):
 
157
        # get_revids_from shouldn't walk the whole ancestry just to get the
 
158
        # answers for the first few revisions.
 
159
        history = self.make_long_linear_ancestry()
 
160
        accessed = track_rev_info_accesses(history)
 
161
        result = history.get_revids_from(None, 'Z')
 
162
        self.assertEqual(set(), accessed)
 
163
        self.assertEqual('Z', result.next())
 
164
        # We already know 'Z' because we passed it in.
 
165
        self.assertEqual(set(), accessed)
 
166
        self.assertEqual('Y', result.next())
 
167
        self.assertEqual(set([history._rev_indices['Z']]), accessed)
 
168
 
 
169
    def test_get_revids_doesnt_over_produce_for_merges(self):
 
170
        # get_revids_from shouldn't walk the whole ancestry just to get the
 
171
        # answers for the first few revisions.
 
172
        history = self.make_long_linear_ancestry()
 
173
        accessed = track_rev_info_accesses(history)
 
174
        result = history.get_revids_from(['X', 'V'], 'Z')
 
175
        self.assertEqual(set(), accessed)
 
176
        self.assertEqual('X', result.next())
 
177
        # We access 'W' because we are checking that W wasn't merged into X.
 
178
        # The important bit is that we aren't getting the whole ancestry.
 
179
        self.assertEqual(set([history._rev_indices[x] for x in 'ZYXW']),
 
180
                         accessed)
 
181
        self.assertEqual('V', result.next())
 
182
        self.assertEqual(set([history._rev_indices[x] for x in 'ZYXWVU']),
 
183
                         accessed)
 
184
        self.assertRaises(StopIteration, result.next)
 
185
        self.assertEqual(set([history._rev_indices[x] for x in 'ZYXWVU']),
 
186
                         accessed)
 
187
 
 
188
 
 
189
 
 
190
class TestHistoryChangeFromRevision(tests.TestCaseWithTransport):
 
191
 
 
192
    def make_single_commit(self):
 
193
        tree = self.make_branch_and_tree('test')
 
194
        rev_id = tree.commit('Commit Message', timestamp=1299838474.317,
 
195
            timezone=3600, committer='Joe Example <joe@example.com>',
 
196
            revprops={})
 
197
        self.addCleanup(tree.branch.lock_write().unlock)
 
198
        rev = tree.branch.repository.get_revision(rev_id)
 
199
        history = _mod_history.History(tree.branch, {})
 
200
        return history, rev
 
201
 
 
202
    def test_simple(self):
 
203
        history, rev = self.make_single_commit()
 
204
        change = history._change_from_revision(rev)
 
205
        self.assertEqual(rev.revision_id, change.revid)
 
206
        self.assertEqual(datetime.fromtimestamp(1299838474.317),
 
207
                         change.date)
 
208
        self.assertEqual(datetime.utcfromtimestamp(1299838474.317),
 
209
                         change.utc_date)
 
210
        self.assertEqual(['Joe Example <joe@example.com>'],
 
211
                         change.authors)
 
212
        self.assertEqual('test', change.branch_nick)
 
213
        self.assertEqual('Commit Message', change.short_comment)
 
214
        self.assertEqual('Commit Message', change.comment)
 
215
        self.assertEqual(['Commit&nbsp;Message'], change.comment_clean)
 
216
        self.assertEqual([], change.parents)
 
217
        self.assertEqual([], change.bugs)
 
218
        self.assertEqual(None, change.tags)
 
219
 
 
220
    def test_tags(self):
 
221
        history, rev = self.make_single_commit()
 
222
        b = history._branch
 
223
        b.tags.set_tag('tag-1', rev.revision_id)
 
224
        b.tags.set_tag('tag-2', rev.revision_id)
 
225
        b.tags.set_tag('Tag-10', rev.revision_id)
 
226
        change = history._change_from_revision(rev)
 
227
        # tags are 'naturally' sorted, sorting numbers in order, and ignoring
 
228
        # case, etc.
 
229
        self.assertEqual('tag-1, tag-2, Tag-10', change.tags)
 
230
 
 
231
    def test_committer_vs_authors(self):
 
232
        tree = self.make_branch_and_tree('test')
 
233
        rev_id = tree.commit('Commit Message', timestamp=1299838474.317,
 
234
            timezone=3600, committer='Joe Example <joe@example.com>',
 
235
            revprops={'authors': u'A Author <aauthor@example.com>\n'
 
236
                                 u'B Author <bauthor@example.com>'})
 
237
        self.addCleanup(tree.branch.lock_write().unlock)
 
238
        rev = tree.branch.repository.get_revision(rev_id)
 
239
        history = _mod_history.History(tree.branch, {})
 
240
        change = history._change_from_revision(rev)
 
241
        self.assertEqual(u'Joe Example <joe@example.com>',
 
242
                         change.committer)
 
243
        self.assertEqual([u'A Author <aauthor@example.com>',
 
244
                          u'B Author <bauthor@example.com>'],
 
245
                         change.authors)
 
246
 
 
247
 
 
248
class TestHistory_IterateSufficiently(tests.TestCase):
 
249
 
 
250
    def assertIterate(self, expected, iterable, stop_at, extra_rev_count):
 
251
        self.assertEqual(expected, _mod_history.History._iterate_sufficiently(
 
252
            iterable, stop_at, extra_rev_count))
 
253
 
 
254
    def test_iter_no_extra(self):
 
255
        lst = list('abcdefghijklmnopqrstuvwxyz')
 
256
        self.assertIterate(['a', 'b', 'c'], iter(lst), 'c', 0)
 
257
        self.assertIterate(['a', 'b', 'c', 'd'], iter(lst), 'd', 0)
 
258
 
 
259
    def test_iter_not_found(self):
 
260
        # If the key in question isn't found, we just exhaust the list
 
261
        lst = list('abcdefghijklmnopqrstuvwxyz')
 
262
        self.assertIterate(lst, iter(lst), 'not-there', 0)
 
263
 
 
264
    def test_iter_with_extra(self):
 
265
        lst = list('abcdefghijklmnopqrstuvwxyz')
 
266
        self.assertIterate(['a', 'b', 'c'], iter(lst), 'b', 1)
 
267
        self.assertIterate(['a', 'b', 'c', 'd', 'e'], iter(lst), 'c', 2)
 
268
 
 
269
    def test_iter_with_too_many_extra(self):
 
270
        lst = list('abcdefghijklmnopqrstuvwxyz')
 
271
        self.assertIterate(lst, iter(lst), 'y', 10)
 
272
        self.assertIterate(lst, iter(lst), 'z', 10)
 
273
 
 
274
    def test_iter_with_extra_None(self):
 
275
        lst = list('abcdefghijklmnopqrstuvwxyz')
 
276
        self.assertIterate(lst, iter(lst), 'z', None)
 
277
 
 
278
 
 
279
 
 
280
class TestHistoryGetView(TestCaseWithExamples):
 
281
 
 
282
    def test_get_view_limited_history(self):
 
283
        # get_view should only load enough history to serve the result, not all
 
284
        # history.
 
285
        history = self.make_long_linear_ancestry()
 
286
        accessed = track_rev_info_accesses(history)
 
287
        revid, start_revid, revid_list = history.get_view('Z', 'Z', None,
 
288
                                                      extra_rev_count=5)
 
289
        self.assertEqual(['Z', 'Y', 'X', 'W', 'V', 'U'], revid_list)
 
290
        self.assertEqual('Z', revid)
 
291
        self.assertEqual('Z', start_revid)
 
292
        self.assertEqual(set([history._rev_indices[x] for x in 'ZYXWVU']),
 
293
                         accessed)