Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2019 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 monorail.feature.federated.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | from __future__ import division |
| 10 | from __future__ import absolute_import |
| 11 | |
| 12 | import unittest |
| 13 | |
| 14 | from features import federated |
| 15 | from framework.exceptions import InvalidExternalIssueReference |
| 16 | |
| 17 | |
| 18 | # Schema: tracker, shortlink. |
| 19 | VALID_SHORTLINKS = [ |
| 20 | ('google', 'b/1'), |
| 21 | ('google', 'b/123456'), |
| 22 | ('google', 'b/1234567890123')] |
| 23 | |
| 24 | |
| 25 | # Schema: tracker, shortlink. |
| 26 | INVALID_SHORTLINKS = [ |
| 27 | ('google', 'b'), |
| 28 | ('google', 'b/'), |
| 29 | ('google', 'b//123'), |
| 30 | ('google', 'b/123/123')] |
| 31 | |
| 32 | |
| 33 | class FederatedTest(unittest.TestCase): |
| 34 | """Test public module methods.""" |
| 35 | |
| 36 | def testIsShortlinkValid_Valid(self): |
| 37 | for _, shortlink in VALID_SHORTLINKS: |
| 38 | self.assertTrue(federated.IsShortlinkValid(shortlink), |
| 39 | 'Expected %s to be a valid shortlink for any tracker.' |
| 40 | % shortlink) |
| 41 | |
| 42 | def testIsShortlinkValid_Invalid(self): |
| 43 | for _, shortlink in INVALID_SHORTLINKS: |
| 44 | self.assertFalse(federated.IsShortlinkValid(shortlink), |
| 45 | 'Expected %s to be an invalid shortlink for any tracker.' |
| 46 | % shortlink) |
| 47 | |
| 48 | def testFromShortlink_Valid(self): |
| 49 | for _, shortlink in VALID_SHORTLINKS: |
| 50 | issue = federated.FromShortlink(shortlink) |
| 51 | self.assertEqual(shortlink, issue.shortlink, ( |
| 52 | 'Expected %s to be converted into a valid tracker object ' |
| 53 | 'with shortlink %s' % (shortlink, issue.shortlink))) |
| 54 | |
| 55 | def testFromShortlink_Invalid(self): |
| 56 | for _, shortlink in INVALID_SHORTLINKS: |
| 57 | self.assertIsNone(federated.FromShortlink(shortlink)) |
| 58 | |
| 59 | |
| 60 | class FederatedIssueTest(unittest.TestCase): |
| 61 | |
| 62 | def testInit_NotImplemented(self): |
| 63 | """By default, __init__ raises NotImplementedError. |
| 64 | |
| 65 | Because __init__ calls IsShortlinkValid. See test below. |
| 66 | """ |
| 67 | with self.assertRaises(NotImplementedError): |
| 68 | federated.FederatedIssue('a') |
| 69 | |
| 70 | def testIsShortlinkValid_NotImplemented(self): |
| 71 | """By default, IsShortlinkValid raises NotImplementedError.""" |
| 72 | with self.assertRaises(NotImplementedError): |
| 73 | federated.FederatedIssue('a').IsShortlinkValid('rutabaga') |
| 74 | |
| 75 | |
| 76 | class GoogleIssueTrackerIssueTest(unittest.TestCase): |
| 77 | |
| 78 | def setUp(self): |
| 79 | self.valid_shortlinks = [s for tracker, s in VALID_SHORTLINKS |
| 80 | if tracker == 'google'] |
| 81 | self.invalid_shortlinks = [s for tracker, s in INVALID_SHORTLINKS |
| 82 | if tracker == 'google'] |
| 83 | |
| 84 | def testInit_ValidatesValidShortlink(self): |
| 85 | for shortlink in self.valid_shortlinks: |
| 86 | issue = federated.GoogleIssueTrackerIssue(shortlink) |
| 87 | self.assertEqual(issue.shortlink, shortlink) |
| 88 | |
| 89 | def testInit_ValidatesInvalidShortlink(self): |
| 90 | for shortlink in self.invalid_shortlinks: |
| 91 | with self.assertRaises(InvalidExternalIssueReference): |
| 92 | federated.GoogleIssueTrackerIssue(shortlink) |
| 93 | |
| 94 | def testIsShortlinkValid_Valid(self): |
| 95 | for shortlink in self.valid_shortlinks: |
| 96 | self.assertTrue( |
| 97 | federated.GoogleIssueTrackerIssue.IsShortlinkValid(shortlink), |
| 98 | 'Expected %s to be a valid shortlink for Google.' |
| 99 | % shortlink) |
| 100 | |
| 101 | def testIsShortlinkValid_Invalid(self): |
| 102 | for shortlink in self.invalid_shortlinks: |
| 103 | self.assertFalse( |
| 104 | federated.GoogleIssueTrackerIssue.IsShortlinkValid(shortlink), |
| 105 | 'Expected %s to be an invalid shortlink for Google.' |
| 106 | % shortlink) |
| 107 | |
| 108 | def testToURL(self): |
| 109 | self.assertEqual('https://issuetracker.google.com/issues/123456', |
| 110 | federated.GoogleIssueTrackerIssue('b/123456').ToURL()) |
| 111 | |
| 112 | def testSummary(self): |
| 113 | self.assertEqual('Google Issue Tracker issue 123456.', |
| 114 | federated.GoogleIssueTrackerIssue('b/123456').Summary()) |