diff --git a/tests/app/celery/test_reporting_tasks.py b/tests/app/celery/test_reporting_tasks.py index 124038d48..8013beb92 100644 --- a/tests/app/celery/test_reporting_tasks.py +++ b/tests/app/celery/test_reporting_tasks.py @@ -4,7 +4,7 @@ from uuid import UUID import pytest from freezegun import freeze_time -from sqlalchemy import select +from sqlalchemy import func, select from app import db from app.celery.reporting_tasks import ( @@ -363,9 +363,12 @@ def test_create_nightly_billing_for_day_use_BST( rate_multiplier=1.0, billable_units=4, ) - - assert Notification.query.count() == 3 - assert FactBilling.query.count() == 0 + stmt = select(func.count()).select_from(Notification) + count = db.session.execute(stmt).scalar() or 0 + assert count == 3 + stmt = select(func.count()).select_from(FactBilling) + count = db.session.execute(stmt).scalar() or 0 + assert count == 0 create_nightly_billing_for_day("2018-03-25") records = FactBilling.query.order_by(FactBilling.local_date).all() diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index 5720b15f9..18cdc1090 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -412,7 +412,7 @@ def test_should_send_template_to_correct_sms_task_and_persist( encryption.encrypt(notification), ) - persisted_notification = Notification.query.one() + persisted_notification = _get_notification_query_one() assert persisted_notification.to == "1" assert persisted_notification.template_id == sample_template_with_placeholders.id assert ( @@ -431,6 +431,11 @@ def test_should_send_template_to_correct_sms_task_and_persist( ) +def _get_notification_query_one(): + stmt = select(Notification) + return db.session.execute(stmt).scalars().one() + + def test_should_save_sms_if_restricted_service_and_valid_number( notify_db_session, mocker ): @@ -451,7 +456,7 @@ def test_should_save_sms_if_restricted_service_and_valid_number( encrypt_notification, ) - persisted_notification = Notification.query.one() + persisted_notification = _get_notification_query_one() assert persisted_notification.to == "1" assert persisted_notification.template_id == template.id assert persisted_notification.template_version == template.version @@ -490,7 +495,7 @@ def test_save_email_should_save_default_email_reply_to_text_on_notification( encryption.encrypt(notification), ) - persisted_notification = Notification.query.one() + persisted_notification = _get_notification_query_one() assert persisted_notification.reply_to_text == "reply_to@digital.fake.gov" @@ -510,7 +515,7 @@ def test_save_sms_should_save_default_sms_sender_notification_reply_to_text_on( encryption.encrypt(notification), ) - persisted_notification = Notification.query.one() + persisted_notification = _get_notification_query_one() assert persisted_notification.reply_to_text == "12345" @@ -577,7 +582,7 @@ def test_should_save_sms_template_to_and_persist_with_job_id(sample_job, mocker) notification_id, encryption.encrypt(notification), ) - persisted_notification = Notification.query.one() + persisted_notification = _get_notification_query_one() assert persisted_notification.to == "1" assert persisted_notification.job_id == sample_job.id assert persisted_notification.template_id == sample_job.template.id @@ -642,7 +647,7 @@ def test_should_use_email_template_and_persist( encryption.encrypt(notification), ) - persisted_notification = Notification.query.one() + persisted_notification = _get_notification_query_one() assert persisted_notification.to == "1" assert ( persisted_notification.template_id == sample_email_template_with_placeholders.id @@ -689,7 +694,7 @@ def test_save_email_should_use_template_version_from_job_not_latest( encryption.encrypt(notification), ) - persisted_notification = Notification.query.one() + persisted_notification = _get_notification_query_one() assert persisted_notification.to == "1" assert persisted_notification.template_id == sample_email_template.id assert persisted_notification.template_version == version_on_notification @@ -718,7 +723,7 @@ def test_should_use_email_template_subject_placeholders( notification_id, encryption.encrypt(notification), ) - persisted_notification = Notification.query.one() + persisted_notification = _get_notification_query_one() assert persisted_notification.to == "1" assert ( persisted_notification.template_id == sample_email_template_with_placeholders.id @@ -759,7 +764,7 @@ def test_save_email_uses_the_reply_to_text_when_provided(sample_email_template, encryption.encrypt(notification), sender_id=other_email_reply_to.id, ) - persisted_notification = Notification.query.one() + persisted_notification = _get_notification_query_one() assert persisted_notification.notification_type == NotificationType.EMAIL assert persisted_notification.reply_to_text == "other@example.com" @@ -784,7 +789,7 @@ def test_save_email_uses_the_default_reply_to_text_if_sender_id_is_none( encryption.encrypt(notification), sender_id=None, ) - persisted_notification = Notification.query.one() + persisted_notification = _get_notification_query_one() assert persisted_notification.notification_type == NotificationType.EMAIL assert persisted_notification.reply_to_text == "default@example.com" @@ -803,7 +808,7 @@ def test_should_use_email_template_and_persist_without_personalisation( notification_id, encryption.encrypt(notification), ) - persisted_notification = Notification.query.one() + persisted_notification = _get_notification_query_one() assert persisted_notification.to == "1" assert persisted_notification.template_id == sample_email_template.id assert persisted_notification.created_at >= now @@ -936,7 +941,7 @@ def test_save_sms_uses_sms_sender_reply_to_text(mocker, notify_db_session): encryption.encrypt(notification), ) - persisted_notification = Notification.query.one() + persisted_notification = _get_notification_query_one() assert persisted_notification.reply_to_text == "+12028675309" @@ -962,7 +967,7 @@ def test_save_sms_uses_non_default_sms_sender_reply_to_text_if_provided( sender_id=new_sender.id, ) - persisted_notification = Notification.query.one() + persisted_notification = _get_notification_query_one() assert persisted_notification.reply_to_text == "new-sender" diff --git a/tests/app/dao/test_api_key_dao.py b/tests/app/dao/test_api_key_dao.py index f63391143..95b971675 100644 --- a/tests/app/dao/test_api_key_dao.py +++ b/tests/app/dao/test_api_key_dao.py @@ -1,9 +1,11 @@ from datetime import timedelta import pytest +from sqlalchemy import func, select from sqlalchemy.exc import IntegrityError from sqlalchemy.orm.exc import NoResultFound +from app import db from app.dao.api_key_dao import ( expire_api_key, get_model_api_keys, @@ -128,8 +130,13 @@ def test_save_api_key_can_create_key_with_same_name_if_other_is_expired(sample_s def test_save_api_key_should_not_create_new_service_history(sample_service): from app.models import Service - assert Service.query.count() == 1 - assert Service.get_history_model().query.count() == 1 + stmt = select(func.count()).select_from(Service) + count = db.session.execute(stmt).scalar() or 0 + assert count == 1 + + stmt = select(func.count()).select_from(Service.get_history_model()) + count = db.session.execute(stmt).scalar() or 0 + assert count == 1 api_key = ApiKey( **{ @@ -141,7 +148,9 @@ def test_save_api_key_should_not_create_new_service_history(sample_service): ) save_model_api_key(api_key) - assert Service.get_history_model().query.count() == 1 + stmt = select(func.count()).select_from(Service.get_history_model()) + count = db.session.execute(stmt).scalar() or 0 + assert count == 1 @pytest.mark.parametrize("days_old, expected_length", [(5, 1), (8, 0)]) diff --git a/tests/app/service/test_callback_rest.py b/tests/app/service/test_callback_rest.py index 28ffe3aff..5cd025d30 100644 --- a/tests/app/service/test_callback_rest.py +++ b/tests/app/service/test_callback_rest.py @@ -1,5 +1,8 @@ import uuid +from sqlalchemy import func, select + +from app import db from app.models import ServiceCallbackApi, ServiceInboundApi from tests.app.db import create_service_callback_api, create_service_inbound_api @@ -101,7 +104,10 @@ def test_delete_service_inbound_api(admin_request, sample_service): ) assert response is None - assert ServiceInboundApi.query.count() == 0 + + stmt = select(func.count()).select_from(ServiceInboundApi) + count = db.session.execute(stmt).scalar() or 0 + assert count == 0 def test_create_service_callback_api(admin_request, sample_service): @@ -207,4 +213,7 @@ def test_delete_service_callback_api(admin_request, sample_service): ) assert response is None - assert ServiceCallbackApi.query.count() == 0 + + stmt = select(func.count()).select_from(ServiceCallbackApi) + count = db.session.execute(stmt).scalar() or 0 + assert count == 0