Skip to content

Commit

Permalink
Merge pull request #1498 from cmu-delphi/pyclient_fix_fix
Browse files Browse the repository at this point in the history
fix: simplify client version check
  • Loading branch information
minhkhul authored Jul 24, 2024
2 parents 21c7df6 + 51ee2f1 commit ca5d74d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 24 deletions.
13 changes: 0 additions & 13 deletions integrations/client/test_delphi_epidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,27 +318,14 @@ def json(self): return json.loads(self.content)
get.reset_mock()
get.return_value = MockJson(b'{"info": {"version": "0.0.1"}}', 200)

# "back up" the value of this private class var and replace w/ default
# so the ._version_check() method runs unencumbered:
e_vdc__save = Epidata._version_checked
Epidata._version_checked = False
# run version check:
Epidata._version_check()
# "restore" class var:
Epidata._version_checked = e_vdc__save

captured = self.capsys.readouterr()
output = captured.err.splitlines()
self.assertEqual(len(output), 1)
self.assertIn("Client version not up to date", output[0])
self.assertIn("\'latest_version\': \'0.0.1\'", output[0])

@patch('delphi.epidata.client.delphi_epidata.Epidata._version_check')
def test_version_check_once(self, version_check):
"""Test that the _version_check() function is only called once on initial module import."""
from delphi.epidata.client.delphi_epidata import Epidata
version_check.assert_not_called()

def test_geo_value(self):
"""test different variants of geo types: single, *, multi."""

Expand Down
15 changes: 4 additions & 11 deletions src/client/delphi_epidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,18 @@ class Epidata:
debug = False # if True, prints extra logging statements
sandbox = False # if True, will not execute any queries

_version_checked = False

@staticmethod
def log(evt, **kwargs):
kwargs['event'] = evt
kwargs['timestamp'] = time.strftime("%Y-%m-%d %H:%M:%S %z")
return sys.stderr.write(str(kwargs) + "\n")

# Check that this client's version matches the most recent available.
# This is indended to run just once per program execution, on initial module load.
# See the bottom of this file for the ultimate call to this method.
# Check that this client's version matches the most recent available. This
# is run just once per program execution, on initial module load (see the
# bottom of the file). This is a function of how Python's module system
# works: https://docs.python.org/3/reference/import.html#the-module-cache
@staticmethod
def _version_check():
if Epidata._version_checked:
# already done; nothing to do!
return

Epidata._version_checked = True

try:
request = requests.get('https://pypi.org/pypi/delphi-epidata/json', timeout=5)
latest_version = request.json()['info']['version']
Expand Down

0 comments on commit ca5d74d

Please sign in to comment.