Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # 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 | """Tests for the reap module.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import unittest |
| 12 | |
| 13 | import mock |
| 14 | import mox |
| 15 | |
| 16 | from mock import Mock |
| 17 | |
| 18 | from framework import reap |
| 19 | from framework import sql |
| 20 | from proto import project_pb2 |
| 21 | from services import service_manager |
| 22 | from services import template_svc |
| 23 | from testing import fake |
| 24 | from testing import testing_helpers |
| 25 | |
| 26 | |
| 27 | class ReapTest(unittest.TestCase): |
| 28 | |
| 29 | def setUp(self): |
| 30 | self.project_service = fake.ProjectService() |
| 31 | self.issue_service = fake.IssueService() |
| 32 | self.issue_star_service = fake.IssueStarService() |
| 33 | self.config_service = fake.ConfigService() |
| 34 | self.features_service = fake.FeaturesService() |
| 35 | self.project_star_service = fake.ProjectStarService() |
| 36 | self.services = service_manager.Services( |
| 37 | project=self.project_service, |
| 38 | issue=self.issue_service, |
| 39 | issue_star=self.issue_star_service, |
| 40 | config=self.config_service, |
| 41 | features=self.features_service, |
| 42 | project_star=self.project_star_service, |
| 43 | template=Mock(spec=template_svc.TemplateService), |
| 44 | user=fake.UserService(), |
| 45 | usergroup=fake.UserGroupService()) |
| 46 | |
| 47 | self.proj1_id = 1001 |
| 48 | self.proj1_issue_id = 111 |
| 49 | self.proj1 = self.project_service.TestAddProject( |
| 50 | name='proj1', project_id=self.proj1_id) |
| 51 | self.proj2_id = 1002 |
| 52 | self.proj2_issue_id = 112 |
| 53 | self.proj2 = self.project_service.TestAddProject( |
| 54 | name='proj2', project_id=self.proj2_id) |
| 55 | |
| 56 | self.mox = mox.Mox() |
| 57 | self.cnxn = self.mox.CreateMock(sql.MonorailConnection) |
| 58 | self.project_service.project_tbl = self.mox.CreateMock(sql.SQLTableManager) |
| 59 | self.issue_service.issue_tbl = self.mox.CreateMock(sql.SQLTableManager) |
| 60 | |
| 61 | def tearDown(self): |
| 62 | self.mox.UnsetStubs() |
| 63 | self.mox.ResetAll() |
| 64 | |
| 65 | def setUpMarkDoomedProjects(self): |
| 66 | self.project_service.project_tbl.Select( |
| 67 | self.cnxn, cols=['project_id'], limit=1000, state='archived', |
| 68 | where=mox.IgnoreArg()).AndReturn([[self.proj1_id]]) |
| 69 | |
| 70 | def testMarkDoomedProjects(self): |
| 71 | self.setUpMarkDoomedProjects() |
| 72 | reaper = reap.Reap('req', 'resp', services=self.services) |
| 73 | |
| 74 | self.mox.ReplayAll() |
| 75 | doomed_project_ids = reaper._MarkDoomedProjects(self.cnxn) |
| 76 | self.mox.VerifyAll() |
| 77 | |
| 78 | self.assertEqual([self.proj1_id], doomed_project_ids) |
| 79 | self.assertEqual(project_pb2.ProjectState.DELETABLE, self.proj1.state) |
| 80 | self.assertEqual('DELETABLE_%s' % self.proj1_id, self.proj1.project_name) |
| 81 | |
| 82 | def setUpExpungeParts(self): |
| 83 | self.project_service.project_tbl.Select( |
| 84 | self.cnxn, cols=['project_id'], limit=100, |
| 85 | state='deletable').AndReturn([[self.proj1_id], [self.proj2_id]]) |
| 86 | self.issue_service.issue_tbl.Select( |
| 87 | self.cnxn, cols=['id'], limit=1000, |
| 88 | project_id=self.proj1_id).AndReturn([[self.proj1_issue_id]]) |
| 89 | self.issue_service.issue_tbl.Select( |
| 90 | self.cnxn, cols=['id'], limit=1000, |
| 91 | project_id=self.proj2_id).AndReturn([[self.proj2_issue_id]]) |
| 92 | |
| 93 | def testExpungeDeletableProjects(self): |
| 94 | self.setUpExpungeParts() |
| 95 | reaper = reap.Reap('req', 'resp', services=self.services) |
| 96 | |
| 97 | self.mox.ReplayAll() |
| 98 | expunged_project_ids = reaper._ExpungeDeletableProjects(self.cnxn) |
| 99 | self.mox.VerifyAll() |
| 100 | |
| 101 | self.assertEqual([self.proj1_id, self.proj2_id], expunged_project_ids) |
| 102 | # Verify all expected expunge methods were called. |
| 103 | self.assertEqual( |
| 104 | [self.proj1_issue_id, self.proj2_issue_id], |
| 105 | self.services.issue_star.expunged_item_ids) |
| 106 | self.assertEqual( |
| 107 | [self.proj1_issue_id, self.proj2_issue_id], |
| 108 | self.services.issue.expunged_issues) |
| 109 | self.assertEqual( |
| 110 | [self.proj1_id, self.proj2_id], self.services.config.expunged_configs) |
| 111 | self.assertEqual( |
| 112 | [self.proj1_id, self.proj2_id], |
| 113 | self.services.features.expunged_saved_queries) |
| 114 | self.assertEqual( |
| 115 | [self.proj1_id, self.proj2_id], |
| 116 | self.services.features.expunged_filter_rules) |
| 117 | self.assertEqual( |
| 118 | [self.proj1_id, self.proj2_id], |
| 119 | self.services.issue.expunged_former_locations) |
| 120 | self.assertEqual( |
| 121 | [self.proj1_id, self.proj2_id], self.services.issue.expunged_local_ids) |
| 122 | self.assertEqual( |
| 123 | [self.proj1_id, self.proj2_id], |
| 124 | self.services.features.expunged_quick_edit) |
| 125 | self.assertEqual( |
| 126 | [self.proj1_id, self.proj2_id], |
| 127 | self.services.project_star.expunged_item_ids) |
| 128 | self.assertEqual(0, len(self.services.project.test_projects)) |
| 129 | self.services.template.ExpungeProjectTemplates.assert_has_calls([ |
| 130 | mock.call(self.cnxn, 1001), |
| 131 | mock.call(self.cnxn, 1002)]) |