~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Test for the exclude_conjoined_tasks param for BugTaskSearchParams."""

__metaclass__ = type

__all__ = []

from storm.store import Store
from testtools.matchers import Equals
from zope.component import getUtility

from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.bugs.interfaces.bugtask import (
    BugTaskSearchParams,
    BugTaskStatus,
    IBugTaskSet,
    )
from lp.registry.interfaces.series import SeriesStatus
from lp.testing import (
    person_logged_in,
    StormStatementRecorder,
    TestCaseWithFactory,
    )
from lp.testing.layers import DatabaseFunctionalLayer
from lp.testing.matchers import HasQueryCount


class TestSearchBase(TestCaseWithFactory):
    """Tests of exclude_conjoined_tasks param."""

    def makeBug(self, milestone):
        bug = self.factory.makeBug(
            product=milestone.product, distribution=milestone.distribution)
        with person_logged_in(milestone.target.owner):
            bug.default_bugtask.transitionToMilestone(
                milestone, milestone.target.owner)
        return bug


class TestProjectExcludeConjoinedMasterSearch(TestSearchBase):
    """Tests of exclude_conjoined_tasks param for project milestones."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestProjectExcludeConjoinedMasterSearch, self).setUp()
        self.bugtask_set = getUtility(IBugTaskSet)
        self.product = self.factory.makeProduct()
        self.milestone = self.factory.makeMilestone(
            product=self.product, name='foo')
        self.bug_count = 2
        self.bugs = [
            self.makeBug(self.milestone)
            for i in range(self.bug_count)]
        self.params = BugTaskSearchParams(
            user=None, milestone=self.milestone, exclude_conjoined_tasks=True)

    def test_search_results_count_simple(self):
        # Verify number of results with no conjoined masters.
        self.assertEqual(
            self.bug_count,
            self.bugtask_set.search(self.params).count())

    def test_search_query_count(self):
        # Verify query count.
        Store.of(self.milestone).flush()
        with StormStatementRecorder() as recorder:
            list(self.bugtask_set.search(self.params))
        # 1 query for the tasks, 1 query for the product (target) eager
        # loading.
        self.assertThat(recorder, HasQueryCount(Equals(2)))

    def test_search_results_count_with_other_productseries_tasks(self):
        # Test with zero conjoined masters and bugtasks targeted to
        # productseries that are not the development focus.
        productseries = self.factory.makeProductSeries(product=self.product)
        extra_bugtasks = 0
        for bug in self.bugs:
            extra_bugtasks += 1
            bugtask = self.factory.makeBugTask(bug=bug, target=productseries)
            with person_logged_in(self.product.owner):
                bugtask.transitionToMilestone(
                    self.milestone, self.product.owner)
            self.assertEqual(
                self.bug_count + extra_bugtasks,
                self.bugtask_set.search(self.params).count())

    def test_search_results_count_with_conjoined_masters(self):
        # Test with increasing numbers of conjoined masters.
        # The conjoined masters will exclude the conjoined slaves from
        # the results.
        tasks = list(self.bugtask_set.search(self.params))
        for bug in self.bugs:
            # The product bugtask is in the results before the conjoined
            # master is added.
            self.assertIn(
                (bug.id, self.product),
                [(task.bug.id, task.product) for task in tasks])
            bugtask = self.factory.makeBugTask(
                bug=bug, target=self.product.development_focus)
            tasks = list(self.bugtask_set.search(self.params))
            # The product bugtask is excluded from the results.
            self.assertEqual(self.bug_count, len(tasks))
            self.assertNotIn(
                (bug.id, self.product),
                [(task.bug.id, task.product) for task in tasks])

    def test_search_results_count_with_wontfix_conjoined_masters(self):
        # Test that conjoined master bugtasks in the WONTFIX status
        # don't cause the bug to be excluded.
        masters = [
            self.factory.makeBugTask(
                bug=bug, target=self.product.development_focus)
            for bug in self.bugs]
        tasks = list(self.bugtask_set.search(self.params))
        wontfix_masters_count = 0
        for bugtask in masters:
            wontfix_masters_count += 1
            self.assertNotIn(
                (bugtask.bug.id, self.product),
                [(task.bug.id, task.product) for task in tasks])
            with person_logged_in(self.product.owner):
                bugtask.transitionToStatus(
                    BugTaskStatus.WONTFIX, self.product.owner)
            tasks = list(self.bugtask_set.search(self.params))
            self.assertEqual(self.bug_count + wontfix_masters_count,
                             len(tasks))
            self.assertIn(
                (bugtask.bug.id, self.product),
                [(task.bug.id, task.product) for task in tasks])


