OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered 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.""" |
| 7 |
| 8 import unittest |
| 9 import mox |
| 10 |
| 11 import settings |
| 12 from search import backendsearch |
| 13 from search import backendsearchpipeline |
| 14 from services import service_manager |
| 15 from testing import fake |
| 16 from testing import testing_helpers |
| 17 |
| 18 |
| 19 class BackendSearchTest(unittest.TestCase): |
| 20 |
| 21 def setUp(self): |
| 22 self.services = service_manager.Services( |
| 23 issue=fake.IssueService(), |
| 24 ) |
| 25 self.mr = testing_helpers.MakeMonorailRequest( |
| 26 path='/_backend/besearch?q=Priority:High&shard=2') |
| 27 self.mr.query_project_names = ['proj'] |
| 28 self.mr.specified_logged_in_user_id = 111L |
| 29 self.mr.specified_me_user_id = 222L |
| 30 self.mr.shard_id = 2 |
| 31 self.servlet = backendsearch.BackendSearch( |
| 32 'req', 'res', services=self.services) |
| 33 self.mox = mox.Mox() |
| 34 |
| 35 def tearDown(self): |
| 36 self.mox.UnsetStubs() |
| 37 self.mox.ResetAll() |
| 38 |
| 39 def testHandleRequest_NoResults(self): |
| 40 """Handle the case where the search has no results.""" |
| 41 pipeline = testing_helpers.Blank( |
| 42 SearchForIIDs=lambda: None, |
| 43 result_iids=[], |
| 44 search_limit_reached=False) |
| 45 self.mox.StubOutWithMock(backendsearchpipeline, 'BackendSearchPipeline') |
| 46 backendsearchpipeline.BackendSearchPipeline( |
| 47 self.mr, self.services, self.servlet.profiler, 100, ['proj'], 111L, 222L |
| 48 ).AndReturn(pipeline) |
| 49 self.mox.ReplayAll() |
| 50 |
| 51 json_data = self.servlet.HandleRequest(self.mr) |
| 52 self.mox.VerifyAll() |
| 53 self.assertEqual([], json_data['unfiltered_iids']) |
| 54 self.assertFalse(json_data['search_limit_reached']) |
| 55 |
| 56 def testHandleRequest_ResultsInOnePagainationPage(self): |
| 57 """Prefetch all result issues and return them.""" |
| 58 allowed_iids = [1, 2, 3, 4, 5, 6, 7, 8] |
| 59 pipeline = testing_helpers.Blank( |
| 60 SearchForIIDs=lambda: None, |
| 61 result_iids=allowed_iids, |
| 62 search_limit_reached=False) |
| 63 self.mox.StubOutWithMock(backendsearchpipeline, 'BackendSearchPipeline') |
| 64 backendsearchpipeline.BackendSearchPipeline( |
| 65 self.mr, self.services, self.servlet.profiler, 100, ['proj'], 111L, 222L |
| 66 ).AndReturn(pipeline) |
| 67 self.mox.StubOutWithMock(self.services.issue, 'GetIssues') |
| 68 # All issues are prefetched because they fit on the first pagination page. |
| 69 self.services.issue.GetIssues(self.mr.cnxn, allowed_iids, shard_id=2) |
| 70 self.mox.ReplayAll() |
| 71 |
| 72 json_data = self.servlet.HandleRequest(self.mr) |
| 73 self.mox.VerifyAll() |
| 74 self.assertEqual([1, 2, 3, 4, 5, 6, 7, 8], json_data['unfiltered_iids']) |
| 75 self.assertFalse(json_data['search_limit_reached']) |
| 76 |
| 77 def testHandleRequest_ResultsExceedPagainationPage(self): |
| 78 """Return all result issue IDs, but only prefetch the first page.""" |
| 79 self.mr.num = 5 |
| 80 pipeline = testing_helpers.Blank( |
| 81 SearchForIIDs=lambda: None, |
| 82 result_iids=[1, 2, 3, 4, 5, 6, 7, 8], |
| 83 search_limit_reached=False) |
| 84 self.mox.StubOutWithMock(backendsearchpipeline, 'BackendSearchPipeline') |
| 85 backendsearchpipeline.BackendSearchPipeline( |
| 86 self.mr, self.services, self.servlet.profiler, 100, ['proj'], 111L, 222L |
| 87 ).AndReturn(pipeline) |
| 88 self.mox.StubOutWithMock(self.services.issue, 'GetIssues') |
| 89 # First 5 issues are prefetched because num=5 |
| 90 self.services.issue.GetIssues(self.mr.cnxn, [1, 2, 3, 4, 5], shard_id=2) |
| 91 self.mox.ReplayAll() |
| 92 |
| 93 json_data = self.servlet.HandleRequest(self.mr) |
| 94 self.mox.VerifyAll() |
| 95 # All are IDs are returned to the frontend. |
| 96 self.assertEqual([1, 2, 3, 4, 5, 6, 7, 8], json_data['unfiltered_iids']) |
| 97 self.assertFalse(json_data['search_limit_reached']) |
| 98 |
| 99 |
| 100 if __name__ == '__main__': |
| 101 unittest.main() |
OLD | NEW |