# Path: ddsc/versioncheck.py
# def get_internal_version():
#     """
#     Returns internal version info.
#     """
#     version_str = get_internal_version_str()
#     return _parse_version_str(version_str)
# 
# def get_pypi_version():
#     """
#     Returns the version info from pypi for this app.
#     """
#     try:
#         response = requests.get(PYPI_URL, timeout=HALF_SECOND_TIMEOUT)
#         response.raise_for_status()
#         data = response.json()
#         version_str = data["info"]["version"]
#         return _parse_version_str(version_str)
#     except requests.exceptions.ConnectionError:
#         raise VersionException(UNABLE_TO_ACCESS_PYPI + " Failed to connect.")
#     except requests.exceptions.Timeout:
#         raise VersionException(UNABLE_TO_ACCESS_PYPI + " Timeout")
# 
# def check_version():
#     """
#     Check version and raises VersionException if the installed DukeDSClient is out of date
#     or unable to access pypi.
#     """
#     pypi_version = get_pypi_version()
#     internal_version = get_internal_version()
# 
#     if pypi_version > internal_version:
#         raise VersionException(UPDATE_VERSION_MESSAGE)
# 
# class VersionException(DDSUserException):
#     def __init__(self, message):
#         super(VersionException, self).__init__(message)


from unittest import TestCase
from ddsc.versioncheck import get_internal_version, get_pypi_version, check_version, VersionException
from mock.mock import patch
import requests


class TestVerifyEncoding(TestCase):
    @patch("ddsc.versioncheck.get_internal_version")
    @patch("ddsc.versioncheck.get_pypi_version")
    def test_check_version_new_version_raises(self, mock_get_pypi_version, mock_get_internal_version):
        """
        When their is a new version at pypi the VersionException should be raised.
        It should contain instructions on how to upgrade DukeDSClient.
        """
        mock_get_pypi_version.return_value = [0, 2, 2]
        mock_get_internal_version.return_value = [0, 1, 2]
        with self.assertRaises(VersionException) as exception_info:
            check_version()
        self.assertIn("pip install --upgrade DukeDSClient", str(exception_info.exception))

    @patch("ddsc.versioncheck.requests")
    def test_check_version_pypi_timeout(self, mock_requests):
        mock_requests.exceptions = requests.exceptions
        mock_requests.get.side_effect = requests.exceptions.Timeout

# Path: ddsc/versioncheck.py
# def get_internal_version():
#     """
#     Returns internal version info.
#     """
#     version_str = get_internal_version_str()
#     return _parse_version_str(version_str)
# 
# def get_pypi_version():
#     """
#     Returns the version info from pypi for this app.
#     """
#     try:
#         response = requests.get(PYPI_URL, timeout=HALF_SECOND_TIMEOUT)
#         response.raise_for_status()
#         data = response.json()
#         version_str = data["info"]["version"]
#         return _parse_version_str(version_str)
#     except requests.exceptions.ConnectionError:
#         raise VersionException(UNABLE_TO_ACCESS_PYPI + " Failed to connect.")
#     except requests.exceptions.Timeout:
#         raise VersionException(UNABLE_TO_ACCESS_PYPI + " Timeout")
# 
# def check_version():
#     """
#     Check version and raises VersionException if the installed DukeDSClient is out of date
#     or unable to access pypi.
#     """
#     pypi_version = get_pypi_version()
#     internal_version = get_internal_version()
# 
#     if pypi_version > internal_version:
#         raise VersionException(UPDATE_VERSION_MESSAGE)
# 
# class VersionException(DDSUserException):
#     def __init__(self, message):
#         super(VersionException, self).__init__(message)


# Path: ddsc/tests/test_versioncheck.py
from unittest import TestCase
from ddsc.versioncheck import get_internal_version, get_pypi_version, check_version, VersionException
from mock.mock import patch
import requests



class TestVerifyEncoding(TestCase):
    @patch("ddsc.versioncheck.get_internal_version")
    @patch("ddsc.versioncheck.get_pypi_version")
    def test_check_version_new_version_raises(self, mock_get_pypi_version, mock_get_internal_version):
        """
        When their is a new version at pypi the VersionException should be raised.
        It should contain instructions on how to upgrade DukeDSClient.
        """
        mock_get_pypi_version.return_value = [0, 2, 2]
        mock_get_internal_version.return_value = [0, 1, 2]
        with self.assertRaises(VersionException) as exception_info:
            check_version()
        self.assertIn("pip install --upgrade DukeDSClient", str(exception_info.exception))

    @patch("ddsc.versioncheck.requests")
    def test_check_version_pypi_timeout(self, mock_requests):
        mock_requests.exceptions = requests.exceptions
        mock_requests.get.side_effect = requests.exceptions.Timeout

        with self.assertRaises(VersionException) as exception_info: