mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
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:
@@ -526,7 +526,7 @@ def sample_email_notification(notify_db_session):
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def sample_notification_history(notify_db, notify_db_session, sample_template):
|
||||
def sample_notification_history(notify_db_session, sample_template):
|
||||
created_at = datetime.utcnow()
|
||||
sent_at = datetime.utcnow()
|
||||
notification_type = sample_template.template_type
|
||||
@@ -545,8 +545,8 @@ def sample_notification_history(notify_db, notify_db_session, sample_template):
|
||||
api_key_id=api_key and api_key.id,
|
||||
sent_at=sent_at
|
||||
)
|
||||
notify_db.session.add(notification_history)
|
||||
notify_db.session.commit()
|
||||
notify_db_session.add(notification_history)
|
||||
notify_db_session.commit()
|
||||
|
||||
return notification_history
|
||||
|
||||
@@ -852,7 +852,7 @@ def letter_volumes_email_template(notify_service):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def notify_service(sample_user):
|
||||
def notify_service(notify_db_session, sample_user):
|
||||
service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID'])
|
||||
if not service:
|
||||
service = Service(
|
||||
@@ -876,19 +876,19 @@ def notify_service(sample_user):
|
||||
}
|
||||
reply_to = ServiceEmailReplyTo(**data)
|
||||
|
||||
db.session.add(reply_to)
|
||||
db.session.commit()
|
||||
notify_db_session.add(reply_to)
|
||||
notify_db_session.commit()
|
||||
|
||||
return service
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def sample_service_guest_list(notify_db, notify_db_session):
|
||||
def sample_service_guest_list(notify_db_session):
|
||||
service = create_service(check_if_service_exists=True)
|
||||
guest_list_user = ServiceGuestList.from_string(service.id, EMAIL_TYPE, 'guest_list_user@digital.gov.uk')
|
||||
|
||||
notify_db.session.add(guest_list_user)
|
||||
notify_db.session.commit()
|
||||
notify_db_session.add(guest_list_user)
|
||||
notify_db_session.commit()
|
||||
return guest_list_user
|
||||
|
||||
|
||||
@@ -933,7 +933,7 @@ def nhs_email_branding(notify_db_session):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def restore_provider_details(notify_db, notify_db_session):
|
||||
def restore_provider_details(notify_db_session):
|
||||
"""
|
||||
We view ProviderDetails as a static in notify_db_session, since we don't modify it... except we do, we updated
|
||||
priority. This fixture is designed to be used in tests that will knowingly touch provider details, to restore them
|
||||
@@ -955,10 +955,10 @@ def restore_provider_details(notify_db, notify_db_session):
|
||||
# also delete these as they depend on provider_details
|
||||
ProviderDetails.query.delete()
|
||||
ProviderDetailsHistory.query.delete()
|
||||
notify_db.session.commit()
|
||||
notify_db.session.add_all(existing_provider_details)
|
||||
notify_db.session.add_all(existing_provider_details_history)
|
||||
notify_db.session.commit()
|
||||
notify_db_session.commit()
|
||||
notify_db_session.add_all(existing_provider_details)
|
||||
notify_db_session.add_all(existing_provider_details_history)
|
||||
notify_db_session.commit()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
Reference in New Issue
Block a user