216
230
self.assertFalse(nomination.canApprove(self.factory.makePerson()))
217
231
self.assertTrue(nomination.canApprove(package_perm.person))
218
232
self.assertTrue(nomination.canApprove(comp_perm.person))
235
class TestApprovePerformance(TestCaseWithFactory):
236
"""Test the performance of `BugNomination.approve`."""
238
layer = DatabaseFunctionalLayer
240
def check_heat_queries(self, nomination):
241
self.assertFalse(nomination.isApproved())
242
# Statement patterns we're looking for:
243
pattern = "^(SELECT Bug.heat|UPDATE .* max_bug_heat)"
244
matcher = re.compile(pattern , re.DOTALL | re.I).match
245
queries_heat = lambda statement: matcher(statement) is not None
246
with person_logged_in(nomination.target.owner):
247
flush_database_updates()
248
with StormStatementRecorder(queries_heat) as recorder:
249
nomination.approve(nomination.target.owner)
250
# Post-process the recorder to only have heat-related statements.
251
recorder.query_data = [
252
data for statement, data in izip(
253
recorder.statements, recorder.query_data)
254
if queries_heat(statement)]
256
"query_data", Content(UTF8_TEXT, lambda: [str(recorder)]))
257
# If there isn't at least one update to max_bug_heat it may mean that
258
# this test is no longer relevant.
259
self.assertThat(recorder, HasQueryCount(Not(Equals(0))))
260
# At present there are two updates to max_bug_heat because
261
# recalculateBugHeatCache is called twice, and, even though it is
262
# lazily evaluated, there are both explicit and implicit flushes in
263
# bugtask subscriber code.
264
self.assertThat(recorder, HasQueryCount(LessThan(3)))
266
def test_heat_queries_for_productseries(self):
267
# The number of heat-related queries when approving a product series
268
# nomination is as low as reasonably possible.
269
series = self.factory.makeProductSeries()
270
bug = self.factory.makeBug(product=series.product)
271
with person_logged_in(series.owner):
272
nomination = bug.addNomination(
273
target=series, owner=series.owner)
274
self.check_heat_queries(nomination)
276
def test_heat_queries_for_distroseries(self):
277
# The number of heat-related queries when approving a distro series
278
# nomination is as low as reasonably possible.
279
series = self.factory.makeDistroSeries()
280
bug = self.factory.makeBug(distribution=series.distribution)
281
with person_logged_in(series.owner):
282
nomination = bug.addNomination(
283
target=series, owner=series.owner)
284
self.check_heat_queries(nomination)