From b4267560b42042fde78a74ad603e399631dbab48 Mon Sep 17 00:00:00 2001 From: James Woglom Date: Sat, 24 Apr 2021 22:48:45 -0400 Subject: [PATCH] tests: add ws2 api tests for retries --- tconnectsync/api/__init__.py | 4 ++++ tconnectsync/api/ws2.py | 4 ++-- tests/api/test_ws2.py | 46 ++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tests/api/test_ws2.py diff --git a/tconnectsync/api/__init__.py b/tconnectsync/api/__init__.py index 5681c7e..af58423 100644 --- a/tconnectsync/api/__init__.py +++ b/tconnectsync/api/__init__.py @@ -29,6 +29,10 @@ class TConnectApi: if self._ws2: return self._ws2 + # Trigger login or re-login via controliq api if necessary + # so userGuid can be accessed from it + self.controliq + self._ws2 = WS2Api(self._ciq.userGuid) return self._ws2 diff --git a/tconnectsync/api/ws2.py b/tconnectsync/api/ws2.py index 2a4bf84..ef80d52 100644 --- a/tconnectsync/api/ws2.py +++ b/tconnectsync/api/ws2.py @@ -10,7 +10,7 @@ logger = logging.getLogger(__name__) class WS2Api: BASE_URL = 'https://tconnectws2.tandemdiabetes.com/' - MAX_RETRIES = 3 + MAX_RETRIES = 2 userGuid = None @@ -68,7 +68,7 @@ class WS2Api: except ApiException as e: if e.status_code == 500: logger.error("HTTP 500 in therapy_timeline_csv (retry count %d): %s" % (tries, e)) - if tries <= self.MAX_RETRIES: + if tries < self.MAX_RETRIES: return self.therapy_timeline_csv(start, end, tries+1) raise e diff --git a/tests/api/test_ws2.py b/tests/api/test_ws2.py new file mode 100644 index 0000000..9d23dcd --- /dev/null +++ b/tests/api/test_ws2.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +import unittest + +from .fake import WS2Api + +from tconnectsync.api.common import ApiException + +class TestWS2Api(unittest.TestCase): + def fake_get_with_http_500(self, num_times): + tries = 0 + def fake_get(endpoint, query): + nonlocal tries, num_times + if "therapytimeline2csv" in endpoint: + if tries < num_times: + tries += 1 + raise ApiException(500, "fake HTTP 500") + + return "" + raise NotImplementedError + + return fake_get + + def test_therapy_timeline_csv_works_after_two_retries(self): + ws2 = WS2Api() + + ws2.get = self.fake_get_with_http_500(2) + + self.assertEqual( + ws2.therapy_timeline_csv('2021-04-01', '2021-04-02'), + { + "readingData": [], + "iobData": [], + "basalData": [], + "bolusData": [] + }) + + def test_therapy_timeline_csv_fails_after_three_retries(self): + ws2 = WS2Api() + + ws2.get = self.fake_get_with_http_500(3) + + self.assertRaises(ApiException, ws2.therapy_timeline_csv, '2021-04-01', '2021-04-02') + +if __name__ == '__main__': + unittest.main() \ No newline at end of file