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 | """Unittests for monorail.search.backendsearch.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 11 | try: |
| 12 | from mox3 import mox |
| 13 | except ImportError: |
| 14 | import mox |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 15 | |
| 16 | import settings |
| 17 | from search import backendsearch |
| 18 | from search import backendsearchpipeline |
| 19 | from services import service_manager |
| 20 | from testing import fake |
| 21 | from testing import testing_helpers |
| 22 | |
| 23 | |
| 24 | class BackendSearchTest(unittest.TestCase): |
| 25 | |
| 26 | def setUp(self): |
| 27 | self.services = service_manager.Services( |
| 28 | issue=fake.IssueService(), |
| 29 | ) |
| 30 | self.mr = testing_helpers.MakeMonorailRequest( |
| 31 | path='/_backend/besearch?q=Priority:High&shard=2') |
| 32 | self.mr.query_project_names = ['proj'] |
| 33 | self.mr.specified_logged_in_user_id = 111 |
| 34 | self.mr.specified_me_user_ids = [222] |
| 35 | self.mr.shard_id = 2 |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 36 | self.servlet = backendsearch.BackendSearch(services=self.services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 37 | self.mox = mox.Mox() |
| 38 | |
| 39 | def tearDown(self): |
| 40 | self.mox.UnsetStubs() |
| 41 | self.mox.ResetAll() |
| 42 | |
| 43 | def testHandleRequest_NoResults(self): |
| 44 | """Handle the case where the search has no results.""" |
| 45 | pipeline = testing_helpers.Blank( |
| 46 | SearchForIIDs=lambda: None, |
| 47 | result_iids=[], |
| 48 | search_limit_reached=False, |
| 49 | error=None) |
| 50 | self.mox.StubOutWithMock(backendsearchpipeline, 'BackendSearchPipeline') |
| 51 | backendsearchpipeline.BackendSearchPipeline( |
| 52 | self.mr, self.services, 100, ['proj'], 111, [222] |
| 53 | ).AndReturn(pipeline) |
| 54 | self.mox.ReplayAll() |
| 55 | |
| 56 | json_data = self.servlet.HandleRequest(self.mr) |
| 57 | self.mox.VerifyAll() |
| 58 | self.assertEqual([], json_data['unfiltered_iids']) |
| 59 | self.assertFalse(json_data['search_limit_reached']) |
| 60 | self.assertEqual(None, json_data['error']) |
| 61 | |
| 62 | def testHandleRequest_ResultsInOnePagainationPage(self): |
| 63 | """Prefetch all result issues and return them.""" |
| 64 | allowed_iids = [1, 2, 3, 4, 5, 6, 7, 8] |
| 65 | pipeline = testing_helpers.Blank( |
| 66 | SearchForIIDs=lambda: None, |
| 67 | result_iids=allowed_iids, |
| 68 | search_limit_reached=False, |
| 69 | error=None) |
| 70 | self.mox.StubOutWithMock(backendsearchpipeline, 'BackendSearchPipeline') |
| 71 | backendsearchpipeline.BackendSearchPipeline( |
| 72 | self.mr, self.services, 100, ['proj'], 111, [222] |
| 73 | ).AndReturn(pipeline) |
| 74 | self.mox.StubOutWithMock(self.services.issue, 'GetIssues') |
| 75 | # All issues are prefetched because they fit on the first pagination page. |
| 76 | self.services.issue.GetIssues(self.mr.cnxn, allowed_iids, shard_id=2) |
| 77 | self.mox.ReplayAll() |
| 78 | |
| 79 | json_data = self.servlet.HandleRequest(self.mr) |
| 80 | self.mox.VerifyAll() |
| 81 | self.assertEqual([1, 2, 3, 4, 5, 6, 7, 8], json_data['unfiltered_iids']) |
| 82 | self.assertFalse(json_data['search_limit_reached']) |
| 83 | self.assertEqual(None, json_data['error']) |
| 84 | |
| 85 | def testHandleRequest_ResultsExceedPagainationPage(self): |
| 86 | """Return all result issue IDs, but only prefetch the first page.""" |
| 87 | self.mr.num = 5 |
| 88 | pipeline = testing_helpers.Blank( |
| 89 | SearchForIIDs=lambda: None, |
| 90 | result_iids=[1, 2, 3, 4, 5, 6, 7, 8], |
| 91 | search_limit_reached=False, |
| 92 | error=None) |
| 93 | self.mox.StubOutWithMock(backendsearchpipeline, 'BackendSearchPipeline') |
| 94 | backendsearchpipeline.BackendSearchPipeline( |
| 95 | self.mr, self.services, 100, ['proj'], 111, [222] |
| 96 | ).AndReturn(pipeline) |
| 97 | self.mox.StubOutWithMock(self.services.issue, 'GetIssues') |
| 98 | # First 5 issues are prefetched because num=5 |
| 99 | self.services.issue.GetIssues(self.mr.cnxn, [1, 2, 3, 4, 5], shard_id=2) |
| 100 | self.mox.ReplayAll() |
| 101 | |
| 102 | json_data = self.servlet.HandleRequest(self.mr) |
| 103 | self.mox.VerifyAll() |
| 104 | # All are IDs are returned to the frontend. |
| 105 | self.assertEqual([1, 2, 3, 4, 5, 6, 7, 8], json_data['unfiltered_iids']) |
| 106 | self.assertFalse(json_data['search_limit_reached']) |
| 107 | self.assertEqual(None, json_data['error']) |
| 108 | |
| 109 | def testHandleRequest_QueryError(self): |
| 110 | """Handle the case where the search has no results.""" |
| 111 | error = ValueError('Malformed query') |
| 112 | pipeline = testing_helpers.Blank( |
| 113 | SearchForIIDs=lambda: None, |
| 114 | result_iids=[], |
| 115 | search_limit_reached=False, |
| 116 | error=error) |
| 117 | self.mox.StubOutWithMock(backendsearchpipeline, 'BackendSearchPipeline') |
| 118 | backendsearchpipeline.BackendSearchPipeline( |
| 119 | self.mr, self.services, 100, ['proj'], 111, [222] |
| 120 | ).AndReturn(pipeline) |
| 121 | self.mox.ReplayAll() |
| 122 | |
| 123 | json_data = self.servlet.HandleRequest(self.mr) |
| 124 | self.mox.VerifyAll() |
| 125 | self.assertEqual([], json_data['unfiltered_iids']) |
| 126 | self.assertFalse(json_data['search_limit_reached']) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 127 | self.assertEqual(str(error), json_data['error']) |