def test_connection_error(mock_get, library):
    mock_get.side_effect = requests.exceptions.ConnectionError

    with patch.dict('os.environ', {'ALTERYX_OPEN_SRC_UPDATE_CHECKER': 'TRUE'}):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            alteryx_open_src_update_checker.check_version(library=library,
                                                          version='0.0.1',
                                                          headers=headers)
            assert len(w) == 0
def test_old_version_live(library):
    with patch.dict('os.environ', {'ALTERYX_OPEN_SRC_UPDATE_CHECKER': 'TRUE'}):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            alteryx_open_src_update_checker.check_version(library=library,
                                                          version='0.0.1',
                                                          headers=headers)
            print(library)
            assert len(w) == 1
            assert "{} is out-of-date: installed == 0.0.1, latest == ".format(
                library) in str(w[-1].message)
def test_default():
    data = get_response_json(headers=headers)
    assert data['library'] == 'featuretools'
    version = data['version']

    with patch.dict('os.environ', {'ALTERYX_OPEN_SRC_UPDATE_CHECKER': 'TRUE'}):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            alteryx_open_src_update_checker.check_version(version=version,
                                                          headers=headers)
            assert len(w) == 0
def test_current_version_live(library):
    data = get_response_json(library=library, headers=headers)
    version = data['version']

    with patch.dict('os.environ', {'ALTERYX_OPEN_SRC_UPDATE_CHECKER': 'TRUE'}):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            alteryx_open_src_update_checker.check_version(library=library,
                                                          version=version,
                                                          headers=headers)
            assert len(w) == 0
def test_non_json_response(mock_get, library):
    mock_response = Mock()
    mock_response.content = "Text response"
    mock_get.return_value = mock_response

    with patch.dict('os.environ', {'ALTERYX_OPEN_SRC_UPDATE_CHECKER': 'TRUE'}):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            alteryx_open_src_update_checker.check_version(library=library,
                                                          headers=headers)
            assert len(w) == 0
def test_bad_response(mock_get, library):
    mock_response = Mock()
    http_error = requests.exceptions.HTTPError()
    mock_response.raise_for_status.side_effect = http_error
    mock_get.return_value = mock_response

    with patch.dict('os.environ', {'ALTERYX_OPEN_SRC_UPDATE_CHECKER': 'TRUE'}):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            alteryx_open_src_update_checker.check_version(library=library,
                                                          headers=headers)
            assert len(w) == 0
def test_current_version_mock(mock_get, library):
    return_json = {
        "is_latest": True,
        "upload_time": "Wed, 24 Apr 2019 15:54:56 GMT",
        "version": "0.0.1"
    }
    mock_response = Mock()
    mock_response.json.return_value = return_json
    mock_get.return_value = mock_response

    with patch.dict('os.environ', {'ALTERYX_OPEN_SRC_UPDATE_CHECKER': 'TRUE'}):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            alteryx_open_src_update_checker.check_version(library=library,
                                                          version='0.0.1',
                                                          headers=headers)
            assert len(w) == 0
def test_old_version_mock(mock_get, library):
    return_json = {
        "is_latest": False,
        "upload_time": "Wed, 24 Apr 2019 15:54:56 GMT",
        "version": "0.7.1"
    }
    mock_response = Mock()
    mock_response.json.return_value = return_json
    mock_get.return_value = mock_response

    with patch.dict('os.environ', {'ALTERYX_OPEN_SRC_UPDATE_CHECKER': 'TRUE'}):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            alteryx_open_src_update_checker.check_version(library=library,
                                                          version='0.0.1',
                                                          headers=headers)
            assert len(w) == 1
            assert "{} is out-of-date: installed == 0.0.1, latest == 0.7.1".format(
                library) == str(w[-1].message)
def test_environment_variables(mock_get, library):
    return_json = {
        "is_latest": False,
        "upload_time": "Wed, 24 Apr 2019 15:54:56 GMT",
        "version": "0.7.1"
    }
    mock_response = Mock()
    mock_response.json.return_value = return_json
    mock_get.return_value = mock_response

    # this test would fail unless the check is skipped by setting
    # the alteryx_open_src_update_checker environment variable to false
    for env_value in ['0', 'False', 'false', 'FALSE']:
        with patch.dict('os.environ',
                        {'ALTERYX_OPEN_SRC_UPDATE_CHECKER': env_value}):
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter("always")
                alteryx_open_src_update_checker.check_version(library=library,
                                                              version='0.0.1',
                                                              headers=headers)
                assert len(w) == 0