blob: 3603583afb48115b2069f79acd33ccabafdff4b0 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2016 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 the component_helpers module."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import unittest
11
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010012from mrproto import tracker_pb2
Copybara854996b2021-09-07 19:36:02 +000013from services import service_manager
14from testing import fake
15from tracker import component_helpers
16from tracker import tracker_bizobj
17
18
19class ComponentHelpersTest(unittest.TestCase):
20
21 def setUp(self):
22 self.config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
23 self.cd1 = tracker_bizobj.MakeComponentDef(
24 1, 789, 'FrontEnd', 'doc', False, [], [111], 0, 0)
25 self.cd2 = tracker_bizobj.MakeComponentDef(
26 2, 789, 'FrontEnd>Splash', 'doc', False, [], [222], 0, 0)
27 self.cd3 = tracker_bizobj.MakeComponentDef(
28 3, 789, 'BackEnd', 'doc', True, [], [111, 333], 0, 0)
29 self.config.component_defs = [self.cd1, self.cd2, self.cd3]
30 self.services = service_manager.Services(
31 user=fake.UserService(),
32 config=fake.ConfigService())
33 self.services.user.TestAddUser('a@example.com', 111)
34 self.services.user.TestAddUser('b@example.com', 222)
35 self.services.user.TestAddUser('c@example.com', 333)
36 self.mr = fake.MonorailRequest(self.services)
37 self.mr.cnxn = fake.MonorailConnection()
38
39 def testParseComponentRequest_Empty(self):
40 post_data = fake.PostData(admins=[''], cc=[''], labels=[''])
41 parsed = component_helpers.ParseComponentRequest(
42 self.mr, post_data, self.services)
43 self.assertEqual('', parsed.leaf_name)
44 self.assertEqual('', parsed.docstring)
45 self.assertEqual([], parsed.admin_usernames)
46 self.assertEqual([], parsed.cc_usernames)
47 self.assertEqual([], parsed.admin_ids)
48 self.assertEqual([], parsed.cc_ids)
49 self.assertFalse(self.mr.errors.AnyErrors())
50
51 def testParseComponentRequest_Normal(self):
52 post_data = fake.PostData(
53 leaf_name=['FrontEnd'],
54 docstring=['The server-side app that serves pages'],
55 deprecated=[False],
56 admins=['a@example.com'],
57 cc=['b@example.com, c@example.com'],
58 labels=['Hot, Cold'])
59 parsed = component_helpers.ParseComponentRequest(
60 self.mr, post_data, self.services)
61 self.assertEqual('FrontEnd', parsed.leaf_name)
62 self.assertEqual('The server-side app that serves pages', parsed.docstring)
63 self.assertEqual(['a@example.com'], parsed.admin_usernames)
64 self.assertEqual(['b@example.com', 'c@example.com'], parsed.cc_usernames)
65 self.assertEqual(['Hot', 'Cold'], parsed.label_strs)
66 self.assertEqual([111], parsed.admin_ids)
67 self.assertEqual([222, 333], parsed.cc_ids)
68 self.assertEqual([0, 1], parsed.label_ids)
69 self.assertFalse(self.mr.errors.AnyErrors())
70
71 def testParseComponentRequest_InvalidUser(self):
72 post_data = fake.PostData(
73 leaf_name=['FrontEnd'],
74 docstring=['The server-side app that serves pages'],
75 deprecated=[False],
76 admins=['a@example.com, invalid_user'],
77 cc=['b@example.com, c@example.com'],
78 labels=[''])
79 parsed = component_helpers.ParseComponentRequest(
80 self.mr, post_data, self.services)
81 self.assertEqual('FrontEnd', parsed.leaf_name)
82 self.assertEqual('The server-side app that serves pages', parsed.docstring)
83 self.assertEqual(['a@example.com', 'invalid_user'], parsed.admin_usernames)
84 self.assertEqual(['b@example.com', 'c@example.com'], parsed.cc_usernames)
85 self.assertEqual([111], parsed.admin_ids)
86 self.assertEqual([222, 333], parsed.cc_ids)
87 self.assertTrue(self.mr.errors.AnyErrors())
88 self.assertEqual('invalid_user unrecognized', self.mr.errors.member_admins)
89
90 def testGetComponentCcIDs(self):
91 issue = tracker_pb2.Issue()
92 issues_components_cc_ids = component_helpers.GetComponentCcIDs(
93 issue, self.config)
94 self.assertEqual(set(), issues_components_cc_ids)
95
96 issue.component_ids = [1, 2]
97 issues_components_cc_ids = component_helpers.GetComponentCcIDs(
98 issue, self.config)
99 self.assertEqual({111, 222}, issues_components_cc_ids)
100
101 def testGetCcIDsForComponentAndAncestors(self):
102 components_cc_ids = component_helpers.GetCcIDsForComponentAndAncestors(
103 self.config, self.cd1)
104 self.assertEqual({111}, components_cc_ids)
105
106 components_cc_ids = component_helpers.GetCcIDsForComponentAndAncestors(
107 self.config, self.cd2)
108 self.assertEqual({111, 222}, components_cc_ids)
109
110 components_cc_ids = component_helpers.GetCcIDsForComponentAndAncestors(
111 self.config, self.cd3)
112 self.assertEqual({111, 333}, components_cc_ids)