I have a process where I'm querying a database in chunks of 100000, and I want to retry up to 5 times if it is a timeout error, otherwise send out an alert. How could I write a unittest for this? I'm not exactly sure how to simulate the environment so I can double check the logic. It's also difficult to test the alert (it's an email), other than just sending it and seeing if it arrives. How would you write a test for this? Here is my code:
def _query_data(self, sql_query: str, retries: int = 5):
cursor = self._return_cursor(self.canvas_conn)
for attempt in range(retries):
try:
cursor.execute(sql_query)
except (ppg2.ProgrammingError, ppg2.OperationalError) as e:
if 'Operation timed out' in str(e):
print('Retrying, attempt {}'.format(attempt))
sleep(3)
continue
elif self.historical_connect:
msg = 'Connected on last run, no longer available.'
self.set_for_alert(msg)
self.historical_connect = False
raise ppg2.Error from e
else:
return self._iterate_results(cursor)
The error gets caught in the process that calls it.
Aucun commentaire:
Enregistrer un commentaire