~launchpad-pqm/launchpad/devel

« back to all changes in this revision

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

[rs=buildbot-poller] automatic merge from stable. Revisions: 14329,
        14330, 14331, 14332, 14333 included.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
from storm.locals import Store
7
7
from testtools.matchers import Equals
 
8
from zope.component import getUtility
8
9
from zope.security.proxy import removeSecurityProxy
9
10
 
 
11
from canonical.database.sqlbase import flush_database_caches
10
12
from canonical.launchpad.ftests import login
11
13
from canonical.launchpad.webapp.servers import LaunchpadTestRequest
12
14
from canonical.testing.layers import LaunchpadFunctionalLayer
 
15
from lp.buildmaster.enums import BuildFarmJobType
 
16
from lp.buildmaster.interfaces.buildfarmjob import IBuildFarmJobSource
13
17
from lp.soyuz.browser.builder import BuilderEditView
14
18
from lp.testing import (
15
19
    StormStatementRecorder,
19
23
from lp.testing.matchers import HasQueryCount
20
24
from lp.testing.sampledata import ADMIN_EMAIL
21
25
from lp.testing.views import create_initialized_view
 
26
from lp.translations.interfaces.translationtemplatesbuild import (
 
27
    ITranslationTemplatesBuildSource,
 
28
    )
22
29
 
23
30
 
24
31
class TestBuilderEditView(TestCaseWithFactory):
62
69
 
63
70
    layer = LaunchpadFunctionalLayer
64
71
 
65
 
    def createRecipeBuildWithBuilder(self, builder=None):
66
 
        build = self.factory.makeSourcePackageRecipeBuild()
 
72
    nb_objects = 2
 
73
 
 
74
    def setUp(self):
 
75
        super(TestBuilderHistoryView, self).setUp()
 
76
        self.builder = self.factory.makeBuilder()
 
77
 
 
78
    def createTranslationTemplateBuildWithBuilder(self):
 
79
        build_farm_job_source = getUtility(IBuildFarmJobSource)
 
80
        build_farm_job = build_farm_job_source.new(
 
81
            BuildFarmJobType.TRANSLATIONTEMPLATESBUILD)
 
82
        source = getUtility(ITranslationTemplatesBuildSource)
 
83
        branch = self.factory.makeBranch()
 
84
        build = source.create(build_farm_job, branch)
 
85
        removeSecurityProxy(build).builder = self.builder
 
86
        return build
 
87
 
 
88
    def createRecipeBuildWithBuilder(self):
 
89
        branch1 = self.factory.makeAnyBranch()
 
90
        branch2 = self.factory.makeAnyBranch()
 
91
        build = self.factory.makeSourcePackageRecipeBuild(
 
92
            recipe=self.factory.makeSourcePackageRecipe(
 
93
                branches=[branch1, branch2]))
67
94
        Store.of(build).flush()
68
 
        if builder is None:
69
 
            builder = self.factory.makeBuilder()
70
 
        removeSecurityProxy(build).builder = builder
71
 
        return build
72
 
 
73
 
    def test_build_history_queries_count(self):
74
 
        # The number of queries issued by setupBuildList is not dependent
75
 
        # on the number of builds.
76
 
        builder = self.factory.makeBuilder()
77
 
        self.createRecipeBuildWithBuilder(builder)
78
 
        self.createRecipeBuildWithBuilder(builder)
79
 
        # Record how many queries are issued when setupBuildList is
80
 
        # called with 2 builds.
 
95
        removeSecurityProxy(build).builder = self.builder
 
96
        return build
 
97
 
 
98
    def createBinaryPackageBuild(self):
 
99
        build = self.factory.makeBinaryPackageBuild()
 
100
        removeSecurityProxy(build).builder = self.builder
 
101
        return build
 
102
 
 
103
    def _record_queries_count(self, tested_method, item_creator):
 
104
        # A simple helper that returns the two storm statement recorders
 
105
        # obtained when running tested_method with {nb_objects} items creater
 
106
        # (using item_creator) and then with {nb_objects}*2 items created.
 
107
        for i in range(self.nb_objects):
 
108
            item_creator()
 
109
        # Record how many queries are issued when tested_method is
 
110
        # called with {nb_objects} items created.
 
111
        flush_database_caches()
81
112
        with StormStatementRecorder() as recorder1:
82
 
            view = create_initialized_view(builder, '+history')
83
 
            view.setupBuildList()
84
 
            self.assertEqual(2, len(view.complete_builds))
85
 
        # Create two more builds.
86
 
        self.createRecipeBuildWithBuilder(builder)
87
 
        self.createRecipeBuildWithBuilder(builder)
 
113
            tested_method()
 
114
        # Create {nb_objects} more items.
 
115
        for i in range(self.nb_objects):
 
116
            item_creator()
88
117
        # Record again the number of queries issued.
 
118
        flush_database_caches()
89
119
        with StormStatementRecorder() as recorder2:
90
 
            view = create_initialized_view(builder, '+history')
91
 
            view.setupBuildList()
92
 
            self.assertEqual(4, len(view.complete_builds))
 
120
            tested_method()
 
121
        return recorder1, recorder2
 
122
 
 
123
    def test_build_history_queries_count_view_recipe_builds(self):
 
124
        # The builder's history view creation (i.e. the call to
 
125
        # view.setupBuildList) issues a constant number of queries
 
126
        # when recipe builds are displayed.
 
127
        def builder_history_render():
 
128
            create_initialized_view(self.builder, '+history').render()
 
129
        recorder1, recorder2 = self._record_queries_count(
 
130
            builder_history_render,
 
131
            self.createRecipeBuildWithBuilder)
 
132
 
 
133
        # XXX: rvb 2011-11-14 bug=890326: The only query remaining is the
 
134
        # one that results from a call to
 
135
        # sourcepackagerecipebuild.buildqueue_record for each recipe build.
 
136
        self.assertThat(
 
137
            recorder2,
 
138
            HasQueryCount(Equals(recorder1.count + 1 * self.nb_objects)))
 
139
 
 
140
    def test_build_history_queries_count_binary_package_builds(self):
 
141
        # Rendering to builder's history issues a constant number of queries
 
142
        # when binary builds are displayed.
 
143
        def builder_history_render():
 
144
            create_initialized_view(self.builder, '+history').render()
 
145
        recorder1, recorder2 = self._record_queries_count(
 
146
            builder_history_render,
 
147
            self.createBinaryPackageBuild)
 
148
 
 
149
        self.assertThat(recorder2, HasQueryCount(Equals(recorder1.count)))
 
150
 
 
151
    def test_build_history_queries_count_translation_template_builds(self):
 
152
        # Rendering to builder's history issues a constant number of queries
 
153
        # when translation template builds are displayed.
 
154
        def builder_history_render():
 
155
            create_initialized_view(self.builder, '+history').render()
 
156
        recorder1, recorder2 = self._record_queries_count(
 
157
            builder_history_render,
 
158
            self.createTranslationTemplateBuildWithBuilder)
93
159
 
94
160
        self.assertThat(recorder2, HasQueryCount(Equals(recorder1.count)))