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