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