tests: add ws2 api tests for retries

This commit is contained in:
James Woglom
2021-04-24 22:48:45 -04:00
parent fe941c432f
commit 18f1149b28
3 changed files with 52 additions and 2 deletions
+4
View File
@@ -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
+2 -2
View File
@@ -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
+46
View File
@@ -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()