Skip to content

Commit

Permalink
One-time version check (#1456)
Browse files Browse the repository at this point in the history
* One-time version check from package registry (once per module load)

---------

Co-authored-by: Dmitry Shemetov <[email protected]>
Co-authored-by: george <[email protected]>
  • Loading branch information
3 people authored Jul 18, 2024
1 parent 46a46a2 commit 35d67a7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
26 changes: 25 additions & 1 deletion integrations/client/test_delphi_epidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# standard library
import time
import json
from json import JSONDecodeError
from requests.models import Response
from unittest.mock import MagicMock, patch

# first party
Expand Down Expand Up @@ -306,6 +306,30 @@ def test_sandbox(self, get, post):
Epidata.debug = False
Epidata.sandbox = False

@patch('requests.get')
def test_version_check(self, get):
"""Test that the _version_check() function correctly logs a version discrepancy."""
class MockJson:
def __init__(self, content, status_code):
self.content = content
self.status_code = status_code
def raise_for_status(self): pass
def json(self): return json.loads(self.content)
get.reset_mock()
get.return_value = MockJson(b'{"info": {"version": "0.0.1"}}', 200)
Epidata._version_check()
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
20 changes: 18 additions & 2 deletions src/client/delphi_epidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ class Epidata:
BASE_URL = "https://api.delphi.cmu.edu/epidata"
auth = None

client_version = __version__

debug = False # if True, prints extra logging statements
sandbox = False # if True, will not execute any queries

Expand All @@ -54,6 +52,24 @@ def log(evt, **kwargs):
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, runs just once per program execution (on initial module load).
@staticmethod
def _version_check():
try:
latest_version = requests.get('https://pypi.org/pypi/delphi-epidata/json').json()['info']['version']
if latest_version != __version__:
Epidata.log(
"Client version not up to date",
client_version=__version__,
latest_version=latest_version
)
except Exception as e:
Epidata.log("Error getting latest client version", exception=str(e))

# Run this once on module load. Use dunder method for Python <= 3.9 compatibility
# https://stackoverflow.com/a/12718272
_version_check.__func__()

# Helper function to cast values and/or ranges to strings
@staticmethod
def _listitem(value):
Expand Down
8 changes: 8 additions & 0 deletions src/client/packaging/pypi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
All notable future changes to the `delphi_epidata` python client will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/).

## [4.1.25] - 2024-07-18

### Includes
- https://github.com/cmu-delphi/delphi-epidata/pull/1456

### Changed
- Added a one-time check which logs a warning when the newest client version does not match the client version in use.

## [4.1.24] - 2024-07-09

### Includes
Expand Down

0 comments on commit 35d67a7

Please sign in to comment.