blob: cd63e7f0af0cf42b257d9e0285032c88e023927e [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 the projectsearch module."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import mock
11import unittest
12
13from framework import profiler
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010014from mrproto import project_pb2
Copybara854996b2021-09-07 19:36:02 +000015from services import service_manager
16from sitewide import projectsearch
17from testing import fake
18from testing import testing_helpers
19
20
21class ProjectSearchTest(unittest.TestCase):
22
23 def setUp(self):
24 self.services = service_manager.Services(
25 project=fake.ProjectService())
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010026 self.services.project.GetVisibleProjects = mock.MagicMock()
Copybara854996b2021-09-07 19:36:02 +000027
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ínezf19ea432024-01-23 20:20:52 +010033 self.addCleanup(mock.patch.stopall)
Copybara854996b2021-09-07 19:36:02 +000034
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ínezf19ea432024-01-23 20:20:52 +010049 self.services.project.GetVisibleProjects.return_value = []
Copybara854996b2021-09-07 19:36:02 +000050
51 pipeline = self.TestPipeline(0, 0)
52
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010053 self.services.project.GetVisibleProjects.assert_called_once()
Copybara854996b2021-09-07 19:36:02 +000054 self.assertListEqual([], pipeline.visible_results)
55
56 def testNonzeroResults(self):
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010057 self.services.project.GetVisibleProjects.return_value = [1, 2, 3]
Copybara854996b2021-09-07 19:36:02 +000058
59 pipeline = self.TestPipeline(3, 3)
60
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010061 self.services.project.GetVisibleProjects.assert_called_once()
Copybara854996b2021-09-07 19:36:02 +000062 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ínezf19ea432024-01-23 20:20:52 +010067 self.services.project.GetVisibleProjects.return_value = list(range(1, 106))
Copybara854996b2021-09-07 19:36:02 +000068
69 pipeline = self.TestPipeline(100, 100)
70
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010071 self.services.project.GetVisibleProjects.assert_called_once()
Copybara854996b2021-09-07 19:36:02 +000072 self.assertEqual(
73 '/hosting/search?num=100&start=100', pipeline.pagination.next_url)