blob: e50efd24de9e33f94ce78b18399dad2b146ebb43 [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 issuelist module."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import time
11import unittest
12
13from framework import framework_constants
14from framework import table_view_helpers
15from framework import template_helpers
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010016from mrproto import tracker_pb2
Copybara854996b2021-09-07 19:36:02 +000017from testing import fake
18from testing import testing_helpers
19from tracker import tablecell
20from tracker import tracker_bizobj
21
22
23class DisplayNameMock(object):
24
25 def __init__(self, name):
26 self.display_name = name
27 self.user = None
28
29
30def MakeTestIssue(local_id, issue_id, summary, status=None):
31 issue = tracker_pb2.Issue()
32 issue.local_id = local_id
33 issue.issue_id = issue_id
34 issue.summary = summary
35 if status:
36 issue.status = status
37 return issue
38
39
40class TableCellUnitTest(unittest.TestCase):
41
42 USERS_BY_ID = {
43 23456: DisplayNameMock('Jason'),
44 34567: DisplayNameMock('Nathan'),
45 }
46
47 def setUp(self):
48 self.issue1 = MakeTestIssue(
49 local_id=1, issue_id=100001, summary='One', status="New")
50 self.issue2 = MakeTestIssue(
51 local_id=2, issue_id=100002, summary='Two', status="Fixed")
52 self.issue3 = MakeTestIssue(
53 local_id=3, issue_id=100003, summary='Three', status="UndefinedString")
54 self.issue5 = MakeTestIssue(
55 local_id=5, issue_id=100005, summary='FiveUnviewable', status="Fixed")
56 self.table_cell_kws = {
57 'col': None,
58 'users_by_id': self.USERS_BY_ID,
59 'non_col_labels': [],
60 'label_values': {},
61 'related_issues': {},
62 'config': tracker_bizobj.MakeDefaultProjectIssueConfig(678),
63 'viewable_iids_set': {100001, 100002, 100003}
64 }
65
66 def testTableCellNote(self):
67 table_cell_kws = self.table_cell_kws.copy()
68 table_cell_kws.update({'note': ''})
69 cell = tablecell.TableCellNote(
70 self.issue1, **table_cell_kws)
71 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_NOTE)
72 self.assertEqual(cell.values, [])
73
74 def testTableCellNote_NoNote(self):
75 table_cell_kws = self.table_cell_kws.copy()
76 table_cell_kws.update({'note': 'some note'})
77 cell = tablecell.TableCellNote(
78 self.issue1, **table_cell_kws)
79 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_NOTE)
80 self.assertEqual(cell.values[0].item, 'some note')
81
82 def testTableCellDateAdded(self):
83 table_cell_kws = self.table_cell_kws.copy()
84 table_cell_kws.update({'date_added': 1234})
85 cell = tablecell.TableCellDateAdded(
86 self.issue1, **table_cell_kws)
87 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ATTR)
88 self.assertEqual(cell.values[0].item, 1234)
89
90 def testTableCellAdderID(self):
91 table_cell_kws = self.table_cell_kws.copy()
92 table_cell_kws.update({'adder_id': 23456})
93 cell = tablecell.TableCellAdderID(
94 self.issue1, **table_cell_kws)
95 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ATTR)
96 self.assertEqual(cell.values[0].item, 'Jason')
97
98 def testTableCellRank(self):
99 table_cell_kws = self.table_cell_kws.copy()
100 table_cell_kws.update({'issue_rank': 3})
101 cell = tablecell.TableCellRank(
102 self.issue1, **table_cell_kws)
103 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ATTR)
104 self.assertEqual(cell.values[0].item, 3)
105
106 def testTableCellID(self):
107 cell = tablecell.TableCellID(
108 MakeTestIssue(4, 4, 'Four'), **self.table_cell_kws)
109 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ID)
110 # Note that the ID itself is accessed from the row, not the cell.
111
112 def testTableCellOwner(self):
113 test_issue = MakeTestIssue(4, 4, 'Four')
114 test_issue.owner_id=23456
115
116 cell = tablecell.TableCellOwner(
117 test_issue, **self.table_cell_kws)
118 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ATTR)
119 self.assertEqual(cell.values[0].item, 'Jason')
120
121 def testTableCellOwnerNoOwner(self):
122 test_issue = MakeTestIssue(4, 4, 'Four')
123 test_issue.owner_id=framework_constants.NO_USER_SPECIFIED
124
125 cell = tablecell.TableCellOwner(
126 test_issue, **self.table_cell_kws)
127 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ATTR)
128 self.assertEqual(cell.values, [])
129
130 def testTableCellReporter(self):
131 test_issue = MakeTestIssue(4, 4, 'Four')
132 test_issue.reporter_id=34567
133
134 cell = tablecell.TableCellReporter(
135 test_issue, **self.table_cell_kws)
136 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ATTR)
137 self.assertEqual(cell.values[0].item, 'Nathan')
138
139 def testTableCellCc(self):
140 test_issue = MakeTestIssue(4, 4, 'Four')
141 test_issue.cc_ids = [23456, 34567]
142
143 cell = tablecell.TableCellCc(
144 test_issue, **self.table_cell_kws)
145 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ATTR)
146 self.assertEqual(cell.values[0].item, 'Jason')
147 self.assertEqual(cell.values[1].item, 'Nathan')
148
149 def testTableCellCcNoCcs(self):
150 cell = tablecell.TableCellCc(
151 MakeTestIssue(4, 4, 'Four'), **self.table_cell_kws)
152 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ATTR)
153 self.assertEqual(cell.values, [])
154
155 def testTableCellAttachmentsNone(self):
156 cell = tablecell.TableCellAttachments(
157 MakeTestIssue(4, 4, 'Four'), **self.table_cell_kws)
158 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ATTR)
159 self.assertEqual(cell.values[0].item, 0)
160
161 def testTableCellAttachments(self):
162 test_issue = MakeTestIssue(4, 4, 'Four')
163 test_issue.attachment_count = 2
164
165 cell = tablecell.TableCellAttachments(
166 test_issue, **self.table_cell_kws)
167 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ATTR)
168 self.assertEqual(cell.values[0].item, 2)
169
170 def testTableCellOpened(self):
171 test_issue = MakeTestIssue(4, 4, 'Four')
172 test_issue.opened_timestamp = 1200000000
173
174 cell = tablecell.TableCellOpened(
175 test_issue, **self.table_cell_kws)
176 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
177 self.assertEqual(cell.values[0].item, 'Jan 2008')
178
179 def testTableCellClosed(self):
180 test_issue = MakeTestIssue(4, 4, 'Four')
181 test_issue.closed_timestamp = None
182
183 cell = tablecell.TableCellClosed(
184 test_issue, **self.table_cell_kws)
185 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
186 self.assertEqual(cell.values, [])
187
188 test_issue.closed_timestamp = 1200000000
189 cell = tablecell.TableCellClosed(
190 test_issue, **self.table_cell_kws)
191 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
192 self.assertEqual(cell.values[0].item, 'Jan 2008')
193
194 def testTableCellModified(self):
195 test_issue = MakeTestIssue(4, 4, 'Four')
196 test_issue.modified_timestamp = None
197
198 cell = tablecell.TableCellModified(
199 test_issue, **self.table_cell_kws)
200 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
201 self.assertEqual(cell.values, [])
202
203 test_issue.modified_timestamp = 1200000000
204 cell = tablecell.TableCellModified(
205 test_issue, **self.table_cell_kws)
206 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
207 self.assertEqual(cell.values[0].item, 'Jan 2008')
208
209 def testTableCellOwnerLastVisit(self):
210 test_issue = MakeTestIssue(4, 4, 'Four')
211 test_issue.owner_id = None
212
213 cell = tablecell.TableCellOwnerLastVisit(
214 test_issue, **self.table_cell_kws)
215 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
216 self.assertEqual(cell.values, [])
217
218 test_issue.owner_id = 23456
219 self.USERS_BY_ID[23456].user = testing_helpers.Blank(last_visit_timestamp=0)
220 cell = tablecell.TableCellOwnerLastVisit(
221 test_issue, **self.table_cell_kws)
222 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
223 self.assertEqual(cell.values, [])
224
225 self.USERS_BY_ID[23456].user.last_visit_timestamp = int(time.time())
226 cell = tablecell.TableCellOwnerLastVisit(
227 test_issue, **self.table_cell_kws)
228 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
229 self.assertEqual(cell.values[0].item, 'Today')
230
231 self.USERS_BY_ID[23456].user.last_visit_timestamp = (
232 int(time.time()) - 25 * framework_constants.SECS_PER_HOUR)
233 cell = tablecell.TableCellOwnerLastVisit(
234 test_issue, **self.table_cell_kws)
235 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
236 self.assertEqual(cell.values[0].item, 'Yesterday')
237
238 def testTableCellBlockedOn(self):
239 test_issue = MakeTestIssue(4, 4, 'Four')
240 test_issue.blocked_on_iids = [
241 self.issue1.issue_id, self.issue2.issue_id, self.issue3.issue_id,
242 self.issue5.issue_id]
243 table_cell_kws = self.table_cell_kws.copy()
244 table_cell_kws['related_issues'] = {
245 self.issue1.issue_id: self.issue1, self.issue2.issue_id: self.issue2,
246 self.issue3.issue_id: self.issue3, self.issue5.issue_id: self.issue5}
247
248 cell = tablecell.TableCellBlockedOn(
249 test_issue, **table_cell_kws)
250 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ISSUES)
251 self.assertEqual(
252 [x.item for x in cell.values], [
253 template_helpers.EZTItem(
254 href='/p/None/issues/detail?id=1',
255 id='1',
256 closed=None,
257 title='One'),
258 template_helpers.EZTItem(
259 href='/p/None/issues/detail?id=3',
260 id='3',
261 closed=None,
262 title='Three'),
263 template_helpers.EZTItem(
264 href='/p/None/issues/detail?id=5',
265 id='5',
266 closed=None,
267 title=''),
268 template_helpers.EZTItem(
269 href='/p/None/issues/detail?id=2',
270 id='2',
271 closed='yes',
272 title='Two')
273 ])
274
275 def testTableCellBlockedOnNone(self):
276 cell = tablecell.TableCellBlockedOn(
277 MakeTestIssue(4, 4, 'Four'), **self.table_cell_kws)
278 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ISSUES)
279 self.assertEqual(cell.values, [])
280
281 def testTableCellBlocking(self):
282 test_issue = MakeTestIssue(4, 4, 'Four')
283 test_issue.blocking_iids = [
284 self.issue1.issue_id, self.issue2.issue_id, self.issue3.issue_id,
285 self.issue5.issue_id]
286 table_cell_kws = self.table_cell_kws.copy()
287 table_cell_kws['related_issues'] = {
288 self.issue1.issue_id: self.issue1, self.issue2.issue_id: self.issue2,
289 self.issue3.issue_id: self.issue3, self.issue5.issue_id: self.issue5}
290
291 cell = tablecell.TableCellBlocking(
292 test_issue, **table_cell_kws)
293 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ISSUES)
294 self.assertEqual(
295 [x.item for x in cell.values], [
296 template_helpers.EZTItem(
297 href='/p/None/issues/detail?id=1',
298 id='1',
299 closed=None,
300 title='One'),
301 template_helpers.EZTItem(
302 href='/p/None/issues/detail?id=3',
303 id='3',
304 closed=None,
305 title='Three'),
306 template_helpers.EZTItem(
307 href='/p/None/issues/detail?id=5',
308 id='5',
309 closed=None,
310 title=''),
311 template_helpers.EZTItem(
312 href='/p/None/issues/detail?id=2',
313 id='2',
314 closed='yes',
315 title='Two')
316 ])
317
318 def testTableCellBlockingNone(self):
319 cell = tablecell.TableCellBlocking(
320 MakeTestIssue(4, 4, 'Four'),
321 **self.table_cell_kws)
322 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ISSUES)
323 self.assertEqual(cell.values, [])
324
325 def testTableCellBlocked(self):
326 test_issue = MakeTestIssue(4, 4, 'Four')
327 test_issue.blocked_on_iids = [1, 2, 3]
328
329 cell = tablecell.TableCellBlocked(
330 test_issue, **self.table_cell_kws)
331 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ATTR)
332 self.assertEqual(cell.values[0].item, 'Yes')
333
334 def testTableCellBlockedNotBlocked(self):
335 cell = tablecell.TableCellBlocked(
336 MakeTestIssue(4, 4, 'Four'), **self.table_cell_kws)
337 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ATTR)
338 self.assertEqual(cell.values[0].item, 'No')
339
340 def testTableCellMergedInto(self):
341 test_issue = MakeTestIssue(4, 4, 'Four')
342 test_issue.merged_into = self.issue2.issue_id
343 table_cell_kws = self.table_cell_kws.copy()
344 table_cell_kws['related_issues'] = {self.issue2.issue_id: self.issue2}
345
346 cell = tablecell.TableCellMergedInto(
347 test_issue, **table_cell_kws)
348 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ISSUES)
349 self.assertEqual(
350 cell.values[0].item,
351 template_helpers.EZTItem(
352 href='/p/None/issues/detail?id=2',
353 id='2',
354 closed='yes',
355 title='Two'))
356
357 def testTableCellMergedIntoUnviewable(self):
358 test_issue = MakeTestIssue(4, 4, 'Four')
359 test_issue.merged_into = self.issue5.issue_id
360 table_cell_kws = self.table_cell_kws.copy()
361 table_cell_kws['related_issues'] = {self.issue5.issue_id: self.issue5}
362
363 cell = tablecell.TableCellMergedInto(
364 test_issue, **table_cell_kws)
365 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ISSUES)
366 self.assertEqual(
367 cell.values[0].item,
368 template_helpers.EZTItem(
369 href='/p/None/issues/detail?id=5', id='5', closed=None, title=''))
370
371 def testTableCellMergedIntoNotMerged(self):
372 cell = tablecell.TableCellMergedInto(
373 MakeTestIssue(4, 4, 'Four'), **self.table_cell_kws)
374 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ISSUES)
375 self.assertEqual(cell.values, [])
376
377 def testTableCellAllLabels(self):
378 labels = ['A', 'B', 'C', 'D-E', 'F-G']
379 derived_labels = ['W', 'X', 'Y-Z']
380
381 test_issue = MakeTestIssue(4, 4, 'Four')
382 test_issue.labels = labels
383 test_issue.derived_labels = derived_labels
384
385 cell = tablecell.TableCellAllLabels(test_issue)
386 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_ATTR)
387 self.assertEqual([v.item for v in cell.values], labels + derived_labels)
388
389
390class TableCellCSVTest(unittest.TestCase):
391
392 USERS_BY_ID = {
393 23456: DisplayNameMock('Jason'),
394 }
395
396 def testTableCellOpenedTimestamp(self):
397 test_issue = MakeTestIssue(4, 4, 'Four')
398 test_issue.opened_timestamp = 1200000000
399
400 cell = tablecell.TableCellOpenedTimestamp(test_issue)
401 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
402 self.assertEqual(cell.values[0].item, 1200000000)
403
404 def testTableCellClosedTimestamp(self):
405 test_issue = MakeTestIssue(4, 4, 'Four')
406 test_issue.closed_timestamp = None
407
408 cell = tablecell.TableCellClosedTimestamp(test_issue)
409 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
410 self.assertEqual(cell.values[0].item, 0)
411
412 test_issue.closed_timestamp = 1200000000
413 cell = tablecell.TableCellClosedTimestamp(test_issue)
414 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
415 self.assertEqual(cell.values[0].item, 1200000000)
416
417 def testTableCellModifiedTimestamp(self):
418 test_issue = MakeTestIssue(4, 4, 'Four')
419 test_issue.modified_timestamp = 0
420
421 cell = tablecell.TableCellModifiedTimestamp(test_issue)
422 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
423 self.assertEqual(cell.values[0].item, 0)
424
425 test_issue.modified_timestamp = 1200000000
426 cell = tablecell.TableCellModifiedTimestamp(test_issue)
427 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
428 self.assertEqual(cell.values[0].item, 1200000000)
429
430 def testTableCellOwnerModifiedTimestamp(self):
431 test_issue = MakeTestIssue(4, 4, 'Four')
432 test_issue.owner_modified_timestamp = 0
433
434 cell = tablecell.TableCellOwnerModifiedTimestamp(test_issue)
435 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
436 self.assertEqual(cell.values[0].item, 0)
437
438 test_issue.owner_modified_timestamp = 1200000000
439 cell = tablecell.TableCellOwnerModifiedTimestamp(test_issue)
440 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
441 self.assertEqual(cell.values[0].item, 1200000000)
442
443 def testTableCellStatusModifiedTimestamp(self):
444 test_issue = MakeTestIssue(4, 4, 'Four')
445 test_issue.status_modified_timestamp = 0
446
447 cell = tablecell.TableCellStatusModifiedTimestamp(test_issue)
448 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
449 self.assertEqual(cell.values[0].item, 0)
450
451 test_issue.status_modified_timestamp = 1200000000
452 cell = tablecell.TableCellStatusModifiedTimestamp(test_issue)
453 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
454 self.assertEqual(cell.values[0].item, 1200000000)
455
456 def testTableCellComponentModifiedTimestamp(self):
457 test_issue = MakeTestIssue(4, 4, 'Four')
458 test_issue.component_modified_timestamp = 0
459
460 cell = tablecell.TableCellComponentModifiedTimestamp(test_issue)
461 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
462 self.assertEqual(cell.values[0].item, 0)
463
464 test_issue.component_modified_timestamp = 1200000000
465 cell = tablecell.TableCellComponentModifiedTimestamp(test_issue)
466 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
467 self.assertEqual(cell.values[0].item, 1200000000)
468
469 def testTableCellOwnerLastVisitDaysAgo(self):
470 test_issue = MakeTestIssue(4, 4, 'Four')
471 test_issue.owner_id = None
472
473 cell = tablecell.TableCellOwnerLastVisitDaysAgo(
474 test_issue, users_by_id=self.USERS_BY_ID)
475 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
476 self.assertEqual(None, cell.values[0].item)
477
478 test_issue.owner_id = 23456
479 self.USERS_BY_ID[23456].user = testing_helpers.Blank(last_visit_timestamp=0)
480 cell = tablecell.TableCellOwnerLastVisitDaysAgo(
481 test_issue, users_by_id=self.USERS_BY_ID)
482 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
483 self.assertEqual(None, cell.values[0].item)
484
485 self.USERS_BY_ID[23456].user.last_visit_timestamp = (
486 int(time.time()) - 25 * 60 * 60)
487 cell = tablecell.TableCellOwnerLastVisitDaysAgo(
488 test_issue, users_by_id=self.USERS_BY_ID)
489 self.assertEqual(cell.type, table_view_helpers.CELL_TYPE_UNFILTERABLE)
490 self.assertEqual(1, cell.values[0].item)