~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/code/model/tests/test_branchmergeproposaljobs.py

[r=abentley,
        rvb][no-qa] UpdatePreviewDiffJob now issues an ObjectModifiedEvent
        when it updates the preview_diff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
1
# Copyright 2010-2011 Canonical Ltd.  This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
 
4
4
"""Tests for branch merge proposal jobs."""
11
11
    )
12
12
 
13
13
from lazr.lifecycle.event import ObjectModifiedEvent
 
14
from lazr.lifecycle.interfaces import IObjectModifiedEvent
14
15
import pytz
15
16
from sqlobject import SQLObjectNotFound
16
17
from storm.locals import Select
20
21
 
21
22
from canonical.config import config
22
23
from canonical.launchpad.webapp.testing import verifyObject
23
 
from canonical.testing.layers import (
24
 
    DatabaseFunctionalLayer,
25
 
    LaunchpadZopelessLayer,
26
 
    )
27
 
from lp.code.adapters.branch import BranchMergeProposalDelta
 
24
from canonical.testing.layers import LaunchpadZopelessLayer
 
25
from lp.code.adapters.branch import BranchMergeProposalNoPreviewDiffDelta
28
26
from lp.code.enums import BranchMergeProposalStatus
29
27
from lp.code.interfaces.branchmergeproposal import (
30
28
    IBranchMergeProposalJob,
60
58
from lp.services.job.model.job import Job
61
59
from lp.services.job.runner import JobRunner
62
60
from lp.services.osutils import override_environ
63
 
from lp.testing import TestCaseWithFactory
 
61
from lp.testing import (
 
62
    EventRecorder,
 
63
    TestCaseWithFactory,
 
64
    )
64
65
from lp.testing.mail_helpers import pop_notifications
65
66
 
66
67
 
67
68
class TestBranchMergeProposalJob(TestCaseWithFactory):
68
69
 
69
 
    layer = DatabaseFunctionalLayer
 
70
    layer = LaunchpadZopelessLayer
70
71
 
71
72
    def test_providesInterface(self):
72
73
        """BranchMergeProposalJob implements expected interfaces."""
97
98
            AttributeError, BranchMergeProposalJobDerived.get, job.id)
98
99
        self.assertRaises(SQLObjectNotFound, UpdatePreviewDiffJob.get, job.id)
99
100
        self.assertRaises(
100
 
            SQLObjectNotFound, MergeProposalNeedsReviewEmailJob.get, job.id + 1)
 
101
            SQLObjectNotFound, MergeProposalNeedsReviewEmailJob.get,
 
102
            job.id + 1)
101
103
        self.assertEqual(job, MergeProposalNeedsReviewEmailJob.get(job.id))
102
104
 
103
105
 
106
108
    layer = LaunchpadZopelessLayer
107
109
 
108
110
    def test_providesInterface(self):
109
 
        """MergeProposalNeedsReviewEmailJob provides the expected interfaces."""
 
111
        """MergeProposalNeedsReviewEmailJob provides expected interfaces."""
110
112
        bmp = self.factory.makeBranchMergeProposal()
111
113
        job = MergeProposalNeedsReviewEmailJob.create(bmp)
112
114
        verifyObject(IMergeProposalNeedsReviewEmailJob, job)
182
184
        verifyObject(IUpdatePreviewDiffJobSource, UpdatePreviewDiffJob)
183
185
 
184
186
    def test_providesInterface(self):
185
 
        """MergeProposalNeedsReviewEmailJob provides the expected interfaces."""
 
187
        """MergeProposalNeedsReviewEmailJob provides expected interfaces."""
186
188
        bmp = self.factory.makeBranchMergeProposal()
187
189
        job = UpdatePreviewDiffJob.create(bmp)
188
190
        verifyObject(IUpdatePreviewDiffJob, job)
207
209
        transaction.commit()
208
210
        self.checkExampleMerge(bmp.preview_diff.text)
209
211
 
 
212
    def test_run_object_events(self):
 
213
        # While the job runs a single IObjectModifiedEvent is issued when the
 
214
        # preview diff has been calculated.
 
215
        self.useBzrBranches(direct_database=True)
 
216
        bmp = create_example_merge(self)[0]
 
217
        job = UpdatePreviewDiffJob.create(bmp)
 
218
        self.factory.makeRevisionsForBranch(bmp.source_branch, count=1)
 
219
        bmp.source_branch.next_mirror_time = None
 
220
        transaction.commit()
 
221
        self.layer.switchDbUser(config.merge_proposal_jobs.dbuser)
 
222
        with EventRecorder() as event_recorder:
 
223
            JobRunner([job]).runAll()
 
224
        bmp_object_events = [
 
225
            event for event in event_recorder.events
 
226
            if (IObjectModifiedEvent.providedBy(event) and
 
227
                event.object == bmp)]
 
228
        self.assertEqual(
 
229
            1, len(bmp_object_events),
 
230
            "Expected one event, got: %r" % bmp_object_events)
 
231
        self.assertEqual(
 
232
            ["preview_diff"], bmp_object_events[0].edited_fields)
 
233
 
210
234
    def test_run_branches_not_ready(self):
211
235
        # If the job has been waiting for a significant period of time (15
212
236
        # minutes for now), we run the job anyway.  The checkReady method
452
476
        bmp = self.makeBranchMergeProposal(
453
477
            set_state=BranchMergeProposalStatus.NEEDS_REVIEW)
454
478
        self.completePendingJobs()
455
 
        old_merge_proposal = BranchMergeProposalDelta.snapshot(bmp)
 
479
        old_merge_proposal = (
 
480
            BranchMergeProposalNoPreviewDiffDelta.snapshot(bmp))
456
481
        bmp.commit_message = 'new commit message'
457
482
        event = ObjectModifiedEvent(
458
483
            bmp, old_merge_proposal, [], bmp.registrant)