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

@@ -258,15 +258,15 @@ def test_send_sms_should_use_template_version_from_notification_not_latest(
(False, KEY_TYPE_TEST)
])
def test_should_call_send_sms_response_task_if_research_mode(
notify_db, sample_service, sample_notification, mocker, research_mode, key_type
notify_db_session, sample_service, sample_notification, mocker, research_mode, key_type
):
mocker.patch('app.mmg_client.send_sms')
mocker.patch('app.delivery.send_to_providers.send_sms_response')
if research_mode:
sample_service.research_mode = True
notify_db.session.add(sample_service)
notify_db.session.commit()
notify_db_session.add(sample_service)
notify_db_session.commit()
sample_notification.key_type = key_type
@@ -459,7 +459,7 @@ def test_get_html_email_renderer_should_return_for_normal_service(sample_service
(BRANDING_BOTH, True),
(BRANDING_ORG_BANNER, False)
])
def test_get_html_email_renderer_with_branding_details(branding_type, govuk_banner, notify_db, sample_service):
def test_get_html_email_renderer_with_branding_details(branding_type, govuk_banner, notify_db_session, sample_service):
email_branding = EmailBranding(
brand_type=branding_type,
@@ -469,8 +469,8 @@ def test_get_html_email_renderer_with_branding_details(branding_type, govuk_bann
text='League of Justice',
)
sample_service.email_branding = email_branding
notify_db.session.add_all([sample_service, email_branding])
notify_db.session.commit()
notify_db_session.add_all([sample_service, email_branding])
notify_db_session.commit()
options = send_to_providers.get_html_email_options(sample_service)
@@ -485,10 +485,10 @@ def test_get_html_email_renderer_with_branding_details(branding_type, govuk_bann
assert options['brand_banner'] is False
def test_get_html_email_renderer_with_branding_details_and_render_govuk_banner_only(notify_db, sample_service):
def test_get_html_email_renderer_with_branding_details_and_render_govuk_banner_only(notify_db_session, sample_service):
sample_service.email_branding = None
notify_db.session.add_all([sample_service])
notify_db.session.commit()
notify_db_session.add_all([sample_service])
notify_db_session.commit()
options = send_to_providers.get_html_email_options(sample_service)