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 backendsearchpipeline module.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 10 | try: |
| 11 | from mox3 import mox |
| 12 | except ImportError: |
| 13 | import mox |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 14 | import unittest |
| 15 | |
| 16 | from google.appengine.api import memcache |
| 17 | from google.appengine.ext import testbed |
| 18 | |
| 19 | import settings |
| 20 | from framework import framework_helpers |
| 21 | from framework import sorting |
| 22 | from framework import sql |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 23 | from mrproto import ast_pb2 |
| 24 | from mrproto import tracker_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 25 | from search import backendsearchpipeline |
| 26 | from search import ast2ast |
| 27 | from search import query2ast |
| 28 | from services import service_manager |
| 29 | from services import tracker_fulltext |
| 30 | from testing import fake |
| 31 | from testing import testing_helpers |
| 32 | from tracker import tracker_bizobj |
| 33 | |
| 34 | |
| 35 | class BackendSearchPipelineTest(unittest.TestCase): |
| 36 | |
| 37 | def setUp(self): |
| 38 | self.cnxn = 'fake cnxn' |
| 39 | self.services = service_manager.Services( |
| 40 | user=fake.UserService(), |
| 41 | usergroup=fake.UserGroupService(), |
| 42 | project=fake.ProjectService(), |
| 43 | issue=fake.IssueService(), |
| 44 | config=fake.ConfigService(), |
| 45 | cache_manager=fake.CacheManager()) |
| 46 | self.services.user.TestAddUser('a@example.com', 111) |
| 47 | self.project = self.services.project.TestAddProject('proj', project_id=789) |
| 48 | self.mr = testing_helpers.MakeMonorailRequest( |
| 49 | path='/p/proj/issues/list?q=Priority:High', |
| 50 | project=self.project) |
| 51 | self.mr.me_user_id = 999 # This value is not used by backend search |
| 52 | self.mr.shard_id = 2 |
| 53 | self.mr.invalidation_timestep = 12345 |
| 54 | |
| 55 | self.mox = mox.Mox() |
| 56 | self.testbed = testbed.Testbed() |
| 57 | self.testbed.activate() |
| 58 | self.testbed.init_user_stub() |
| 59 | self.testbed.init_memcache_stub() |
| 60 | sorting.InitializeArtValues(self.services) |
| 61 | |
| 62 | def tearDown(self): |
| 63 | self.testbed.deactivate() |
| 64 | self.mox.UnsetStubs() |
| 65 | self.mox.ResetAll() |
| 66 | |
| 67 | def SetUpPromises(self, exp_query): |
| 68 | self.mox.StubOutWithMock(framework_helpers, 'Promise') |
| 69 | framework_helpers.Promise( |
| 70 | backendsearchpipeline._GetQueryResultIIDs, self.mr.cnxn, |
| 71 | self.services, 'is:open', exp_query, [789], |
| 72 | mox.IsA(tracker_pb2.ProjectIssueConfig), ['project', 'id'], |
| 73 | ('Issue.shard = %s', [2]), 2, self.mr.invalidation_timestep |
| 74 | ).AndReturn('fake promise 1') |
| 75 | |
| 76 | def testMakePromises_Anon(self): |
| 77 | """A backend pipeline does not personalize the query of anon users.""" |
| 78 | self.SetUpPromises('Priority:High') |
| 79 | self.mox.ReplayAll() |
| 80 | backendsearchpipeline.BackendSearchPipeline( |
| 81 | self.mr, self.services, 100, ['proj'], None, []) |
| 82 | self.mox.VerifyAll() |
| 83 | |
| 84 | def testMakePromises_SignedIn(self): |
| 85 | """A backend pipeline immediately personalizes and runs the query.""" |
| 86 | self.mr.query = 'owner:me' |
| 87 | self.SetUpPromises('owner:111') |
| 88 | self.mox.ReplayAll() |
| 89 | backendsearchpipeline.BackendSearchPipeline( |
| 90 | self.mr, self.services, 100, ['proj'], 111, [111]) |
| 91 | self.mox.VerifyAll() |
| 92 | |
| 93 | def testSearchForIIDs(self): |
| 94 | self.SetUpPromises('Priority:High') |
| 95 | self.mox.ReplayAll() |
| 96 | be_pipeline = backendsearchpipeline.BackendSearchPipeline( |
| 97 | self.mr, self.services, 100, ['proj'], 111, [111]) |
| 98 | be_pipeline.result_iids_promise = testing_helpers.Blank( |
| 99 | WaitAndGetValue=lambda: ([10002, 10052], False, None)) |
| 100 | be_pipeline.SearchForIIDs() |
| 101 | self.mox.VerifyAll() |
| 102 | self.assertEqual([10002, 10052], be_pipeline.result_iids) |
| 103 | self.assertEqual(False, be_pipeline.search_limit_reached) |
| 104 | |
| 105 | |
| 106 | class BackendSearchPipelineMethodsTest(unittest.TestCase): |
| 107 | |
| 108 | def setUp(self): |
| 109 | self.cnxn = 'fake cnxn' |
| 110 | self.config = tracker_bizobj.MakeDefaultProjectIssueConfig(789) |
| 111 | self.services = service_manager.Services( |
| 112 | user=fake.UserService(), |
| 113 | usergroup=fake.UserGroupService(), |
| 114 | project=fake.ProjectService(), |
| 115 | issue=fake.IssueService(), |
| 116 | config=fake.ConfigService(), |
| 117 | cache_manager=fake.CacheManager()) |
| 118 | self.services.user.TestAddUser('a@example.com', 111) |
| 119 | self.project = self.services.project.TestAddProject('proj', project_id=789) |
| 120 | self.mr = testing_helpers.MakeMonorailRequest( |
| 121 | path='/p/proj/issues/list?q=Priority:High', |
| 122 | project=self.project) |
| 123 | |
| 124 | self.mox = mox.Mox() |
| 125 | self.testbed = testbed.Testbed() |
| 126 | self.testbed.activate() |
| 127 | self.testbed.init_user_stub() |
| 128 | self.testbed.init_memcache_stub() |
| 129 | |
| 130 | def tearDown(self): |
| 131 | self.testbed.deactivate() |
| 132 | self.mox.UnsetStubs() |
| 133 | self.mox.ResetAll() |
| 134 | |
| 135 | def testSearchProjectCan_Normal(self): |
| 136 | query_ast = query2ast.ParseUserQuery( |
| 137 | 'Priority:High', 'is:open', query2ast.BUILTIN_ISSUE_FIELDS, |
| 138 | self.config) |
| 139 | simplified_query_ast = ast2ast.PreprocessAST( |
| 140 | self.cnxn, query_ast, [789], self.services, self.config) |
| 141 | conj = simplified_query_ast.conjunctions[0] |
| 142 | self.mox.StubOutWithMock(tracker_fulltext, 'SearchIssueFullText') |
| 143 | tracker_fulltext.SearchIssueFullText( |
| 144 | [789], conj, 2).AndReturn((None, False)) |
| 145 | self.mox.StubOutWithMock(self.services.issue, 'RunIssueQuery') |
| 146 | self.services.issue.RunIssueQuery( |
| 147 | self.cnxn, mox.IsA(list), mox.IsA(list), mox.IsA(list), |
| 148 | shard_id=2).AndReturn(([10002, 10052], False)) |
| 149 | self.mox.ReplayAll() |
| 150 | result, capped, err = backendsearchpipeline.SearchProjectCan( |
| 151 | self.cnxn, self.services, [789], query_ast, 2, self.config) |
| 152 | self.mox.VerifyAll() |
| 153 | self.assertEqual([10002, 10052], result) |
| 154 | self.assertFalse(capped) |
| 155 | self.assertEqual(None, err) |
| 156 | |
| 157 | def testSearchProjectCan_DBCapped(self): |
| 158 | query_ast = query2ast.ParseUserQuery( |
| 159 | 'Priority:High', 'is:open', query2ast.BUILTIN_ISSUE_FIELDS, |
| 160 | self.config) |
| 161 | simplified_query_ast = ast2ast.PreprocessAST( |
| 162 | self.cnxn, query_ast, [789], self.services, self.config) |
| 163 | conj = simplified_query_ast.conjunctions[0] |
| 164 | self.mox.StubOutWithMock(tracker_fulltext, 'SearchIssueFullText') |
| 165 | tracker_fulltext.SearchIssueFullText( |
| 166 | [789], conj, 2).AndReturn((None, False)) |
| 167 | self.mox.StubOutWithMock(self.services.issue, 'RunIssueQuery') |
| 168 | self.services.issue.RunIssueQuery( |
| 169 | self.cnxn, mox.IsA(list), mox.IsA(list), mox.IsA(list), |
| 170 | shard_id=2).AndReturn(([10002, 10052], True)) |
| 171 | self.mox.ReplayAll() |
| 172 | result, capped, err = backendsearchpipeline.SearchProjectCan( |
| 173 | self.cnxn, self.services, [789], query_ast, 2, self.config) |
| 174 | self.mox.VerifyAll() |
| 175 | self.assertEqual([10002, 10052], result) |
| 176 | self.assertTrue(capped) |
| 177 | self.assertEqual(None, err) |
| 178 | |
| 179 | def testSearchProjectCan_FTSCapped(self): |
| 180 | query_ast = query2ast.ParseUserQuery( |
| 181 | 'Priority:High', 'is:open', query2ast.BUILTIN_ISSUE_FIELDS, |
| 182 | self.config) |
| 183 | simplified_query_ast = ast2ast.PreprocessAST( |
| 184 | self.cnxn, query_ast, [789], self.services, self.config) |
| 185 | conj = simplified_query_ast.conjunctions[0] |
| 186 | self.mox.StubOutWithMock(tracker_fulltext, 'SearchIssueFullText') |
| 187 | tracker_fulltext.SearchIssueFullText( |
| 188 | [789], conj, 2).AndReturn(([10002, 10052], True)) |
| 189 | self.mox.StubOutWithMock(self.services.issue, 'RunIssueQuery') |
| 190 | self.services.issue.RunIssueQuery( |
| 191 | self.cnxn, mox.IsA(list), mox.IsA(list), mox.IsA(list), |
| 192 | shard_id=2).AndReturn(([10002, 10052], False)) |
| 193 | self.mox.ReplayAll() |
| 194 | result, capped, err = backendsearchpipeline.SearchProjectCan( |
| 195 | self.cnxn, self.services, [789], query_ast, 2, self.config) |
| 196 | self.mox.VerifyAll() |
| 197 | self.assertEqual([10002, 10052], result) |
| 198 | self.assertTrue(capped) |
| 199 | self.assertEqual(None, err) |
| 200 | |
| 201 | def testGetQueryResultIIDs(self): |
| 202 | sd = ['project', 'id'] |
| 203 | slice_term = ('Issue.shard = %s', [2]) |
| 204 | query_ast = query2ast.ParseUserQuery( |
| 205 | 'Priority:High', 'is:open', query2ast.BUILTIN_ISSUE_FIELDS, |
| 206 | self.config) |
| 207 | query_ast = backendsearchpipeline._FilterSpam(query_ast) |
| 208 | |
| 209 | self.mox.StubOutWithMock(backendsearchpipeline, 'SearchProjectCan') |
| 210 | backendsearchpipeline.SearchProjectCan( |
| 211 | self.cnxn, self.services, [789], query_ast, 2, self.config, |
| 212 | sort_directives=sd, where=[slice_term], |
| 213 | query_desc='getting query issue IDs' |
| 214 | ).AndReturn(([10002, 10052], False, None)) |
| 215 | self.mox.ReplayAll() |
| 216 | result, capped, err = backendsearchpipeline._GetQueryResultIIDs( |
| 217 | self.cnxn, self.services, 'is:open', 'Priority:High', |
| 218 | [789], self.config, sd, slice_term, 2, 12345) |
| 219 | self.mox.VerifyAll() |
| 220 | self.assertEqual([10002, 10052], result) |
| 221 | self.assertFalse(capped) |
| 222 | self.assertEqual(None, err) |
| 223 | self.assertEqual( |
| 224 | ([10002, 10052], 12345), |
| 225 | memcache.get('789;is:open;Priority:High;project id;2')) |
| 226 | |
| 227 | def testGetSpamQueryResultIIDs(self): |
| 228 | sd = ['project', 'id'] |
| 229 | slice_term = ('Issue.shard = %s', [2]) |
| 230 | query_ast = query2ast.ParseUserQuery( |
| 231 | 'Priority:High is:spam', 'is:open', query2ast.BUILTIN_ISSUE_FIELDS, |
| 232 | self.config) |
| 233 | |
| 234 | query_ast = backendsearchpipeline._FilterSpam(query_ast) |
| 235 | |
| 236 | self.mox.StubOutWithMock(backendsearchpipeline, 'SearchProjectCan') |
| 237 | backendsearchpipeline.SearchProjectCan( |
| 238 | self.cnxn, self.services, [789], query_ast, 2, self.config, |
| 239 | sort_directives=sd, where=[slice_term], |
| 240 | query_desc='getting query issue IDs' |
| 241 | ).AndReturn(([10002, 10052], False, None)) |
| 242 | self.mox.ReplayAll() |
| 243 | result, capped, err = backendsearchpipeline._GetQueryResultIIDs( |
| 244 | self.cnxn, self.services, 'is:open', 'Priority:High is:spam', |
| 245 | [789], self.config, sd, slice_term, 2, 12345) |
| 246 | self.mox.VerifyAll() |
| 247 | self.assertEqual([10002, 10052], result) |
| 248 | self.assertFalse(capped) |
| 249 | self.assertEqual(None, err) |
| 250 | self.assertEqual( |
| 251 | ([10002, 10052], 12345), |
| 252 | memcache.get('789;is:open;Priority:High is:spam;project id;2')) |