blob: f018f2f99d303d8ed7e87b2d0f8643820c75dd6c [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Unittests for monorail.search.backendsearch."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import unittest
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020011try:
12 from mox3 import mox
13except ImportError:
14 import mox
Copybara854996b2021-09-07 19:36:02 +000015
16import settings
17from search import backendsearch
18from search import backendsearchpipeline
19from services import service_manager
20from testing import fake
21from testing import testing_helpers
22
23
24class 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ínez9f9ade52022-10-10 23:20:11 +020036 self.servlet = backendsearch.BackendSearch(services=self.services)
Copybara854996b2021-09-07 19:36:02 +000037 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ínezf19ea432024-01-23 20:20:52 +0100127 self.assertEqual(str(error), json_data['error'])