~loggerhead-team/loggerhead/experimental

« back to all changes in this revision

Viewing changes to loggerhead/tests/test_history.py

  • Committer: John Arbash Meinel
  • Date: 2011-04-11 14:55:55 UTC
  • mfrom: (419.2.26 loggerhead)
  • Revision ID: john@arbash-meinel.com-20110411145555-7xflzcipb32kuknh
Merge trunk into the history-db branch.

We had to disable the mainline tests, because they were monkey-patching
a parameter that we have removed. We need to figure out a way to
do a similar test.

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