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

@@ -263,7 +263,6 @@ def test_persist_notification_sets_daily_limit_cache_if_one_does_not_exists(
(False, 'notify-internal-tasks', 'sms', 'test', 'research-mode-tasks', 'provider_tasks.deliver_sms'),
])
def test_send_notification_to_queue(
notify_db,
notify_db_session,
research_mode,
requested_queue,

View File

@@ -169,7 +169,7 @@ def test_receive_notification_without_permissions_does_not_create_inbound_even_w
([INBOUND_SMS_TYPE], False),
([SMS_TYPE], False),
])
def test_check_permissions_for_inbound_sms(notify_db, notify_db_session, permissions, expected_response):
def test_check_permissions_for_inbound_sms(notify_db_session, permissions, expected_response):
service = create_service(service_permissions=permissions)
assert has_inbound_sms_permissions(service.permissions) is expected_response

View File

@@ -544,13 +544,13 @@ def test_get_notifications_for_service_returns_merged_template_content(client, s
def test_get_notification_selects_correct_template_for_personalisation(client,
notify_db,
notify_db_session,
sample_template):
create_notification(sample_template)
original_content = sample_template.content
sample_template.content = '((name))'
dao_update_template(sample_template)
notify_db.session.commit()
notify_db_session.commit()
create_notification(sample_template, personalisation={"name": "foo"})

View File

@@ -217,7 +217,6 @@ def test_service_can_send_to_recipient_passes_for_guest_list_recipient_passes(sa
{"mobile_number": "07513332413"},
])
def test_service_can_send_to_recipient_fails_when_ignoring_guest_list(
notify_db,
notify_db_session,
sample_service,
recipient,