blob: dd2796c9387126709cb91cf994ea91e426c2b244 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 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 config_svc module."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import re
12import unittest
13import logging
14import mock
15
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
27from proto import tracker_pb2
28from 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)
223 self.assertItemsEqual([789], list(config_dict.keys()))
224 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()
283 self.assertItemsEqual(keys, list(config_dict.keys()))
284
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()
291 self.assertItemsEqual(keys, list(config_dict.keys()))
292
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
444 def testLookupLabelIDs_Hit(self):
445 label_dicts = {1: 'Security', 2: 'UX'}, {'security': 1, 'ux': 2}
446 self.config_service.label_cache.CacheItem(789, label_dicts)
447 # No mock calls set up because none are needed.
448 self.mox.ReplayAll()
449 self.assertEqual(
450 [1, 2],
451 self.config_service.LookupLabelIDs(self.cnxn, 789, ['Security', 'UX']))
452 self.mox.VerifyAll()
453
454 def testLookupIDsOfLabelsMatching_Hit(self):
455 label_dicts = {1: 'Security', 2: 'UX'}, {'security': 1, 'ux': 2}
456 self.config_service.label_cache.CacheItem(789, label_dicts)
457 # No mock calls set up because none are needed.
458 self.mox.ReplayAll()
459 self.assertItemsEqual(
460 [1],
461 self.config_service.LookupIDsOfLabelsMatching(
462 self.cnxn, 789, re.compile('Sec.*')))
463 self.assertItemsEqual(
464 [1, 2],
465 self.config_service.LookupIDsOfLabelsMatching(
466 self.cnxn, 789, re.compile('.*')))
467 self.assertItemsEqual(
468 [],
469 self.config_service.LookupIDsOfLabelsMatching(
470 self.cnxn, 789, re.compile('Zzzzz.*')))
471 self.mox.VerifyAll()
472
473 def SetUpLookupLabelIDsAnyProject(self, label, id_rows):
474 self.config_service.labeldef_tbl.Select(
475 self.cnxn, cols=['id'], label=label).AndReturn(id_rows)
476
477 def testLookupLabelIDsAnyProject(self):
478 self.SetUpLookupLabelIDsAnyProject('Security', [(1,)])
479 self.mox.ReplayAll()
480 actual = self.config_service.LookupLabelIDsAnyProject(
481 self.cnxn, 'Security')
482 self.mox.VerifyAll()
483 self.assertEqual([1], actual)
484
485 def SetUpLookupIDsOfLabelsMatchingAnyProject(self, id_label_rows):
486 self.config_service.labeldef_tbl.Select(
487 self.cnxn, cols=['id', 'label']).AndReturn(id_label_rows)
488
489 def testLookupIDsOfLabelsMatchingAnyProject(self):
490 id_label_rows = [(1, 'Security'), (2, 'UX')]
491 self.SetUpLookupIDsOfLabelsMatchingAnyProject(id_label_rows)
492 self.mox.ReplayAll()
493 actual = self.config_service.LookupIDsOfLabelsMatchingAnyProject(
494 self.cnxn, re.compile('(Sec|Zzz).*'))
495 self.mox.VerifyAll()
496 self.assertEqual([1], actual)
497
498 ### Status lookups
499
500 def testGetStatusDefRows(self):
501 rows = 'foo'
502 self.config_service.status_row_2lc.CacheItem(789, rows)
503 actual = self.config_service.GetStatusDefRows(self.cnxn, 789)
504 self.assertEqual(rows, actual)
505
506 def SetUpGetStatusDefRowsAnyProject(self, rows):
507 self.config_service.statusdef_tbl.Select(
508 self.cnxn, cols=config_svc.STATUSDEF_COLS,
509 order_by=[('rank DESC', []), ('status DESC', [])]).AndReturn(
510 rows)
511
512 def testGetStatusDefRowsAnyProject(self):
513 rows = 'foo'
514 self.SetUpGetStatusDefRowsAnyProject(rows)
515 self.mox.ReplayAll()
516 actual = self.config_service.GetStatusDefRowsAnyProject(self.cnxn)
517 self.mox.VerifyAll()
518 self.assertEqual(rows, actual)
519
520 def testDeserializeStatuses(self):
521 statusdef_rows = [(1, 789, 1, 'New', True, 'doc', False),
522 (2, 789, 2, 'Fixed', False, 'doc', True)]
523 actual = self.config_service._DeserializeStatuses(statusdef_rows)
524 id_to_name, name_to_id, closed_ids = actual
525 self.assertEqual({1: 'New', 2: 'Fixed'}, id_to_name)
526 self.assertEqual({'new': 1, 'fixed': 2}, name_to_id)
527 self.assertEqual([2], closed_ids)
528
529 def testEnsureStatusCacheEntry_Hit(self):
530 status_dicts = 'foo'
531 self.config_service.status_cache.CacheItem(789, status_dicts)
532 # No mock calls set up because none are needed.
533 self.mox.ReplayAll()
534 self.config_service._EnsureStatusCacheEntry(self.cnxn, 789)
535 self.mox.VerifyAll()
536
537 def SetUpEnsureStatusCacheEntry_Miss(self, keys, rows):
538 self.config_service.statusdef_tbl.Select(
539 self.cnxn, cols=config_svc.STATUSDEF_COLS, project_id=keys,
540 order_by=[('rank DESC', []), ('status DESC', [])]).AndReturn(
541 rows)
542
543 def testEnsureStatusCacheEntry_Miss(self):
544 statusdef_rows = [(1, 789, 1, 'New', True, 'doc', False),
545 (2, 789, 2, 'Fixed', False, 'doc', True)]
546 self.SetUpEnsureStatusCacheEntry_Miss([789], statusdef_rows)
547 self.mox.ReplayAll()
548 self.config_service._EnsureStatusCacheEntry(self.cnxn, 789)
549 self.mox.VerifyAll()
550 status_dicts = {1: 'New', 2: 'Fixed'}, {'new': 1, 'fixed': 2}, [2]
551 self.assertEqual(
552 status_dicts, self.config_service.status_cache.GetItem(789))
553
554 def testLookupStatus_Hit(self):
555 status_dicts = {1: 'New', 2: 'Fixed'}, {'new': 1, 'fixed': 2}, [2]
556 self.config_service.status_cache.CacheItem(789, status_dicts)
557 # No mock calls set up because none are needed.
558 self.mox.ReplayAll()
559 self.assertEqual(
560 'New', self.config_service.LookupStatus(self.cnxn, 789, 1))
561 self.assertEqual(
562 'Fixed', self.config_service.LookupStatus(self.cnxn, 789, 2))
563 self.mox.VerifyAll()
564
565 def testLookupStatusID_Hit(self):
566 status_dicts = {1: 'New', 2: 'Fixed'}, {'new': 1, 'fixed': 2}, [2]
567 self.config_service.status_cache.CacheItem(789, status_dicts)
568 # No mock calls set up because none are needed.
569 self.mox.ReplayAll()
570 self.assertEqual(
571 1, self.config_service.LookupStatusID(self.cnxn, 789, 'New'))
572 self.assertEqual(
573 2, self.config_service.LookupStatusID(self.cnxn, 789, 'Fixed'))
574 self.mox.VerifyAll()
575
576 def testLookupStatusIDs_Hit(self):
577 status_dicts = {1: 'New', 2: 'Fixed'}, {'new': 1, 'fixed': 2}, [2]
578 self.config_service.status_cache.CacheItem(789, status_dicts)
579 # No mock calls set up because none are needed.
580 self.mox.ReplayAll()
581 self.assertEqual(
582 [1, 2],
583 self.config_service.LookupStatusIDs(self.cnxn, 789, ['New', 'Fixed']))
584 self.mox.VerifyAll()
585
586 def testLookupClosedStatusIDs_Hit(self):
587 status_dicts = {1: 'New', 2: 'Fixed'}, {'new': 1, 'fixed': 2}, [2]
588 self.config_service.status_cache.CacheItem(789, status_dicts)
589 # No mock calls set up because none are needed.
590 self.mox.ReplayAll()
591 self.assertEqual(
592 [2],
593 self.config_service.LookupClosedStatusIDs(self.cnxn, 789))
594 self.mox.VerifyAll()
595
596 def SetUpLookupClosedStatusIDsAnyProject(self, id_rows):
597 self.config_service.statusdef_tbl.Select(
598 self.cnxn, cols=['id'], means_open=False).AndReturn(
599 id_rows)
600
601 def testLookupClosedStatusIDsAnyProject(self):
602 self.SetUpLookupClosedStatusIDsAnyProject([(2,)])
603 self.mox.ReplayAll()
604 actual = self.config_service.LookupClosedStatusIDsAnyProject(self.cnxn)
605 self.mox.VerifyAll()
606 self.assertEqual([2], actual)
607
608 def SetUpLookupStatusIDsAnyProject(self, status, id_rows):
609 self.config_service.statusdef_tbl.Select(
610 self.cnxn, cols=['id'], status=status).AndReturn(id_rows)
611
612 def testLookupStatusIDsAnyProject(self):
613 self.SetUpLookupStatusIDsAnyProject('New', [(1,)])
614 self.mox.ReplayAll()
615 actual = self.config_service.LookupStatusIDsAnyProject(self.cnxn, 'New')
616 self.mox.VerifyAll()
617 self.assertEqual([1], actual)
618
619 ### Issue tracker configuration objects
620
621 def SetUpGetProjectConfigs(self, project_ids):
622 self.config_service.projectissueconfig_tbl.Select(
623 self.cnxn, cols=config_svc.PROJECTISSUECONFIG_COLS,
624 project_id=project_ids).AndReturn([])
625 self.config_service.statusdef_tbl.Select(
626 self.cnxn, cols=config_svc.STATUSDEF_COLS,
627 project_id=project_ids, where=[('rank IS NOT NULL', [])],
628 order_by=[('rank', [])]).AndReturn([])
629 self.config_service.labeldef_tbl.Select(
630 self.cnxn, cols=config_svc.LABELDEF_COLS,
631 project_id=project_ids, where=[('rank IS NOT NULL', [])],
632 order_by=[('rank', [])]).AndReturn([])
633 self.config_service.approvaldef2approver_tbl.Select(
634 self.cnxn, cols=config_svc.APPROVALDEF2APPROVER_COLS,
635 project_id=project_ids).AndReturn([])
636 self.config_service.approvaldef2survey_tbl.Select(
637 self.cnxn, cols=config_svc.APPROVALDEF2SURVEY_COLS,
638 project_id=project_ids).AndReturn([])
639 self.config_service.fielddef_tbl.Select(
640 self.cnxn, cols=config_svc.FIELDDEF_COLS,
641 project_id=project_ids, order_by=[('field_name', [])]).AndReturn([])
642 self.config_service.componentdef_tbl.Select(
643 self.cnxn, cols=config_svc.COMPONENTDEF_COLS,
644 is_deleted=False,
645 project_id=project_ids, order_by=[('path', [])]).AndReturn([])
646
647 def testGetProjectConfigs(self):
648 project_ids = [789, 679]
649 self.SetUpGetProjectConfigs(project_ids)
650
651 self.mox.ReplayAll()
652 config_dict = self.config_service.GetProjectConfigs(
653 self.cnxn, [789, 679], use_cache=False)
654 self.assertEqual(2, len(config_dict))
655 for pid in project_ids:
656 self.assertEqual(pid, config_dict[pid].project_id)
657 self.mox.VerifyAll()
658
659 def testGetProjectConfig_Hit(self):
660 project_id = 789
661 config = tracker_bizobj.MakeDefaultProjectIssueConfig(project_id)
662 self.config_service.config_2lc.CacheItem(project_id, config)
663
664 self.mox.ReplayAll()
665 actual = self.config_service.GetProjectConfig(self.cnxn, project_id)
666 self.assertEqual(config, actual)
667 self.mox.VerifyAll()
668
669 def testGetProjectConfig_Miss(self):
670 project_id = 789
671 self.SetUpGetProjectConfigs([project_id])
672
673 self.mox.ReplayAll()
674 config = self.config_service.GetProjectConfig(self.cnxn, project_id)
675 self.assertEqual(project_id, config.project_id)
676 self.mox.VerifyAll()
677
678 def SetUpStoreConfig_Default(self, project_id):
679 self.config_service.projectissueconfig_tbl.InsertRow(
680 self.cnxn, replace=True,
681 project_id=project_id,
682 statuses_offer_merge='Duplicate',
683 exclusive_label_prefixes='Type Priority Milestone',
684 default_template_for_developers=0,
685 default_template_for_users=0,
686 default_col_spec=tracker_constants.DEFAULT_COL_SPEC,
687 default_sort_spec='',
688 default_x_attr='',
689 default_y_attr='',
690 member_default_query='',
691 custom_issue_entry_url=None,
692 commit=False)
693
694 self.SetUpUpdateWellKnownLabels_Default(project_id)
695 self.SetUpUpdateWellKnownStatuses_Default(project_id)
696 self.cnxn.Commit()
697
698 def SetUpUpdateWellKnownLabels_JustCache(self, project_id):
699 by_id = {
700 idx + 1: label for idx, (label, _, _) in enumerate(
701 tracker_constants.DEFAULT_WELL_KNOWN_LABELS)}
702 by_name = {name.lower(): label_id
703 for label_id, name in by_id.items()}
704 label_dicts = by_id, by_name
705 self.config_service.label_cache.CacheAll({project_id: label_dicts})
706
707 def SetUpUpdateWellKnownLabels_Default(self, project_id):
708 self.SetUpUpdateWellKnownLabels_JustCache(project_id)
709 update_labeldef_rows = [
710 (idx + 1, project_id, idx, label, doc, deprecated)
711 for idx, (label, doc, deprecated) in enumerate(
712 tracker_constants.DEFAULT_WELL_KNOWN_LABELS)]
713 self.config_service.labeldef_tbl.Update(
714 self.cnxn, {'rank': None}, project_id=project_id, commit=False)
715 self.config_service.labeldef_tbl.InsertRows(
716 self.cnxn, config_svc.LABELDEF_COLS, update_labeldef_rows,
717 replace=True, commit=False)
718 self.config_service.labeldef_tbl.InsertRows(
719 self.cnxn, config_svc.LABELDEF_COLS[1:], [], commit=False)
720
721 def SetUpUpdateWellKnownStatuses_Default(self, project_id):
722 by_id = {
723 idx + 1: status for idx, (status, _, _, _) in enumerate(
724 tracker_constants.DEFAULT_WELL_KNOWN_STATUSES)}
725 by_name = {name.lower(): label_id
726 for label_id, name in by_id.items()}
727 closed_ids = [
728 idx + 1 for idx, (_, _, means_open, _) in enumerate(
729 tracker_constants.DEFAULT_WELL_KNOWN_STATUSES)
730 if not means_open]
731 status_dicts = by_id, by_name, closed_ids
732 self.config_service.status_cache.CacheAll({789: status_dicts})
733
734 update_statusdef_rows = [
735 (idx + 1, project_id, idx, status, means_open, doc, deprecated)
736 for idx, (status, doc, means_open, deprecated) in enumerate(
737 tracker_constants.DEFAULT_WELL_KNOWN_STATUSES)]
738 self.config_service.statusdef_tbl.Update(
739 self.cnxn, {'rank': None}, project_id=project_id, commit=False)
740 self.config_service.statusdef_tbl.InsertRows(
741 self.cnxn, config_svc.STATUSDEF_COLS, update_statusdef_rows,
742 replace=True, commit=False)
743 self.config_service.statusdef_tbl.InsertRows(
744 self.cnxn, config_svc.STATUSDEF_COLS[1:], [], commit=False)
745
746 def SetUpUpdateApprovals_Default(
747 self, approval_id, approver_rows, survey_row):
748 self.config_service.approvaldef2approver_tbl.Delete(
749 self.cnxn, approval_id=approval_id, commit=False)
750
751 self.config_service.approvaldef2approver_tbl.InsertRows(
752 self.cnxn,
753 config_svc.APPROVALDEF2APPROVER_COLS,
754 approver_rows,
755 commit=False)
756
757 approval_id, survey, project_id = survey_row
758 self.config_service.approvaldef2survey_tbl.Delete(
759 self.cnxn, approval_id=approval_id, commit=False)
760 self.config_service.approvaldef2survey_tbl.InsertRow(
761 self.cnxn,
762 approval_id=approval_id,
763 survey=survey,
764 project_id=project_id,
765 commit=False)
766
767 def testStoreConfig(self):
768 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
769 self.SetUpStoreConfig_Default(789)
770
771 self.mox.ReplayAll()
772 self.config_service.StoreConfig(self.cnxn, config)
773 self.mox.VerifyAll()
774
775 def testUpdateWellKnownLabels(self):
776 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
777 self.SetUpUpdateWellKnownLabels_Default(789)
778
779 self.mox.ReplayAll()
780 self.config_service._UpdateWellKnownLabels(self.cnxn, config)
781 self.mox.VerifyAll()
782
783 def testUpdateWellKnownLabels_Duplicate(self):
784 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
785 config.well_known_labels.append(config.well_known_labels[0])
786 self.SetUpUpdateWellKnownLabels_JustCache(789)
787
788 self.mox.ReplayAll()
789 with self.assertRaises(exceptions.InputException) as cm:
790 self.config_service._UpdateWellKnownLabels(self.cnxn, config)
791 self.mox.VerifyAll()
792 self.assertEqual(
793 'Defined label "Type-Defect" twice',
794 cm.exception.message)
795
796 def testUpdateWellKnownStatuses(self):
797 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
798 self.SetUpUpdateWellKnownStatuses_Default(789)
799
800 self.mox.ReplayAll()
801 self.config_service._UpdateWellKnownStatuses(self.cnxn, config)
802 self.mox.VerifyAll()
803
804 def testUpdateApprovals(self):
805 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
806 approver_rows = [(123, 111, 789), (123, 222, 789)]
807 survey_row = (123, 'Q1\nQ2', 789)
808 first_approval = tracker_bizobj.MakeFieldDef(
809 123, 789, 'FirstApproval', tracker_pb2.FieldTypes.APPROVAL_TYPE,
810 None, '', False, False, False, None, None, '', False, '', '',
811 tracker_pb2.NotifyTriggers.NEVER, 'no_action', 'the first one', False)
812 config.field_defs = [first_approval]
813 config.approval_defs = [tracker_pb2.ApprovalDef(
814 approval_id=123, approver_ids=[111, 222], survey='Q1\nQ2')]
815 self.SetUpUpdateApprovals_Default(123, approver_rows, survey_row)
816
817 self.mox.ReplayAll()
818 self.config_service._UpdateApprovals(self.cnxn, config)
819 self.mox.VerifyAll()
820
821 def testUpdateConfig(self):
822 pass # TODO(jrobbins): add a test for this
823
824 def SetUpExpungeConfig(self, project_id):
825 self.config_service.statusdef_tbl.Delete(self.cnxn, project_id=project_id)
826 self.config_service.labeldef_tbl.Delete(self.cnxn, project_id=project_id)
827 self.config_service.projectissueconfig_tbl.Delete(
828 self.cnxn, project_id=project_id)
829
830 self.config_service.config_2lc.InvalidateKeys(self.cnxn, [project_id])
831
832 def testExpungeConfig(self):
833 self.SetUpExpungeConfig(789)
834
835 self.mox.ReplayAll()
836 self.config_service.ExpungeConfig(self.cnxn, 789)
837 self.mox.VerifyAll()
838
839 def testExpungeUsersInConfigs(self):
840
841 self.config_service.component2admin_tbl.Delete = mock.Mock()
842 self.config_service.component2cc_tbl.Delete = mock.Mock()
843 self.config_service.componentdef_tbl.Update = mock.Mock()
844
845 self.config_service.fielddef2admin_tbl.Delete = mock.Mock()
846 self.config_service.fielddef2editor_tbl.Delete = mock.Mock()
847 self.config_service.approvaldef2approver_tbl.Delete = mock.Mock()
848
849 user_ids = [111, 222, 333]
850 self.config_service.ExpungeUsersInConfigs(self.cnxn, user_ids, limit=50)
851
852 self.config_service.component2admin_tbl.Delete.assert_called_once_with(
853 self.cnxn, admin_id=user_ids, commit=False, limit=50)
854 self.config_service.component2cc_tbl.Delete.assert_called_once_with(
855 self.cnxn, cc_id=user_ids, commit=False, limit=50)
856 cdef_calls = [
857 mock.call(
858 self.cnxn, {'creator_id': framework_constants.DELETED_USER_ID},
859 creator_id=user_ids, commit=False, limit=50),
860 mock.call(
861 self.cnxn, {'modifier_id': framework_constants.DELETED_USER_ID},
862 modifier_id=user_ids, commit=False, limit=50)]
863 self.config_service.componentdef_tbl.Update.assert_has_calls(cdef_calls)
864
865 self.config_service.fielddef2admin_tbl.Delete.assert_called_once_with(
866 self.cnxn, admin_id=user_ids, commit=False, limit=50)
867 self.config_service.fielddef2editor_tbl.Delete.assert_called_once_with(
868 self.cnxn, editor_id=user_ids, commit=False, limit=50)
869 self.config_service.approvaldef2approver_tbl.Delete.assert_called_once_with(
870 self.cnxn, approver_id=user_ids, commit=False, limit=50)
871
872 ### Custom field definitions
873
874 def SetUpCreateFieldDef(self, project_id):
875 self.config_service.fielddef_tbl.InsertRow(
876 self.cnxn,
877 project_id=project_id,
878 field_name='PercentDone',
879 field_type='int_type',
880 applicable_type='Defect',
881 applicable_predicate='',
882 is_required=False,
883 is_multivalued=False,
884 is_niche=False,
885 min_value=1,
886 max_value=100,
887 regex=None,
888 needs_member=None,
889 needs_perm=None,
890 grants_perm=None,
891 notify_on='never',
892 date_action='no_action',
893 docstring='doc',
894 approval_id=None,
895 is_phase_field=False,
896 is_restricted_field=True,
897 commit=False).AndReturn(1)
898 self.config_service.fielddef2admin_tbl.InsertRows(
899 self.cnxn, config_svc.FIELDDEF2ADMIN_COLS, [(1, 111)], commit=False)
900 self.config_service.fielddef2editor_tbl.InsertRows(
901 self.cnxn, config_svc.FIELDDEF2EDITOR_COLS, [(1, 222)], commit=False)
902 self.cnxn.Commit()
903
904 def testCreateFieldDef(self):
905 self.SetUpCreateFieldDef(789)
906
907 self.mox.ReplayAll()
908 field_id = self.config_service.CreateFieldDef(
909 self.cnxn,
910 789,
911 'PercentDone',
912 'int_type',
913 'Defect',
914 '',
915 False,
916 False,
917 False,
918 1,
919 100,
920 None,
921 None,
922 None,
923 None,
924 0,
925 'no_action',
926 'doc', [111], [222],
927 is_restricted_field=True)
928 self.mox.VerifyAll()
929 self.assertEqual(1, field_id)
930
931 def SetUpSoftDeleteFieldDefs(self, field_ids):
932 self.config_service.fielddef_tbl.Update(
933 self.cnxn, {'is_deleted': True}, id=field_ids)
934
935 def testSoftDeleteFieldDefs(self):
936 self.SetUpSoftDeleteFieldDefs([1])
937
938 self.mox.ReplayAll()
939 self.config_service.SoftDeleteFieldDefs(self.cnxn, 789, [1])
940 self.mox.VerifyAll()
941
942 def SetUpUpdateFieldDef(self, field_id, new_values, admin_rows, editor_rows):
943 self.config_service.fielddef_tbl.Update(
944 self.cnxn, new_values, id=field_id, commit=False)
945 self.config_service.fielddef2admin_tbl.Delete(
946 self.cnxn, field_id=field_id, commit=False)
947 self.config_service.fielddef2admin_tbl.InsertRows(
948 self.cnxn, config_svc.FIELDDEF2ADMIN_COLS, admin_rows, commit=False)
949 self.config_service.fielddef2editor_tbl.Delete(
950 self.cnxn, field_id=field_id, commit=False)
951 self.config_service.fielddef2editor_tbl.InsertRows(
952 self.cnxn, config_svc.FIELDDEF2EDITOR_COLS, editor_rows, commit=False)
953 self.cnxn.Commit()
954
955 def testUpdateFieldDef_NoOp(self):
956 new_values = {}
957 self.SetUpUpdateFieldDef(1, new_values, [], [])
958
959 self.mox.ReplayAll()
960 self.config_service.UpdateFieldDef(
961 self.cnxn, 789, 1, admin_ids=[], editor_ids=[])
962 self.mox.VerifyAll()
963
964 def testUpdateFieldDef_Normal(self):
965 new_values = dict(
966 field_name='newname',
967 applicable_type='defect',
968 applicable_predicate='pri:1',
969 is_required=True,
970 is_niche=True,
971 is_multivalued=True,
972 min_value=32,
973 max_value=212,
974 regex='a.*b',
975 needs_member=True,
976 needs_perm='EditIssue',
977 grants_perm='DeleteIssue',
978 notify_on='any_comment',
979 docstring='new doc',
980 is_restricted_field=True)
981 self.SetUpUpdateFieldDef(1, new_values, [(1, 111)], [(1, 222)])
982
983 self.mox.ReplayAll()
984 new_values = new_values.copy()
985 new_values['notify_on'] = 1
986 self.config_service.UpdateFieldDef(
987 self.cnxn, 789, 1, admin_ids=[111], editor_ids=[222], **new_values)
988 self.mox.VerifyAll()
989
990 ### Component definitions
991
992 def SetUpFindMatchingComponentIDsAnyProject(self, _exact, rows):
993 # TODO(jrobbins): more details here.
994 self.config_service.componentdef_tbl.Select(
995 self.cnxn, cols=['id'], where=mox.IsA(list)).AndReturn(rows)
996
997 def testFindMatchingComponentIDsAnyProject_Rooted(self):
998 self.SetUpFindMatchingComponentIDsAnyProject(True, [(1,), (2,), (3,)])
999
1000 self.mox.ReplayAll()
1001 comp_ids = self.config_service.FindMatchingComponentIDsAnyProject(
1002 self.cnxn, ['WindowManager', 'NetworkLayer'])
1003 self.mox.VerifyAll()
1004 self.assertItemsEqual([1, 2, 3], comp_ids)
1005
1006 def testFindMatchingComponentIDsAnyProject_NonRooted(self):
1007 self.SetUpFindMatchingComponentIDsAnyProject(False, [(1,), (2,), (3,)])
1008
1009 self.mox.ReplayAll()
1010 comp_ids = self.config_service.FindMatchingComponentIDsAnyProject(
1011 self.cnxn, ['WindowManager', 'NetworkLayer'], exact=False)
1012 self.mox.VerifyAll()
1013 self.assertItemsEqual([1, 2, 3], comp_ids)
1014
1015 def SetUpCreateComponentDef(self, comp_id):
1016 self.config_service.componentdef_tbl.InsertRow(
1017 self.cnxn, project_id=789, path='WindowManager',
1018 docstring='doc', deprecated=False, commit=False,
1019 created=0, creator_id=0).AndReturn(comp_id)
1020 self.config_service.component2admin_tbl.InsertRows(
1021 self.cnxn, config_svc.COMPONENT2ADMIN_COLS, [], commit=False)
1022 self.config_service.component2cc_tbl.InsertRows(
1023 self.cnxn, config_svc.COMPONENT2CC_COLS, [], commit=False)
1024 self.config_service.component2label_tbl.InsertRows(
1025 self.cnxn, config_svc.COMPONENT2LABEL_COLS, [], commit=False)
1026 self.cnxn.Commit()
1027
1028 def testCreateComponentDef(self):
1029 self.SetUpCreateComponentDef(1)
1030
1031 self.mox.ReplayAll()
1032 comp_id = self.config_service.CreateComponentDef(
1033 self.cnxn, 789, 'WindowManager', 'doc', False, [], [], 0, 0, [])
1034 self.mox.VerifyAll()
1035 self.assertEqual(1, comp_id)
1036
1037 def SetUpUpdateComponentDef(self, component_id):
1038 self.config_service.component2admin_tbl.Delete(
1039 self.cnxn, component_id=component_id, commit=False)
1040 self.config_service.component2admin_tbl.InsertRows(
1041 self.cnxn, config_svc.COMPONENT2ADMIN_COLS, [], commit=False)
1042 self.config_service.component2cc_tbl.Delete(
1043 self.cnxn, component_id=component_id, commit=False)
1044 self.config_service.component2cc_tbl.InsertRows(
1045 self.cnxn, config_svc.COMPONENT2CC_COLS, [], commit=False)
1046 self.config_service.component2label_tbl.Delete(
1047 self.cnxn, component_id=component_id, commit=False)
1048 self.config_service.component2label_tbl.InsertRows(
1049 self.cnxn, config_svc.COMPONENT2LABEL_COLS, [], commit=False)
1050
1051 self.config_service.componentdef_tbl.Update(
1052 self.cnxn,
1053 {'path': 'DisplayManager', 'docstring': 'doc', 'deprecated': True},
1054 id=component_id, commit=False)
1055 self.cnxn.Commit()
1056
1057 def testUpdateComponentDef(self):
1058 self.SetUpUpdateComponentDef(1)
1059
1060 self.mox.ReplayAll()
1061 self.config_service.UpdateComponentDef(
1062 self.cnxn, 789, 1, path='DisplayManager', docstring='doc',
1063 deprecated=True, admin_ids=[], cc_ids=[], label_ids=[])
1064 self.mox.VerifyAll()
1065
1066 def SetUpSoftDeleteComponentDef(self, component_id):
1067 self.config_service.componentdef_tbl.Update(
1068 self.cnxn, {'is_deleted': True}, commit=False, id=component_id)
1069 self.cnxn.Commit()
1070
1071 def testSoftDeleteComponentDef(self):
1072 self.SetUpSoftDeleteComponentDef(1)
1073
1074 self.mox.ReplayAll()
1075 self.config_service.DeleteComponentDef(self.cnxn, 789, 1)
1076 self.mox.VerifyAll()
1077
1078 ### Memcache management
1079
1080 def testInvalidateMemcache(self):
1081 pass # TODO(jrobbins): write this
1082
1083 def testInvalidateMemcacheShards(self):
1084 NOW = 1234567
1085 memcache.set('789;1', NOW)
1086 memcache.set('789;2', NOW - 1000)
1087 memcache.set('789;3', NOW - 2000)
1088 memcache.set('all;1', NOW)
1089 memcache.set('all;2', NOW - 1000)
1090 memcache.set('all;3', NOW - 2000)
1091
1092 # Delete some of them.
1093 self.config_service._InvalidateMemcacheShards(
1094 [(789, 1), (789, 2), (789,9)])
1095
1096 self.assertIsNone(memcache.get('789;1'))
1097 self.assertIsNone(memcache.get('789;2'))
1098 self.assertEqual(NOW - 2000, memcache.get('789;3'))
1099 self.assertIsNone(memcache.get('all;1'))
1100 self.assertIsNone(memcache.get('all;2'))
1101 self.assertEqual(NOW - 2000, memcache.get('all;3'))
1102
1103 def testInvalidateMemcacheForEntireProject(self):
1104 NOW = 1234567
1105 memcache.set('789;1', NOW)
1106 memcache.set('config:789', 'serialized config')
1107 memcache.set('label_rows:789', 'serialized label rows')
1108 memcache.set('status_rows:789', 'serialized status rows')
1109 memcache.set('field_rows:789', 'serialized field rows')
1110 memcache.set('890;1', NOW) # Other projects will not be affected.
1111
1112 self.config_service.InvalidateMemcacheForEntireProject(789)
1113
1114 self.assertIsNone(memcache.get('789;1'))
1115 self.assertIsNone(memcache.get('config:789'))
1116 self.assertIsNone(memcache.get('status_rows:789'))
1117 self.assertIsNone(memcache.get('label_rows:789'))
1118 self.assertIsNone(memcache.get('field_rows:789'))
1119 self.assertEqual(NOW, memcache.get('890;1'))
1120
1121 def testUsersInvolvedInConfig_Empty(self):
1122 templates = []
1123 config = tracker_pb2.ProjectIssueConfig()
1124 self.assertEqual(set(), self.config_service.UsersInvolvedInConfig(
1125 config, templates))
1126
1127 def testUsersInvolvedInConfig_Default(self):
1128 templates = [
1129 tracker_bizobj.ConvertDictToTemplate(t)
1130 for t in tracker_constants.DEFAULT_TEMPLATES]
1131 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
1132 self.assertEqual(set(), self.config_service.UsersInvolvedInConfig(
1133 config, templates))
1134
1135 def testUsersInvolvedInConfig_Normal(self):
1136 config = tracker_bizobj.MakeDefaultProjectIssueConfig(789)
1137 templates = [
1138 tracker_bizobj.ConvertDictToTemplate(t)
1139 for t in tracker_constants.DEFAULT_TEMPLATES]
1140 templates[0].owner_id = 111
1141 templates[0].admin_ids = [111, 222]
1142 config.field_defs = [
1143 tracker_pb2.FieldDef(admin_ids=[333], editor_ids=[444])
1144 ]
1145 actual = self.config_service.UsersInvolvedInConfig(config, templates)
1146 self.assertEqual({111, 222, 333, 444}, actual)