# coding: utf-8
"""test_find_fork."""
# pylint: disable=no-self-use
from __future__ import absolute_import, division, print_function, unicode_literals

from os import path
import unittest

from six import PY3

from find_forks.__init__ import CONFIG
from find_forks.find_forks import add_forks, determine_names, find_forks, main

from .__init__ import BASEPATH

if PY3:
    from unittest.mock import patch, MagicMock, Mock  # pylint: disable=no-name-in-module
else:
    from mock import patch, MagicMock, Mock


class FindForksCommon(unittest.TestCase):
    @staticmethod
    def make_mock(json_response):
        """Used in test_interesting.py."""
        response_mock = MagicMock()
        response_mock.read = Mock(return_value=json_response)
        if PY3:
            response_mock.status = 200
            response_mock.getheader = Mock(return_value='<https://api.github.com/repos/frost-nzcr4/find_forks/forks?page=2>; rel="next", '
                                           '<https://api.github.com/repos/frost-nzcr4/find_forks/forks?page=3>; rel="last"')
        else:
            response_mock.code = 200
            response_mock.info = Mock(return_value=(('link', '<https://api.github.com/repos/frost-nzcr4/find_forks/forks?page=2>; rel="next", '
                                                             '<https://api.github.com/repos/frost-nzcr4/find_forks/forks?page=3>; rel="last"'), ))

        return response_mock

    def make_test(self, response_mock):
        """Used in test_interesting.py."""
        url = 'https://github.com/frost-nzcr4/find_forks'
        with patch('find_forks.find_forks.urllib.request.urlopen', return_value=response_mock) as urlopen_mock:
            with patch('find_forks.git_wrapper.subprocess.call', return_value=None):
                self.assertEqual(add_forks(url), 'https://api.github.com/repos/frost-nzcr4/find_forks/forks?page=2')
                urlopen_mock.assert_called_once_with(url, timeout=6)
                if PY3:
                    response_mock.status = 404
                else:
                    response_mock.code = 404
                self.assertIsNone(add_forks(url))


class FindForksTest(FindForksCommon):
    def test_add_forks(self):
        self.assertIsNone(add_forks('httttps://unavailable!url'))

        with open(path.join(BASEPATH, 'fixture/response.json'), 'rb') as fixture:
            json_response = fixture.read()
        response_mock = self.make_mock(json_response)
        self.make_test(response_mock)

    def test_determine_names(self):
        """To run this test you'll need to prepare git first, run:

        git remote add test-origin-1 https://github.com/frost-nzcr4/find_forks.git
        git remote add test-origin-2 https://github.com/yagmort/symfony1.git
        git remote add test-origin-3 git@github.com:tjerkw/Android-SlideExpandableListView.git
        """
        user, repo = determine_names()
        self.assertEqual(user, 'frost-nzcr4')
        self.assertEqual(repo, 'find_forks')

        user, repo = determine_names('test-origin-1')
        self.assertEqual(user, 'frost-nzcr4')
        self.assertEqual(repo, 'webmoney')

        user, repo = determine_names('test-origin-2')
        self.assertEqual(user, 'yagmort')
        self.assertEqual(repo, 'symfony1')

        user, repo = determine_names('test-origin-3')
        self.assertEqual(user, 'tjerkw')
        self.assertEqual(repo, 'Android-SlideExpandableListView')

        with self.assertRaises(RuntimeError):
            user, repo = determine_names('name-with-an-error')

    def test_find_forks(self):
        sent_args = {
            'per_page': 99,
            'start_page': 3
        }
        url = 'https://api.github.com/repos/frost-nzcr4/find_forks/forks?per_page=%s&page=%s' % (sent_args['per_page'], sent_args['start_page'])

        with patch('find_forks.git_wrapper.subprocess.call', return_value=None) as call_mock:
            with patch('find_forks.find_forks.add_forks', return_value=None) as add_forks_mock:
                find_forks(**sent_args)
        add_forks_mock.assert_called_once_with(url)
        call_mock.assert_called_once()

    def test_main(self):
        with patch('find_forks.find_forks.find_forks', return_value=None) as find_forks_mock:
            main()
            sent_args = CONFIG.copy()
            sent_args.update({'user': None, 'repo': None, 'no_fetch': False})
            find_forks_mock.assert_called_once_with(**sent_args)

            # Test __version__ exceptions.
            find_forks_mock = MagicMock(side_effect=SystemError())
            del find_forks_mock.__version__
            modules = {
                'find_forks.__init__': find_forks_mock
            }
            with patch.dict('sys.modules', modules):
                self.assertRaises(ImportError, main)

