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