remove usage of notify_db fixture in unit tests

* notify_db fixture creates the database connection and ensures the test
  db exists and has migrations applied etc. It will run once per session
  (test run).
* notify_db_session fixture runs after your test finishes and deletes
  all non static (eg type table) data.

In unit tests that hit the database (ie: most of them), 99% of the time
we will need to use notify_db_session to ensure everything is reset. The
only time we don't need to use it is when we're querying things such as
"ensure get X works when database is empty". This is such a low
percentage of tests that it's easier for us to just use
notify_db_session every time, and ensure that all our tests run much
more consistently, at the cost of a small bit of performance when
running tests.

We used to use notify_db to access the session object for manually
adding, committing, etc. To dissuade usage of that fixture I've moved
that to the `notify_db_session`. I've then removed all uses of notify_db
that I could find in the codebase.

As a note, if you're writing a test that uses a `sample_x` fixture, all
of those fixtures rely on notify_db_session so you'll get the teardown
functionality for free. If you're just calling eg `create_x` db.py
functions, then you'll need to make you add notify_db_session fixture to
your test, even if you aren't manually accessing the session.
This commit is contained in:
Leo Hemsted
2022-05-03 17:00:51 +01:00
parent 867e8fbce3
commit 6181c60f75
33 changed files with 151 additions and 162 deletions

View File

@@ -29,7 +29,7 @@ def test_process_ses_results(sample_email_template):
assert process_ses_results(response=ses_notification_callback(reference='ref1'))
def test_process_ses_results_retry_called(sample_email_template, notify_db, mocker):
def test_process_ses_results_retry_called(sample_email_template, mocker):
create_notification(sample_email_template, reference='ref1', sent_at=datetime.utcnow(), status='sending')
mocker.patch("app.dao.notifications_dao.dao_update_notifications_by_reference", side_effect=Exception("EXPECTED"))
@@ -102,7 +102,7 @@ def test_ses_callback_should_not_update_notification_status_if_already_delivered
assert mock_upd.call_count == 0
def test_ses_callback_should_retry_if_notification_is_new(client, notify_db, mocker):
def test_ses_callback_should_retry_if_notification_is_new(client, notify_db_session, mocker):
mock_retry = mocker.patch('app.celery.process_ses_receipts_tasks.process_ses_results.retry')
mock_logger = mocker.patch('app.celery.process_ses_receipts_tasks.current_app.logger.error')
@@ -112,7 +112,7 @@ def test_ses_callback_should_retry_if_notification_is_new(client, notify_db, moc
assert mock_retry.call_count == 1
def test_ses_callback_should_log_if_notification_is_missing(client, notify_db, mocker):
def test_ses_callback_should_log_if_notification_is_missing(client, notify_db_session, mocker):
mock_retry = mocker.patch('app.celery.process_ses_receipts_tasks.process_ses_results.retry')
mock_logger = mocker.patch('app.celery.process_ses_receipts_tasks.current_app.logger.warning')
@@ -122,7 +122,7 @@ def test_ses_callback_should_log_if_notification_is_missing(client, notify_db, m
mock_logger.assert_called_once_with('notification not found for reference: ref (update to delivered)')
def test_ses_callback_should_not_retry_if_notification_is_old(client, notify_db, mocker):
def test_ses_callback_should_not_retry_if_notification_is_old(client, notify_db_session, mocker):
mock_retry = mocker.patch('app.celery.process_ses_receipts_tasks.process_ses_results.retry')
mock_logger = mocker.patch('app.celery.process_ses_receipts_tasks.current_app.logger.error')