class TestProjectGroupExcludeConjoinedMasterSearch(TestSearchBase):
    """Tests of exclude_conjoined_tasks param for project group milestones."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestProjectGroupExcludeConjoinedMasterSearch, self).setUp()
        self.bugtask_set = getUtility(IBugTaskSet)
        self.projectgroup = self.factory.makeProject()
        self.bug_count = 2
        self.bug_products = {}
        for i in range(self.bug_count):
            product = self.factory.makeProduct(project=self.projectgroup)
            product_milestone = self.factory.makeMilestone(
                product=product, name='foo')
            bug = self.makeBug(product_milestone)
            self.bug_products[bug] = product
        self.milestone = self.projectgroup.getMilestone('foo')
        self.params = BugTaskSearchParams(
            user=None, milestone=self.milestone, exclude_conjoined_tasks=True)

    def test_search_results_count_simple(self):
        # Verify number of results with no conjoined masters.
        self.assertEqual(
            self.bug_count,
            self.bugtask_set.search(self.params).count())

    def test_search_query_count(self):
        # Verify query count.
        Store.of(self.projectgroup).flush()
        with StormStatementRecorder() as recorder:
            list(self.bugtask_set.search(self.params))
        # 1 query for the tasks, 1 query for the product (target) eager
        # loading.
        self.assertThat(recorder, HasQueryCount(Equals(2)))

    def test_search_results_count_with_other_productseries_tasks(self):
        # Test with zero conjoined masters and bugtasks targeted to
        # productseries that are not the development focus.
        extra_bugtasks = 0
        for bug, product in self.bug_products.items():
            extra_bugtasks += 1
            productseries = self.factory.makeProductSeries(product=product)
            bugtask = self.factory.makeBugTask(bug=bug, target=productseries)
            with person_logged_in(product.owner):
                bugtask.transitionToMilestone(
                    product.getMilestone(self.milestone.name), product.owner)
            self.assertEqual(
                self.bug_count + extra_bugtasks,
                self.bugtask_set.search(self.params).count())

    def test_search_results_count_with_conjoined_masters(self):
        # Test with increasing numbers of conjoined masters.
        tasks = list(self.bugtask_set.search(self.params))
        for bug, product in self.bug_products.items():
            self.assertIn(
                (bug.id, product),
                [(task.bug.id, task.product) for task in tasks])
            self.factory.makeBugTask(
                bug=bug, target=product.development_focus)
            tasks = list(self.bugtask_set.search(self.params))
            self.assertEqual(
                self.bug_count,
                self.bugtask_set.search(self.params).count())
            self.assertNotIn(
                (bug.id, product),
                [(task.bug.id, task.product) for task in tasks])

    def test_search_results_count_with_irrelevant_conjoined_masters(self):
        # Verify that a conjoined master in one project of the project
        # group doesn't cause a bugtask on another project in the group
        # to be excluded from the project group milestone's bugs.
        extra_bugtasks = 0
        for bug, product in self.bug_products.items():
            extra_bugtasks += 1
            other_product = self.factory.makeProduct(
                project=self.projectgroup)
            # Create a new milestone with the same name.
            other_product_milestone = self.factory.makeMilestone(
                product=other_product,
                name=bug.default_bugtask.milestone.name)
            # Add bugtask on the new product and select the milestone.
            other_product_bugtask = self.factory.makeBugTask(
                bug=bug, target=other_product)
            with person_logged_in(other_product.owner):
                other_product_bugtask.transitionToMilestone(
                    other_product_milestone, other_product.owner)
            # Add conjoined master for the milestone on the new product.
            self.factory.makeBugTask(
                bug=bug, target=other_product.development_focus)
            # The bug count should not change, since we are just adding
            # bugtasks on existing bugs.
            self.assertEqual(
                self.bug_count + extra_bugtasks,
                self.bugtask_set.search(self.params).count())

    def test_search_results_count_with_wontfix_conjoined_masters(self):
        # Test that conjoined master bugtasks in the WONTFIX status
        # don't cause the bug to be excluded.
        masters = [
            self.factory.makeBugTask(
                bug=bug, target=product.development_focus)
            for bug, product in self.bug_products.items()]
        unexcluded_count = 0
        for bugtask in masters:
            unexcluded_count += 1
            with person_logged_in(product.owner):
                bugtask.transitionToStatus(
                    BugTaskStatus.WONTFIX, bugtask.target.owner)
            self.assertEqual(
                self.bug_count + unexcluded_count,
                self.bugtask_set.search(self.params).count())


class TestDistributionExcludeConjoinedMasterSearch(TestSearchBase):
    """Tests of exclude_conjoined_tasks param for distribution milestones."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestDistributionExcludeConjoinedMasterSearch, self).setUp()
        self.bugtask_set = getUtility(IBugTaskSet)
        self.distro = getUtility(ILaunchpadCelebrities).ubuntu
        self.milestone = self.factory.makeMilestone(
            distribution=self.distro, name='foo')
        self.bug_count = 2
        self.bugs = [
            self.makeBug(self.milestone)
            for i in range(self.bug_count)]
        self.params = BugTaskSearchParams(
            user=None, milestone=self.milestone, exclude_conjoined_tasks=True)

    def test_search_results_count_simple(self):
        # Verify number of results with no conjoined masters.
        self.assertEqual(
            self.bug_count,
            self.bugtask_set.search(self.params).count())

    def test_search_query_count(self):
        # Verify query count.
        # 1. Query all the distroseries to determine the distro's
        #    currentseries.
        # 2. Query the bugtasks.
        Store.of(self.milestone).flush()
        with StormStatementRecorder() as recorder:
            list(self.bugtask_set.search(self.params))
        self.assertThat(recorder, HasQueryCount(Equals(2)))

    def test_search_results_count_with_other_productseries_tasks(self):
        # Test with zero conjoined masters and bugtasks targeted to
        # productseries that are not the development focus.
        distroseries = self.factory.makeDistroSeries(
            distribution=self.distro, status=SeriesStatus.SUPPORTED)
        extra_bugtasks = 0
        for bug in self.bugs:
            extra_bugtasks += 1
            bugtask = self.factory.makeBugTask(bug=bug, target=distroseries)
            with person_logged_in(self.distro.owner):
                bugtask.transitionToMilestone(
                    self.milestone, self.distro.owner)
            self.assertEqual(
                self.bug_count + extra_bugtasks,
                self.bugtask_set.search(self.params).count())

    def test_search_results_count_with_conjoined_masters(self):
        # Test with increasing numbers of conjoined masters.
        tasks = list(self.bugtask_set.search(self.params))
        for bug in self.bugs:
            # The distro bugtask is in the results before the conjoined
            # master is added.
            self.assertIn(
                (bug.id, self.distro),
                [(task.bug.id, task.distribution) for task in tasks])
            self.factory.makeBugTask(
                bug=bug, target=self.distro.currentseries)
            tasks = list(self.bugtask_set.search(self.params))
            # The product bugtask is excluded from the results.
            self.assertEqual(self.bug_count, len(tasks))
            self.assertNotIn(
                (bug.id, self.distro),
                [(task.bug.id, task.distribution) for task in tasks])

    def test_search_results_count_with_wontfix_conjoined_masters(self):
        # Test that conjoined master bugtasks in the WONTFIX status
        # don't cause the bug to be excluded.
        masters = [
            self.factory.makeBugTask(
                bug=bug, target=self.distro.currentseries)
            for bug in self.bugs]
        wontfix_masters_count = 0
        tasks = list(self.bugtask_set.search(self.params))
        for bugtask in masters:
            wontfix_masters_count += 1
            # The distro bugtask is still excluded by the conjoined
            # master.
            self.assertNotIn(
                (bugtask.bug.id, self.distro),
                [(task.bug.id, task.distribution) for task in tasks])
            with person_logged_in(self.distro.owner):
                bugtask.transitionToStatus(
                    BugTaskStatus.WONTFIX, self.distro.owner)
            tasks = list(self.bugtask_set.search(self.params))
            self.assertEqual(
                self.bug_count + wontfix_masters_count,
                self.bugtask_set.search(self.params).count())
            # The distro bugtask is no longer excluded by the conjoined
            # master, since its status is WONTFIX.
            self.assertIn(
                (bugtask.bug.id, self.distro),
                [(task.bug.id, task.distribution) for task in tasks])