~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/bugs/model/tests/test_bug.py

[r=jtv][bug=874250] Load preferredemail when calculating bug
 subscribers of any kind via BugSubscriptionInfo.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
from lp.bugs.errors import BugCannotBePrivate
23
23
from lp.bugs.interfaces.bugnotification import IBugNotificationSet
24
24
from lp.bugs.interfaces.bugtask import BugTaskStatus
 
25
from lp.bugs.mail.bugnotificationrecipients import BugNotificationRecipients
25
26
from lp.bugs.model.bug import (
26
27
    BugNotification,
27
28
    BugSubscriptionInfo,
35
36
    feature_flags,
36
37
    login_person,
37
38
    person_logged_in,
 
39
    record_two_runs,
38
40
    set_feature_flag,
39
41
    StormStatementRecorder,
40
42
    TestCaseWithFactory,
166
168
        with StormStatementRecorder() as recorder:
167
169
            subscribers = list(bug.getDirectSubscribers())
168
170
            self.assertThat(len(subscribers), Equals(10 + 1))
169
 
            self.assertThat(recorder, HasQueryCount(Equals(1)))
 
171
            self.assertThat(recorder, HasQueryCount(Equals(2)))
170
172
 
171
173
    def test_mark_as_duplicate_query_count(self):
172
174
        bug = self.factory.makeBug()
476
478
        self.assertContentEqual(public_branches, linked_branches)
477
479
        self.assertNotIn(private_branch, linked_branches)
478
480
 
 
481
    def test_getDirectSubscribers_with_recipients_query_count(self):
 
482
        # getDirectSubscribers() uses a constant number of queries when given
 
483
        # a recipients argument regardless of the number of subscribers.
 
484
        bug = self.factory.makeBug()
 
485
 
 
486
        def create_subscriber():
 
487
            subscriber = self.factory.makePerson()
 
488
            with person_logged_in(subscriber):
 
489
                bug.subscribe(subscriber, subscriber)
 
490
 
 
491
        def get_subscribers():
 
492
            recipients = BugNotificationRecipients()
 
493
            subs = bug.getDirectSubscribers(recipients=recipients)
 
494
            list(subs)  # Ensure they're pulled.
 
495
 
 
496
        recorder1, recorder2 = record_two_runs(
 
497
            get_subscribers, create_subscriber, 3)
 
498
        self.assertThat(
 
499
            recorder2, HasQueryCount(Equals(recorder1.count)))
 
500
 
 
501
    def test_getSubscribersFromDuplicates_with_recipients_query_count(self):
 
502
        # getSubscribersFromDuplicates() uses a constant number of queries
 
503
        # when given a recipients argument regardless of the number of
 
504
        # subscribers.
 
505
        bug = self.factory.makeBug()
 
506
        duplicate_bug = self.factory.makeBug()
 
507
        with person_logged_in(duplicate_bug.owner):
 
508
            duplicate_bug.markAsDuplicate(bug)
 
509
 
 
510
        def create_subscriber():
 
511
            subscriber = self.factory.makePerson()
 
512
            with person_logged_in(subscriber):
 
513
                duplicate_bug.subscribe(subscriber, subscriber)
 
514
 
 
515
        def get_subscribers():
 
516
            recipients = BugNotificationRecipients()
 
517
            subs = bug.getSubscribersFromDuplicates(recipients=recipients)
 
518
            list(subs)  # Ensure they're pulled.
 
519
 
 
520
        recorder1, recorder2 = record_two_runs(
 
521
            get_subscribers, create_subscriber, 3)
 
522
        self.assertThat(
 
523
            recorder2, HasQueryCount(Equals(recorder1.count)))
 
524
 
 
525
    def test_getAlsoNotifiedSubscribers_with_recipients_query_count(self):
 
526
        # getAlsoNotifiedSubscribers() uses a constant number of queries when
 
527
        # given a recipients argument regardless of the number of subscribers.
 
528
        bug = self.factory.makeBug()
 
529
 
 
530
        def create_stuff():
 
531
            # Create a new bugtask, set its assignee, set its pillar's
 
532
            # official_malone=True, and subscribe someone to its target.
 
533
            bugtask = self.factory.makeBugTask(bug=bug)
 
534
            with person_logged_in(bugtask.owner):
 
535
                bugtask.transitionToAssignee(bugtask.owner)
 
536
            with person_logged_in(bugtask.pillar.owner):
 
537
                bugtask.pillar.official_malone = True
 
538
            subscriber = self.factory.makePerson()
 
539
            with person_logged_in(subscriber):
 
540
                bugtask.target.addSubscription(
 
541
                    subscriber, subscriber)
 
542
 
 
543
        def get_subscribers():
 
544
            recipients = BugNotificationRecipients()
 
545
            subs = bug.getAlsoNotifiedSubscribers(recipients=recipients)
 
546
            list(subs)  # Ensure they're pulled.
 
547
 
 
548
        recorder1, recorder2 = record_two_runs(
 
549
            get_subscribers, create_stuff, 3)
 
550
        self.assertThat(
 
551
            recorder2, HasQueryCount(Equals(recorder1.count)))
 
552
 
479
553
 
480
554
class TestBugPrivateAndSecurityRelatedUpdatesMixin:
481
555
 
553
627
        # If the bug is for a private project, then other direct subscribers
554
628
        # should be unsubscribed.
555
629
 
556
 
        (bug, bug_owner,  bugtask_a, bugtask_b, default_bugtask) = (
 
630
        (bug, bug_owner, bugtask_a, bugtask_b, default_bugtask) = (
557
631
            self.createBugTasksAndSubscribers())
558
632
        initial_subscribers = set((
559
633
            self.factory.makePerson(), bugtask_a.owner, bug_owner,
586
660
        # If the bug is for a private project, then other direct subscribers
587
661
        # should be unsubscribed.
588
662
 
589
 
        (bug, bug_owner,  bugtask_a, bugtask_b, default_bugtask) = (
 
663
        (bug, bug_owner, bugtask_a, bugtask_b, default_bugtask) = (
590
664
            self.createBugTasksAndSubscribers(private_security_related=True))
591
665
        initial_subscribers = set((
592
666
            self.factory.makePerson(), bug_owner,
617
691
        # If the bug is for a private project, then other direct subscribers
618
692
        # should be unsubscribed.
619
693
 
620
 
        (bug, bug_owner,  bugtask_a, bugtask_b, default_bugtask) = (
 
694
        (bug, bug_owner, bugtask_a, bugtask_b, default_bugtask) = (
621
695
            self.createBugTasksAndSubscribers(private_security_related=True))
622
696
        initial_subscribers = set((
623
 
            self.factory.makePerson(),  bug_owner,
 
697
            self.factory.makePerson(), bug_owner,
624
698
            bugtask_a.pillar.security_contact, bugtask_a.pillar.driver,
625
699
            bugtask_a.pillar.bug_supervisor))
626
700
 
644
718
        # When a bug is marked as private=false and security_related=false,
645
719
        # any existing subscriptions are left alone.
646
720
 
647
 
        (bug, bug_owner,  bugtask_a, bugtask_b, default_bugtask) = (
 
721
        (bug, bug_owner, bugtask_a, bugtask_b, default_bugtask) = (
648
722
            self.createBugTasksAndSubscribers(private_security_related=True))
649
723
        initial_subscribers = set((
650
724
            self.factory.makePerson(), bug_owner,
751
825
        # The bug supervisors are unsubscribed if a bug is made public and an
752
826
        # email is sent telling them they have been unsubscribed.
753
827
 
754
 
        (bug, bug_owner,  bugtask_a, bugtask_b, default_bugtask) = (
 
828
        (bug, bug_owner, bugtask_a, bugtask_b, default_bugtask) = (
755
829
            self.createBugTasksAndSubscribers(private_security_related=True))
756
830
 
757
831
        with person_logged_in(bug_owner):
796
870
        # set to false and an email is sent telling them they have been
797
871
        # unsubscribed.
798
872
 
799
 
        (bug, bug_owner,  bugtask_a, bugtask_b, default_bugtask) = (
 
873
        (bug, bug_owner, bugtask_a, bugtask_b, default_bugtask) = (
800
874
            self.createBugTasksAndSubscribers(private_security_related=True))
801
875
 
802
876
        with person_logged_in(bug_owner):