blob: 800a91351c024f035deaab4bfc1345a55bcef43e [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed 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"""Unit tests for helpers module."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
12
13from features import hotlist_helpers
14from features import features_constants
15from framework import profiler
16from framework import table_view_helpers
17from framework import sorting
18from services import service_manager
19from testing import testing_helpers
20from testing import fake
21from tracker import tablecell
22from tracker import tracker_bizobj
23from proto import features_pb2
24from proto import tracker_pb2
25
26
27class HotlistTableDataTest(unittest.TestCase):
28
29 def setUp(self):
30 self.services = service_manager.Services(
31 issue=fake.IssueService(),
32 features=fake.FeaturesService(),
33 issue_star=fake.AbstractStarService(),
34 config=fake.ConfigService(),
35 project=fake.ProjectService(),
36 user=fake.UserService(),
37 cache_manager=fake.CacheManager())
38 self.services.project.TestAddProject('ProjectName', project_id=1)
39
40 self.services.user.TestAddUser('annajowang@email.com', 111)
41 self.services.user.TestAddUser('claremont@email.com', 222)
42 issue1 = fake.MakeTestIssue(
43 1, 1, 'issue_summary', 'New', 111, project_name='ProjectName')
44 self.services.issue.TestAddIssue(issue1)
45 issue2 = fake.MakeTestIssue(
46 1, 2, 'issue_summary2', 'New', 111, project_name='ProjectName')
47 self.services.issue.TestAddIssue(issue2)
48 issue3 = fake.MakeTestIssue(
49 1, 3, 'issue_summary3', 'New', 222, project_name='ProjectName')
50 self.services.issue.TestAddIssue(issue3)
51 issues = [issue1, issue2, issue3]
52 hotlist_items = [
53 (issue.issue_id, rank, 222, None, '') for
54 rank, issue in enumerate(issues)]
55
56 self.hotlist_items_list = [
57 features_pb2.MakeHotlistItem(
58 issue_id, rank=rank, adder_id=adder_id,
59 date_added=date, note=note) for (
60 issue_id, rank, adder_id, date, note) in hotlist_items]
61 self.test_hotlist = self.services.features.TestAddHotlist(
62 'hotlist', hotlist_id=123, owner_ids=[111],
63 hotlist_item_fields=hotlist_items)
64 sorting.InitializeArtValues(self.services)
65 self.mr = None
66
67 def setUpCreateHotlistTableDataTestMR(self, **kwargs):
68 self.mr = testing_helpers.MakeMonorailRequest(**kwargs)
69 self.services.user.TestAddUser('annajo@email.com', 148)
70 self.mr.auth.effective_ids = {148}
71 self.mr.col_spec = 'ID Summary Modified'
72
73 def testCreateHotlistTableData(self):
74 self.setUpCreateHotlistTableDataTestMR(hotlist=self.test_hotlist)
75 table_data, table_related_dict = hotlist_helpers.CreateHotlistTableData(
76 self.mr, self.hotlist_items_list, self.services)
77 self.assertEqual(len(table_data), 3)
78 start_index = 100001
79 for row in table_data:
80 self.assertEqual(row.project_name, 'ProjectName')
81 self.assertEqual(row.issue_id, start_index)
82 start_index += 1
83 self.assertEqual(len(table_related_dict['column_values']), 3)
84
85 # test none of the shown columns show up in unshown_columns
86 self.assertTrue(
87 set(self.mr.col_spec.split()).isdisjoint(
88 table_related_dict['unshown_columns']))
89 self.assertEqual(table_related_dict['is_cross_project'], False)
90 self.assertEqual(len(table_related_dict['pagination'].visible_results), 3)
91
92 def testCreateHotlistTableData_Pagination(self):
93 self.setUpCreateHotlistTableDataTestMR(
94 hotlist=self.test_hotlist, path='/123?num=2')
95 table_data, _ = hotlist_helpers.CreateHotlistTableData(
96 self.mr, self.hotlist_items_list, self.services)
97 self.assertEqual(len(table_data), 2)
98
99 def testCreateHotlistTableData_EndPagination(self):
100 self.setUpCreateHotlistTableDataTestMR(
101 hotlist=self.test_hotlist, path='/123?num=2&start=2')
102 table_data, _ = hotlist_helpers.CreateHotlistTableData(
103 self.mr, self.hotlist_items_list, self.services)
104 self.assertEqual(len(table_data), 1)
105
106
107class MakeTableDataTest(unittest.TestCase):
108
109 def test_MakeTableData(self):
110 issues = [fake.MakeTestIssue(
111 789, 1, 'issue_summary', 'New', 111, project_name='ProjectName',
112 issue_id=1001)]
113 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
114 cell_factories = {
115 'summary': table_view_helpers.TableCellSummary}
116 table_data = hotlist_helpers._MakeTableData(
117 issues, [], ['summary'], [], {} , cell_factories,
118 {}, set(), config, None, 29, 'stars')
119 self.assertEqual(len(table_data), 1)
120 row = table_data[0]
121 self.assertEqual(row.issue_id, 1001)
122 self.assertEqual(row.local_id, 1)
123 self.assertEqual(row.project_name, 'ProjectName')
124 self.assertEqual(row.issue_ref, 'ProjectName:1')
125 self.assertTrue('hotlist_id=29' in row.issue_ctx_url)
126 self.assertTrue('sort=stars' in row.issue_ctx_url)
127
128
129class GetAllProjectsOfIssuesTest(unittest.TestCase):
130
131 issue_x_1 = tracker_pb2.Issue()
132 issue_x_1.project_id = 789
133
134 issue_x_2 = tracker_pb2.Issue()
135 issue_x_2.project_id = 789
136
137 issue_y_1 = tracker_pb2.Issue()
138 issue_y_1.project_id = 678
139
140 def testGetAllProjectsOfIssues_Normal(self):
141 issues = [self.issue_x_1, self.issue_x_2]
142 self.assertEqual(
143 hotlist_helpers.GetAllProjectsOfIssues(issues),
144 set([789]))
145 issues = [self.issue_x_1, self.issue_x_2, self.issue_y_1]
146 self.assertEqual(
147 hotlist_helpers.GetAllProjectsOfIssues(issues),
148 set([678, 789]))
149
150 def testGetAllProjectsOfIssues_Empty(self):
151 self.assertEqual(
152 hotlist_helpers.GetAllProjectsOfIssues([]),
153 set())
154
155
156class HelpersUnitTest(unittest.TestCase):
157
158 # TODO(jojwang): Write Tests for GetAllConfigsOfProjects
159 def setUp(self):
160 self.services = service_manager.Services(issue=fake.IssueService(),
161 config=fake.ConfigService(),
162 project=fake.ProjectService(),
163 features=fake.FeaturesService(),
164 user=fake.UserService())
165 self.project = self.services.project.TestAddProject(
166 'ProjectName', project_id=1, owner_ids=[111])
167
168 self.services.user.TestAddUser('annajowang@email.com', 111)
169 self.services.user.TestAddUser('claremont@email.com', 222)
170 self.issue1 = fake.MakeTestIssue(
171 1, 1, 'issue_summary', 'New', 111,
172 project_name='ProjectName', labels='restrict-view-Googler')
173 self.services.issue.TestAddIssue(self.issue1)
174 self.issue3 = fake.MakeTestIssue(
175 1, 3, 'issue_summary3', 'New', 222, project_name='ProjectName')
176 self.services.issue.TestAddIssue(self.issue3)
177 self.issue4 = fake.MakeTestIssue(
178 1, 4, 'issue_summary4', 'Fixed', 222, closed_timestamp=232423,
179 project_name='ProjectName')
180 self.services.issue.TestAddIssue(self.issue4)
181 self.issues = [self.issue1, self.issue3, self.issue4]
182 self.mr = testing_helpers.MakeMonorailRequest()
183
184 def testFilterIssues(self):
185 test_allowed_issues = hotlist_helpers.FilterIssues(
186 self.mr.cnxn, self.mr.auth, 2, self.issues, self.services)
187 self.assertEqual(len(test_allowed_issues), 1)
188 self.assertEqual(test_allowed_issues[0].local_id, 3)
189
190 def testFilterIssues_ShowClosed(self):
191 test_allowed_issues = hotlist_helpers.FilterIssues(
192 self.mr.cnxn, self.mr.auth, 1, self.issues, self.services)
193 self.assertEqual(len(test_allowed_issues), 2)
194 self.assertEqual(test_allowed_issues[0].local_id, 3)
195 self.assertEqual(test_allowed_issues[1].local_id, 4)
196
197 def testMembersWithoutGivenIDs(self):
198 h = features_pb2.Hotlist()
199 owners, editors, followers = hotlist_helpers.MembersWithoutGivenIDs(
200 h, set())
201 # Check lists are empty
202 self.assertFalse(owners)
203 self.assertFalse(editors)
204 self.assertFalse(followers)
205
206 h.owner_ids.extend([1, 2, 3])
207 h.editor_ids.extend([4, 5, 6])
208 h.follower_ids.extend([7, 8, 9])
209 owners, editors, followers = hotlist_helpers.MembersWithoutGivenIDs(
210 h, {10, 11, 12})
211 self.assertEqual(h.owner_ids, owners)
212 self.assertEqual(h.editor_ids, editors)
213 self.assertEqual(h.follower_ids, followers)
214
215 owners, editors, followers = hotlist_helpers.MembersWithoutGivenIDs(
216 h, set())
217 self.assertEqual(h.owner_ids, owners)
218 self.assertEqual(h.editor_ids, editors)
219 self.assertEqual(h.follower_ids, followers)
220
221 owners, editors, followers = hotlist_helpers.MembersWithoutGivenIDs(
222 h, {1, 4, 7})
223 self.assertEqual([2, 3], owners)
224 self.assertEqual([5, 6], editors)
225 self.assertEqual([8, 9], followers)
226
227 def testMembersWithGivenIDs(self):
228 h = features_pb2.Hotlist()
229
230 # empty GivenIDs give empty member lists from originally empty member lists
231 owners, editors, followers = hotlist_helpers.MembersWithGivenIDs(
232 h, set(), 'follower')
233 self.assertFalse(owners)
234 self.assertFalse(editors)
235 self.assertFalse(followers)
236
237 # empty GivenIDs return original non-empty member lists
238 h.owner_ids.extend([1, 2, 3])
239 h.editor_ids.extend([4, 5, 6])
240 h.follower_ids.extend([7, 8, 9])
241 owners, editors, followers = hotlist_helpers.MembersWithGivenIDs(
242 h, set(), 'editor')
243 self.assertEqual(owners, h.owner_ids)
244 self.assertEqual(editors, h.editor_ids)
245 self.assertEqual(followers, h.follower_ids)
246
247 # non-member GivenIDs return updated member lists
248 owners, editors, followers = hotlist_helpers.MembersWithGivenIDs(
249 h, {10, 11, 12}, 'owner')
250 self.assertEqual(owners, [1, 2, 3, 10, 11, 12])
251 self.assertEqual(editors, [4, 5, 6])
252 self.assertEqual(followers, [7, 8, 9])
253
254 # member GivenIDs return updated member lists
255 owners, editors, followers = hotlist_helpers.MembersWithGivenIDs(
256 h, {1, 4, 7}, 'editor')
257 self.assertEqual(owners, [2, 3])
258 self.assertEqual(editors, [5, 6, 1, 4, 7])
259 self.assertEqual(followers, [8, 9])
260
261 def testGetURLOfHotlist(self):
262 cnxn = 'fake cnxn'
263 user = self.services.user.TestAddUser('claremont@email.com', 432)
264 user.obscure_email = False
265 hotlist1 = self.services.features.TestAddHotlist(
266 'hotlist1', hotlist_id=123, owner_ids=[432])
267 url = hotlist_helpers.GetURLOfHotlist(
268 cnxn, hotlist1, self.services.user)
269 self.assertEqual('/u/claremont@email.com/hotlists/hotlist1', url)
270
271 url = hotlist_helpers.GetURLOfHotlist(
272 cnxn, hotlist1, self.services.user, url_for_token=True)
273 self.assertEqual('/u/432/hotlists/hotlist1', url)
274
275 user.obscure_email = True
276 url = hotlist_helpers.GetURLOfHotlist(
277 cnxn, hotlist1, self.services.user)
278 self.assertEqual('/u/432/hotlists/hotlist1', url)
279
280 # Test that a Hotlist without an owner has an empty URL.
281 hotlist_unowned = self.services.features.TestAddHotlist('hotlist2',
282 hotlist_id=234, owner_ids=[])
283 url = hotlist_helpers.GetURLOfHotlist(cnxn, hotlist_unowned,
284 self.services.user)
285 self.assertFalse(url)