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