fix more sqlalchemy

This commit is contained in:
Kenneth Kehl
2024-11-14 11:26:53 -08:00
parent 99a92e705e
commit 6d8fdab5a3
4 changed files with 48 additions and 22 deletions
+7 -4
View File
@@ -4,7 +4,7 @@ from uuid import UUID
import pytest import pytest
from freezegun import freeze_time from freezegun import freeze_time
from sqlalchemy import select from sqlalchemy import func, select
from app import db from app import db
from app.celery.reporting_tasks import ( from app.celery.reporting_tasks import (
@@ -363,9 +363,12 @@ def test_create_nightly_billing_for_day_use_BST(
rate_multiplier=1.0, rate_multiplier=1.0,
billable_units=4, billable_units=4,
) )
stmt = select(func.count()).select_from(Notification)
assert Notification.query.count() == 3 count = db.session.execute(stmt).scalar() or 0
assert FactBilling.query.count() == 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") create_nightly_billing_for_day("2018-03-25")
records = FactBilling.query.order_by(FactBilling.local_date).all() records = FactBilling.query.order_by(FactBilling.local_date).all()
+18 -13
View File
@@ -412,7 +412,7 @@ def test_should_send_template_to_correct_sms_task_and_persist(
encryption.encrypt(notification), encryption.encrypt(notification),
) )
persisted_notification = Notification.query.one() persisted_notification = _get_notification_query_one()
assert persisted_notification.to == "1" assert persisted_notification.to == "1"
assert persisted_notification.template_id == sample_template_with_placeholders.id assert persisted_notification.template_id == sample_template_with_placeholders.id
assert ( 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( def test_should_save_sms_if_restricted_service_and_valid_number(
notify_db_session, mocker notify_db_session, mocker
): ):
@@ -451,7 +456,7 @@ def test_should_save_sms_if_restricted_service_and_valid_number(
encrypt_notification, encrypt_notification,
) )
persisted_notification = Notification.query.one() persisted_notification = _get_notification_query_one()
assert persisted_notification.to == "1" assert persisted_notification.to == "1"
assert persisted_notification.template_id == template.id assert persisted_notification.template_id == template.id
assert persisted_notification.template_version == template.version 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), 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" 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), encryption.encrypt(notification),
) )
persisted_notification = Notification.query.one() persisted_notification = _get_notification_query_one()
assert persisted_notification.reply_to_text == "12345" 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, notification_id,
encryption.encrypt(notification), encryption.encrypt(notification),
) )
persisted_notification = Notification.query.one() persisted_notification = _get_notification_query_one()
assert persisted_notification.to == "1" assert persisted_notification.to == "1"
assert persisted_notification.job_id == sample_job.id assert persisted_notification.job_id == sample_job.id
assert persisted_notification.template_id == sample_job.template.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), encryption.encrypt(notification),
) )
persisted_notification = Notification.query.one() persisted_notification = _get_notification_query_one()
assert persisted_notification.to == "1" assert persisted_notification.to == "1"
assert ( assert (
persisted_notification.template_id == sample_email_template_with_placeholders.id 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), encryption.encrypt(notification),
) )
persisted_notification = Notification.query.one() persisted_notification = _get_notification_query_one()
assert persisted_notification.to == "1" assert persisted_notification.to == "1"
assert persisted_notification.template_id == sample_email_template.id assert persisted_notification.template_id == sample_email_template.id
assert persisted_notification.template_version == version_on_notification assert persisted_notification.template_version == version_on_notification
@@ -718,7 +723,7 @@ def test_should_use_email_template_subject_placeholders(
notification_id, notification_id,
encryption.encrypt(notification), encryption.encrypt(notification),
) )
persisted_notification = Notification.query.one() persisted_notification = _get_notification_query_one()
assert persisted_notification.to == "1" assert persisted_notification.to == "1"
assert ( assert (
persisted_notification.template_id == sample_email_template_with_placeholders.id 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), encryption.encrypt(notification),
sender_id=other_email_reply_to.id, 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.notification_type == NotificationType.EMAIL
assert persisted_notification.reply_to_text == "other@example.com" 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), encryption.encrypt(notification),
sender_id=None, sender_id=None,
) )
persisted_notification = Notification.query.one() persisted_notification = _get_notification_query_one()
assert persisted_notification.notification_type == NotificationType.EMAIL assert persisted_notification.notification_type == NotificationType.EMAIL
assert persisted_notification.reply_to_text == "default@example.com" 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, notification_id,
encryption.encrypt(notification), encryption.encrypt(notification),
) )
persisted_notification = Notification.query.one() persisted_notification = _get_notification_query_one()
assert persisted_notification.to == "1" assert persisted_notification.to == "1"
assert persisted_notification.template_id == sample_email_template.id assert persisted_notification.template_id == sample_email_template.id
assert persisted_notification.created_at >= now 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), encryption.encrypt(notification),
) )
persisted_notification = Notification.query.one() persisted_notification = _get_notification_query_one()
assert persisted_notification.reply_to_text == "+12028675309" 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, sender_id=new_sender.id,
) )
persisted_notification = Notification.query.one() persisted_notification = _get_notification_query_one()
assert persisted_notification.reply_to_text == "new-sender" assert persisted_notification.reply_to_text == "new-sender"
+12 -3
View File
@@ -1,9 +1,11 @@
from datetime import timedelta from datetime import timedelta
import pytest import pytest
from sqlalchemy import func, select
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.orm.exc import NoResultFound
from app import db
from app.dao.api_key_dao import ( from app.dao.api_key_dao import (
expire_api_key, expire_api_key,
get_model_api_keys, 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): def test_save_api_key_should_not_create_new_service_history(sample_service):
from app.models import Service from app.models import Service
assert Service.query.count() == 1 stmt = select(func.count()).select_from(Service)
assert Service.get_history_model().query.count() == 1 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( 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) 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)]) @pytest.mark.parametrize("days_old, expected_length", [(5, 1), (8, 0)])
+11 -2
View File
@@ -1,5 +1,8 @@
import uuid import uuid
from sqlalchemy import func, select
from app import db
from app.models import ServiceCallbackApi, ServiceInboundApi from app.models import ServiceCallbackApi, ServiceInboundApi
from tests.app.db import create_service_callback_api, create_service_inbound_api 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 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): 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 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