OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered by a BSD-style |
| 3 # license that can be found in the LICENSE file or at |
| 4 # https://developers.google.com/open-source/licenses/bsd |
| 5 |
| 6 """Unittests for monorail.feature.activities.""" |
| 7 |
| 8 import unittest |
| 9 |
| 10 import mox |
| 11 |
| 12 from features import activities |
| 13 from framework import framework_views |
| 14 from framework import profiler |
| 15 from proto import tracker_pb2 |
| 16 from proto import user_pb2 |
| 17 from services import service_manager |
| 18 from testing import fake |
| 19 from testing import testing_helpers |
| 20 |
| 21 |
| 22 class ActivitiesTest(unittest.TestCase): |
| 23 |
| 24 def setUp(self): |
| 25 self.services = service_manager.Services( |
| 26 config=fake.ConfigService(), |
| 27 issue=fake.IssueService(), |
| 28 user=fake.UserService(), |
| 29 project=fake.ProjectService(), |
| 30 ) |
| 31 |
| 32 self.project_name = 'proj' |
| 33 self.project_id = 987 |
| 34 self.project = self.services.project.TestAddProject( |
| 35 self.project_name, project_id=self.project_id, |
| 36 process_inbound_email=True) |
| 37 |
| 38 self.issue_id = 11 |
| 39 self.issue_local_id = 100 |
| 40 self.issue = tracker_pb2.Issue() |
| 41 self.issue.issue_id = self.issue_id |
| 42 self.issue.project_id = self.project_id |
| 43 self.issue.local_id = self.issue_local_id |
| 44 self.services.issue.TestAddIssue(self.issue) |
| 45 |
| 46 self.comment_id = 123 |
| 47 self.comment_timestamp = 120 |
| 48 self.user_id = 2 |
| 49 self.mr_after = 1234 |
| 50 |
| 51 self.mox = mox.Mox() |
| 52 |
| 53 def tearDown(self): |
| 54 self.mox.UnsetStubs() |
| 55 self.mox.ResetAll() |
| 56 |
| 57 def testActivities_NoUpdates(self): |
| 58 mr = testing_helpers.MakeMonorailRequest() |
| 59 updates_data = activities.GatherUpdatesData( |
| 60 self.services, mr, profiler.Profiler(), project_ids=[self.project_id], |
| 61 user_ids=None, ending=None, updates_page_url=None, autolink=None, |
| 62 highlight=None) |
| 63 |
| 64 self.assertIsNone(updates_data['pagination']) |
| 65 self.assertIsNone(updates_data['no_stars']) |
| 66 self.assertIsNone(updates_data['updates_data']) |
| 67 self.assertEqual('yes', updates_data['no_activities']) |
| 68 self.assertIsNone(updates_data['ending_type']) |
| 69 |
| 70 def createAndAssertUpdates(self, project_ids=None, user_ids=None, |
| 71 ascending=True): |
| 72 user = user_pb2.MakeUser() |
| 73 comment_1 = tracker_pb2.IssueComment( |
| 74 id=self.comment_id, issue_id=self.issue_id, |
| 75 project_id=self.project_id, user_id=self.user_id, |
| 76 content='this is the 1st comment', |
| 77 timestamp=self.comment_timestamp) |
| 78 self.mox.StubOutWithMock(self.services.issue, 'GetComments') |
| 79 |
| 80 created_order = 'created' |
| 81 field = 'project_id' if project_ids else 'commenter_id' |
| 82 where_clauses = [('Issue.id = Comment.issue_id', [])] |
| 83 if project_ids: |
| 84 where_clauses.append(('Comment.project_id IN (%s)', project_ids)) |
| 85 if user_ids: |
| 86 where_clauses.append(('Comment.commenter_id IN (%s)', user_ids)) |
| 87 if ascending: |
| 88 where_clauses.append(('created > %s', [self.mr_after])) |
| 89 else: |
| 90 created_order += ' DESC' |
| 91 self.services.issue.GetComments( |
| 92 mox.IgnoreArg(), deleted_by=None, |
| 93 joins=[('Issue', [])], limit=activities.UPDATES_PER_PAGE + 1, |
| 94 order_by=[(created_order, [])], |
| 95 use_clause='USE INDEX (%s) USE INDEX FOR ORDER BY (%s)' % (field, |
| 96 field), |
| 97 where=where_clauses).AndReturn([comment_1]) |
| 98 |
| 99 self.mox.StubOutWithMock(framework_views, 'MakeAllUserViews') |
| 100 framework_views.MakeAllUserViews( |
| 101 mox.IgnoreArg(), self.services.user, [self.user_id], []).AndReturn( |
| 102 {self.user_id: user}) |
| 103 |
| 104 self.mox.ReplayAll() |
| 105 |
| 106 mr = testing_helpers.MakeMonorailRequest() |
| 107 if ascending: |
| 108 mr.after = self.mr_after |
| 109 |
| 110 updates_page_url='testing/testing' |
| 111 updates_data = activities.GatherUpdatesData( |
| 112 self.services, mr, profiler.Profiler(), project_ids=project_ids, |
| 113 user_ids=user_ids, ending=None, autolink=None, |
| 114 highlight='highlightme', updates_page_url=updates_page_url) |
| 115 self.mox.VerifyAll() |
| 116 |
| 117 if mr.after: |
| 118 pagination = updates_data['pagination'] |
| 119 self.assertIsNone(pagination.last) |
| 120 self.assertEquals('%s?before=%d' % (updates_page_url.split('/')[-1], |
| 121 self.comment_timestamp), |
| 122 pagination.next_url) |
| 123 self.assertEquals('%s?after=%d' % (updates_page_url.split('/')[-1], |
| 124 self.comment_timestamp), |
| 125 pagination.prev_url) |
| 126 |
| 127 activity_view = updates_data['updates_data'].older[0] |
| 128 self.assertEqual( |
| 129 '<a class="ot-issue-link"\n href="/p//issues/detail?id=%s#c_id%s"\n >' |
| 130 'issue %s</a>\n\n()\n\n\n\n\n \n commented on' % ( |
| 131 self.issue_local_id, self.comment_id, self.issue_local_id), |
| 132 activity_view.escaped_title) |
| 133 self.assertEqual( |
| 134 '<span class="ot-issue-comment">\n this is the 1st comment\n</span>', |
| 135 activity_view.escaped_body) |
| 136 self.assertEqual('highlightme', activity_view.highlight) |
| 137 self.assertEqual(self.project_name, activity_view.project_name) |
| 138 |
| 139 def testActivities_AscendingProjectUpdates(self): |
| 140 self.createAndAssertUpdates(project_ids=[self.project_id], ascending=True) |
| 141 |
| 142 def testActivities_DescendingProjectUpdates(self): |
| 143 self.createAndAssertUpdates(project_ids=[self.project_id], ascending=False) |
| 144 |
| 145 def testActivities_AscendingUserUpdates(self): |
| 146 self.createAndAssertUpdates(user_ids=[self.user_id], ascending=True) |
| 147 |
| 148 def testActivities_DescendingUserUpdates(self): |
| 149 self.createAndAssertUpdates(user_ids=[self.user_id], ascending=False) |
| 150 |
| 151 def testActivities_SpecifyProjectAndUser(self): |
| 152 self.createAndAssertUpdates( |
| 153 project_ids=[self.project_id], user_ids=[self.user_id], ascending=False) |
| 154 |
OLD | NEW |