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