blob: d54353aca4495a06c2bf1fd2844b44a02b709b55 [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 grid_view_helpers classes and functions."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import unittest
11
12from framework import framework_constants
13from framework import framework_views
14from framework import grid_view_helpers
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010015from mrproto import tracker_pb2
Copybara854996b2021-09-07 19:36:02 +000016from testing import fake
17from tracker import tracker_bizobj
18
19
20class GridViewHelpersTest(unittest.TestCase):
21
22 def setUp(self):
23 self.default_cols = 'a b c'
24 self.builtin_cols = 'a b x y z'
25 self.config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
26
27 self.art1 = fake.MakeTestIssue(
28 789, 1, 'a summary', '', 0, derived_owner_id=111, star_count=12,
29 derived_labels='Priority-Medium Hot Mstone-1 Mstone-2',
30 derived_status='Overdue')
31 self.art2 = fake.MakeTestIssue(
32 789, 1, 'a summary', 'New', 111, star_count=12, merged_into=200001,
33 labels='Priority-Medium Type-DEFECT Hot Mstone-1 Mstone-2')
34 self.users_by_id = {
35 111: framework_views.StuffUserView(111, 'foo@example.com', True),
36 }
37
38 def testSortGridHeadings(self):
39 config = fake.MakeTestConfig(
40 789, labels=('Priority-High Priority-Medium Priority-Low Hot Cold '
41 'Milestone-Near Milestone-Far '
42 'Day-Sun Day-Mon Day-Tue Day-Wed Day-Thu Day-Fri Day-Sat'),
43 statuses=('New Accepted Started Fixed WontFix Invalid Duplicate'))
44 config.field_defs = [
45 tracker_pb2.FieldDef(field_id=1, project_id=789, field_name='Day',
46 field_type=tracker_pb2.FieldTypes.ENUM_TYPE)]
47 asc_accessors = {
48 'id': 'some function that is not called',
49 'reporter': 'some function that is not called',
50 'opened': 'some function that is not called',
51 'modified': 'some function that is not called',
52 }
53
54 # Verify that status headings are sorted according to the status
55 # values defined in the config.
56 col_name = 'status'
57 headings = ['Duplicate', 'Limbo', 'New', 'OnHold', 'Accepted', 'Fixed']
58 sorted_headings = grid_view_helpers.SortGridHeadings(
59 col_name, headings, self.users_by_id, config, asc_accessors)
60 self.assertEqual(
61 sorted_headings,
62 ['New', 'Accepted', 'Fixed', 'Duplicate', 'Limbo', 'OnHold'])
63
64 # Verify that special columns are sorted alphabetically or numerically.
65 col_name = 'id'
66 headings = [1, 2, 5, 3, 4]
67 sorted_headings = grid_view_helpers.SortGridHeadings(
68 col_name, headings, self.users_by_id, config, asc_accessors)
69 self.assertEqual(sorted_headings,
70 [1, 2, 3, 4, 5])
71
72 # Verify that label value headings are sorted according to the labels
73 # values defined in the config.
74 col_name = 'priority'
75 headings = ['Medium', 'High', 'Low', 'dont-care']
76 sorted_headings = grid_view_helpers.SortGridHeadings(
77 col_name, headings, self.users_by_id, config, asc_accessors)
78 self.assertEqual(sorted_headings,
79 ['High', 'Medium', 'Low', 'dont-care'])
80
81 # Verify that enum headings are sorted according to the labels
82 # values defined in the config.
83 col_name = 'day'
84 headings = ['Tue', 'Fri', 'Sun', 'Dogday', 'Wed', 'Caturday', 'Low']
85 sorted_headings = grid_view_helpers.SortGridHeadings(
86 col_name, headings, self.users_by_id, config, asc_accessors)
87 self.assertEqual(sorted_headings,
88 ['Sun', 'Tue', 'Wed', 'Fri',
89 'Caturday', 'Dogday', 'Low'])
90
91 def testGetArtifactAttr_Explicit(self):
92 label_values = grid_view_helpers.MakeLabelValuesDict(self.art2)
93
94 id_vals = grid_view_helpers.GetArtifactAttr(
95 self.art2, 'id', self.users_by_id, label_values, self.config, {})
96 self.assertEqual([1], id_vals)
97 summary_vals = grid_view_helpers.GetArtifactAttr(
98 self.art2, 'summary', self.users_by_id, label_values, self.config, {})
99 self.assertEqual(['a summary'], summary_vals)
100 status_vals = grid_view_helpers.GetArtifactAttr(
101 self.art2, 'status', self.users_by_id, label_values, self.config, {})
102 self.assertEqual(['New'], status_vals)
103 stars_vals = grid_view_helpers.GetArtifactAttr(
104 self.art2, 'stars', self.users_by_id, label_values, self.config, {})
105 self.assertEqual([12], stars_vals)
106 owner_vals = grid_view_helpers.GetArtifactAttr(
107 self.art2, 'owner', self.users_by_id, label_values, self.config, {})
108 self.assertEqual(['f...@example.com'], owner_vals)
109 priority_vals = grid_view_helpers.GetArtifactAttr(
110 self.art2, 'priority', self.users_by_id, label_values, self.config, {})
111 self.assertEqual(['Medium'], priority_vals)
112 mstone_vals = grid_view_helpers.GetArtifactAttr(
113 self.art2, 'mstone', self.users_by_id, label_values, self.config, {})
114 self.assertEqual(['1', '2'], mstone_vals)
115 foo_vals = grid_view_helpers.GetArtifactAttr(
116 self.art2, 'foo', self.users_by_id, label_values, self.config, {})
117 self.assertEqual([framework_constants.NO_VALUES], foo_vals)
118 art3 = fake.MakeTestIssue(
119 987, 5, 'unecessary summary', 'New', 111, star_count=12,
120 issue_id=200001, project_name='other-project')
121 related_issues = {200001: art3}
122 merged_into_vals = grid_view_helpers.GetArtifactAttr(
123 self.art2, 'mergedinto', self.users_by_id, label_values,
124 self.config, related_issues)
125 self.assertEqual(['other-project:5'], merged_into_vals)
126
127 def testGetArtifactAttr_Derived(self):
128 label_values = grid_view_helpers.MakeLabelValuesDict(self.art1)
129 status_vals = grid_view_helpers.GetArtifactAttr(
130 self.art1, 'status', self.users_by_id, label_values, self.config, {})
131 self.assertEqual(['Overdue'], status_vals)
132 owner_vals = grid_view_helpers.GetArtifactAttr(
133 self.art1, 'owner', self.users_by_id, label_values, self.config, {})
134 self.assertEqual(['f...@example.com'], owner_vals)
135 priority_vals = grid_view_helpers.GetArtifactAttr(
136 self.art1, 'priority', self.users_by_id, label_values, self.config, {})
137 self.assertEqual(['Medium'], priority_vals)
138 mstone_vals = grid_view_helpers.GetArtifactAttr(
139 self.art1, 'mstone', self.users_by_id, label_values, self.config, {})
140 self.assertEqual(['1', '2'], mstone_vals)
141
142 def testMakeLabelValuesDict_Empty(self):
143 art = fake.MakeTestIssue(
144 789, 1, 'a summary', '', 0, derived_owner_id=111, star_count=12)
145 label_values = grid_view_helpers.MakeLabelValuesDict(art)
146 self.assertEqual({}, label_values)
147
148 def testMakeLabelValuesDict(self):
149 art = fake.MakeTestIssue(
150 789, 1, 'a summary', '', 0, derived_owner_id=111, star_count=12,
151 labels=['Priority-Medium', 'Hot', 'Mstone-1', 'Mstone-2'])
152 label_values = grid_view_helpers.MakeLabelValuesDict(art)
153 self.assertEqual(
154 {'priority': ['Medium'], 'mstone': ['1', '2']},
155 label_values)
156
157 art = fake.MakeTestIssue(
158 789, 1, 'a summary', '', 0, derived_owner_id=111, star_count=12,
159 labels='Priority-Medium Hot Mstone-1'.split(),
160 derived_labels=['Mstone-2'])
161 label_values = grid_view_helpers.MakeLabelValuesDict(art)
162 self.assertEqual(
163 {'priority': ['Medium'], 'mstone': ['1', '2']},
164 label_values)
165
166 def testMakeDrillDownSearch(self):
167 self.assertEqual('-has:milestone ',
168 grid_view_helpers.MakeDrillDownSearch('milestone', '----'))
169 self.assertEqual('milestone=22 ',
170 grid_view_helpers.MakeDrillDownSearch('milestone', '22'))
171 self.assertEqual(
172 'owner=a@example.com ',
173 grid_view_helpers.MakeDrillDownSearch('owner', 'a@example.com'))
174
175 def testAnyArtifactHasNoAttr_Empty(self):
176 artifacts = []
177 all_label_values = {}
178 self.assertFalse(grid_view_helpers.AnyArtifactHasNoAttr(
179 artifacts, 'milestone', self.users_by_id, all_label_values,
180 self.config, {}))
181
182 def testAnyArtifactHasNoAttr(self):
183 artifacts = [self.art1]
184 all_label_values = {
185 self.art1.local_id: grid_view_helpers.MakeLabelValuesDict(self.art1),
186 }
187 self.assertFalse(grid_view_helpers.AnyArtifactHasNoAttr(
188 artifacts, 'mstone', self.users_by_id, all_label_values,
189 self.config, {}))
190 self.assertTrue(grid_view_helpers.AnyArtifactHasNoAttr(
191 artifacts, 'milestone', self.users_by_id, all_label_values,
192 self.config, {}))
193
194 def testGetGridViewData(self):
195 # TODO(jojwang): write this test
196 pass
197
198 def testPrepareForMakeGridData(self):
199 # TODO(jojwang): write this test
200 pass