OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered 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 the field_helpers module.""" |
| 7 |
| 8 import time |
| 9 import unittest |
| 10 |
| 11 from framework import template_helpers |
| 12 from proto import tracker_pb2 |
| 13 from services import service_manager |
| 14 from testing import fake |
| 15 from testing import testing_helpers |
| 16 from tracker import field_helpers |
| 17 from tracker import tracker_bizobj |
| 18 |
| 19 |
| 20 class FieldHelpersTest(unittest.TestCase): |
| 21 |
| 22 def setUp(self): |
| 23 self.config = tracker_bizobj.MakeDefaultProjectIssueConfig(789) |
| 24 self.services = service_manager.Services( |
| 25 user=fake.UserService()) |
| 26 self.mr = testing_helpers.MakeMonorailRequest( |
| 27 project=fake.Project(), services=self.services) |
| 28 self.mr.cnxn = fake.MonorailConnection() |
| 29 self.errors = template_helpers.EZTError() |
| 30 |
| 31 def testParseFieldDefRequest_Empty(self): |
| 32 post_data = fake.PostData() |
| 33 parsed = field_helpers.ParseFieldDefRequest(post_data, self.config) |
| 34 self.assertEqual('', parsed.field_name) |
| 35 self.assertEqual(None, parsed.field_type_str) |
| 36 self.assertEqual(None, parsed.min_value) |
| 37 self.assertEqual(None, parsed.max_value) |
| 38 self.assertEqual(None, parsed.regex) |
| 39 self.assertFalse(parsed.needs_member) |
| 40 self.assertEqual('', parsed.needs_perm) |
| 41 self.assertEqual('', parsed.grants_perm) |
| 42 self.assertEqual(0, parsed.notify_on) |
| 43 self.assertFalse(parsed.is_required) |
| 44 self.assertFalse(parsed.is_multivalued) |
| 45 self.assertEqual('', parsed.field_docstring) |
| 46 self.assertEqual('', parsed.choices_text) |
| 47 self.assertEqual('', parsed.applicable_type) |
| 48 self.assertEqual('', parsed.applicable_predicate) |
| 49 unchanged_labels = [ |
| 50 (label_def.label, label_def.label_docstring, False) |
| 51 for label_def in self.config.well_known_labels] |
| 52 self.assertEqual(unchanged_labels, parsed.revised_labels) |
| 53 |
| 54 def testParseFieldDefRequest_Normal(self): |
| 55 post_data = fake.PostData( |
| 56 name=['somefield'], |
| 57 field_type=['INT_TYPE'], |
| 58 min_value=['11'], |
| 59 max_value=['99'], |
| 60 regex=['.*'], |
| 61 needs_member=['Yes'], |
| 62 needs_perm=['Commit'], |
| 63 grants_perm=['View'], |
| 64 notify_on=['any_comment'], |
| 65 is_required=['Yes'], |
| 66 is_multivalued=['Yes'], |
| 67 docstring=['It is just some field'], |
| 68 choices=['Hot = Lots of activity\nCold = Not much activity'], |
| 69 applicable_type=['Defect']) |
| 70 parsed = field_helpers.ParseFieldDefRequest(post_data, self.config) |
| 71 self.assertEqual('somefield', parsed.field_name) |
| 72 self.assertEqual('INT_TYPE', parsed.field_type_str) |
| 73 self.assertEqual(11, parsed.min_value) |
| 74 self.assertEqual(99, parsed.max_value) |
| 75 self.assertEqual('.*', parsed.regex) |
| 76 self.assertTrue(parsed.needs_member) |
| 77 self.assertEqual('Commit', parsed.needs_perm) |
| 78 self.assertEqual('View', parsed.grants_perm) |
| 79 self.assertEqual(1, parsed.notify_on) |
| 80 self.assertTrue(parsed.is_required) |
| 81 self.assertTrue(parsed.is_multivalued) |
| 82 self.assertEqual('It is just some field', parsed.field_docstring) |
| 83 self.assertEqual('Hot = Lots of activity\nCold = Not much activity', |
| 84 parsed.choices_text) |
| 85 self.assertEqual('Defect', parsed.applicable_type) |
| 86 self.assertEqual('', parsed.applicable_predicate) |
| 87 unchanged_labels = [ |
| 88 (label_def.label, label_def.label_docstring, False) |
| 89 for label_def in self.config.well_known_labels] |
| 90 new_labels = [ |
| 91 ('somefield-Hot', 'Lots of activity', False), |
| 92 ('somefield-Cold', 'Not much activity', False)] |
| 93 self.assertEqual(unchanged_labels + new_labels, parsed.revised_labels) |
| 94 |
| 95 def testParseChoicesIntoWellKnownLabels_NewFieldDef(self): |
| 96 choices_text = 'Hot = Lots of activity\nCold = Not much activity' |
| 97 field_name = 'somefield' |
| 98 revised_labels = field_helpers._ParseChoicesIntoWellKnownLabels( |
| 99 choices_text, field_name, self.config) |
| 100 unchanged_labels = [ |
| 101 (label_def.label, label_def.label_docstring, False) |
| 102 for label_def in self.config.well_known_labels] |
| 103 new_labels = [ |
| 104 ('somefield-Hot', 'Lots of activity', False), |
| 105 ('somefield-Cold', 'Not much activity', False)] |
| 106 self.assertEqual(unchanged_labels + new_labels, revised_labels) |
| 107 |
| 108 def testParseChoicesIntoWellKnownLabels_ConvertExistingLabel(self): |
| 109 choices_text = 'High = Must be fixed\nMedium = Might slip' |
| 110 field_name = 'Priority' |
| 111 revised_labels = field_helpers._ParseChoicesIntoWellKnownLabels( |
| 112 choices_text, field_name, self.config) |
| 113 kept_labels = [ |
| 114 (label_def.label, label_def.label_docstring, False) |
| 115 for label_def in self.config.well_known_labels |
| 116 if not label_def.label.startswith('Priority-')] |
| 117 new_labels = [ |
| 118 ('Priority-High', 'Must be fixed', False), |
| 119 ('Priority-Medium', 'Might slip', False)] |
| 120 self.maxDiff = None |
| 121 self.assertEqual(kept_labels + new_labels, revised_labels) |
| 122 |
| 123 def testShiftEnumFieldsIntoLabels_Empty(self): |
| 124 labels = [] |
| 125 labels_remove = [] |
| 126 field_val_strs = {} |
| 127 field_val_strs_remove = {} |
| 128 field_helpers.ShiftEnumFieldsIntoLabels( |
| 129 labels, labels_remove, field_val_strs, field_val_strs_remove, |
| 130 self.config) |
| 131 self.assertEqual([], labels) |
| 132 self.assertEqual([], labels_remove) |
| 133 self.assertEqual({}, field_val_strs) |
| 134 self.assertEqual({}, field_val_strs_remove) |
| 135 |
| 136 def testShiftEnumFieldsIntoLabels_NoOp(self): |
| 137 labels = ['Security', 'Performance', 'Pri-1', 'M-2'] |
| 138 labels_remove = ['ReleaseBlock'] |
| 139 field_val_strs = {123: ['CPU']} |
| 140 field_val_strs_remove = {234: ['Small']} |
| 141 field_helpers.ShiftEnumFieldsIntoLabels( |
| 142 labels, labels_remove, field_val_strs, field_val_strs_remove, |
| 143 self.config) |
| 144 self.assertEqual(['Security', 'Performance', 'Pri-1', 'M-2'], labels) |
| 145 self.assertEqual(['ReleaseBlock'], labels_remove) |
| 146 self.assertEqual({123: ['CPU']}, field_val_strs) |
| 147 self.assertEqual({234: ['Small']}, field_val_strs_remove) |
| 148 |
| 149 def testShiftEnumFieldsIntoLabels_FoundSomeEnumFields(self): |
| 150 self.config.field_defs.append( |
| 151 tracker_bizobj.MakeFieldDef( |
| 152 123, 789, 'Component', tracker_pb2.FieldTypes.ENUM_TYPE, None, |
| 153 '', False, False, None, None, '', False, '', '', |
| 154 tracker_pb2.NotifyTriggers.NEVER, 'What HW part is affected?', |
| 155 False)) |
| 156 self.config.field_defs.append( |
| 157 tracker_bizobj.MakeFieldDef( |
| 158 234, 789, 'Size', tracker_pb2.FieldTypes.ENUM_TYPE, None, |
| 159 '', False, False, None, None, '', False, '', '', |
| 160 tracker_pb2.NotifyTriggers.NEVER, 'How big is this work item?', |
| 161 False)) |
| 162 labels = ['Security', 'Performance', 'Pri-1', 'M-2'] |
| 163 labels_remove = ['ReleaseBlock'] |
| 164 field_val_strs = {123: ['CPU']} |
| 165 field_val_strs_remove = {234: ['Small']} |
| 166 field_helpers.ShiftEnumFieldsIntoLabels( |
| 167 labels, labels_remove, field_val_strs, field_val_strs_remove, |
| 168 self.config) |
| 169 self.assertEqual( |
| 170 ['Security', 'Performance', 'Pri-1', 'M-2', 'Component-CPU'], |
| 171 labels) |
| 172 self.assertEqual(['ReleaseBlock', 'Size-Small'], labels_remove) |
| 173 self.assertEqual({}, field_val_strs) |
| 174 self.assertEqual({}, field_val_strs_remove) |
| 175 |
| 176 def testParseOneFieldValue_IntType(self): |
| 177 fd = tracker_bizobj.MakeFieldDef( |
| 178 123, 789, 'Foo', tracker_pb2.FieldTypes.INT_TYPE, None, |
| 179 '', False, False, None, None, '', False, '', '', |
| 180 tracker_pb2.NotifyTriggers.NEVER, 'doc', False) |
| 181 fv = field_helpers._ParseOneFieldValue( |
| 182 self.mr.cnxn, self.services.user, fd, '8675309') |
| 183 self.assertEqual(fv.field_id, 123) |
| 184 self.assertEqual(fv.int_value, 8675309) |
| 185 |
| 186 def testParseOneFieldValue_StrType(self): |
| 187 fd = tracker_bizobj.MakeFieldDef( |
| 188 123, 789, 'Foo', tracker_pb2.FieldTypes.STR_TYPE, None, |
| 189 '', False, False, None, None, '', False, '', '', |
| 190 tracker_pb2.NotifyTriggers.NEVER, 'doc', False) |
| 191 fv = field_helpers._ParseOneFieldValue( |
| 192 self.mr.cnxn, self.services.user, fd, '8675309') |
| 193 self.assertEqual(fv.field_id, 123) |
| 194 self.assertEqual(fv.str_value, '8675309') |
| 195 |
| 196 def testParseOneFieldValue_UserType(self): |
| 197 self.services.user.TestAddUser('user@example.com', 111L) |
| 198 fd = tracker_bizobj.MakeFieldDef( |
| 199 123, 789, 'Foo', tracker_pb2.FieldTypes.USER_TYPE, None, |
| 200 '', False, False, None, None, '', False, '', '', |
| 201 tracker_pb2.NotifyTriggers.NEVER, 'doc', False) |
| 202 fv = field_helpers._ParseOneFieldValue( |
| 203 self.mr.cnxn, self.services.user, fd, 'user@example.com') |
| 204 self.assertEqual(fv.field_id, 123) |
| 205 self.assertEqual(fv.user_id, 111) |
| 206 |
| 207 def testParseFieldValues_Empty(self): |
| 208 field_val_strs = {} |
| 209 field_values = field_helpers.ParseFieldValues( |
| 210 self.mr.cnxn, self.services.user, field_val_strs, self.config) |
| 211 self.assertEqual([], field_values) |
| 212 |
| 213 def testParseFieldValues_Normal(self): |
| 214 fd = tracker_bizobj.MakeFieldDef( |
| 215 123, 789, 'CPU', tracker_pb2.FieldTypes.INT_TYPE, None, |
| 216 '', False, False, None, None, '', False, '', '', |
| 217 tracker_pb2.NotifyTriggers.NEVER, 'doc', False) |
| 218 self.config.field_defs.append(fd) |
| 219 field_val_strs = { |
| 220 123: ['80386', '68040']} |
| 221 field_values = field_helpers.ParseFieldValues( |
| 222 self.mr.cnxn, self.services.user, field_val_strs, self.config) |
| 223 fv1 = tracker_bizobj.MakeFieldValue(123, 80386, None, None, False) |
| 224 fv2 = tracker_bizobj.MakeFieldValue(123, 68040, None, None, False) |
| 225 self.assertEqual([fv1, fv2], field_values) |
| 226 |
| 227 def testValidateOneCustomField_IntType(self): |
| 228 fd = tracker_bizobj.MakeFieldDef( |
| 229 123, 789, 'CPU', tracker_pb2.FieldTypes.INT_TYPE, None, |
| 230 '', False, False, None, None, '', False, '', '', |
| 231 tracker_pb2.NotifyTriggers.NEVER, 'doc', False) |
| 232 fv = tracker_bizobj.MakeFieldValue(123, 8086, None, None, False) |
| 233 msg = field_helpers._ValidateOneCustomField( |
| 234 self.mr, self.services, fd, fv) |
| 235 self.assertIsNone(msg) |
| 236 |
| 237 fd.min_value = 1 |
| 238 fd.max_value = 999 |
| 239 msg = field_helpers._ValidateOneCustomField( |
| 240 self.mr, self.services, fd, fv) |
| 241 self.assertEqual('Value must be <= 999', msg) |
| 242 |
| 243 fv.int_value = 0 |
| 244 msg = field_helpers._ValidateOneCustomField( |
| 245 self.mr, self.services, fd, fv) |
| 246 self.assertEqual('Value must be >= 1', msg) |
| 247 |
| 248 def testValidateOneCustomField_StrType(self): |
| 249 fd = tracker_bizobj.MakeFieldDef( |
| 250 123, 789, 'CPU', tracker_pb2.FieldTypes.STR_TYPE, None, |
| 251 '', False, False, None, None, '', False, '', '', |
| 252 tracker_pb2.NotifyTriggers.NEVER, 'doc', False) |
| 253 fv = tracker_bizobj.MakeFieldValue(123, None, 'i386', None, False) |
| 254 msg = field_helpers._ValidateOneCustomField( |
| 255 self.mr, self.services, fd, fv) |
| 256 self.assertIsNone(msg) |
| 257 |
| 258 fd.regex = r'^\d*$' |
| 259 msg = field_helpers._ValidateOneCustomField( |
| 260 self.mr, self.services, fd, fv) |
| 261 self.assertEqual(r'Value must match regular expression: ^\d*$', msg) |
| 262 |
| 263 fv.str_value = '386' |
| 264 msg = field_helpers._ValidateOneCustomField( |
| 265 self.mr, self.services, fd, fv) |
| 266 self.assertIsNone(msg) |
| 267 |
| 268 def testValidateOneCustomField_UserType(self): |
| 269 pass # TODO(jrobbins): write this test. |
| 270 |
| 271 def testValidateOneCustomField_OtherType(self): |
| 272 # There are currently no validation options for date-type custom fields. |
| 273 fd = tracker_bizobj.MakeFieldDef( |
| 274 123, 789, 'Deadline', tracker_pb2.FieldTypes.DATE_TYPE, None, |
| 275 '', False, False, None, None, '', False, '', '', |
| 276 tracker_pb2.NotifyTriggers.NEVER, 'doc', False) |
| 277 fv = tracker_bizobj.MakeFieldValue( |
| 278 123, int(time.time()), None, None, False) |
| 279 msg = field_helpers._ValidateOneCustomField( |
| 280 self.mr, self.services, fd, fv) |
| 281 self.assertIsNone(msg) |
| 282 |
| 283 def testValidateCustomFields_NoCustomFieldValues(self): |
| 284 field_helpers.ValidateCustomFields( |
| 285 self.mr, self.services, [], self.config, self.errors) |
| 286 self.assertFalse(self.errors.AnyErrors()) |
| 287 |
| 288 def testValidateCustomFields_NoErrors(self): |
| 289 fd = tracker_bizobj.MakeFieldDef( |
| 290 123, 789, 'CPU', tracker_pb2.FieldTypes.INT_TYPE, None, |
| 291 '', False, False, None, None, '', False, '', '', |
| 292 tracker_pb2.NotifyTriggers.NEVER, 'doc', False) |
| 293 self.config.field_defs.append(fd) |
| 294 fv1 = tracker_bizobj.MakeFieldValue(123, 8086, None, None, False) |
| 295 fv2 = tracker_bizobj.MakeFieldValue(123, 486, None, None, False) |
| 296 |
| 297 field_helpers.ValidateCustomFields( |
| 298 self.mr, self.services, [fv1, fv2], self.config, self.errors) |
| 299 self.assertFalse(self.errors.AnyErrors()) |
| 300 |
| 301 def testValidateCustomFields_SomeErrors(self): |
| 302 fd = tracker_bizobj.MakeFieldDef( |
| 303 123, 789, 'CPU', tracker_pb2.FieldTypes.INT_TYPE, None, |
| 304 '', False, False, None, None, '', False, '', '', |
| 305 tracker_pb2.NotifyTriggers.NEVER, 'doc', False) |
| 306 self.config.field_defs.append(fd) |
| 307 fv1 = tracker_bizobj.MakeFieldValue(123, 8086, None, None, False) |
| 308 fv2 = tracker_bizobj.MakeFieldValue(123, 486, None, None, False) |
| 309 |
| 310 fd.min_value = 1 |
| 311 fd.max_value = 999 |
| 312 field_helpers.ValidateCustomFields( |
| 313 self.mr, self.services, [fv1, fv2], self.config, self.errors) |
| 314 self.assertTrue(self.errors.AnyErrors()) |
| 315 self.assertEqual(1, len(self.errors.custom_fields)) |
| 316 custom_field_error = self.errors.custom_fields[0] |
| 317 self.assertEqual(123, custom_field_error.field_id) |
| 318 self.assertEqual('Value must be <= 999', custom_field_error.message) |
| 319 |
| 320 |
| 321 if __name__ == '__main__': |
| 322 unittest.main() |
OLD | NEW |