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 """Tests for issue tracker bizobj functions.""" |
| 7 |
| 8 import unittest |
| 9 |
| 10 from framework import framework_constants |
| 11 from framework import framework_views |
| 12 from proto import tracker_pb2 |
| 13 from tracker import tracker_bizobj |
| 14 |
| 15 |
| 16 class BizobjTest(unittest.TestCase): |
| 17 |
| 18 def testGetOwnerId(self): |
| 19 issue = tracker_pb2.Issue() |
| 20 self.assertEquals( |
| 21 tracker_bizobj.GetOwnerId(issue), framework_constants.NO_USER_SPECIFIED) |
| 22 |
| 23 issue.derived_owner_id = 123 |
| 24 self.assertEquals(tracker_bizobj.GetOwnerId(issue), 123) |
| 25 |
| 26 issue.owner_id = 456 |
| 27 self.assertEquals(tracker_bizobj.GetOwnerId(issue), 456) |
| 28 |
| 29 def testGetStatus(self): |
| 30 issue = tracker_pb2.Issue() |
| 31 self.assertEquals(tracker_bizobj.GetStatus(issue), '') |
| 32 |
| 33 issue.derived_status = 'InReview' |
| 34 self.assertEquals(tracker_bizobj.GetStatus(issue), 'InReview') |
| 35 |
| 36 issue.status = 'Forgotten' |
| 37 self.assertEquals(tracker_bizobj.GetStatus(issue), 'Forgotten') |
| 38 |
| 39 def testGetCcIds(self): |
| 40 issue = tracker_pb2.Issue() |
| 41 self.assertEquals(tracker_bizobj.GetCcIds(issue), []) |
| 42 |
| 43 issue.derived_cc_ids.extend([1, 2, 3]) |
| 44 self.assertEquals(tracker_bizobj.GetCcIds(issue), [1, 2, 3]) |
| 45 |
| 46 issue.cc_ids.extend([4, 5, 6]) |
| 47 self.assertEquals(tracker_bizobj.GetCcIds(issue), [4, 5, 6, 1, 2, 3]) |
| 48 |
| 49 def testGetLabels(self): |
| 50 issue = tracker_pb2.Issue() |
| 51 self.assertEquals(tracker_bizobj.GetLabels(issue), []) |
| 52 |
| 53 issue.derived_labels.extend(['a', 'b', 'c']) |
| 54 self.assertEquals(tracker_bizobj.GetLabels(issue), ['a', 'b', 'c']) |
| 55 |
| 56 issue.labels.extend(['d', 'e', 'f']) |
| 57 self.assertEquals(tracker_bizobj.GetLabels(issue), |
| 58 ['d', 'e', 'f', 'a', 'b', 'c']) |
| 59 |
| 60 def CheckDefaultConfig(self, config): |
| 61 self.assertTrue(len(config.well_known_statuses) > 0) |
| 62 self.assertTrue(config.statuses_offer_merge > 0) |
| 63 self.assertTrue(len(config.well_known_labels) > 0) |
| 64 self.assertTrue(len(config.templates) > 0) |
| 65 self.assertTrue(len(config.exclusive_label_prefixes) > 0) |
| 66 # TODO(jrobbins): test actual values from default config |
| 67 |
| 68 def testMakeDefaultProjectIssueConfig(self): |
| 69 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789) |
| 70 config.default_template_for_developers = 1 |
| 71 config.default_template_for_users = 2 |
| 72 self.CheckDefaultConfig(config) |
| 73 |
| 74 def testConvertDictToTemplate(self): |
| 75 template = tracker_bizobj.ConvertDictToTemplate( |
| 76 dict(name='name', content='content', summary='summary', |
| 77 status='status', owner_id=111L)) |
| 78 self.assertEqual('name', template.name) |
| 79 self.assertEqual('content', template.content) |
| 80 self.assertEqual('summary', template.summary) |
| 81 self.assertEqual('status', template.status) |
| 82 self.assertEqual(111L, template.owner_id) |
| 83 self.assertFalse(template.summary_must_be_edited) |
| 84 self.assertTrue(template.owner_defaults_to_member) |
| 85 self.assertFalse(template.component_required) |
| 86 |
| 87 template = tracker_bizobj.ConvertDictToTemplate( |
| 88 dict(name='name', content='content', labels=['a', 'b', 'c'])) |
| 89 self.assertListEqual( |
| 90 ['a', 'b', 'c'], list(template.labels)) |
| 91 |
| 92 template = tracker_bizobj.ConvertDictToTemplate( |
| 93 dict(name='name', content='content', summary_must_be_edited=True, |
| 94 owner_defaults_to_member=True, component_required=True)) |
| 95 self.assertTrue(template.summary_must_be_edited) |
| 96 self.assertTrue(template.owner_defaults_to_member) |
| 97 self.assertTrue(template.component_required) |
| 98 |
| 99 template = tracker_bizobj.ConvertDictToTemplate( |
| 100 dict(name='name', content='content', summary_must_be_edited=False, |
| 101 owner_defaults_to_member=False, component_required=False)) |
| 102 self.assertFalse(template.summary_must_be_edited) |
| 103 self.assertFalse(template.owner_defaults_to_member) |
| 104 self.assertFalse(template.component_required) |
| 105 |
| 106 def testHarmonizeConfigs_Empty(self): |
| 107 harmonized = tracker_bizobj.HarmonizeConfigs([]) |
| 108 self.CheckDefaultConfig(harmonized) |
| 109 |
| 110 def testHarmonizeConfigs(self): |
| 111 c1 = tracker_bizobj.MakeDefaultProjectIssueConfig(789) |
| 112 harmonized = tracker_bizobj.HarmonizeConfigs([c1]) |
| 113 self.assertListEqual( |
| 114 [stat.status for stat in c1.well_known_statuses], |
| 115 [stat.status for stat in harmonized.well_known_statuses]) |
| 116 self.assertListEqual( |
| 117 [lab.label for lab in c1.well_known_labels], |
| 118 [lab.label for lab in harmonized.well_known_labels]) |
| 119 self.assertEqual('', harmonized.default_sort_spec) |
| 120 |
| 121 c2 = tracker_bizobj.MakeDefaultProjectIssueConfig(678) |
| 122 tracker_bizobj.SetConfigStatuses(c2, [ |
| 123 ('Unconfirmed', '', True, False), |
| 124 ('New', '', True, True), |
| 125 ('Accepted', '', True, False), |
| 126 ('Begun', '', True, False), |
| 127 ('Fixed', '', False, False), |
| 128 ('Obsolete', '', False, False)]) |
| 129 tracker_bizobj.SetConfigLabels(c2, [ |
| 130 ('Pri-0', '', False), |
| 131 ('Priority-High', '', True), |
| 132 ('Pri-1', '', False), |
| 133 ('Priority-Medium', '', True), |
| 134 ('Pri-2', '', False), |
| 135 ('Priority-Low', '', True), |
| 136 ('Pri-3', '', False), |
| 137 ('Pri-4', '', False)]) |
| 138 c2.default_sort_spec = 'Pri -status' |
| 139 |
| 140 harmonized = tracker_bizobj.HarmonizeConfigs([c1, c2]) |
| 141 result_statuses = [stat.status |
| 142 for stat in harmonized.well_known_statuses] |
| 143 result_labels = [lab.label |
| 144 for lab in harmonized.well_known_labels] |
| 145 self.assertListEqual( |
| 146 ['Unconfirmed', 'New', 'Accepted', 'Begun', 'Started', 'Fixed', |
| 147 'Obsolete', 'Verified', 'Invalid', 'Duplicate', 'WontFix', 'Done'], |
| 148 result_statuses) |
| 149 self.assertListEqual( |
| 150 ['Pri-0', 'Type-Defect', 'Type-Enhancement', 'Type-Task', |
| 151 'Type-Other', 'Priority-Critical', 'Priority-High', |
| 152 'Pri-1', 'Priority-Medium', 'Pri-2', 'Priority-Low', 'Pri-3', |
| 153 'Pri-4'], |
| 154 result_labels[:result_labels.index('OpSys-All')]) |
| 155 self.assertEqual('Pri -status', harmonized.default_sort_spec.strip()) |
| 156 |
| 157 def testCombineOrderedLists_Empty(self): |
| 158 self.assertEqual([], tracker_bizobj._CombineOrderedLists([])) |
| 159 |
| 160 def testCombineOrderedLists_Normal(self): |
| 161 a = ['Mon', 'Wed', 'Fri'] |
| 162 b = ['Mon', 'Tue'] |
| 163 c = ['Wed', 'Thu'] |
| 164 self.assertEqual(['Mon', 'Tue', 'Wed', 'Thu', 'Fri'], |
| 165 tracker_bizobj._CombineOrderedLists([a, b, c])) |
| 166 |
| 167 d = ['Mon', 'StartOfWeek', 'Wed', 'MidWeek', 'Fri', 'EndOfWeek'] |
| 168 self.assertEqual(['Mon', 'StartOfWeek', 'Tue', 'Wed', 'MidWeek', 'Thu', |
| 169 'Fri', 'EndOfWeek'], |
| 170 tracker_bizobj._CombineOrderedLists([a, b, c, d])) |
| 171 |
| 172 def testUsersInvolvedInComment(self): |
| 173 comment = tracker_pb2.IssueComment() |
| 174 self.assertEqual({0}, tracker_bizobj.UsersInvolvedInComment(comment)) |
| 175 |
| 176 comment.user_id = 111L |
| 177 self.assertEqual( |
| 178 {111L}, tracker_bizobj.UsersInvolvedInComment(comment)) |
| 179 |
| 180 amendment = tracker_pb2.Amendment(newvalue='foo') |
| 181 comment.amendments.append(amendment) |
| 182 self.assertEqual( |
| 183 {111L}, tracker_bizobj.UsersInvolvedInComment(comment)) |
| 184 |
| 185 amendment.added_user_ids.append(222L) |
| 186 amendment.removed_user_ids.append(333L) |
| 187 self.assertEqual({111L, 222L, 333L}, |
| 188 tracker_bizobj.UsersInvolvedInComment(comment)) |
| 189 |
| 190 def testUsersInvolvedInCommentList(self): |
| 191 self.assertEqual(set(), tracker_bizobj.UsersInvolvedInCommentList([])) |
| 192 |
| 193 c1 = tracker_pb2.IssueComment() |
| 194 c1.user_id = 111L |
| 195 c1.amendments.append(tracker_pb2.Amendment(newvalue='foo')) |
| 196 |
| 197 c2 = tracker_pb2.IssueComment() |
| 198 c2.user_id = 111L |
| 199 c2.amendments.append(tracker_pb2.Amendment( |
| 200 added_user_ids=[222L], removed_user_ids=[333L])) |
| 201 |
| 202 self.assertEqual({111L}, |
| 203 tracker_bizobj.UsersInvolvedInCommentList([c1])) |
| 204 |
| 205 self.assertEqual({111L, 222L, 333L}, |
| 206 tracker_bizobj.UsersInvolvedInCommentList([c2])) |
| 207 |
| 208 self.assertEqual({111L, 222L, 333L}, |
| 209 tracker_bizobj.UsersInvolvedInCommentList([c1, c2])) |
| 210 |
| 211 def testMakeAmendment(self): |
| 212 amendment = tracker_bizobj.MakeAmendment( |
| 213 tracker_pb2.FieldID.STATUS, 'new', [111L], [222L]) |
| 214 self.assertEqual(tracker_pb2.FieldID.STATUS, amendment.field) |
| 215 self.assertEqual('new', amendment.newvalue) |
| 216 self.assertEqual([111L], amendment.added_user_ids) |
| 217 self.assertEqual([222L], amendment.removed_user_ids) |
| 218 |
| 219 def testPlusMinusString(self): |
| 220 self.assertEqual('', tracker_bizobj._PlusMinusString([], [])) |
| 221 self.assertEqual('-a -b c d', |
| 222 tracker_bizobj._PlusMinusString(['c', 'd'], ['a', 'b'])) |
| 223 |
| 224 def testPlusMinusAmendment(self): |
| 225 amendment = tracker_bizobj._PlusMinusAmendment( |
| 226 tracker_pb2.FieldID.STATUS, ['add1', 'add2'], ['remove1']) |
| 227 self.assertEqual(tracker_pb2.FieldID.STATUS, amendment.field) |
| 228 self.assertEqual('-remove1 add1 add2', amendment.newvalue) |
| 229 |
| 230 def testPlusMinusRefsAmendment(self): |
| 231 ref1 = (None, 1) |
| 232 ref2 = ('other-proj', 2) |
| 233 amendment = tracker_bizobj._PlusMinusRefsAmendment( |
| 234 tracker_pb2.FieldID.STATUS, [ref1], [ref2]) |
| 235 self.assertEqual(tracker_pb2.FieldID.STATUS, amendment.field) |
| 236 self.assertEqual('-other-proj:2 1', amendment.newvalue) |
| 237 |
| 238 def testMakeSummaryAmendment(self): |
| 239 amendment = tracker_bizobj.MakeSummaryAmendment('', None) |
| 240 self.assertEqual(tracker_pb2.FieldID.SUMMARY, amendment.field) |
| 241 self.assertEqual('', amendment.newvalue) |
| 242 self.assertEqual(None, amendment.oldvalue) |
| 243 |
| 244 amendment = tracker_bizobj.MakeSummaryAmendment('new summary', '') |
| 245 self.assertEqual(tracker_pb2.FieldID.SUMMARY, amendment.field) |
| 246 self.assertEqual('new summary', amendment.newvalue) |
| 247 self.assertEqual('', amendment.oldvalue) |
| 248 |
| 249 def testMakeStatusAmendment(self): |
| 250 amendment = tracker_bizobj.MakeStatusAmendment('', None) |
| 251 self.assertEqual(tracker_pb2.FieldID.STATUS, amendment.field) |
| 252 self.assertEqual('', amendment.newvalue) |
| 253 self.assertEqual(None, amendment.oldvalue) |
| 254 |
| 255 amendment = tracker_bizobj.MakeStatusAmendment('New', '') |
| 256 self.assertEqual(tracker_pb2.FieldID.STATUS, amendment.field) |
| 257 self.assertEqual('New', amendment.newvalue) |
| 258 self.assertEqual('', amendment.oldvalue) |
| 259 |
| 260 def testMakeOwnerAmendment(self): |
| 261 amendment = tracker_bizobj.MakeOwnerAmendment(111L, 0) |
| 262 self.assertEqual(tracker_pb2.FieldID.OWNER, amendment.field) |
| 263 self.assertEqual('', amendment.newvalue) |
| 264 self.assertEqual([111L], amendment.added_user_ids) |
| 265 self.assertEqual([0], amendment.removed_user_ids) |
| 266 |
| 267 def testMakeCcAmendment(self): |
| 268 amendment = tracker_bizobj.MakeCcAmendment([111L], [222L]) |
| 269 self.assertEqual(tracker_pb2.FieldID.CC, amendment.field) |
| 270 self.assertEqual('', amendment.newvalue) |
| 271 self.assertEqual([111L], amendment.added_user_ids) |
| 272 self.assertEqual([222L], amendment.removed_user_ids) |
| 273 |
| 274 def testMakeLabelsAmendment(self): |
| 275 amendment = tracker_bizobj.MakeLabelsAmendment(['added1'], ['removed1']) |
| 276 self.assertEqual(tracker_pb2.FieldID.LABELS, amendment.field) |
| 277 self.assertEqual('-removed1 added1', amendment.newvalue) |
| 278 |
| 279 def testMakeBlockedOnAmendment(self): |
| 280 ref1 = (None, 1) |
| 281 ref2 = ('other-proj', 2) |
| 282 amendment = tracker_bizobj.MakeBlockedOnAmendment([ref1], [ref2]) |
| 283 self.assertEqual(tracker_pb2.FieldID.BLOCKEDON, amendment.field) |
| 284 self.assertEqual('-other-proj:2 1', amendment.newvalue) |
| 285 |
| 286 def testMakeBlockingAmendment(self): |
| 287 ref1 = (None, 1) |
| 288 ref2 = ('other-proj', 2) |
| 289 amendment = tracker_bizobj.MakeBlockingAmendment([ref1], [ref2]) |
| 290 self.assertEqual(tracker_pb2.FieldID.BLOCKING, amendment.field) |
| 291 self.assertEqual('-other-proj:2 1', amendment.newvalue) |
| 292 |
| 293 def testMakeMergedIntoAmendment(self): |
| 294 ref1 = (None, 1) |
| 295 ref2 = ('other-proj', 2) |
| 296 amendment = tracker_bizobj.MakeMergedIntoAmendment(ref1, ref2) |
| 297 self.assertEqual(tracker_pb2.FieldID.MERGEDINTO, amendment.field) |
| 298 self.assertEqual('-other-proj:2 1', amendment.newvalue) |
| 299 |
| 300 def testAmendmentString(self): |
| 301 users_by_id = { |
| 302 111L: framework_views.UserView(111L, 'username@gmail.com', True) |
| 303 } |
| 304 summary_amendment = tracker_bizobj.MakeSummaryAmendment('new summary', None) |
| 305 self.assertEqual( |
| 306 'new summary', |
| 307 tracker_bizobj.AmendmentString(summary_amendment, users_by_id)) |
| 308 |
| 309 status_amendment = tracker_bizobj.MakeStatusAmendment('', None) |
| 310 self.assertEqual( |
| 311 '', tracker_bizobj.AmendmentString(status_amendment, users_by_id)) |
| 312 status_amendment = tracker_bizobj.MakeStatusAmendment('Assigned', 'New') |
| 313 self.assertEqual( |
| 314 'Assigned', |
| 315 tracker_bizobj.AmendmentString(status_amendment, users_by_id)) |
| 316 |
| 317 owner_amendment = tracker_bizobj.MakeOwnerAmendment(0, 0) |
| 318 self.assertEqual( |
| 319 '----', tracker_bizobj.AmendmentString(owner_amendment, users_by_id)) |
| 320 owner_amendment = tracker_bizobj.MakeOwnerAmendment(111L, 0) |
| 321 self.assertEqual( |
| 322 'usern...@gmail.com', |
| 323 tracker_bizobj.AmendmentString(owner_amendment, users_by_id)) |
| 324 |
| 325 def testAmendmentLinks(self): |
| 326 users_by_id = { |
| 327 111L: framework_views.UserView(111L, 'foo@gmail.com', False), |
| 328 222L: framework_views.UserView(222L, 'bar@gmail.com', False), |
| 329 333L: framework_views.UserView(333L, 'baz@gmail.com', False) |
| 330 } |
| 331 # SUMMARY |
| 332 summary_amendment = tracker_bizobj.MakeSummaryAmendment('new summary', None) |
| 333 self.assertEqual( |
| 334 [{'value': 'new summary', 'url': None}], |
| 335 tracker_bizobj.AmendmentLinks(summary_amendment, users_by_id, 'proj')) |
| 336 |
| 337 # OWNER |
| 338 owner_amendment = tracker_bizobj.MakeOwnerAmendment(0, 0) |
| 339 self.assertEqual( |
| 340 [{'value': '----', 'url': None}], |
| 341 tracker_bizobj.AmendmentLinks(owner_amendment, users_by_id, 'proj')) |
| 342 owner_amendment = tracker_bizobj.MakeOwnerAmendment(111L, 0) |
| 343 self.assertEqual( |
| 344 [{'value': 'foo@gmail.com', 'url': None}], |
| 345 tracker_bizobj.AmendmentLinks(owner_amendment, users_by_id, 'proj')) |
| 346 |
| 347 # BLOCKEDON, BLOCKING, MERGEDINTO |
| 348 blocking_amendment = tracker_bizobj.MakeBlockingAmendment( |
| 349 [(None, 123), ('blah', 234)], [(None, 345), ('blah', 456)]) |
| 350 self.assertEqual([ |
| 351 {'value': '-345', 'url': '/p/proj/issues/detail?id=345'}, |
| 352 {'value': '-blah:456', 'url': '/p/blah/issues/detail?id=456'}, |
| 353 {'value': '123', 'url': '/p/proj/issues/detail?id=123'}, |
| 354 {'value': 'blah:234', 'url': '/p/blah/issues/detail?id=234'}], |
| 355 tracker_bizobj.AmendmentLinks(blocking_amendment, users_by_id, 'proj')) |
| 356 |
| 357 # newvalue catchall |
| 358 label_amendment = tracker_bizobj.MakeLabelsAmendment( |
| 359 ['My-Label', 'Your-Label'], ['Their-Label']) |
| 360 self.assertEqual([ |
| 361 {'value': '-Their-Label', 'url': None}, |
| 362 {'value': 'My-Label', 'url': None}, |
| 363 {'value': 'Your-Label', 'url': None}], |
| 364 tracker_bizobj.AmendmentLinks(label_amendment, users_by_id, 'proj')) |
| 365 |
| 366 # CC, or CUSTOM with user type |
| 367 cc_amendment = tracker_bizobj.MakeCcAmendment([222L, 333L], [111L]) |
| 368 self.assertEqual([ |
| 369 {'value': '-foo@gmail.com', 'url': None}, |
| 370 {'value': 'bar@gmail.com', 'url': None}, |
| 371 {'value': 'baz@gmail.com', 'url': None}], |
| 372 tracker_bizobj.AmendmentLinks(cc_amendment, users_by_id, 'proj')) |
| 373 user_amendment = tracker_bizobj.MakeAmendment( |
| 374 tracker_pb2.FieldID.CUSTOM, None, [222L, 333L], [111L], 'ultracc') |
| 375 self.assertEqual([ |
| 376 {'value': '-foo@gmail.com', 'url': None}, |
| 377 {'value': 'bar@gmail.com', 'url': None}, |
| 378 {'value': 'baz@gmail.com', 'url': None}], |
| 379 tracker_bizobj.AmendmentLinks(user_amendment, users_by_id, 'proj')) |
| 380 |
| 381 |
| 382 def testDiffValueLists(self): |
| 383 added, removed = tracker_bizobj.DiffValueLists([], []) |
| 384 self.assertItemsEqual([], added) |
| 385 self.assertItemsEqual([], removed) |
| 386 |
| 387 added, removed = tracker_bizobj.DiffValueLists([], None) |
| 388 self.assertItemsEqual([], added) |
| 389 self.assertItemsEqual([], removed) |
| 390 |
| 391 added, removed = tracker_bizobj.DiffValueLists([1, 2], []) |
| 392 self.assertItemsEqual([1, 2], added) |
| 393 self.assertItemsEqual([], removed) |
| 394 |
| 395 added, removed = tracker_bizobj.DiffValueLists([], [8, 9]) |
| 396 self.assertItemsEqual([], added) |
| 397 self.assertItemsEqual([8, 9], removed) |
| 398 |
| 399 added, removed = tracker_bizobj.DiffValueLists([1, 2], [8, 9]) |
| 400 self.assertItemsEqual([1, 2], added) |
| 401 self.assertItemsEqual([8, 9], removed) |
| 402 |
| 403 added, removed = tracker_bizobj.DiffValueLists([1, 2, 5, 6], [5, 6, 8, 9]) |
| 404 self.assertItemsEqual([1, 2], added) |
| 405 self.assertItemsEqual([8, 9], removed) |
| 406 |
| 407 added, removed = tracker_bizobj.DiffValueLists([5, 6], [5, 6, 8, 9]) |
| 408 self.assertItemsEqual([], added) |
| 409 self.assertItemsEqual([8, 9], removed) |
| 410 |
| 411 added, removed = tracker_bizobj.DiffValueLists([1, 2, 5, 6], [5, 6]) |
| 412 self.assertItemsEqual([1, 2], added) |
| 413 self.assertItemsEqual([], removed) |
| 414 |
| 415 added, removed = tracker_bizobj.DiffValueLists( |
| 416 [1, 2, 2, 5, 6], [5, 6, 8, 9]) |
| 417 self.assertItemsEqual([1, 2, 2], added) |
| 418 self.assertItemsEqual([8, 9], removed) |
| 419 |
| 420 added, removed = tracker_bizobj.DiffValueLists( |
| 421 [1, 2, 5, 6], [5, 6, 8, 8, 9]) |
| 422 self.assertItemsEqual([1, 2], added) |
| 423 self.assertItemsEqual([8, 8, 9], removed) |
| 424 |
| 425 def testFormatIssueRef(self): |
| 426 self.assertEqual('', tracker_bizobj.FormatIssueRef(None)) |
| 427 |
| 428 self.assertEqual( |
| 429 'p:1', tracker_bizobj.FormatIssueRef(('p', 1))) |
| 430 |
| 431 self.assertEqual( |
| 432 '1', tracker_bizobj.FormatIssueRef((None, 1))) |
| 433 |
| 434 def testParseIssueRef(self): |
| 435 self.assertEqual(None, tracker_bizobj.ParseIssueRef('')) |
| 436 self.assertEqual(None, tracker_bizobj.ParseIssueRef(' \t ')) |
| 437 |
| 438 ref_pn, ref_id = tracker_bizobj.ParseIssueRef('1') |
| 439 self.assertEqual(None, ref_pn) |
| 440 self.assertEqual(1, ref_id) |
| 441 |
| 442 ref_pn, ref_id = tracker_bizobj.ParseIssueRef('-1') |
| 443 self.assertEqual(None, ref_pn) |
| 444 self.assertEqual(1, ref_id) |
| 445 |
| 446 ref_pn, ref_id = tracker_bizobj.ParseIssueRef('p:2') |
| 447 self.assertEqual('p', ref_pn) |
| 448 self.assertEqual(2, ref_id) |
| 449 |
| 450 ref_pn, ref_id = tracker_bizobj.ParseIssueRef('-p:2') |
| 451 self.assertEqual('p', ref_pn) |
| 452 self.assertEqual(2, ref_id) |
| 453 |
| 454 def testSafeParseIssueRef(self): |
| 455 self.assertEqual(None, tracker_bizobj._SafeParseIssueRef('-')) |
| 456 self.assertEqual(None, tracker_bizobj._SafeParseIssueRef('test:')) |
| 457 ref_pn, ref_id = tracker_bizobj.ParseIssueRef('p:2') |
| 458 self.assertEqual('p', ref_pn) |
| 459 self.assertEqual(2, ref_id) |
| 460 |
| 461 def testGetFieldValueWithRawValue(self): |
| 462 class MockUser(object): |
| 463 def __init__(self): |
| 464 self.email = 'test@example.com' |
| 465 users_by_id = {111: MockUser()} |
| 466 |
| 467 class MockFieldValue(object): |
| 468 def __init__(self, int_value=None, str_value=None, user_id=None): |
| 469 self.int_value = int_value |
| 470 self.str_value = str_value |
| 471 self.user_id = user_id |
| 472 |
| 473 # Test user types. |
| 474 # Use user_id from the field_value and get user from users_by_id. |
| 475 val = tracker_bizobj.GetFieldValueWithRawValue( |
| 476 field_type=tracker_pb2.FieldTypes.USER_TYPE, |
| 477 users_by_id=users_by_id, |
| 478 field_value=MockFieldValue(user_id=111), |
| 479 raw_value=113, |
| 480 ) |
| 481 self.assertEqual('test@example.com', val) |
| 482 # Specify user_id that does not exist in users_by_id. |
| 483 val = tracker_bizobj.GetFieldValueWithRawValue( |
| 484 field_type=tracker_pb2.FieldTypes.USER_TYPE, |
| 485 users_by_id=users_by_id, |
| 486 field_value=MockFieldValue(user_id=112), |
| 487 raw_value=113, |
| 488 ) |
| 489 self.assertEqual(112, val) |
| 490 # Pass in empty users_by_id. |
| 491 val = tracker_bizobj.GetFieldValueWithRawValue( |
| 492 field_type=tracker_pb2.FieldTypes.USER_TYPE, |
| 493 users_by_id={}, |
| 494 field_value=MockFieldValue(user_id=111), |
| 495 raw_value=113, |
| 496 ) |
| 497 self.assertEqual(111, val) |
| 498 # Test different raw_values. |
| 499 raw_value_tests = ( |
| 500 (111, 'test@example.com'), |
| 501 (112, 112), |
| 502 (framework_constants.NO_USER_NAME, framework_constants.NO_USER_NAME)) |
| 503 for (raw_value, expected_output) in raw_value_tests: |
| 504 val = tracker_bizobj.GetFieldValueWithRawValue( |
| 505 field_type=tracker_pb2.FieldTypes.USER_TYPE, |
| 506 users_by_id=users_by_id, |
| 507 field_value=None, |
| 508 raw_value=raw_value, |
| 509 ) |
| 510 self.assertEqual(expected_output, val) |
| 511 |
| 512 # Test enum types. |
| 513 # The returned value should be the raw_value regardless of field_value being |
| 514 # specified. |
| 515 for field_value in (MockFieldValue(), None): |
| 516 val = tracker_bizobj.GetFieldValueWithRawValue( |
| 517 field_type=tracker_pb2.FieldTypes.ENUM_TYPE, |
| 518 users_by_id=users_by_id, |
| 519 field_value=field_value, |
| 520 raw_value='abc', |
| 521 ) |
| 522 self.assertEqual('abc', val) |
| 523 |
| 524 # Test int type. |
| 525 # Use int_type from the field_value. |
| 526 val = tracker_bizobj.GetFieldValueWithRawValue( |
| 527 field_type=tracker_pb2.FieldTypes.INT_TYPE, |
| 528 users_by_id=users_by_id, |
| 529 field_value=MockFieldValue(int_value=100), |
| 530 raw_value=101, |
| 531 ) |
| 532 self.assertEqual(100, val) |
| 533 # Use the raw_value when field_value is not specified. |
| 534 val = tracker_bizobj.GetFieldValueWithRawValue( |
| 535 field_type=tracker_pb2.FieldTypes.INT_TYPE, |
| 536 users_by_id=users_by_id, |
| 537 field_value=None, |
| 538 raw_value=101, |
| 539 ) |
| 540 self.assertEqual(101, val) |
| 541 |
| 542 # Test str type. |
| 543 # Use str_type from the field_value. |
| 544 val = tracker_bizobj.GetFieldValueWithRawValue( |
| 545 field_type=tracker_pb2.FieldTypes.STR_TYPE, |
| 546 users_by_id=users_by_id, |
| 547 field_value=MockFieldValue(str_value='testing'), |
| 548 raw_value='test', |
| 549 ) |
| 550 self.assertEqual('testing', val) |
| 551 # Use the raw_value when field_value is not specified. |
| 552 val = tracker_bizobj.GetFieldValueWithRawValue( |
| 553 field_type=tracker_pb2.FieldTypes.STR_TYPE, |
| 554 users_by_id=users_by_id, |
| 555 field_value=None, |
| 556 raw_value='test', |
| 557 ) |
| 558 self.assertEqual('test', val) |
| 559 |
| 560 |
| 561 if __name__ == '__main__': |
| 562 unittest.main() |
OLD | NEW |