Skip to content

Commit

Permalink
Merge pull request #8 from FeatureLabs/add_env_var_check
Browse files Browse the repository at this point in the history
Added environment variable and updated tests
  • Loading branch information
gsheni committed May 20, 2019
2 parents a53478e + 1443506 commit 221f8e4
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 41 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -16,11 +16,11 @@ lint-fix:

.PHONY: test
test: lint
pytest featuretools_update_client/tests
pytest -s -vv -x featuretools_update_client/tests

.PHONY: testcoverage
testcoverage: lint
pytest featuretools_update_client/tests --cov=featuretools_update_client
pytest -s -vv -x featuretools_update_client/tests --cov=featuretools_update_client

.PHONY: installdeps
installdeps:
Expand Down
24 changes: 15 additions & 9 deletions featuretools_update_client/functions.py
@@ -1,3 +1,4 @@
import os
import warnings

import featuretools as ft
Expand All @@ -12,15 +13,20 @@ def initialize(version=ft.__version__):


def check_version(version=ft.__version__):
data = get_response_json(version=version)
ft_update_check = os.environ.get('FEATURETOOLS_UPDATE_CHECKER', True)

try:
is_latest = data['is_latest']
version = data['version']

except Exception:
if ft_update_check in ['0', 'False', 'false', 'FALSE']:
return
else:
data = get_response_json(version=version)

try:
is_latest = data['is_latest']
version = data['version']

except Exception:
return

if not is_latest:
msg = "Featuretools is out-of-date, latest == %s" % version
warnings.warn(msg)
if not is_latest:
msg = "Featuretools is out-of-date, latest == %s" % version
warnings.warn(msg)
125 changes: 95 additions & 30 deletions featuretools_update_client/tests/test_featuretools_update_client.py
Expand Up @@ -25,18 +25,60 @@ def test_current_version_live(self):
data = get_response_json(version='0.7')
version = data['version']

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version(version=version)
self.assertEqual(len(w), 0)
with patch.dict('os.environ', {'FEATURETOOLS_UPDATE_CHECKER': 'TRUE'}):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version(version=version)
self.assertEqual(len(w), 0)

@skipIf(SKIP_REAL, 'Skipping tests that hit the real API server.')
def test_old_version_live(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version(version='0.7')
self.assertEqual(len(w), 1)
assert "Featuretools is out-of-date, latest ==" in str(w[-1].message)
with patch.dict('os.environ', {'FEATURETOOLS_UPDATE_CHECKER': 'TRUE'}):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version(version='0.7')
self.assertEqual(len(w), 1)
assert "Featuretools is out-of-date, latest ==" in str(w[-1].message)

@patch('featuretools_update_client.utils.requests.get')
def test_timeout(self, mock_get):
mock_get.side_effect = requests.exceptions.Timeout

with patch.dict('os.environ', {'FEATURETOOLS_UPDATE_CHECKER': 'TRUE'}):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version(version='0.7.1')
self.assertEqual(len(w), 0)

@patch('featuretools_update_client.utils.requests.get')
def test_connection_error(self, mock_get):
mock_get.side_effect = requests.exceptions.ConnectionError

with patch.dict('os.environ', {'FEATURETOOLS_UPDATE_CHECKER': 'TRUE'}):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version(version='0.7.1')
self.assertEqual(len(w), 0)

@patch('featuretools_update_client.utils.requests.get')
def test_too_many_redirects(self, mock_get):
mock_get.side_effect = requests.exceptions.TooManyRedirects

with patch.dict('os.environ', {'FEATURETOOLS_UPDATE_CHECKER': 'TRUE'}):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version(version='0.7.1')
self.assertEqual(len(w), 0)

@patch('featuretools_update_client.utils.requests.get')
def test_httperror(self, mock_get):
mock_get.side_effect = requests.exceptions.HTTPError

with patch.dict('os.environ', {'FEATURETOOLS_UPDATE_CHECKER': 'TRUE'}):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version(version='0.7.1')
self.assertEqual(len(w), 0)

@patch('featuretools_update_client.utils.requests.get')
def test_current_version_mock(self, mock_get):
Expand All @@ -47,10 +89,11 @@ def test_current_version_mock(self, mock_get):
mock_response.json.return_value = return_json
mock_get.return_value = mock_response

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version(version='0.7.1')
self.assertEqual(len(w), 0)
with patch.dict('os.environ', {'FEATURETOOLS_UPDATE_CHECKER': 'TRUE'}):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version(version='0.7.1')
self.assertEqual(len(w), 0)

@patch('featuretools_update_client.utils.requests.get')
def test_old_version_mock(self, mock_get):
Expand All @@ -61,11 +104,12 @@ def test_old_version_mock(self, mock_get):
mock_response.json.return_value = return_json
mock_get.return_value = mock_response

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version(version='0.7')
self.assertEqual(len(w), 1)
assert "Featuretools is out-of-date, latest ==" in str(w[-1].message)
with patch.dict('os.environ', {'FEATURETOOLS_UPDATE_CHECKER': 'TRUE'}):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version(version='0.7')
self.assertEqual(len(w), 1)
assert "Featuretools is out-of-date, latest ==" in str(w[-1].message)

@patch('featuretools_update_client.utils.requests.get')
def test_ok_but_empty_response(self, mock_get):
Expand All @@ -74,10 +118,11 @@ def test_ok_but_empty_response(self, mock_get):
mock_response.json.return_value = return_json
mock_get.return_value = mock_response

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version()
self.assertEqual(len(w), 0)
with patch.dict('os.environ', {'FEATURETOOLS_UPDATE_CHECKER': 'TRUE'}):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version()
self.assertEqual(len(w), 0)

@patch('featuretools_update_client.utils.requests.get')
def test_bad_response(self, mock_get):
Expand All @@ -86,18 +131,38 @@ def test_bad_response(self, mock_get):
mock_response.raise_for_status.side_effect = http_error
mock_get.return_value = mock_response

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version()
self.assertEqual(len(w), 0)
with patch.dict('os.environ', {'FEATURETOOLS_UPDATE_CHECKER': 'TRUE'}):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version()
self.assertEqual(len(w), 0)

@patch('featuretools_update_client.utils.requests.get')
def test_non_json_response(self, mock_get):
mock_response = Mock()
mock_response.content = "Text response"
mock_get.return_value = mock_response

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version()
self.assertEqual(len(w), 0)
with patch.dict('os.environ', {'FEATURETOOLS_UPDATE_CHECKER': 'TRUE'}):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version()
self.assertEqual(len(w), 0)

@patch('featuretools_update_client.utils.requests.get')
def test_environment_variables(self, mock_get):
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 FEATURETOOLS_UPDATE_CHECKER environment variable to false
for env_value in ['0', 'False', 'false', 'FALSE']:
with patch.dict('os.environ', {'FEATURETOOLS_UPDATE_CHECKER': env_value}):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
featuretools_update_client.check_version(version='0.7')
self.assertEqual(len(w), 0)

0 comments on commit 221f8e4

Please sign in to comment.