From 78ac1ee094004b3cef75b598f81f540fd78a8158 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 31 Oct 2024 14:25:35 -0700 Subject: [PATCH] fix more --- .ds.baseline | 6 ++--- tests/app/celery/test_reporting_tasks.py | 23 ++++++++++++------- .../test_receive_notification.py | 11 +++++++-- .../test_send_notification.py | 11 +++++++-- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/.ds.baseline b/.ds.baseline index bcae9f1e8..8aaa131c5 100644 --- a/.ds.baseline +++ b/.ds.baseline @@ -277,7 +277,7 @@ "filename": "tests/app/notifications/test_receive_notification.py", "hashed_secret": "913a73b565c8e2c8ed94497580f619397709b8b6", "is_verified": false, - "line_number": 24, + "line_number": 26, "is_secret": false }, { @@ -285,7 +285,7 @@ "filename": "tests/app/notifications/test_receive_notification.py", "hashed_secret": "d70eab08607a4d05faa2d0d6647206599e9abc65", "is_verified": false, - "line_number": 54, + "line_number": 56, "is_secret": false } ], @@ -384,5 +384,5 @@ } ] }, - "generated_at": "2024-10-31T18:48:03Z" + "generated_at": "2024-10-31T21:25:32Z" } diff --git a/tests/app/celery/test_reporting_tasks.py b/tests/app/celery/test_reporting_tasks.py index a32f68fc3..124038d48 100644 --- a/tests/app/celery/test_reporting_tasks.py +++ b/tests/app/celery/test_reporting_tasks.py @@ -4,7 +4,9 @@ from uuid import UUID import pytest from freezegun import freeze_time +from sqlalchemy import select +from app import db from app.celery.reporting_tasks import ( create_nightly_billing, create_nightly_billing_for_day, @@ -132,11 +134,11 @@ def test_create_nightly_billing_for_day_checks_history( status=NotificationStatus.DELIVERED, ) - records = FactBilling.query.all() + records = _get_fact_billing_records() assert len(records) == 0 create_nightly_billing_for_day(str(yesterday.date())) - records = FactBilling.query.all() + records = _get_fact_billing_records() assert len(records) == 1 record = records[0] @@ -144,6 +146,11 @@ def test_create_nightly_billing_for_day_checks_history( assert record.notifications_sent == 2 +def _get_fact_billing_records(): + stmt = select(FactBilling) + return db.session.execute(stmt).scalars().all() + + @pytest.mark.parametrize( "second_rate, records_num, billable_units, multiplier", [(1.0, 1, 2, [1]), (2.0, 2, 1, [1, 2])], @@ -181,7 +188,7 @@ def test_create_nightly_billing_for_day_sms_rate_multiplier( billable_units=1, ) - records = FactBilling.query.all() + records = _get_fact_billing_records() assert len(records) == 0 create_nightly_billing_for_day(str(yesterday.date())) @@ -221,7 +228,7 @@ def test_create_nightly_billing_for_day_different_templates( billable_units=0, ) - records = FactBilling.query.all() + records = _get_fact_billing_records() assert len(records) == 0 create_nightly_billing_for_day(str(yesterday.date())) @@ -265,7 +272,7 @@ def test_create_nightly_billing_for_day_same_sent_by( billable_units=1, ) - records = FactBilling.query.all() + records = _get_fact_billing_records() assert len(records) == 0 create_nightly_billing_for_day(str(yesterday.date())) @@ -296,11 +303,11 @@ def test_create_nightly_billing_for_day_null_sent_by_sms( billable_units=1, ) - records = FactBilling.query.all() + records = _get_fact_billing_records() assert len(records) == 0 create_nightly_billing_for_day(str(yesterday.date())) - records = FactBilling.query.all() + records = _get_fact_billing_records() assert len(records) == 1 record = records[0] @@ -384,7 +391,7 @@ def test_create_nightly_billing_for_day_update_when_record_exists( billable_units=1, ) - records = FactBilling.query.all() + records = _get_fact_billing_records() assert len(records) == 0 create_nightly_billing_for_day("2018-01-14") diff --git a/tests/app/notifications/test_receive_notification.py b/tests/app/notifications/test_receive_notification.py index c95088803..e13b8d82e 100644 --- a/tests/app/notifications/test_receive_notification.py +++ b/tests/app/notifications/test_receive_notification.py @@ -4,7 +4,9 @@ from unittest import mock import pytest from flask import json +from sqlalchemy import func, select +from app import db from app.enums import ServicePermissionType from app.models import InboundSms from app.notifications.receive_notifications import ( @@ -99,7 +101,9 @@ def test_receive_notification_from_sns_without_permissions_does_not_persist( parsed_response = json.loads(response.get_data(as_text=True)) assert parsed_response["result"] == "success" - assert InboundSms.query.count() == 0 + stmt = select(func.count()).select_from(InboundSms) + count = db.session.execute(stmt).scalar() or 0 + assert count == 0 assert mocked.called is False @@ -285,7 +289,10 @@ def test_receive_notification_error_if_not_single_matching_service( # we still return 'RECEIVED' to MMG assert response.status_code == 200 assert response.get_data(as_text=True) == "RECEIVED" - assert InboundSms.query.count() == 0 + + stmt = select(func.count()).select_from(InboundSms) + count = db.session.execute(stmt).scalar() or 0 + assert count == 0 @pytest.mark.skip(reason="Need to implement inbound SNS tests. Body here from MMG") diff --git a/tests/app/service/send_notification/test_send_notification.py b/tests/app/service/send_notification/test_send_notification.py index dcd6cc8e7..fd37f7592 100644 --- a/tests/app/service/send_notification/test_send_notification.py +++ b/tests/app/service/send_notification/test_send_notification.py @@ -5,8 +5,10 @@ import pytest from flask import current_app, json from freezegun import freeze_time from notifications_python_client.authentication import create_jwt_token +from sqlalchemy import func, select import app +from app import db from app.dao import notifications_dao from app.dao.api_key_dao import save_model_api_key from app.dao.services_dao import dao_update_service @@ -883,7 +885,9 @@ def test_should_not_persist_notification_or_send_email_if_simulated_email( assert response.status_code == 201 apply_async.assert_not_called() - assert Notification.query.count() == 0 + stmt = select(func.count()).select_from(Notification) + count = db.session.execute(stmt).scalar() or 0 + assert count == 0 @pytest.mark.parametrize("to_sms", ["+14254147755", "+14254147167"]) @@ -906,7 +910,10 @@ def test_should_not_persist_notification_or_send_sms_if_simulated_number( assert response.status_code == 201 apply_async.assert_not_called() - assert Notification.query.count() == 0 + + stmt = select(func.count()).select_from(Notification) + count = db.session.execute(stmt).scalar() or 0 + assert count == 0 @pytest.mark.parametrize("key_type", [KeyType.NORMAL, KeyType.TEAM])