blob: 4100d3e9bb88a2962c6535d0d9287854fc29930a [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 config_svc module."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
Copybara854996b2021-09-07 19:36:02 +000010import logging
11import mock
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010012import re
13import six
14import unittest
Copybara854996b2021-09-07 19:36:02 +000015
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020016try:
17 from mox3 import mox
18except ImportError:
19 import mox
Copybara854996b2021-09-07 19:36:02 +000020
21from google.appengine.api import memcache
22from google.appengine.ext import testbed
23
24from framework import exceptions
25from framework import framework_constants
26from framework import sql
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010027from mrproto import tracker_pb2
Copybara854996b2021-09-07 19:36:02 +000028from services import config_svc
29from services import template_svc
30from testing import fake
31from tracker import tracker_bizobj
32from tracker import tracker_constants
33
34LABEL_ROW_SHARDS = config_svc.LABEL_ROW_SHARDS
35
36
37def MakeConfigService(cache_manager, my_mox):
38 config_service = config_svc.ConfigService(cache_manager)
39 for table_var in ['projectissueconfig_tbl', 'statusdef_tbl', 'labeldef_tbl',
40 'fielddef_tbl', 'fielddef2admin_tbl', 'fielddef2editor_tbl',
41 'componentdef_tbl', 'component2admin_tbl',
42 'component2cc_tbl', 'component2label_tbl',
43 'approvaldef2approver_tbl', 'approvaldef2survey_tbl']:
44 setattr(config_service, table_var, my_mox.CreateMock(sql.SQLTableManager))
45
46 return config_service
47
48
49class LabelRowTwoLevelCacheTest(unittest.TestCase):
50
51 def setUp(self):
52 self.mox = mox.Mox()
53 self.cnxn = 'fake connection'
54 self.cache_manager = fake.CacheManager()
55 self.config_service = MakeConfigService(self.cache_manager, self.mox)
56 self.label_row_2lc = self.config_service.label_row_2lc
57
58 self.rows = [(1, 789, 1, 'A', 'doc', False),
59 (2, 789, 2, 'B', 'doc', False),
60 (3, 678, 1, 'C', 'doc', True),
61 (4, 678, None, 'D', 'doc', False)]
62
63 def tearDown(self):
64 self.mox.UnsetStubs()
65 self.mox.ResetAll()
66
67 def testDeserializeLabelRows_Empty(self):
68 label_row_dict = self.label_row_2lc._DeserializeLabelRows([])
69 self.assertEqual({}, label_row_dict)
70
71 def testDeserializeLabelRows_Normal(self):
72 label_rows_dict = self.label_row_2lc._DeserializeLabelRows(self.rows)
73 expected = {
74 (789, 1): [(1, 789, 1, 'A', 'doc', False)],
75 (789, 2): [(2, 789, 2, 'B', 'doc', False)],
76 (678, 3): [(3, 678, 1, 'C', 'doc', True)],
77 (678, 4): [(4, 678, None, 'D', 'doc', False)],
78 }
79 self.assertEqual(expected, label_rows_dict)
80
81 def SetUpFetchItems(self, keys, rows):
82 for (project_id, shard_id) in keys:
83 sharded_rows = [row for row in rows
84 if row[0] % LABEL_ROW_SHARDS == shard_id]
85 self.config_service.labeldef_tbl.Select(
86 self.cnxn, cols=config_svc.LABELDEF_COLS, project_id=project_id,
87 where=[('id %% %s = %s', [LABEL_ROW_SHARDS, shard_id])]).AndReturn(
88 sharded_rows)
89
90 def testFetchItems(self):
91 keys = [(567, 0), (678, 0), (789, 0),
92 (567, 1), (678, 1), (789, 1),
93 (567, 2), (678, 2), (789, 2),
94 (567, 3), (678, 3), (789, 3),
95 (567, 4), (678, 4), (789, 4),
96 ]
97 self.SetUpFetchItems(keys, self.rows)
98 self.mox.ReplayAll()
99 label_rows_dict = self.label_row_2lc.FetchItems(self.cnxn, keys)
100 self.mox.VerifyAll()
101 expected = {
102 (567, 0): [],
103 (678, 0): [],
104 (789, 0): [],
105 (567, 1): [],
106 (678, 1): [],
107 (789, 1): [(1, 789, 1, 'A', 'doc', False)],
108 (567, 2): [],
109 (678, 2): [],
110 (789, 2): [(2, 789, 2, 'B', 'doc', False)],
111 (567, 3): [],
112 (678, 3): [(3, 678, 1, 'C', 'doc', True)],
113 (789, 3): [],
114 (567, 4): [],
115 (678, 4): [(4, 678, None, 'D', 'doc', False)],
116 (789, 4): [],
117 }
118 self.assertEqual(expected, label_rows_dict)
119
120
121class StatusRowTwoLevelCacheTest(unittest.TestCase):
122
123 def setUp(self):
124 self.mox = mox.Mox()
125 self.cnxn = 'fake connection'
126 self.cache_manager = fake.CacheManager()
127 self.config_service = MakeConfigService(self.cache_manager, self.mox)
128 self.status_row_2lc = self.config_service.status_row_2lc
129
130 self.rows = [(1, 789, 1, 'A', True, 'doc', False),
131 (2, 789, 2, 'B', False, 'doc', False),
132 (3, 678, 1, 'C', True, 'doc', True),
133 (4, 678, None, 'D', True, 'doc', False)]
134
135 def tearDown(self):
136 self.mox.UnsetStubs()
137 self.mox.ResetAll()
138
139 def testDeserializeStatusRows_Empty(self):
140 status_row_dict = self.status_row_2lc._DeserializeStatusRows([])
141 self.assertEqual({}, status_row_dict)
142
143 def testDeserializeStatusRows_Normal(self):
144 status_rows_dict = self.status_row_2lc._DeserializeStatusRows(self.rows)
145 expected = {
146 678: [(3, 678, 1, 'C', True, 'doc', True),
147 (4, 678, None, 'D', True, 'doc', False)],
148 789: [(1, 789, 1, 'A', True, 'doc', False),
149 (2, 789, 2, 'B', False, 'doc', False)],
150 }
151 self.assertEqual(expected, status_rows_dict)
152
153 def SetUpFetchItems(self, keys, rows):
154 self.config_service.statusdef_tbl.Select(
155 self.cnxn, cols=config_svc.STATUSDEF_COLS, project_id=keys,
156 order_by=[('rank DESC', []), ('status DESC', [])]).AndReturn(
157 rows)
158
159 def testFetchItems(self):
160 keys = [567, 678, 789]
161 self.SetUpFetchItems(keys, self.rows)
162 self.mox.ReplayAll()
163 status_rows_dict = self.status_row_2lc.FetchItems(self.cnxn, keys)
164 self.mox.VerifyAll()
165 expected = {
166 567: [],
167 678: [(3, 678, 1, 'C', True, 'doc', True),
168 (4, 678, None, 'D', True, 'doc', False)],
169 789: [(1, 789, 1, 'A', True, 'doc', False),
170 (2, 789, 2, 'B', False, 'doc', False)],
171 }
172 self.assertEqual(expected, status_rows_dict)
173
174
175class ConfigRowTwoLevelCacheTest(unittest.TestCase):
176
177 def setUp(self):
178 self.mox = mox.Mox()
179 self.cnxn = 'fake connection'
180 self.cache_manager = fake.CacheManager()
181 self.config_service = MakeConfigService(self.cache_manager, self.mox)
182 self.config_2lc = self.config_service.config_2lc
183
184 self.config_rows = [
185 (789, 'Duplicate', 'Pri Type', 1, 2,
186 'Type Pri Summary', '-Pri', 'Mstone', 'Owner',
187 '', None)]
188 self.statusdef_rows = [(1, 789, 1, 'New', True, 'doc', False),
189 (2, 789, 2, 'Fixed', False, 'doc', False)]
190 self.labeldef_rows = [(1, 789, 1, 'Security', 'doc', False),
191 (2, 789, 2, 'UX', 'doc', False)]
192 self.fielddef_rows = [
193 (
194 1, 789, None, 'Field', 'INT_TYPE', 'Defect', '', False, False,
195 False, 1, 99, None, '', '', None, 'NEVER', 'no_action', 'doc',
196 False, None, False, False)
197 ]
198 self.approvaldef2approver_rows = [(2, 101, 789), (2, 102, 789)]
199 self.approvaldef2survey_rows = [(2, 'Q1\nQ2\nQ3', 789)]
200 self.fielddef2admin_rows = [(1, 111), (1, 222)]
201 self.fielddef2editor_rows = [(1, 111), (1, 222), (1, 333)]
202 self.componentdef_rows = []
203 self.component2admin_rows = []
204 self.component2cc_rows = []
205 self.component2label_rows = []
206
207 def tearDown(self):
208 self.mox.UnsetStubs()
209 self.mox.ResetAll()
210
211 def testDeserializeIssueConfigs_Empty(self):
212 config_dict = self.config_2lc._DeserializeIssueConfigs(
213 [], [], [], [], [], [], [], [], [], [], [], [])
214 self.assertEqual({}, config_dict)
215
216 def testDeserializeIssueConfigs_Normal(self):
217 config_dict = self.config_2lc._DeserializeIssueConfigs(
218 self.config_rows, self.statusdef_rows, self.labeldef_rows,
219 self.fielddef_rows, self.fielddef2admin_rows, self.fielddef2editor_rows,
220 self.componentdef_rows, self.component2admin_rows,
221 self.component2cc_rows, self.component2label_rows,
222 self.approvaldef2approver_rows, self.approvaldef2survey_rows)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100223 six.assertCountEqual(self, [789], list(config_dict.keys()))
Copybara854996b2021-09-07 19:36:02 +0000224 config = config_dict[789]
225 self.assertEqual(789, config.project_id)
226 self.assertEqual(['Duplicate'], config.statuses_offer_merge)
227 self.assertEqual(len(self.labeldef_rows), len(config.well_known_labels))
228 self.assertEqual(len(self.statusdef_rows), len(config.well_known_statuses))
229 self.assertEqual(len(self.fielddef_rows), len(config.field_defs))
230 self.assertEqual(len(self.componentdef_rows), len(config.component_defs))
231 self.assertEqual(
232 len(self.fielddef2admin_rows), len(config.field_defs[0].admin_ids))
233 self.assertEqual(
234 len(self.fielddef2editor_rows), len(config.field_defs[0].editor_ids))
235 self.assertEqual(len(self.approvaldef2approver_rows),
236 len(config.approval_defs[0].approver_ids))
237 self.assertEqual(config.approval_defs[0].survey, 'Q1\nQ2\nQ3')
238
239 def SetUpFetchConfigs(self, project_ids):
240 self.config_service.projectissueconfig_tbl.Select(
241 self.cnxn, cols=config_svc.PROJECTISSUECONFIG_COLS,
242 project_id=project_ids).AndReturn(self.config_rows)
243
244 self.config_service.statusdef_tbl.Select(
245 self.cnxn, cols=config_svc.STATUSDEF_COLS, project_id=project_ids,
246 where=[('rank IS NOT NULL', [])], order_by=[('rank', [])]).AndReturn(
247 self.statusdef_rows)
248
249 self.config_service.labeldef_tbl.Select(
250 self.cnxn, cols=config_svc.LABELDEF_COLS, project_id=project_ids,
251 where=[('rank IS NOT NULL', [])], order_by=[('rank', [])]).AndReturn(
252 self.labeldef_rows)
253
254 self.config_service.approvaldef2approver_tbl.Select(
255 self.cnxn, cols=config_svc.APPROVALDEF2APPROVER_COLS,
256 project_id=project_ids).AndReturn(self.approvaldef2approver_rows)
257 self.config_service.approvaldef2survey_tbl.Select(
258 self.cnxn, cols=config_svc.APPROVALDEF2SURVEY_COLS,
259 project_id=project_ids).AndReturn(self.approvaldef2survey_rows)
260
261 self.config_service.fielddef_tbl.Select(
262 self.cnxn, cols=config_svc.FIELDDEF_COLS, project_id=project_ids,
263 order_by=[('field_name', [])]).AndReturn(self.fielddef_rows)
264 field_ids = [row[0] for row in self.fielddef_rows]
265 self.config_service.fielddef2admin_tbl.Select(
266 self.cnxn, cols=config_svc.FIELDDEF2ADMIN_COLS,
267 field_id=field_ids).AndReturn(self.fielddef2admin_rows)
268 self.config_service.fielddef2editor_tbl.Select(
269 self.cnxn, cols=config_svc.FIELDDEF2EDITOR_COLS,
270 field_id=field_ids).AndReturn(self.fielddef2editor_rows)
271
272 self.config_service.componentdef_tbl.Select(
273 self.cnxn, cols=config_svc.COMPONENTDEF_COLS, project_id=project_ids,
274 is_deleted=False,
275 order_by=[('path', [])]).AndReturn(self.componentdef_rows)
276
277 def testFetchConfigs(self):
278 keys = [789]
279 self.SetUpFetchConfigs(keys)
280 self.mox.ReplayAll()
281 config_dict = self.config_2lc._FetchConfigs(self.cnxn, keys)
282 self.mox.VerifyAll()
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100283 six.assertCountEqual(self, keys, list(config_dict.keys()))
Copybara854996b2021-09-07 19:36:02 +0000284
285 def testFetchItems(self):
286 keys = [678, 789]
287 self.SetUpFetchConfigs(keys)
288 self.mox.ReplayAll()
289 config_dict = self.config_2lc.FetchItems(self.cnxn, keys)
290 self.mox.VerifyAll()
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100291 six.assertCountEqual(self, keys, list(config_dict.keys()))
Copybara854996b2021-09-07 19:36:02 +0000292
293
294class ConfigServiceTest(unittest.TestCase):
295
296 def setUp(self):
297 self.testbed = testbed.Testbed()
298 self.testbed.activate()
299 self.testbed.init_memcache_stub()
300
301 self.mox = mox.Mox()
302 self.cnxn = self.mox.CreateMock(sql.MonorailConnection)
303 self.cache_manager = fake.CacheManager()
304 self.config_service = MakeConfigService(self.cache_manager, self.mox)
305
306 def tearDown(self):
307 self.testbed.deactivate()
308 self.mox.UnsetStubs()
309 self.mox.ResetAll()
310
311 ### Label lookups
312
313 def testGetLabelDefRows_Hit(self):
314 self.config_service.label_row_2lc.CacheItem((789, 0), [])
315 self.config_service.label_row_2lc.CacheItem((789, 1), [])
316 self.config_service.label_row_2lc.CacheItem((789, 2), [])
317 self.config_service.label_row_2lc.CacheItem(
318 (789, 3), [(3, 678, 1, 'C', 'doc', True)])
319 self.config_service.label_row_2lc.CacheItem(
320 (789, 4), [(4, 678, None, 'D', 'doc', False)])
321 self.config_service.label_row_2lc.CacheItem((789, 5), [])
322 self.config_service.label_row_2lc.CacheItem((789, 6), [])
323 self.config_service.label_row_2lc.CacheItem((789, 7), [])
324 self.config_service.label_row_2lc.CacheItem((789, 8), [])
325 self.config_service.label_row_2lc.CacheItem((789, 9), [])
326 actual = self.config_service.GetLabelDefRows(self.cnxn, 789)
327 expected = [
328 (3, 678, 1, 'C', 'doc', True),
329 (4, 678, None, 'D', 'doc', False)]
330 self.assertEqual(expected, actual)
331
332 def SetUpGetLabelDefRowsAnyProject(self, rows):
333 self.config_service.labeldef_tbl.Select(
334 self.cnxn, cols=config_svc.LABELDEF_COLS, where=None,
335 order_by=[('rank DESC', []), ('label DESC', [])]).AndReturn(
336 rows)
337
338 def testGetLabelDefRowsAnyProject(self):
339 rows = 'foo'
340 self.SetUpGetLabelDefRowsAnyProject(rows)
341 self.mox.ReplayAll()
342 actual = self.config_service.GetLabelDefRowsAnyProject(self.cnxn)
343 self.mox.VerifyAll()
344 self.assertEqual(rows, actual)
345
346 def testDeserializeLabels(self):
347 labeldef_rows = [(1, 789, 1, 'Security', 'doc', False),
348 (2, 789, 2, 'UX', 'doc', True)]
349 id_to_name, name_to_id = self.config_service._DeserializeLabels(
350 labeldef_rows)
351 self.assertEqual({1: 'Security', 2: 'UX'}, id_to_name)
352 self.assertEqual({'security': 1, 'ux': 2}, name_to_id)
353
354 def testEnsureLabelCacheEntry_Hit(self):
355 label_dicts = 'foo'
356 self.config_service.label_cache.CacheItem(789, label_dicts)
357 # No mock calls set up because none are needed.
358 self.mox.ReplayAll()
359 self.config_service._EnsureLabelCacheEntry(self.cnxn, 789)
360 self.mox.VerifyAll()
361
362 def SetUpEnsureLabelCacheEntry_Miss(self, project_id, rows):
363 for shard_id in range(0, LABEL_ROW_SHARDS):
364 shard_rows = [row for row in rows
365 if row[0] % LABEL_ROW_SHARDS == shard_id]
366 self.config_service.labeldef_tbl.Select(
367 self.cnxn, cols=config_svc.LABELDEF_COLS, project_id=project_id,
368 where=[('id %% %s = %s', [LABEL_ROW_SHARDS, shard_id])]).AndReturn(
369 shard_rows)
370
371 def testEnsureLabelCacheEntry_Miss(self):
372 labeldef_rows = [(1, 789, 1, 'Security', 'doc', False),
373 (2, 789, 2, 'UX', 'doc', True)]
374 self.SetUpEnsureLabelCacheEntry_Miss(789, labeldef_rows)
375 self.mox.ReplayAll()
376 self.config_service._EnsureLabelCacheEntry(self.cnxn, 789)
377 self.mox.VerifyAll()
378 label_dicts = {1: 'Security', 2: 'UX'}, {'security': 1, 'ux': 2}
379 self.assertEqual(label_dicts, self.config_service.label_cache.GetItem(789))
380
381 def testLookupLabel_Hit(self):
382 label_dicts = {1: 'Security', 2: 'UX'}, {'security': 1, 'ux': 2}
383 self.config_service.label_cache.CacheItem(789, label_dicts)
384 # No mock calls set up because none are needed.
385 self.mox.ReplayAll()
386 self.assertEqual(
387 'Security', self.config_service.LookupLabel(self.cnxn, 789, 1))
388 self.assertEqual(
389 'UX', self.config_service.LookupLabel(self.cnxn, 789, 2))
390 self.mox.VerifyAll()
391
392 def testLookupLabelID_Hit(self):
393 label_dicts = {1: 'Security', 2: 'UX'}, {'security': 1, 'ux': 2}
394 self.config_service.label_cache.CacheItem(789, label_dicts)
395 # No mock calls set up because none are needed.
396 self.mox.ReplayAll()
397 self.assertEqual(
398 1, self.config_service.LookupLabelID(self.cnxn, 789, 'Security'))
399 self.assertEqual(
400 2, self.config_service.LookupLabelID(self.cnxn, 789, 'UX'))
401 self.mox.VerifyAll()
402
403 def testLookupLabelID_MissAndDoubleCheck(self):
404 label_dicts = {1: 'Security', 2: 'UX'}, {'security': 1, 'ux': 2}
405 self.config_service.label_cache.CacheItem(789, label_dicts)
406
407 self.config_service.labeldef_tbl.Select(
408 self.cnxn, cols=['id'], project_id=789,
409 where=[('LOWER(label) = %s', ['newlabel'])],
410 limit=1).AndReturn([(3,)])
411 self.mox.ReplayAll()
412 self.assertEqual(
413 3, self.config_service.LookupLabelID(self.cnxn, 789, 'NewLabel'))
414 self.mox.VerifyAll()
415
416 def testLookupLabelID_MissAutocreate(self):
417 label_dicts = {1: 'Security', 2: 'UX'}, {'security': 1, 'ux': 2}
418 self.config_service.label_cache.CacheItem(789, label_dicts)
419
420 self.config_service.labeldef_tbl.Select(
421 self.cnxn, cols=['id'], project_id=789,
422 where=[('LOWER(label) = %s', ['newlabel'])],
423 limit=1).AndReturn([])
424 self.config_service.labeldef_tbl.InsertRow(
425 self.cnxn, project_id=789, label='NewLabel').AndReturn(3)
426 self.mox.ReplayAll()
427 self.assertEqual(
428 3, self.config_service.LookupLabelID(self.cnxn, 789, 'NewLabel'))
429 self.mox.VerifyAll()
430
431 def testLookupLabelID_MissDontAutocreate(self):
432 label_dicts = {1: 'Security', 2: 'UX'}, {'security': 1, 'ux': 2}
433 self.config_service.label_cache.CacheItem(789, label_dicts)
434
435 self.config_service.labeldef_tbl.Select(
436 self.cnxn, cols=['id'], project_id=789,
437 where=[('LOWER(label) = %s', ['newlabel'])],
438 limit=1).AndReturn([])
439 self.mox.ReplayAll()
440 self.assertIsNone(self.config_service.LookupLabelID(
441 self.cnxn, 789, 'NewLabel', autocreate=False))
442 self.mox.VerifyAll()
443
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100444 def testLookupLabelID_CaseSensitive(self):
445 label_dicts = {101: 'security', 201: 'ux'}, {'security': 101, 'ux': 201}
446 self.config_service.label_cache.CacheItem(789, label_dicts)
447
448 self.config_service.labeldef_tbl.Select(
449 self.cnxn,
450 cols=['id'],
451 project_id=789,
452 where=[('label = %s', ['Security'])],
453 limit=1).AndReturn([])
454 self.mox.ReplayAll()
455 self.assertIsNone(
456 self.config_service.LookupLabelID(
457 self.cnxn, 789, 'Security', autocreate=False, case_sensitive=True))
458 self.mox.VerifyAll()
459
Copybara854996b2021-09-07 19:36:02 +0000460 def testLookupLabelIDs_Hit(self):
461 label_dicts = {1: 'Security', 2: 'UX'}, {'security': 1, 'ux': 2}
462 self.config_service.label_cache.CacheItem(789, label_dicts)
463 # No mock calls set up because none are needed.
464 self.mox.ReplayAll()
465 self.assertEqual(
466 [1, 2],
467 self.config_service.LookupLabelIDs(self.cnxn, 789, ['Security', 'UX']))
468 self.mox.VerifyAll()
469
470 def testLookupIDsOfLabelsMatching_Hit(self):
471 label_dicts = {1: 'Security', 2: 'UX'}, {'security': 1, 'ux': 2}
472 self.config_service.label_cache.CacheItem(789, label_dicts)
473 # No mock calls set up because none are needed.
474 self.mox.ReplayAll()
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100475 six.assertCountEqual(
476 self, [1],
Copybara854996b2021-09-07 19:36:02 +0000477 self.config_service.LookupIDsOfLabelsMatching(
478 self.cnxn, 789, re.compile('Sec.*')))
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100479 six.assertCountEqual(
480 self, [1, 2],
Copybara854996b2021-09-07 19:36:02 +0000481 self.config_service.LookupIDsOfLabelsMatching(
482 self.cnxn, 789, re.compile('.*')))
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100483 six.assertCountEqual(
484 self, [],
Copybara854996b2021-09-07 19:36:02 +0000485 self.config_service.LookupIDsOfLabelsMatching(
486 self.cnxn, 789, re.compile('Zzzzz.*')))
487 self.mox.VerifyAll()
488
489 def SetUpLookupLabelIDsAnyProject(self, label, id_rows):
490 self.config_service.labeldef_tbl.Select(
491 self.cnxn, cols=['id'], label=label).AndReturn(id_rows)
492
493 def testLookupLabelIDsAnyProject(self):
494 self.SetUpLookupLabelIDsAnyProject('Security', [(1,)])
495 self.mox.ReplayAll()
496 actual = self.config_service.LookupLabelIDsAnyProject(
497 self.cnxn, 'Security')
498 self.mox.VerifyAll()
499 self.assertEqual([1], actual)
500
501 def SetUpLookupIDsOfLabelsMatchingAnyProject(self, id_label_rows):
502 self.config_service.labeldef_tbl.Select(
503 self.cnxn, cols=['id', 'label']).AndReturn(id_label_rows)
504
505 def testLookupIDsOfLabelsMatchingAnyProject(self):
506 id_label_rows = [(1, 'Security'), (2, 'UX')]
507 self.SetUpLookupIDsOfLabelsMatchingAnyProject(id_label_rows)
508 self.mox.ReplayAll()
509 actual = self.config_service.LookupIDsOfLabelsMatchingAnyProject(
510 self.cnxn, re.compile('(Sec|Zzz).*'))
511 self.mox.VerifyAll()
512 self.assertEqual([1], actual)
513
514 ### Status lookups
515
516 def testGetStatusDefRows(self):
517 rows = 'foo'
518 self.config_service.status_row_2lc.CacheItem(789, rows)
519 actual = self.config_service.GetStatusDefRows(self.cnxn, 789)
520 self.assertEqual(rows, actual)
521
522 def SetUpGetStatusDefRowsAnyProject(self, rows):
523 self.config_service.statusdef_tbl.Select(
524 self.cnxn, cols=config_svc.STATUSDEF_COLS,
525 order_by=[('rank DESC', []), ('status DESC', [])]).AndReturn(
526 rows)
527
528 def testGetStatusDefRowsAnyProject(self):
529 rows = 'foo'
530 self.SetUpGetStatusDefRowsAnyProject(rows)
531 self.mox.ReplayAll()
532 actual = self.config_service.GetStatusDefRowsAnyProject(self.cnxn)
533 self.mox.VerifyAll()
534 self.assertEqual(rows, actual)
535
536 def testDeserializeStatuses(self):
537 statusdef_rows = [(1, 789, 1, 'New', True, 'doc', False),
538 (2, 789, 2, 'Fixed', False, 'doc', True)]
539 actual = self.config_service._DeserializeStatuses(statusdef_rows)
540 id_to_name, name_to_id, closed_ids = actual
541 self.assertEqual({1: 'New', 2: 'Fixed'}, id_to_name)
542 self.assertEqual({'new': 1, 'fixed': 2}, name_to_id)
543 self.assertEqual([2], closed_ids)
544
545 def testEnsureStatusCacheEntry_Hit(self):
546 status_dicts = 'foo'
547 self.config_service.status_cache.CacheItem(789, status_dicts)
548 # No mock calls set up because none are needed.
549 self.mox.ReplayAll()
550 self.config_service._EnsureStatusCacheEntry(self.cnxn, 789)
551 self.mox.VerifyAll()
552
553 def SetUpEnsureStatusCacheEntry_Miss(self, keys, rows):
554 self.config_service.statusdef_tbl.Select(
555 self.cnxn, cols=config_svc.STATUSDEF_COLS, project_id=keys,
556 order_by=[('rank DESC', []), ('status DESC', [])]).AndReturn(
557 rows)
558
559 def testEnsureStatusCacheEntry_Miss(self):
560 statusdef_rows = [(1, 789, 1, 'New', True, 'doc', False),
561 (2, 789, 2, 'Fixed', False, 'doc', True)]
562 self.SetUpEnsureStatusCacheEntry_Miss([789], statusdef_rows)
563 self.mox.ReplayAll()
564 self.config_service._EnsureStatusCacheEntry(self.cnxn, 789)
565 self.mox.VerifyAll()
566 status_dicts = {1: 'New', 2: 'Fixed'}, {'new': 1, 'fixed': 2}, [2]
567 self.assertEqual(
568 status_dicts, self.config_service.status_cache.GetItem(789))
569
570 def testLookupStatus_Hit(self):
571 status_dicts = {1: 'New', 2: 'Fixed'}, {'new': 1, 'fixed': 2}, [2]
572 self.config_service.status_cache.CacheItem(789, status_dicts)
573 # No mock calls set up because none are needed.
574 self.mox.ReplayAll()
575 self.assertEqual(
576 'New', self.config_service.LookupStatus(self.cnxn, 789, 1))
577 self.assertEqual(
578 'Fixed', self.config_service.LookupStatus(self.cnxn, 789, 2))
579 self.mox.VerifyAll()
580
581 def testLookupStatusID_Hit(self):
582 status_dicts = {1: 'New', 2: 'Fixed'}, {'new': 1, 'fixed': 2}, [2]
583 self.config_service.status_cache.CacheItem(789, status_dicts)
584 # No mock calls set up because none are needed.
585 self.mox.ReplayAll()
586 self.assertEqual(
587 1, self.config_service.LookupStatusID(self.cnxn, 789, 'New'))
588 self.assertEqual(
589 2, self.config_service.LookupStatusID(self.cnxn, 789, 'Fixed'))
590 self.mox.VerifyAll()
591
592 def testLookupStatusIDs_Hit(self):
593 status_dicts = {1: 'New', 2: 'Fixed'}, {'new': 1, 'fixed': 2}, [2]
594 self.config_service.status_cache.CacheItem(789, status_dicts)
595 # No mock calls set up because none are needed.
596 self.mox.ReplayAll()
597 self.assertEqual(
598 [1, 2],
599 self.config_service.LookupStatusIDs(self.cnxn, 789, ['New', 'Fixed']))
600 self.mox.VerifyAll()
601
602 def testLookupClosedStatusIDs_Hit(self):
603 status_dicts = {1: 'New', 2: 'Fixed'}, {'new': 1, 'fixed': 2}, [2]
604 self.config_service.status_cache.CacheItem(789, status_dicts)
605 # No mock calls set up because none are needed.
606 self.mox.ReplayAll()
607 self.assertEqual(
608 [2],
609 self.config_service.LookupClosedStatusIDs(self.cnxn, 789))
610 self.mox.VerifyAll()
611
612 def SetUpLookupClosedStatusIDsAnyProject(self, id_rows):
613 self.config_service.statusdef_tbl.Select(
614 self.cnxn, cols=['id'], means_open=False).AndReturn(
615 id_rows)
616
617 def testLookupClosedStatusIDsAnyProject(self):
618 self.SetUpLookupClosedStatusIDsAnyProject([(2,)])
619 self.mox.ReplayAll()
620 actual = self.config_service.LookupClosedStatusIDsAnyProject(self.cnxn)
621 self.mox.VerifyAll()
622 self.assertEqual([2], actual)
623
624 def SetUpLookupStatusIDsAnyProject(self, status, id_rows):
625 self.config_service.statusdef_tbl.Select(
626 self.cnxn, cols=['id'], status=status).AndReturn(id_rows)
627
628 def testLookupStatusIDsAnyProject(self):
629 self.SetUpLookupStatusIDsAnyProject('New', [(1,)])
630 self.mox.ReplayAll()
631 actual = self.config_service.LookupStatusIDsAnyProject(self.cnxn, 'New')
632 self.mox.VerifyAll()
633 self.assertEqual([1], actual)
634
635 ### Issue tracker configuration objects
636
637 def SetUpGetProjectConfigs(self, project_ids):
638 self.config_service.projectissueconfig_tbl.Select(
639 self.cnxn, cols=config_svc.PROJECTISSUECONFIG_COLS,
640 project_id=project_ids).AndReturn([])
641 self.config_service.statusdef_tbl.Select(
642 self.cnxn, cols=config_svc.STATUSDEF_COLS,
643 project_id=project_ids, where=[('rank IS NOT NULL', [])],
644 order_by=[('rank', [])]).AndReturn([])
645 self.config_service.labeldef_tbl.Select(
646 self.cnxn, cols=config_svc.LABELDEF_COLS,
647 project_id=project_ids, where=[('rank IS NOT NULL', [])],
648 order_by=[('rank', [])]).AndReturn([])
649 self.config_service.approvaldef2approver_tbl.Select(
650 self.cnxn, cols=config_svc.APPROVALDEF2APPROVER_COLS,
651 project_id=project_ids).AndReturn([])
652 self.config_service.approvaldef2survey_tbl.Select(
653 self.cnxn, cols=config_svc.APPROVALDEF2SURVEY_COLS,
654 project_id=project_ids).AndReturn([])
655 self.config_service.fielddef_tbl.Select(
656 self.cnxn, cols=config_svc.FIELDDEF_COLS,
657 project_id=project_ids, order_by=[('field_name', [])]).AndReturn([])
658 self.config_service.componentdef_tbl.Select(
659 self.cnxn, cols=config_svc.COMPONENTDEF_COLS,
660 is_deleted=False,
661 project_id=project_ids, order_by=[('path', [])]).AndReturn([])
662
663 def testGetProjectConfigs(self):
664 project_ids = [789, 679]
665 self.SetUpGetProjectConfigs(project_ids)
666
667 self.mox.ReplayAll()
668 config_dict = self.config_service.GetProjectConfigs(
669 self.cnxn, [789, 679], use_cache=False)
670 self.assertEqual(2, len(config_dict))
671 for pid in project_ids:
672 self.assertEqual(pid, config_dict[pid].project_id)
673 self.mox.VerifyAll()
674
675 def testGetProjectConfig_Hit(self):
676 project_id = 789
677 config = tracker_bizobj.MakeDefaultProjectIssueConfig(project_id)
678 self.config_service.config_2lc.CacheItem(project_id, config)
679
680 self.mox.ReplayAll()
681 actual = self.config_service.GetProjectConfig(self.cnxn, project_id)
682 self.assertEqual(config, actual)
683 self.mox.VerifyAll()
684
685 def testGetProjectConfig_Miss(self):
686 project_id = 789
687 self.SetUpGetProjectConfigs([project_id])
688
689 self.mox.ReplayAll()
690 config = self.config_service.GetProjectConfig(self.cnxn, project_id)
691 self.assertEqual(project_id, config.project_id)
692 self.mox.VerifyAll()
693
694 def SetUpStoreConfig_Default(self, project_id):
695 self.config_service.projectissueconfig_tbl.InsertRow(
696 self.cnxn, replace=True,
697 project_id=project_id,
698 statuses_offer_merge='Duplicate',
699 exclusive_label_prefixes='Type Priority Milestone',
700 default_template_for_developers=0,
701 default_template_for_users=0,
702 default_col_spec=tracker_constants.DEFAULT_COL_SPEC,
703 default_sort_spec='',
704 default_x_attr='',
705 default_y_attr='',
706 member_default_query='',
707 custom_issue_entry_url=None,
708 commit=False)
709
710 self.SetUpUpdateWellKnownLabels_Default(project_id)
711 self.SetUpUpdateWellKnownStatuses_Default(project_id)
712 self.cnxn.Commit()
713
714 def SetUpUpdateWellKnownLabels_JustCache(self, project_id):
715 by_id = {
716 idx + 1: label for idx, (label, _, _) in enumerate(
717 tracker_constants.DEFAULT_WELL_KNOWN_LABELS)}
718 by_name = {name.lower(): label_id
719 for label_id, name in by_id.items()}
720 label_dicts = by_id, by_name
721 self.config_service.label_cache.CacheAll({project_id: label_dicts})
722
723 def SetUpUpdateWellKnownLabels_Default(self, project_id):
724 self.SetUpUpdateWellKnownLabels_JustCache(project_id)
725 update_labeldef_rows = [
726 (idx + 1, project_id, idx, label, doc, deprecated)
727 for idx, (label, doc, deprecated) in enumerate(
728 tracker_constants.DEFAULT_WELL_KNOWN_LABELS)]
729 self.config_service.labeldef_tbl.Update(
730 self.cnxn, {'rank': None}, project_id=project_id, commit=False)
731 self.config_service.labeldef_tbl.InsertRows(
732 self.cnxn, config_svc.LABELDEF_COLS, update_labeldef_rows,
733 replace=True, commit=False)
734 self.config_service.labeldef_tbl.InsertRows(
735 self.cnxn, config_svc.LABELDEF_COLS[1:], [], commit=False)
736
737 def SetUpUpdateWellKnownStatuses_Default(self, project_id):
738 by_id = {
739 idx + 1: status for idx, (status, _, _, _) in enumerate(
740 tracker_constants.DEFAULT_WELL_KNOWN_STATUSES)}
741 by_name = {name.lower(): label_id
742 for label_id, name in by_id.items()}
743 closed_ids = [
744 idx + 1 for idx, (_, _, means_open, _) in enumerate(
745 tracker_constants.DEFAULT_WELL_KNOWN_STATUSES)
746 if not means_open]
747 status_dicts = by_id, by_name, closed_ids
748 self.config_service.status_cache.CacheAll({789: status_dicts})
749
750 update_statusdef_rows = [
751 (idx + 1, project_id, idx, status, means_open, doc, deprecated)
752 for idx, (status, doc, means_open, deprecated) in enumerate(
753 tracker_constants.DEFAULT_WELL_KNOWN_STATUSES)]
754 self.config_service.statusdef_tbl.Update(
755 self.cnxn, {'rank': None}, project_id=project_id, commit=False)
756 self.config_service.statusdef_tbl.InsertRows(
757 self.cnxn, config_svc.STATUSDEF_COLS, update_statusdef_rows,
758 replace=True, commit=False)
759 self.config_service.statusdef_tbl.InsertRows(
760 self.cnxn, config_svc.STATUSDEF_COLS[1:], [], commit=False)
761
762 def SetUpUpdateApprovals_Default(
763 self, approval_id, approver_rows, survey_row):
764 self.config_service.approvaldef2approver_tbl.Delete(
765 self.cnxn, approval_id=approval_id, commit=False)
766
767 self.config_service.approvaldef2approver_tbl.InsertRows(
768 self.cnxn,
769 config_svc.APPROVALDEF2APPROVER_COLS,
770 approver_rows,
771 commit=False)
772
773 approval_id, survey, project_id = survey_row
774 self.config_service.approvaldef2survey_tbl.Delete(
775 self.cnxn, approval_id=approval_id, commit=False)
776 self.config_service.approvaldef2survey_tbl.InsertRow(
777 self.cnxn,
778 approval_id=approval_id,
779 survey=survey,
780 project_id=project_id,
781 commit=False)
782
783 def testStoreConfig(self):
784 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
785 self.SetUpStoreConfig_Default(789)
786
787 self.mox.ReplayAll()
788 self.config_service.StoreConfig(self.cnxn, config)
789 self.mox.VerifyAll()
790
791 def testUpdateWellKnownLabels(self):
792 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
793 self.SetUpUpdateWellKnownLabels_Default(789)
794
795 self.mox.ReplayAll()
796 self.config_service._UpdateWellKnownLabels(self.cnxn, config)
797 self.mox.VerifyAll()
798
799 def testUpdateWellKnownLabels_Duplicate(self):
800 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
801 config.well_known_labels.append(config.well_known_labels[0])
802 self.SetUpUpdateWellKnownLabels_JustCache(789)
803
804 self.mox.ReplayAll()
805 with self.assertRaises(exceptions.InputException) as cm:
806 self.config_service._UpdateWellKnownLabels(self.cnxn, config)
807 self.mox.VerifyAll()
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100808 self.assertEqual('Defined label "Type-Defect" twice', str(cm.exception))
Copybara854996b2021-09-07 19:36:02 +0000809
810 def testUpdateWellKnownStatuses(self):
811 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
812 self.SetUpUpdateWellKnownStatuses_Default(789)
813
814 self.mox.ReplayAll()
815 self.config_service._UpdateWellKnownStatuses(self.cnxn, config)
816 self.mox.VerifyAll()
817
818 def testUpdateApprovals(self):
819 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
820 approver_rows = [(123, 111, 789), (123, 222, 789)]
821 survey_row = (123, 'Q1\nQ2', 789)
822 first_approval = tracker_bizobj.MakeFieldDef(
823 123, 789, 'FirstApproval', tracker_pb2.FieldTypes.APPROVAL_TYPE,
824 None, '', False, False, False, None, None, '', False, '', '',
825 tracker_pb2.NotifyTriggers.NEVER, 'no_action', 'the first one', False)
826 config.field_defs = [first_approval]
827 config.approval_defs = [tracker_pb2.ApprovalDef(
828 approval_id=123, approver_ids=[111, 222], survey='Q1\nQ2')]
829 self.SetUpUpdateApprovals_Default(123, approver_rows, survey_row)
830
831 self.mox.ReplayAll()
832 self.config_service._UpdateApprovals(self.cnxn, config)
833 self.mox.VerifyAll()
834
835 def testUpdateConfig(self):
836 pass # TODO(jrobbins): add a test for this
837
838 def SetUpExpungeConfig(self, project_id):
839 self.config_service.statusdef_tbl.Delete(self.cnxn, project_id=project_id)
840 self.config_service.labeldef_tbl.Delete(self.cnxn, project_id=project_id)
841 self.config_service.projectissueconfig_tbl.Delete(
842 self.cnxn, project_id=project_id)
843
844 self.config_service.config_2lc.InvalidateKeys(self.cnxn, [project_id])
845
846 def testExpungeConfig(self):
847 self.SetUpExpungeConfig(789)
848
849 self.mox.ReplayAll()
850 self.config_service.ExpungeConfig(self.cnxn, 789)
851 self.mox.VerifyAll()
852
853 def testExpungeUsersInConfigs(self):
854
855 self.config_service.component2admin_tbl.Delete = mock.Mock()
856 self.config_service.component2cc_tbl.Delete = mock.Mock()
857 self.config_service.componentdef_tbl.Update = mock.Mock()
858
859 self.config_service.fielddef2admin_tbl.Delete = mock.Mock()
860 self.config_service.fielddef2editor_tbl.Delete = mock.Mock()
861 self.config_service.approvaldef2approver_tbl.Delete = mock.Mock()
862
863 user_ids = [111, 222, 333]
864 self.config_service.ExpungeUsersInConfigs(self.cnxn, user_ids, limit=50)
865
866 self.config_service.component2admin_tbl.Delete.assert_called_once_with(
867 self.cnxn, admin_id=user_ids, commit=False, limit=50)
868 self.config_service.component2cc_tbl.Delete.assert_called_once_with(
869 self.cnxn, cc_id=user_ids, commit=False, limit=50)
870 cdef_calls = [
871 mock.call(
872 self.cnxn, {'creator_id': framework_constants.DELETED_USER_ID},
873 creator_id=user_ids, commit=False, limit=50),
874 mock.call(
875 self.cnxn, {'modifier_id': framework_constants.DELETED_USER_ID},
876 modifier_id=user_ids, commit=False, limit=50)]
877 self.config_service.componentdef_tbl.Update.assert_has_calls(cdef_calls)
878
879 self.config_service.fielddef2admin_tbl.Delete.assert_called_once_with(
880 self.cnxn, admin_id=user_ids, commit=False, limit=50)
881 self.config_service.fielddef2editor_tbl.Delete.assert_called_once_with(
882 self.cnxn, editor_id=user_ids, commit=False, limit=50)
883 self.config_service.approvaldef2approver_tbl.Delete.assert_called_once_with(
884 self.cnxn, approver_id=user_ids, commit=False, limit=50)
885
886 ### Custom field definitions
887
888 def SetUpCreateFieldDef(self, project_id):
889 self.config_service.fielddef_tbl.InsertRow(
890 self.cnxn,
891 project_id=project_id,
892 field_name='PercentDone',
893 field_type='int_type',
894 applicable_type='Defect',
895 applicable_predicate='',
896 is_required=False,
897 is_multivalued=False,
898 is_niche=False,
899 min_value=1,
900 max_value=100,
901 regex=None,
902 needs_member=None,
903 needs_perm=None,
904 grants_perm=None,
905 notify_on='never',
906 date_action='no_action',
907 docstring='doc',
908 approval_id=None,
909 is_phase_field=False,
910 is_restricted_field=True,
911 commit=False).AndReturn(1)
912 self.config_service.fielddef2admin_tbl.InsertRows(
913 self.cnxn, config_svc.FIELDDEF2ADMIN_COLS, [(1, 111)], commit=False)
914 self.config_service.fielddef2editor_tbl.InsertRows(
915 self.cnxn, config_svc.FIELDDEF2EDITOR_COLS, [(1, 222)], commit=False)
916 self.cnxn.Commit()
917
918 def testCreateFieldDef(self):
919 self.SetUpCreateFieldDef(789)
920
921 self.mox.ReplayAll()
922 field_id = self.config_service.CreateFieldDef(
923 self.cnxn,
924 789,
925 'PercentDone',
926 'int_type',
927 'Defect',
928 '',
929 False,
930 False,
931 False,
932 1,
933 100,
934 None,
935 None,
936 None,
937 None,
938 0,
939 'no_action',
940 'doc', [111], [222],
941 is_restricted_field=True)
942 self.mox.VerifyAll()
943 self.assertEqual(1, field_id)
944
945 def SetUpSoftDeleteFieldDefs(self, field_ids):
946 self.config_service.fielddef_tbl.Update(
947 self.cnxn, {'is_deleted': True}, id=field_ids)
948
949 def testSoftDeleteFieldDefs(self):
950 self.SetUpSoftDeleteFieldDefs([1])
951
952 self.mox.ReplayAll()
953 self.config_service.SoftDeleteFieldDefs(self.cnxn, 789, [1])
954 self.mox.VerifyAll()
955
956 def SetUpUpdateFieldDef(self, field_id, new_values, admin_rows, editor_rows):
957 self.config_service.fielddef_tbl.Update(
958 self.cnxn, new_values, id=field_id, commit=False)
959 self.config_service.fielddef2admin_tbl.Delete(
960 self.cnxn, field_id=field_id, commit=False)
961 self.config_service.fielddef2admin_tbl.InsertRows(
962 self.cnxn, config_svc.FIELDDEF2ADMIN_COLS, admin_rows, commit=False)
963 self.config_service.fielddef2editor_tbl.Delete(
964 self.cnxn, field_id=field_id, commit=False)
965 self.config_service.fielddef2editor_tbl.InsertRows(
966 self.cnxn, config_svc.FIELDDEF2EDITOR_COLS, editor_rows, commit=False)
967 self.cnxn.Commit()
968
969 def testUpdateFieldDef_NoOp(self):
970 new_values = {}
971 self.SetUpUpdateFieldDef(1, new_values, [], [])
972
973 self.mox.ReplayAll()
974 self.config_service.UpdateFieldDef(
975 self.cnxn, 789, 1, admin_ids=[], editor_ids=[])
976 self.mox.VerifyAll()
977
978 def testUpdateFieldDef_Normal(self):
979 new_values = dict(
980 field_name='newname',
981 applicable_type='defect',
982 applicable_predicate='pri:1',
983 is_required=True,
984 is_niche=True,
985 is_multivalued=True,
986 min_value=32,
987 max_value=212,
988 regex='a.*b',
989 needs_member=True,
990 needs_perm='EditIssue',
991 grants_perm='DeleteIssue',
992 notify_on='any_comment',
993 docstring='new doc',
994 is_restricted_field=True)
995 self.SetUpUpdateFieldDef(1, new_values, [(1, 111)], [(1, 222)])
996
997 self.mox.ReplayAll()
998 new_values = new_values.copy()
999 new_values['notify_on'] = 1
1000 self.config_service.UpdateFieldDef(
1001 self.cnxn, 789, 1, admin_ids=[111], editor_ids=[222], **new_values)
1002 self.mox.VerifyAll()
1003
1004 ### Component definitions
1005
1006 def SetUpFindMatchingComponentIDsAnyProject(self, _exact, rows):
1007 # TODO(jrobbins): more details here.
1008 self.config_service.componentdef_tbl.Select(
1009 self.cnxn, cols=['id'], where=mox.IsA(list)).AndReturn(rows)
1010
1011 def testFindMatchingComponentIDsAnyProject_Rooted(self):
1012 self.SetUpFindMatchingComponentIDsAnyProject(True, [(1,), (2,), (3,)])
1013
1014 self.mox.ReplayAll()
1015 comp_ids = self.config_service.FindMatchingComponentIDsAnyProject(
1016 self.cnxn, ['WindowManager', 'NetworkLayer'])
1017 self.mox.VerifyAll()
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001018 six.assertCountEqual(self, [1, 2, 3], comp_ids)
Copybara854996b2021-09-07 19:36:02 +00001019
1020 def testFindMatchingComponentIDsAnyProject_NonRooted(self):
1021 self.SetUpFindMatchingComponentIDsAnyProject(False, [(1,), (2,), (3,)])
1022
1023 self.mox.ReplayAll()
1024 comp_ids = self.config_service.FindMatchingComponentIDsAnyProject(
1025 self.cnxn, ['WindowManager', 'NetworkLayer'], exact=False)
1026 self.mox.VerifyAll()
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001027 six.assertCountEqual(self, [1, 2, 3], comp_ids)
Copybara854996b2021-09-07 19:36:02 +00001028
1029 def SetUpCreateComponentDef(self, comp_id):
1030 self.config_service.componentdef_tbl.InsertRow(
1031 self.cnxn, project_id=789, path='WindowManager',
1032 docstring='doc', deprecated=False, commit=False,
1033 created=0, creator_id=0).AndReturn(comp_id)
1034 self.config_service.component2admin_tbl.InsertRows(
1035 self.cnxn, config_svc.COMPONENT2ADMIN_COLS, [], commit=False)
1036 self.config_service.component2cc_tbl.InsertRows(
1037 self.cnxn, config_svc.COMPONENT2CC_COLS, [], commit=False)
1038 self.config_service.component2label_tbl.InsertRows(
1039 self.cnxn, config_svc.COMPONENT2LABEL_COLS, [], commit=False)
1040 self.cnxn.Commit()
1041
1042 def testCreateComponentDef(self):
1043 self.SetUpCreateComponentDef(1)
1044
1045 self.mox.ReplayAll()
1046 comp_id = self.config_service.CreateComponentDef(
1047 self.cnxn, 789, 'WindowManager', 'doc', False, [], [], 0, 0, [])
1048 self.mox.VerifyAll()
1049 self.assertEqual(1, comp_id)
1050
1051 def SetUpUpdateComponentDef(self, component_id):
1052 self.config_service.component2admin_tbl.Delete(
1053 self.cnxn, component_id=component_id, commit=False)
1054 self.config_service.component2admin_tbl.InsertRows(
1055 self.cnxn, config_svc.COMPONENT2ADMIN_COLS, [], commit=False)
1056 self.config_service.component2cc_tbl.Delete(
1057 self.cnxn, component_id=component_id, commit=False)
1058 self.config_service.component2cc_tbl.InsertRows(
1059 self.cnxn, config_svc.COMPONENT2CC_COLS, [], commit=False)
1060 self.config_service.component2label_tbl.Delete(
1061 self.cnxn, component_id=component_id, commit=False)
1062 self.config_service.component2label_tbl.InsertRows(
1063 self.cnxn, config_svc.COMPONENT2LABEL_COLS, [], commit=False)
1064
1065 self.config_service.componentdef_tbl.Update(
1066 self.cnxn,
1067 {'path': 'DisplayManager', 'docstring': 'doc', 'deprecated': True},
1068 id=component_id, commit=False)
1069 self.cnxn.Commit()
1070
1071 def testUpdateComponentDef(self):
1072 self.SetUpUpdateComponentDef(1)
1073
1074 self.mox.ReplayAll()
1075 self.config_service.UpdateComponentDef(
1076 self.cnxn, 789, 1, path='DisplayManager', docstring='doc',
1077 deprecated=True, admin_ids=[], cc_ids=[], label_ids=[])
1078 self.mox.VerifyAll()
1079
1080 def SetUpSoftDeleteComponentDef(self, component_id):
1081 self.config_service.componentdef_tbl.Update(
1082 self.cnxn, {'is_deleted': True}, commit=False, id=component_id)
1083 self.cnxn.Commit()
1084
1085 def testSoftDeleteComponentDef(self):
1086 self.SetUpSoftDeleteComponentDef(1)
1087
1088 self.mox.ReplayAll()
1089 self.config_service.DeleteComponentDef(self.cnxn, 789, 1)
1090 self.mox.VerifyAll()
1091
1092 ### Memcache management
1093
1094 def testInvalidateMemcache(self):
1095 pass # TODO(jrobbins): write this
1096
1097 def testInvalidateMemcacheShards(self):
1098 NOW = 1234567
1099 memcache.set('789;1', NOW)
1100 memcache.set('789;2', NOW - 1000)
1101 memcache.set('789;3', NOW - 2000)
1102 memcache.set('all;1', NOW)
1103 memcache.set('all;2', NOW - 1000)
1104 memcache.set('all;3', NOW - 2000)
1105
1106 # Delete some of them.
1107 self.config_service._InvalidateMemcacheShards(
1108 [(789, 1), (789, 2), (789,9)])
1109
1110 self.assertIsNone(memcache.get('789;1'))
1111 self.assertIsNone(memcache.get('789;2'))
1112 self.assertEqual(NOW - 2000, memcache.get('789;3'))
1113 self.assertIsNone(memcache.get('all;1'))
1114 self.assertIsNone(memcache.get('all;2'))
1115 self.assertEqual(NOW - 2000, memcache.get('all;3'))
1116
1117 def testInvalidateMemcacheForEntireProject(self):
1118 NOW = 1234567
1119 memcache.set('789;1', NOW)
1120 memcache.set('config:789', 'serialized config')
1121 memcache.set('label_rows:789', 'serialized label rows')
1122 memcache.set('status_rows:789', 'serialized status rows')
1123 memcache.set('field_rows:789', 'serialized field rows')
1124 memcache.set('890;1', NOW) # Other projects will not be affected.
1125
1126 self.config_service.InvalidateMemcacheForEntireProject(789)
1127
1128 self.assertIsNone(memcache.get('789;1'))
1129 self.assertIsNone(memcache.get('config:789'))
1130 self.assertIsNone(memcache.get('status_rows:789'))
1131 self.assertIsNone(memcache.get('label_rows:789'))
1132 self.assertIsNone(memcache.get('field_rows:789'))
1133 self.assertEqual(NOW, memcache.get('890;1'))
1134
1135 def testUsersInvolvedInConfig_Empty(self):
1136 templates = []
1137 config = tracker_pb2.ProjectIssueConfig()
1138 self.assertEqual(set(), self.config_service.UsersInvolvedInConfig(
1139 config, templates))
1140
1141 def testUsersInvolvedInConfig_Default(self):
1142 templates = [
1143 tracker_bizobj.ConvertDictToTemplate(t)
1144 for t in tracker_constants.DEFAULT_TEMPLATES]
1145 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
1146 self.assertEqual(set(), self.config_service.UsersInvolvedInConfig(
1147 config, templates))
1148
1149 def testUsersInvolvedInConfig_Normal(self):
1150 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
1151 templates = [
1152 tracker_bizobj.ConvertDictToTemplate(t)
1153 for t in tracker_constants.DEFAULT_TEMPLATES]
1154 templates[0].owner_id = 111
1155 templates[0].admin_ids = [111, 222]
1156 config.field_defs = [
1157 tracker_pb2.FieldDef(admin_ids=[333], editor_ids=[444])
1158 ]
1159 actual = self.config_service.UsersInvolvedInConfig(config, templates)
1160 self.assertEqual({111, 222, 333, 444}, actual)