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 the projectsearch module.""" |
| 7 |
| 8 import unittest |
| 9 |
| 10 import mox |
| 11 |
| 12 from framework import profiler |
| 13 from proto import project_pb2 |
| 14 from services import service_manager |
| 15 from sitewide import projectsearch |
| 16 from testing import fake |
| 17 from testing import testing_helpers |
| 18 |
| 19 |
| 20 class ProjectSearchTest(unittest.TestCase): |
| 21 |
| 22 def setUp(self): |
| 23 self.services = service_manager.Services( |
| 24 project=fake.ProjectService()) |
| 25 |
| 26 for idx, letter in enumerate('abcdefghijklmnopqrstuvwxyz'): |
| 27 self.services.project.TestAddProject(letter, project_id=idx + 1) |
| 28 |
| 29 self.mox = mox.Mox() |
| 30 self.mox.StubOutWithMock(self.services.project, 'GetVisibleLiveProjects') |
| 31 |
| 32 def tearDown(self): |
| 33 self.mox.UnsetStubs() |
| 34 self.mox.ResetAll() |
| 35 |
| 36 def TestPipeline( |
| 37 self, mr=None, expected_start=1, expected_last=None, |
| 38 expected_len=None): |
| 39 if not mr: |
| 40 mr = testing_helpers.MakeMonorailRequest() |
| 41 |
| 42 if expected_last is None and expected_len is not None: |
| 43 expected_last = expected_len |
| 44 |
| 45 mr.can = 1 |
| 46 prof = profiler.Profiler() |
| 47 |
| 48 pipeline = projectsearch.ProjectSearchPipeline(mr, self.services, prof) |
| 49 pipeline.SearchForIDs() |
| 50 pipeline.GetProjectsAndPaginate('fake cnxn', '/hosting/search') |
| 51 self.assertEqual(expected_start, pipeline.pagination.start) |
| 52 if expected_last is not None: |
| 53 self.assertEqual(expected_last, pipeline.pagination.last) |
| 54 if expected_len is not None: |
| 55 self.assertEqual(expected_len, len(pipeline.visible_results)) |
| 56 |
| 57 return pipeline |
| 58 |
| 59 def SetUpZeroResults(self): |
| 60 self.services.project.GetVisibleLiveProjects( |
| 61 mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn([]) |
| 62 |
| 63 def testZeroResults(self): |
| 64 self.SetUpZeroResults() |
| 65 self.mox.ReplayAll() |
| 66 pipeline = self.TestPipeline( |
| 67 expected_last=0, expected_len=0) |
| 68 self.mox.VerifyAll() |
| 69 self.assertListEqual([], pipeline.visible_results) |
| 70 |
| 71 def SetUpNonzeroResults(self): |
| 72 self.services.project.GetVisibleLiveProjects( |
| 73 mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn([1, 2, 3]) |
| 74 |
| 75 def testNonzeroResults(self): |
| 76 self.SetUpNonzeroResults() |
| 77 self.mox.ReplayAll() |
| 78 pipeline = self.TestPipeline( |
| 79 expected_last=3, expected_len=3) |
| 80 self.mox.VerifyAll() |
| 81 self.assertListEqual( |
| 82 [1, 2, 3], [p.project_id for p in pipeline.visible_results]) |
| 83 |
| 84 def SetUpTwoPageResults(self): |
| 85 self.services.project.GetVisibleLiveProjects( |
| 86 mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( |
| 87 range(1, 16)) |
| 88 |
| 89 def testTwoPageResults(self): |
| 90 """Test more than one pagination page of results.""" |
| 91 self.SetUpTwoPageResults() |
| 92 self.mox.ReplayAll() |
| 93 pipeline = self.TestPipeline( |
| 94 expected_last=10, expected_len=10) |
| 95 self.mox.VerifyAll() |
| 96 self.assertEqual( |
| 97 '/hosting/search?num=10&start=10', pipeline.pagination.next_url) |
| 98 |
| 99 |
| 100 if __name__ == '__main__': |
| 101 unittest.main() |
OLD | NEW |