~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/soyuz/browser/tests/test_builder.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2012-01-02 15:48:28 UTC
  • mfrom: (14513.2.4 builders-timeout-903827)
  • Revision ID: launchpad@pqm.canonical.com-20120102154828-r4echs7u11g99u8b
[r=adeuring][bug=903827] Cache objects prior to displaying builders'
        homepage.

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 the lp.soyuz.browser.builder module."""
5
5
 
6
6
__metaclass__ = type
7
7
 
 
8
from testtools.matchers import LessThan
 
9
from zope.component import getUtility
 
10
from zope.security.proxy import removeSecurityProxy
 
11
 
 
12
from lp.buildmaster.interfaces.builder import IBuilderSet
 
13
from lp.buildmaster.interfaces.buildqueue import IBuildQueueSet
8
14
from lp.services.webapp import canonical_url
9
 
from lp.testing import TestCaseWithFactory
10
 
from lp.testing.layers import DatabaseFunctionalLayer
 
15
from lp.soyuz.browser.tests.test_builder_views import BuildCreationMixin
 
16
from lp.testing import (
 
17
    record_two_runs,
 
18
    TestCaseWithFactory,
 
19
    )
 
20
from lp.testing.layers import (
 
21
    DatabaseFunctionalLayer,
 
22
    LaunchpadFunctionalLayer,
 
23
    )
 
24
from lp.testing.matchers import HasQueryCount
11
25
from lp.testing.publication import test_traverse
 
26
from lp.testing.views import create_initialized_view
 
27
from lp.translations.interfaces.translationtemplatesbuildjob import (
 
28
    ITranslationTemplatesBuildJobSource,
 
29
    )
12
30
 
13
31
 
14
32
class TestBuildersNavigation(TestCaseWithFactory):
38
56
        self.assertEqual(
39
57
            canonical_url(build),
40
58
            request.response.getHeader('location'))
 
59
 
 
60
 
 
61
def builders_homepage_render():
 
62
    builders = getUtility(IBuilderSet)
 
63
    create_initialized_view(builders, "+index").render()
 
64
 
 
65
 
 
66
class TestBuildersHomepage(TestCaseWithFactory, BuildCreationMixin):
 
67
 
 
68
    layer = LaunchpadFunctionalLayer
 
69
 
 
70
    # XXX rvb: the 3 additional queries per build are the result of the calls
 
71
    # to:
 
72
    # - builder.currentjob
 
73
    # - buildqueue.specific_job
 
74
    # These could be converted into cachedproperty and pre-populated in
 
75
    # bulk but several tests assert that the value returned by these
 
76
    # these properties are up to date.  Since they are not really expensive
 
77
    # to compute I'll leave them as regular properties for now.
 
78
 
 
79
    def test_builders_binary_package_build_query_count(self):
 
80
        def create_build():
 
81
            build = self.createBinaryPackageBuild()
 
82
            queue = build.queueBuild()
 
83
            queue.markAsBuilding(build.builder)
 
84
 
 
85
        recorder1, recorder2 = record_two_runs(
 
86
            builders_homepage_render, create_build, 2)
 
87
 
 
88
        self.assertThat(
 
89
            recorder2,
 
90
            HasQueryCount(LessThan(recorder1.count + 3 * 2 + 1)))
 
91
 
 
92
    def test_builders_recipe_build_query_count(self):
 
93
        def create_build():
 
94
            build = self.createRecipeBuildWithBuilder()
 
95
            queue = build.queueBuild()
 
96
            queue.markAsBuilding(build.builder)
 
97
 
 
98
        recorder1, recorder2 = record_two_runs(
 
99
            builders_homepage_render, create_build, 2)
 
100
 
 
101
        self.assertThat(
 
102
            recorder2,
 
103
            HasQueryCount(LessThan(recorder1.count + 3 * 2 + 1)))
 
104
 
 
105
    def test_builders_translation_template_build_query_count(self):
 
106
        def create_build():
 
107
            jobset = getUtility(ITranslationTemplatesBuildJobSource)
 
108
            branch = self.factory.makeBranch()
 
109
            specific_job = jobset.create(branch)
 
110
            queueset = getUtility(IBuildQueueSet)
 
111
            # Using rSP is required to get the job id.
 
112
            naked_job = removeSecurityProxy(specific_job.job)
 
113
            job_id = naked_job.id
 
114
            queue = queueset.get(job_id)
 
115
            queue.markAsBuilding(self.factory.makeBuilder())
 
116
 
 
117
        recorder1, recorder2 = record_two_runs(
 
118
            builders_homepage_render, create_build, 2)
 
119
 
 
120
        self.assertThat(
 
121
            recorder2,
 
122
            HasQueryCount(LessThan(recorder1.count + 3 * 2 + 1)))