From 6c44f81d1084b4c2590c5b1f350ebe6d10f18d84 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Mon, 18 Nov 2024 07:36:16 -0800 Subject: [PATCH] more --- tests/app/conftest.py | 19 ++++++++++------ .../app/dao/test_service_callback_api_dao.py | 22 +++++++++++++++---- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 38e2e80d2..b0bbf132b 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -6,7 +6,7 @@ import pytest import pytz import requests_mock from flask import current_app, url_for -from sqlalchemy import select +from sqlalchemy import delete, select from sqlalchemy.orm.session import make_transient from app import db @@ -805,7 +805,7 @@ def mou_signed_templates(notify_service): def create_custom_template( service, user, template_config_name, template_type, content="", subject=None ): - template = Template.query.get(current_app.config[template_config_name]) + template = db.session.get(Template, current_app.config[template_config_name]) if not template: data = { "id": current_app.config[template_config_name], @@ -826,7 +826,7 @@ def create_custom_template( @pytest.fixture def notify_service(notify_db_session, sample_user): - service = Service.query.get(current_app.config["NOTIFY_SERVICE_ID"]) + service = db.session.get(Service, current_app.config["NOTIFY_SERVICE_ID"]) if not service: service = Service( name="Notify Service", @@ -915,8 +915,12 @@ def restore_provider_details(notify_db_session): Note: This doesn't technically require notify_db_session (only notify_db), but kept as a requirement to encourage good usage - if you're modifying ProviderDetails' state then it's good to clear down the rest of the DB too """ - existing_provider_details = ProviderDetails.query.all() - existing_provider_details_history = ProviderDetailsHistory.query.all() + existing_provider_details = ( + db.session.execute(select(ProviderDetails)).scalars().all() + ) + existing_provider_details_history = ( + db.session.execute(select(ProviderDetailsHistory)).scalars().all() + ) # make transient removes the objects from the session - since we'll want to delete them later for epd in existing_provider_details: make_transient(epd) @@ -926,8 +930,9 @@ def restore_provider_details(notify_db_session): yield # also delete these as they depend on provider_details - ProviderDetails.query.delete() - ProviderDetailsHistory.query.delete() + db.session.execute(delete(ProviderDetails)) + db.session.execute(delete(ProviderDetailsHistory)) + db.session.commit() notify_db_session.commit() notify_db_session.add_all(existing_provider_details) notify_db_session.add_all(existing_provider_details_history) diff --git a/tests/app/dao/test_service_callback_api_dao.py b/tests/app/dao/test_service_callback_api_dao.py index 7f245a839..1bff31f67 100644 --- a/tests/app/dao/test_service_callback_api_dao.py +++ b/tests/app/dao/test_service_callback_api_dao.py @@ -38,7 +38,11 @@ def test_save_service_callback_api(sample_service): assert callback_api.updated_at is None versioned = ( - ServiceCallbackApi.get_history_model().query.filter_by(id=callback_api.id).one() + db.session.execute( + select(ServiceCallbackApi.get_history_model()).filter_by(id=callback_api.id) + ) + .scalars() + .one() ) assert versioned.id == callback_api.id assert versioned.service_id == sample_service.id @@ -98,7 +102,13 @@ def test_update_service_callback_can_add_two_api_of_different_types(sample_servi callback_type=CallbackType.COMPLAINT, ) save_service_callback_api(complaint) - results = ServiceCallbackApi.query.order_by(ServiceCallbackApi.callback_type).all() + results = ( + db.session.execute( + select(ServiceCallbackApi).order_by(ServiceCallbackApi.callback_type) + ) + .scalars() + .all() + ) assert len(results) == 2 callbacks = [complaint.serialize(), delivery_status.serialize()] @@ -136,8 +146,12 @@ def test_update_service_callback_api(sample_service): assert updated.updated_at is not None versioned_results = ( - ServiceCallbackApi.get_history_model() - .query.filter_by(id=saved_callback_api.id) + db.session.execute( + select(ServiceCallbackApi.get_history_model()).filter_by( + id=saved_callback_api.id + ) + ) + .scalars() .all() ) assert len(versioned_results) == 2