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