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

@@ -14,7 +14,7 @@ def test_get_provider_details_returns_all_providers(admin_request, notify_db_ses
assert {'ses', 'firetext', 'mmg', 'dvla'} <= {x['identifier'] for x in json_resp}
def test_get_provider_details_by_id(client, notify_db):
def test_get_provider_details_by_id(client, notify_db_session):
response = client.get(
'/provider-details',
headers=[create_admin_authorization_header()]
@@ -103,7 +103,7 @@ def test_should_not_be_able_to_update_disallowed_fields(client, restore_provider
assert resp.status_code == 400
def test_get_provider_versions_contains_correct_fields(client, notify_db):
def test_get_provider_versions_contains_correct_fields(client, notify_db_session):
provider = ProviderDetailsHistory.query.first()
response = client.get(
'/provider-details/{}/versions'.format(provider.id),