from unittest import TestCase
from mock import patch

import check_blockers


class CheckBlockers(TestCase):

    def test_parse_args(self):
        args = check_blockers.parse_args(['master', '17'])
        self.assertEqual('master', args.branch)
        self.assertEqual('17', args.pull_request)

    def test_get_lp_bugs_with_master(self):
        args = check_blockers.parse_args(['master', '17'])
        with patch('check_blockers.get_json') as gj:
            data = {'entries': []}
            gj.return_value = data
            check_blockers.get_lp_bugs(args)
            gj.assert_called_with((check_blockers.LP_BUGS.format('juju-core')))

    def test_get_lp_bugs_with_devel(self):
        args = check_blockers.parse_args(['1.20', '17'])
        with patch('check_blockers.DEVEL') as devel:
            devel.return_value = '1.20'
            with patch('check_blockers.get_json') as gj:
                data = {'entries': []}
                gj.return_value = data
                check_blockers.get_lp_bugs(args)
                gj.assert_called_with(
                    (check_blockers.LP_BUGS.format('juju-core/1.20')))

    def test_get_lp_bugs_without_blocking_bugs(self):
        args = check_blockers.parse_args(['master', '17'])
        with patch('check_blockers.get_json') as gj:
            empty_bug_list = {'entries': []}
            gj.return_value = empty_bug_list
            bugs = check_blockers.get_lp_bugs(args)
            self.assertEqual({}, bugs)

    def test_get_lp_bugs_with_blocking_bugs(self):
        args = check_blockers.parse_args(['master', '17'])
        with patch('check_blockers.get_json') as gj:
            bug_list = {
                'entries': [
                    {'self_link': 'https://lp/j/98765'},
                    {'self_link': 'https://lp/j/54321'},
                    ]}
            gj.return_value = bug_list
            bugs = check_blockers.get_lp_bugs(args)
            self.assertEqual(['54321', '98765'], sorted(bugs.keys()))

    def test_get_reason_without_blocking_bugs(self):
        args = check_blockers.parse_args(['master', '17'])
        with patch('check_blockers.get_json') as gj:
            code, reason = check_blockers.get_reason({}, args)
            self.assertEqual(0, code)
            self.assertEqual('No blocking bugs', reason)
            self.assertEqual(0, gj.call_count)

    def test_get_reason_without_comments(self):
        args = check_blockers.parse_args(['master', '17'])
        with patch('check_blockers.get_json') as gj:
            gj.return_value = []
            bugs = {'98765': {'self_link': 'https://lp/j/98765'}}
            code, reason = check_blockers.get_reason(bugs, args)
            self.assertEqual(1, code)
            self.assertEqual('Could not get 17 comments from github', reason)
            gj.assert_called_with((check_blockers.GH_COMMENTS.format('17')))

    def test_get_reason_with_blockers_no_match(self):
        args = check_blockers.parse_args(['master', '17'])
        with patch('check_blockers.get_json') as gj:
            gj.return_value = [{'body': '$$merge$$'}]
            bugs = {'98765': {'self_link': 'https://lp/j/98765'}}
            code, reason = check_blockers.get_reason(bugs, args)
            self.assertEqual(1, code)
            self.assertEqual("Does not match ['$$fixes-98765$$']", reason)

    def test_get_reason_with_blockers_with_match(self):
        args = check_blockers.parse_args(['master', '17'])
        with patch('check_blockers.get_json') as gj:
            gj.return_value = [
                {'body': '$$merge$$'},
                {'body': 'la la $$fixes-98765$$ ha ha'}]
            bugs = {'98765': {'self_link': 'https://lp/j/98765'}}
            code, reason = check_blockers.get_reason(bugs, args)
            self.assertEqual(0, code)
            self.assertEqual("Matches $$fixes-98765$$", reason)

@@ -71,16 +71,109 @@
 ckers%0A%0A%0A
+JUJUBOT_USER = %7B'login': 'jujubot', 'id': 7779494%7D%0AOTHER_USER = %7B'login': 'user', 'id': 1%7D%0A%0A%0A
 class Ch
@@ -3195,19 +3195,39 @@
 merge$$'
+, 'user': OTHER_USER
 %7D%5D%0A
-
         
@@ -3699,16 +3699,36 @@
 merge$$'
+, 'user': OTHER_USER
 %7D,%0A     
@@ -3776,16 +3776,36 @@
 $ ha ha'
+, 'user': OTHER_USER
 %7D%5D%0A     
@@ -3960,32 +3960,32 @@
 tEqual(0, code)%0A
-
             self
@@ -4024,16 +4024,609 @@
 765$$%22, reason)%0A
+%0A    def test_get_reason_with_blockers_with_jujubot_comment(self):%0A        args = check_blockers.parse_args(%5B'master', '17'%5D)%0A        with patch('check_blockers.get_json') as gj:%0A            gj.return_value = %5B%0A                %7B'body': '$$merge$$', 'user': OTHER_USER%7D,%0A                %7B'body': 'la la $$fixes-98765$$ ha ha', 'user': JUJUBOT_USER%7D%5D%0A            bugs = %7B'98765': %7B'self_link': 'https://lp/j/98765'%7D%7D%0A            code, reason = check_blockers.get_reason(bugs, args)%0A            self.assertEqual(1, code)%0A            self.assertEqual(%22Does not match %5B'$$fixes-98765$$'%5D%22, reason)%0A

Fix tests broken by uncaffeinated change to rules.