[((672, 683), 'mock.MagicMock', 'MagicMock', ([], {}), '()\n', (681, 683), False, 'from mock import patch, MagicMock, Mock\n'), ((713, 745), 'mock.Mock', 'Mock', ([], {'return_value': 'json_response'}), '(return_value=json_response)\n', (717, 745), False, 'from mock import patch, MagicMock, Mock\n'), ((2897, 2914), 'find_forks.find_forks.determine_names', 'determine_names', ([], {}), '()\n', (2912, 2914), False, 'from find_forks.find_forks import add_forks, determine_names, find_forks, main\n'), ((3028, 3060), 'find_forks.find_forks.determine_names', 'determine_names', (['"""test-origin-1"""'], {}), "('test-origin-1')\n", (3043, 3060), False, 'from find_forks.find_forks import add_forks, determine_names, find_forks, main\n'), ((3172, 3204), 'find_forks.find_forks.determine_names', 'determine_names', (['"""test-origin-2"""'], {}), "('test-origin-2')\n", (3187, 3204), False, 'from find_forks.find_forks import add_forks, determine_names, find_forks, main\n'), ((3312, 3344), 'find_forks.find_forks.determine_names', 'determine_names', (['"""test-origin-3"""'], {}), "('test-origin-3')\n", (3327, 3344), False, 'from find_forks.find_forks import add_forks, determine_names, find_forks, main\n'), ((839, 1028), 'mock.Mock', 'Mock', ([], {'return_value': '"""<https://api.github.com/repos/frost-nzcr4/find_forks/forks?page=2>; rel="next", <https://api.github.com/repos/frost-nzcr4/find_forks/forks?page=3>; rel="last\\""""'}), '(return_value=\n    \'<https://api.github.com/repos/frost-nzcr4/find_forks/forks?page=2>; rel="next", <https://api.github.com/repos/frost-nzcr4/find_forks/forks?page=3>; rel="last"\'\n    )\n', (843, 1028), False, 'from mock import patch, MagicMock, Mock\n'), ((1149, 1350), 'mock.Mock', 'Mock', ([], {'return_value': '((\'link\',\n    \'<https://api.github.com/repos/frost-nzcr4/find_forks/forks?page=2>; rel="next", <https://api.github.com/repos/frost-nzcr4/find_forks/forks?page=3>; rel="last"\'\n    ),)'}), '(return_value=((\'link\',\n    \'<https://api.github.com/repos/frost-nzcr4/find_forks/forks?page=2>; rel="next", <https://api.github.com/repos/frost-nzcr4/find_forks/forks?page=3>; rel="last"\'\n    ),))\n', (1153, 1350), False, 'from mock import patch, MagicMock, Mock\n'), ((1592, 1678), 'mock.patch', 'patch', (['"""find_forks.find_forks.urllib.request.urlopen"""'], {'return_value': 'response_mock'}), "('find_forks.find_forks.urllib.request.urlopen', return_value=\n    response_mock)\n", (1597, 1678), False, 'from mock import patch, MagicMock, Mock\n'), ((2246, 2284), 'find_forks.find_forks.add_forks', 'add_forks', (['"""httttps://unavailable!url"""'], {}), "('httttps://unavailable!url')\n", (2255, 2284), False, 'from find_forks.find_forks import add_forks, determine_names, find_forks, main\n'), ((3524, 3561), 'find_forks.find_forks.determine_names', 'determine_names', (['"""name-with-an-error"""'], {}), "('name-with-an-error')\n", (3539, 3561), False, 'from find_forks.find_forks import add_forks, determine_names, find_forks, main\n'), ((3841, 3907), 'mock.patch', 'patch', (['"""find_forks.git_wrapper.subprocess.call"""'], {'return_value': 'None'}), "('find_forks.git_wrapper.subprocess.call', return_value=None)\n", (3846, 3907), False, 'from mock import patch, MagicMock, Mock\n'), ((4188, 4248), 'mock.patch', 'patch', (['"""find_forks.find_forks.find_forks"""'], {'return_value': 'None'}), "('find_forks.find_forks.find_forks', return_value=None)\n", (4193, 4248), False, 'from mock import patch, MagicMock, Mock\n'), ((4281, 4287), 'find_forks.find_forks.main', 'main', ([], {}), '()\n', (4285, 4287), False, 'from find_forks.find_forks import add_forks, determine_names, find_forks, main\n'), ((4312, 4325), 'find_forks.__init__.CONFIG.copy', 'CONFIG.copy', ([], {}), '()\n', (4323, 4325), False, 'from find_forks.__init__ import CONFIG\n'), ((1708, 1774), 'mock.patch', 'patch', (['"""find_forks.git_wrapper.subprocess.call"""'], {'return_value': 'None'}), "('find_forks.git_wrapper.subprocess.call', return_value=None)\n", (1713, 1774), False, 'from mock import patch, MagicMock, Mock\n'), ((2305, 2349), 'os.path.join', 'path.join', (['BASEPATH', '"""fixture/response.json"""'], {}), "(BASEPATH, 'fixture/response.json')\n", (2314, 2349), False, 'from os import path\n'), ((3939, 3998), 'mock.patch', 'patch', (['"""find_forks.find_forks.add_forks"""'], {'return_value': 'None'}), "('find_forks.find_forks.add_forks', return_value=None)\n", (3944, 3998), False, 'from mock import patch, MagicMock, Mock\n'), ((4034, 4057), 'find_forks.find_forks.find_forks', 'find_forks', ([], {}), '(**sent_args)\n', (4044, 4057), False, 'from find_forks.find_forks import add_forks, determine_names, find_forks, main\n'), ((4734, 4768), 'mock.patch.dict', 'patch.dict', (['"""sys.modules"""', 'modules'], {}), "('sys.modules', modules)\n", (4744, 4768), False, 'from mock import patch, MagicMock, Mock\n'), ((1809, 1823), 'find_forks.find_forks.add_forks', 'add_forks', (['url'], {}), '(url)\n', (1818, 1823), False, 'from find_forks.find_forks import add_forks, determine_names, find_forks, main\n'), ((2134, 2148), 'find_forks.find_forks.add_forks', 'add_forks', (['url'], {}), '(url)\n', (2143, 2148), False, 'from find_forks.find_forks import add_forks, determine_names, find_forks, main\n')]