~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/bugs/browser/tests/test_bugtask.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-08-31 17:40:09 UTC
  • mfrom: (13752.3.11 bug-comments-lazy-load)
  • Revision ID: launchpad@pqm.canonical.com-20110831174009-jxniqis4fzhnds6m
[r=jtv][bug=558642] Bug comments can now be loaded asynchronously for
        feature-flagged users.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
import transaction
7
7
 
8
 
from datetime import datetime
 
8
from datetime import (
 
9
    datetime,
 
10
    timedelta,
 
11
    )
9
12
 
10
13
from lazr.lifecycle.event import ObjectModifiedEvent
11
14
from lazr.lifecycle.snapshot import Snapshot
20
23
from zope.interface import providedBy
21
24
from zope.security.proxy import removeSecurityProxy
22
25
 
 
26
from canonical.config import config
23
27
from canonical.launchpad.ftests import (
24
28
    ANONYMOUS,
25
29
    login,
33
37
    LaunchpadFunctionalLayer,
34
38
    )
35
39
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
 
40
from lp.bugs.adapters.bugchange import BugTaskStatusChange
36
41
from lp.bugs.browser.bugtask import (
37
42
    BugActivityItem,
38
43
    BugTaskEditView,
992
997
        self.assertEquals(
993
998
            "- foo<br />+ bar &amp;&lt;&gt;",
994
999
            BugActivityItem(bug.activity[-1]).change_details)
 
1000
 
 
1001
 
 
1002
class TestBugTaskBatchedCommentsAndActivityView(TestCaseWithFactory):
 
1003
    """Tests for the BugTaskBatchedCommentsAndActivityView class."""
 
1004
 
 
1005
    layer = LaunchpadFunctionalLayer
 
1006
 
 
1007
    def _makeNoisyBug(self, comments_only=False):
 
1008
        """Create and return a bug with a lot of comments and activity."""
 
1009
        bug = self.factory.makeBug(
 
1010
            date_created=datetime.now(UTC) - timedelta(days=30))
 
1011
        with person_logged_in(bug.owner):
 
1012
            if not comments_only:
 
1013
                for i in range(10):
 
1014
                    task = self.factory.makeBugTask(bug=bug)
 
1015
                    change = BugTaskStatusChange(
 
1016
                        task, datetime.now(UTC), task.product.owner, 'status',
 
1017
                        BugTaskStatus.NEW, BugTaskStatus.TRIAGED)
 
1018
                    bug.addChange(change)
 
1019
            for i in range (10):
 
1020
                msg = self.factory.makeMessage(
 
1021
                    owner=bug.owner, content="Message %i." % i,
 
1022
                    datecreated=datetime.now(UTC) - timedelta(days=20-i))
 
1023
                bug.linkMessage(msg, user=bug.owner)
 
1024
        return bug
 
1025
 
 
1026
    def test_offset(self):
 
1027
        # BugTaskBatchedCommentsAndActivityView.offset returns the
 
1028
        # current offset being used to select a batch of bug comments
 
1029
        # and activity. If one is not specified, the view's
 
1030
        # visible_initial_comments count will be returned (so that
 
1031
        # comments already shown on the page won't appear twice).
 
1032
        bug_task = self.factory.makeBugTask()
 
1033
        view = create_initialized_view(bug_task, '+batched-comments')
 
1034
        self.assertEqual(view.visible_initial_comments, view.offset)
 
1035
        view = create_initialized_view(
 
1036
            bug_task, '+batched-comments', form={'offset': 100})
 
1037
        self.assertEqual(100, view.offset)
 
1038
 
 
1039
    def test_batch_size(self):
 
1040
        # BugTaskBatchedCommentsAndActivityView.batch_size returns the
 
1041
        # current batch_size being used to select a batch of bug comments
 
1042
        # and activity or the default configured batch size if one has
 
1043
        # not been specified.
 
1044
        bug_task = self.factory.makeBugTask()
 
1045
        view = create_initialized_view(bug_task, '+batched-comments')
 
1046
        self.assertEqual(
 
1047
            config.malone.comments_list_default_batch_size,
 
1048
            view.batch_size)
 
1049
        view = create_initialized_view(
 
1050
            bug_task, '+batched-comments', form={'batch_size': 20})
 
1051
        self.assertEqual(20, view.batch_size)
 
1052
 
 
1053
    def test_event_groups_only_returns_batch_size_results(self):
 
1054
        # BugTaskBatchedCommentsAndActivityView._event_groups will
 
1055
        # return only batch_size results.
 
1056
        bug = self._makeNoisyBug()
 
1057
        view = create_initialized_view(
 
1058
            bug.default_bugtask, '+batched-comments',
 
1059
            form={'batch_size': 10})
 
1060
        self.assertEqual(10, len([group for group in view._event_groups]))
 
1061
 
 
1062
    def test_activity_and_comments_matches_unbatched_version(self):
 
1063
        # BugTaskBatchedCommentsAndActivityView extends BugTaskView in
 
1064
        # order to add the batching logic and reduce rendering
 
1065
        # overheads. The results of activity_and_comments is the same
 
1066
        # for both.
 
1067
        # We create a bug with comments only so that we can test the
 
1068
        # contents of activity_and_comments properly. Trying to test it
 
1069
        # with multiply different datatypes is fragile at best.
 
1070
        bug = self._makeNoisyBug(comments_only=True)
 
1071
        # We create a batched view with an offset of 0 so that all the
 
1072
        # comments are returned.
 
1073
        batched_view = create_initialized_view(
 
1074
            bug.default_bugtask, '+batched-comments',
 
1075
            form={'offset': 0})
 
1076
        unbatched_view = create_initialized_view(
 
1077
            bug.default_bugtask, '+index')
 
1078
        self.assertEqual(
 
1079
            len(unbatched_view.activity_and_comments),
 
1080
            len(batched_view.activity_and_comments))
 
1081
        for i in range(len(unbatched_view.activity_and_comments)):
 
1082
            unbatched_item = unbatched_view.activity_and_comments[i]
 
1083
            batched_item = batched_view.activity_and_comments[i]
 
1084
            self.assertEqual(
 
1085
                unbatched_item['comment'].text_for_display,
 
1086
                batched_item['comment'].text_for_display)