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