def setUp(self):
     self.handler = BisectorHandler()
     self.handler.set_build_range([
         {'build_url': 'http://build_url_0', 'repository': 'my'}
     ])
 def setUp(self):
     self.handler = BisectorHandler()
     self.handler.set_build_range([{"build_url": "http://build_url_0", "repository": "my"}])
class TestBisectorHandler(unittest.TestCase):
    def setUp(self):
        self.handler = BisectorHandler()
        self.handler.set_build_range([
            {'build_url': 'http://build_url_0', 'repository': 'my'}
        ])

    def test_initialize(self):
        self.handler.set_build_range([
            Mock(changeset='1', repo_url='my'),
            Mock(),
            Mock(changeset='3', repo_url='my'),
        ])
        self.handler.initialize()
        self.assertEqual(self.handler.found_repo, 'my')
        self.assertEqual(self.handler.good_revision, '1')
        self.assertEqual(self.handler.bad_revision, '3')

    def test_get_pushlog_url(self):
        self.handler.found_repo = 'https://hg.mozilla.repo'
        self.handler.good_revision = '2'
        self.handler.bad_revision = '6'
        self.assertEqual(
            self.handler.get_pushlog_url(),
            "https://hg.mozilla.repo/pushloghtml?fromchange=2&tochange=6")

    def test_get_pushlog_url_same_chsets(self):
        self.handler.found_repo = 'https://hg.mozilla.repo'
        self.handler.good_revision = self.handler.bad_revision = '2'
        self.assertEqual(
            self.handler.get_pushlog_url(),
            "https://hg.mozilla.repo/pushloghtml?changeset=2")

    @patch('mozregression.bisector.LOG')
    def test_print_range(self, logger):
        self.handler.found_repo = 'https://hg.mozilla.repo'
        self.handler.good_revision = '2'
        self.handler.bad_revision = '6'
        log = []
        logger.info = log.append

        self.handler.print_range()
        self.assertEqual(log[0], "Last good revision: 2")
        self.assertEqual(log[1], "First bad revision: 6")
        self.assertIn(self.handler.get_pushlog_url(), log[2])

    @patch('mozregression.bisector.BisectorHandler._print_progress')
    def test_build_good(self, _print_progress):
        self.handler.build_good(0, [{"changeset": '123'},
                                    {"changeset": '456'}])
        _print_progress.assert_called_with([{"changeset": '123'},
                                            {"changeset": '456'}])

    @patch('mozregression.bisector.BisectorHandler._print_progress')
    def test_build_bad(self, _print_progress):
        # with at least two, _print_progress will be called
        self.handler.build_bad(0, [{"changeset": '123'}, {"changeset": '456'}])
        _print_progress.assert_called_with([{"changeset": '123'},
                                            {"changeset": '456'}])
class TestBisectorHandler(unittest.TestCase):
    def setUp(self):
        self.handler = BisectorHandler()
        self.handler.set_build_range([{"build_url": "http://build_url_0", "repository": "my"}])

    def test_initialize(self):
        self.handler.set_build_range([Mock(changeset="1", repo_url="my"), Mock(), Mock(changeset="3", repo_url="my")])
        self.handler.initialize()
        self.assertEqual(self.handler.found_repo, "my")
        self.assertEqual(self.handler.good_revision, "1")
        self.assertEqual(self.handler.bad_revision, "3")

    def test_get_pushlog_url(self):
        self.handler.found_repo = "https://hg.mozilla.repo"
        self.handler.good_revision = "2"
        self.handler.bad_revision = "6"
        self.assertEqual(self.handler.get_pushlog_url(), "https://hg.mozilla.repo/pushloghtml?fromchange=2&tochange=6")

    def test_get_pushlog_url_same_chsets(self):
        self.handler.found_repo = "https://hg.mozilla.repo"
        self.handler.good_revision = self.handler.bad_revision = "2"
        self.assertEqual(self.handler.get_pushlog_url(), "https://hg.mozilla.repo/pushloghtml?changeset=2")

    @patch("mozregression.bisector.LOG")
    def test_print_range(self, logger):
        self.handler.found_repo = "https://hg.mozilla.repo"
        self.handler.good_revision = "2"
        self.handler.bad_revision = "6"
        log = []
        logger.info = log.append

        self.handler.print_range()
        self.assertEqual(log[0], "Last good revision: 2")
        self.assertEqual(log[1], "First bad revision: 6")
        self.assertIn(self.handler.get_pushlog_url(), log[2])

    @patch("mozregression.bisector.BisectorHandler._print_progress")
    def test_build_good(self, _print_progress):
        self.handler.build_good(0, [{"changeset": "123"}, {"changeset": "456"}])
        _print_progress.assert_called_with([{"changeset": "123"}, {"changeset": "456"}])

    @patch("mozregression.bisector.BisectorHandler._print_progress")
    def test_build_bad(self, _print_progress):
        # with at least two, _print_progress will be called
        self.handler.build_bad(0, [{"changeset": "123"}, {"changeset": "456"}])
        _print_progress.assert_called_with([{"changeset": "123"}, {"changeset": "456"}])