Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # 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 the projectsearch module.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import mock |
| 12 | import unittest |
| 13 | |
| 14 | from framework import profiler |
| 15 | from proto import project_pb2 |
| 16 | from services import service_manager |
| 17 | from sitewide import projectsearch |
| 18 | from testing import fake |
| 19 | from testing import testing_helpers |
| 20 | |
| 21 | |
| 22 | class ProjectSearchTest(unittest.TestCase): |
| 23 | |
| 24 | def setUp(self): |
| 25 | self.services = service_manager.Services( |
| 26 | project=fake.ProjectService()) |
| 27 | self.services.project.GetVisibleLiveProjects = mock.MagicMock() |
| 28 | |
| 29 | for idx, letter in enumerate('abcdefghijklmnopqrstuvwxyz'): |
| 30 | self.services.project.TestAddProject(letter, project_id=idx + 1) |
| 31 | for idx in range(27, 110): |
| 32 | self.services.project.TestAddProject(str(idx), project_id=idx) |
| 33 | |
| 34 | self.addCleanup(mock.patch.stopall()) |
| 35 | |
| 36 | def TestPipeline(self, expected_last, expected_len): |
| 37 | mr = testing_helpers.MakeMonorailRequest() |
| 38 | mr.can = 1 |
| 39 | |
| 40 | pipeline = projectsearch.ProjectSearchPipeline(mr, self.services) |
| 41 | pipeline.SearchForIDs() |
| 42 | pipeline.GetProjectsAndPaginate('fake cnxn', '/hosting/search') |
| 43 | self.assertEqual(1, pipeline.pagination.start) |
| 44 | self.assertEqual(expected_last, pipeline.pagination.last) |
| 45 | self.assertEqual(expected_len, len(pipeline.visible_results)) |
| 46 | |
| 47 | return pipeline |
| 48 | |
| 49 | def testZeroResults(self): |
| 50 | self.services.project.GetVisibleLiveProjects.return_value = [] |
| 51 | |
| 52 | pipeline = self.TestPipeline(0, 0) |
| 53 | |
| 54 | self.services.project.GetVisibleLiveProjects.assert_called_once() |
| 55 | self.assertListEqual([], pipeline.visible_results) |
| 56 | |
| 57 | def testNonzeroResults(self): |
| 58 | self.services.project.GetVisibleLiveProjects.return_value = [1, 2, 3] |
| 59 | |
| 60 | pipeline = self.TestPipeline(3, 3) |
| 61 | |
| 62 | self.services.project.GetVisibleLiveProjects.assert_called_once() |
| 63 | self.assertListEqual( |
| 64 | [1, 2, 3], [p.project_id for p in pipeline.visible_results]) |
| 65 | |
| 66 | def testTwoPageResults(self): |
| 67 | """Test more than one pagination page of results.""" |
| 68 | self.services.project.GetVisibleLiveProjects.return_value = list( |
| 69 | range(1, 106)) |
| 70 | |
| 71 | pipeline = self.TestPipeline(100, 100) |
| 72 | |
| 73 | self.services.project.GetVisibleLiveProjects.assert_called_once() |
| 74 | self.assertEqual( |
| 75 | '/hosting/search?num=100&start=100', pipeline.pagination.next_url) |