Files
notifications-api/tests/app/celery/test_reporting_tasks.py
T

645 lines
20 KiB
Python
Raw Normal View History

from datetime import date, datetime, time, timedelta
2018-09-25 15:59:08 +01:00
from decimal import Decimal
2023-06-15 10:45:03 -07:00
from uuid import UUID
2018-09-25 15:59:08 +01:00
import pytest
from freezegun import freeze_time
2024-11-14 11:26:53 -08:00
from sqlalchemy import func, select
2018-09-25 15:59:08 +01:00
2024-10-31 14:25:35 -07:00
from app import db
from app.celery.reporting_tasks import (
create_nightly_billing,
create_nightly_billing_for_day,
2021-03-10 13:55:06 +00:00
create_nightly_notification_status,
create_nightly_notification_status_for_service_and_day,
)
2021-03-10 13:55:06 +00:00
from app.config import QueueNames
from app.dao.fact_billing_dao import get_rate
2024-02-09 12:24:09 -05:00
from app.enums import KeyType, NotificationStatus, NotificationType, TemplateType
2024-01-16 15:12:57 -05:00
from app.models import FactBilling, FactNotificationStatus, Notification
2024-05-23 13:59:51 -07:00
from app.utils import utc_now
2021-03-10 13:55:06 +00:00
from tests.app.db import (
create_notification,
create_notification_history,
2021-03-10 13:55:06 +00:00
create_rate,
create_service,
create_template,
)
2018-03-14 17:07:52 +00:00
2018-09-17 14:12:06 +01:00
def mocker_get_rate(
2023-04-11 13:29:37 -07:00
non_letter_rates, notification_type, local_date, rate_multiplier=None
2018-09-17 14:12:06 +01:00
):
2024-02-28 12:41:57 -05:00
if notification_type == NotificationType.SMS:
2018-03-14 17:07:52 +00:00
return Decimal(1.33)
2024-02-28 12:41:57 -05:00
elif notification_type == NotificationType.EMAIL:
2018-03-14 17:07:52 +00:00
return Decimal(0)
2023-08-29 14:54:30 -07:00
@freeze_time("2019-08-01T05:30")
@pytest.mark.parametrize(
"day_start, expected_kwargs",
[
(None, [f"2019-07-{31-i}" for i in range(10)]),
("2019-07-21", [f"2019-07-{21-i}" for i in range(10)]),
],
)
def test_create_nightly_billing_triggers_tasks_for_days(
notify_api, mocker, day_start, expected_kwargs
):
mock_celery = mocker.patch(
"app.celery.reporting_tasks.create_nightly_billing_for_day"
)
create_nightly_billing(day_start)
assert mock_celery.apply_async.call_count == 10
for i in range(10):
2023-08-29 14:54:30 -07:00
assert mock_celery.apply_async.call_args_list[i][1]["kwargs"] == {
"process_day": expected_kwargs[i]
}
2023-08-29 14:54:30 -07:00
@freeze_time("2019-08-01T00:30")
def test_create_nightly_notification_status_triggers_tasks(
notify_api,
sample_service,
sample_template,
mocker,
):
mock_celery = mocker.patch(
2023-08-29 14:54:30 -07:00
"app.celery.reporting_tasks.create_nightly_notification_status_for_service_and_day"
).apply_async
2023-08-29 14:54:30 -07:00
create_notification(template=sample_template, created_at="2019-07-31")
create_nightly_notification_status()
mock_celery.assert_called_with(
kwargs={
2023-08-29 14:54:30 -07:00
"service_id": sample_service.id,
"process_day": "2019-07-31",
2024-02-28 12:41:57 -05:00
"notification_type": NotificationType.SMS,
},
2023-08-29 14:54:30 -07:00
queue=QueueNames.REPORTING,
)
2023-08-29 14:54:30 -07:00
@freeze_time("2019-08-01T00:30")
@pytest.mark.parametrize(
"notification_date, expected_types_aggregated",
[
("2019-08-01", set()),
2024-02-28 12:41:57 -05:00
("2019-07-31", {NotificationType.EMAIL, NotificationType.SMS}),
("2019-07-28", {NotificationType.EMAIL, NotificationType.SMS}),
2023-08-29 14:54:30 -07:00
("2019-07-21", set()),
],
)
def test_create_nightly_notification_status_triggers_relevant_tasks(
notify_api,
sample_service,
mocker,
notification_date,
expected_types_aggregated,
):
mock_celery = mocker.patch(
2023-08-29 14:54:30 -07:00
"app.celery.reporting_tasks.create_nightly_notification_status_for_service_and_day"
).apply_async
2024-01-10 09:48:32 -05:00
for notification_type in NotificationType:
template = create_template(sample_service, template_type=notification_type)
create_notification(template=template, created_at=notification_date)
create_nightly_notification_status()
2023-08-29 14:54:30 -07:00
types = {
call.kwargs["kwargs"]["notification_type"] for call in mock_celery.mock_calls
}
assert types == expected_types_aggregated
def test_create_nightly_billing_for_day_checks_history(
2023-08-29 14:54:30 -07:00
sample_service, sample_template, mocker
):
yesterday = datetime.now() - timedelta(days=1)
2023-08-29 14:54:30 -07:00
mocker.patch("app.dao.fact_billing_dao.get_rate", side_effect=mocker_get_rate)
create_notification(
created_at=yesterday,
2023-05-18 09:46:36 -07:00
template=sample_template,
2024-02-09 12:24:09 -05:00
status=NotificationStatus.SENDING,
)
create_notification_history(
created_at=yesterday,
2023-05-18 09:46:36 -07:00
template=sample_template,
2024-02-09 12:24:09 -05:00
status=NotificationStatus.DELIVERED,
)
2024-10-31 14:25:35 -07:00
records = _get_fact_billing_records()
assert len(records) == 0
create_nightly_billing_for_day(str(yesterday.date()))
2024-10-31 14:25:35 -07:00
records = _get_fact_billing_records()
assert len(records) == 1
record = records[0]
2024-02-28 12:41:57 -05:00
assert record.notification_type == NotificationType.SMS
assert record.notifications_sent == 2
2024-10-31 14:25:35 -07:00
def _get_fact_billing_records():
stmt = select(FactBilling)
return db.session.execute(stmt).scalars().all()
2023-08-29 14:54:30 -07:00
@pytest.mark.parametrize(
"second_rate, records_num, billable_units, multiplier",
[(1.0, 1, 2, [1]), (2.0, 2, 1, [1, 2])],
)
def test_create_nightly_billing_for_day_sms_rate_multiplier(
sample_service,
sample_template,
mocker,
second_rate,
records_num,
billable_units,
2023-08-29 14:54:30 -07:00
multiplier,
):
2018-03-14 17:07:52 +00:00
yesterday = datetime.now() - timedelta(days=1)
2023-08-29 14:54:30 -07:00
mocker.patch("app.dao.fact_billing_dao.get_rate", side_effect=mocker_get_rate)
2018-03-14 17:07:52 +00:00
# These are sms notifications
2018-09-25 15:59:08 +01:00
create_notification(
2018-03-14 17:07:52 +00:00
created_at=yesterday,
template=sample_template,
2024-02-09 12:24:09 -05:00
status=NotificationStatus.DELIVERED,
2023-08-29 14:54:30 -07:00
sent_by="sns",
2018-03-14 17:07:52 +00:00
international=False,
rate_multiplier=1.0,
billable_units=1,
)
2018-09-25 15:59:08 +01:00
create_notification(
2018-03-14 17:07:52 +00:00
created_at=yesterday,
template=sample_template,
2024-02-09 12:24:09 -05:00
status=NotificationStatus.DELIVERED,
2023-08-29 14:54:30 -07:00
sent_by="sns",
2018-03-14 17:07:52 +00:00
international=False,
rate_multiplier=second_rate,
billable_units=1,
)
2018-03-16 09:22:34 +00:00
2024-10-31 14:25:35 -07:00
records = _get_fact_billing_records()
2018-03-14 17:07:52 +00:00
assert len(records) == 0
2018-03-16 09:22:34 +00:00
create_nightly_billing_for_day(str(yesterday.date()))
2024-11-15 10:43:13 -08:00
records = (
db.session.execute(select(FactBilling).order_by("rate_multiplier"))
.scalars()
.all()
)
2018-03-14 17:07:52 +00:00
assert len(records) == records_num
2018-03-14 17:07:52 +00:00
for i, record in enumerate(records):
2022-11-21 11:49:59 -05:00
assert record.local_date == datetime.date(yesterday)
2018-03-14 17:07:52 +00:00
assert record.rate == Decimal(1.33)
assert record.billable_units == billable_units
assert record.rate_multiplier == multiplier[i]
def test_create_nightly_billing_for_day_different_templates(
2023-08-29 14:54:30 -07:00
sample_service, sample_template, sample_email_template, mocker
):
2018-03-14 17:07:52 +00:00
yesterday = datetime.now() - timedelta(days=1)
2023-08-29 14:54:30 -07:00
mocker.patch("app.dao.fact_billing_dao.get_rate", side_effect=mocker_get_rate)
2018-03-14 17:07:52 +00:00
2018-09-25 15:59:08 +01:00
create_notification(
2018-03-14 17:07:52 +00:00
created_at=yesterday,
template=sample_template,
2024-02-09 12:24:09 -05:00
status=NotificationStatus.DELIVERED,
2023-08-29 14:54:30 -07:00
sent_by="sns",
2018-03-14 17:07:52 +00:00
international=False,
rate_multiplier=1.0,
billable_units=1,
)
2018-09-25 15:59:08 +01:00
create_notification(
2018-03-14 17:07:52 +00:00
created_at=yesterday,
template=sample_email_template,
2024-02-09 12:24:09 -05:00
status=NotificationStatus.DELIVERED,
2023-08-29 14:54:30 -07:00
sent_by="sns",
2018-03-14 17:07:52 +00:00
international=False,
rate_multiplier=0,
billable_units=0,
)
2018-03-16 09:22:34 +00:00
2024-10-31 14:25:35 -07:00
records = _get_fact_billing_records()
2018-03-14 17:07:52 +00:00
assert len(records) == 0
create_nightly_billing_for_day(str(yesterday.date()))
2018-03-16 09:22:34 +00:00
2024-11-15 10:43:13 -08:00
records = (
db.session.execute(select(FactBilling).order_by("rate_multiplier"))
2024-11-15 10:57:19 -08:00
.scalars()
2024-11-15 10:43:13 -08:00
.all()
)
2018-03-14 17:07:52 +00:00
assert len(records) == 2
multiplier = [0, 1]
billable_units = [0, 1]
rate = [0, Decimal(1.33)]
2018-03-14 17:07:52 +00:00
for i, record in enumerate(records):
2022-11-21 11:49:59 -05:00
assert record.local_date == datetime.date(yesterday)
2018-03-14 17:07:52 +00:00
assert record.rate == rate[i]
assert record.billable_units == billable_units[i]
assert record.rate_multiplier == multiplier[i]
2023-05-18 10:56:58 -07:00
def test_create_nightly_billing_for_day_same_sent_by(
2023-08-29 14:54:30 -07:00
sample_service, sample_template, sample_email_template, mocker
):
2018-03-14 17:07:52 +00:00
yesterday = datetime.now() - timedelta(days=1)
2023-08-29 14:54:30 -07:00
mocker.patch("app.dao.fact_billing_dao.get_rate", side_effect=mocker_get_rate)
2018-03-14 17:07:52 +00:00
# These are sms notifications
2018-09-25 15:59:08 +01:00
create_notification(
2018-03-14 17:07:52 +00:00
created_at=yesterday,
template=sample_template,
2024-02-09 12:24:09 -05:00
status=NotificationStatus.DELIVERED,
2023-08-29 14:54:30 -07:00
sent_by="sns",
2018-03-14 17:07:52 +00:00
international=False,
rate_multiplier=1.0,
billable_units=1,
)
2018-09-25 15:59:08 +01:00
create_notification(
2018-03-14 17:07:52 +00:00
created_at=yesterday,
template=sample_template,
2024-02-09 12:24:09 -05:00
status=NotificationStatus.DELIVERED,
2023-08-29 14:54:30 -07:00
sent_by="sns",
2018-03-14 17:07:52 +00:00
international=False,
rate_multiplier=1.0,
billable_units=1,
)
2018-03-16 09:22:34 +00:00
2024-10-31 14:25:35 -07:00
records = _get_fact_billing_records()
2018-03-14 17:07:52 +00:00
assert len(records) == 0
create_nightly_billing_for_day(str(yesterday.date()))
2018-03-16 09:22:34 +00:00
2024-11-15 10:43:13 -08:00
records = (
db.session.execute(select(FactBilling).order_by("rate_multiplier"))
.scalars()
.all()
)
2023-05-18 10:56:58 -07:00
assert len(records) == 1
for _, record in enumerate(records):
2022-11-21 11:49:59 -05:00
assert record.local_date == datetime.date(yesterday)
2018-03-14 17:07:52 +00:00
assert record.rate == Decimal(1.33)
2023-05-18 10:56:58 -07:00
assert record.billable_units == 2
2018-03-14 17:07:52 +00:00
assert record.rate_multiplier == 1.0
2022-10-14 14:45:27 +00:00
def test_create_nightly_billing_for_day_null_sent_by_sms(
2023-08-29 14:54:30 -07:00
sample_service, sample_template, mocker
):
2018-03-14 17:07:52 +00:00
yesterday = datetime.now() - timedelta(days=1)
2023-08-29 14:54:30 -07:00
mocker.patch("app.dao.fact_billing_dao.get_rate", side_effect=mocker_get_rate)
2018-03-14 17:07:52 +00:00
2018-09-25 15:59:08 +01:00
create_notification(
2018-03-14 17:07:52 +00:00
created_at=yesterday,
template=sample_template,
2024-02-09 12:24:09 -05:00
status=NotificationStatus.DELIVERED,
2018-03-14 17:07:52 +00:00
sent_by=None,
international=False,
rate_multiplier=1.0,
billable_units=1,
)
2024-10-31 14:25:35 -07:00
records = _get_fact_billing_records()
2018-03-14 17:07:52 +00:00
assert len(records) == 0
2018-03-16 09:22:34 +00:00
create_nightly_billing_for_day(str(yesterday.date()))
2024-10-31 14:25:35 -07:00
records = _get_fact_billing_records()
2018-03-14 17:07:52 +00:00
assert len(records) == 1
2018-03-14 17:07:52 +00:00
record = records[0]
2022-11-21 11:49:59 -05:00
assert record.local_date == datetime.date(yesterday)
2018-03-14 17:07:52 +00:00
assert record.rate == Decimal(1.33)
assert record.billable_units == 1
assert record.rate_multiplier == 1
2023-08-29 14:54:30 -07:00
assert record.provider == "unknown"
2018-03-14 17:07:52 +00:00
2018-09-25 15:59:08 +01:00
def test_get_rate_for_sms_and_email(notify_db_session):
non_letter_rates = [
2024-02-28 12:41:57 -05:00
create_rate(datetime(2017, 12, 1), 0.15, NotificationType.SMS),
create_rate(datetime(2017, 12, 1), 0, NotificationType.EMAIL),
]
2018-03-14 17:07:52 +00:00
2024-02-28 12:41:57 -05:00
rate = get_rate(non_letter_rates, NotificationType.SMS, date(2018, 1, 1))
2018-03-14 17:07:52 +00:00
assert rate == Decimal(0.15)
2024-02-28 12:41:57 -05:00
rate = get_rate(non_letter_rates, NotificationType.EMAIL, date(2018, 1, 1))
2018-03-14 17:07:52 +00:00
assert rate == Decimal(0)
2018-03-27 10:37:56 +01:00
2023-08-29 14:54:30 -07:00
@freeze_time("2018-03-26T04:30:00")
2018-03-27 10:37:56 +01:00
# summer time starts on 2018-03-25
def test_create_nightly_billing_for_day_use_BST(
2023-08-29 14:54:30 -07:00
sample_service, sample_template, mocker
):
mocker.patch("app.dao.fact_billing_dao.get_rate", side_effect=mocker_get_rate)
2018-03-27 10:37:56 +01:00
# too late
2018-09-25 15:59:08 +01:00
create_notification(
2022-11-10 16:54:48 -05:00
created_at=datetime(2018, 3, 26, 4, 1),
2018-03-27 10:37:56 +01:00
template=sample_template,
2024-02-09 12:24:09 -05:00
status=NotificationStatus.DELIVERED,
2018-03-27 10:37:56 +01:00
rate_multiplier=1.0,
billable_units=1,
)
2018-09-25 15:59:08 +01:00
create_notification(
created_at=datetime(2018, 3, 25, 23, 59),
2018-03-27 10:37:56 +01:00
template=sample_template,
2024-02-09 12:24:09 -05:00
status=NotificationStatus.DELIVERED,
2018-03-27 10:37:56 +01:00
rate_multiplier=1.0,
billable_units=2,
2018-03-27 10:37:56 +01:00
)
# too early
create_notification(
created_at=datetime(2018, 3, 24, 23, 59),
template=sample_template,
2024-02-09 12:24:09 -05:00
status=NotificationStatus.DELIVERED,
rate_multiplier=1.0,
billable_units=4,
)
2024-11-14 11:26:53 -08:00
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
2023-08-29 14:54:30 -07:00
create_nightly_billing_for_day("2018-03-25")
2024-11-15 10:43:13 -08:00
records = (
db.session.execute(select(FactBilling).order_by(FactBilling.local_date))
.scalars()
.all()
)
2018-03-27 10:37:56 +01:00
assert len(records) == 1
2022-11-21 11:49:59 -05:00
assert records[0].local_date == date(2018, 3, 25)
assert records[0].billable_units == 2
2023-08-29 14:54:30 -07:00
@freeze_time("2018-01-15T08:30:00")
def test_create_nightly_billing_for_day_update_when_record_exists(
2023-08-29 14:54:30 -07:00
sample_service, sample_template, mocker
):
mocker.patch("app.dao.fact_billing_dao.get_rate", side_effect=mocker_get_rate)
2018-09-25 15:59:08 +01:00
create_notification(
created_at=datetime.now() - timedelta(days=1),
template=sample_template,
2024-02-09 12:24:09 -05:00
status=NotificationStatus.DELIVERED,
sent_by=None,
international=False,
rate_multiplier=1.0,
billable_units=1,
)
2024-10-31 14:25:35 -07:00
records = _get_fact_billing_records()
assert len(records) == 0
2023-08-29 14:54:30 -07:00
create_nightly_billing_for_day("2018-01-14")
2024-11-15 10:43:13 -08:00
records = (
db.session.execute(select(FactBilling).order_by(FactBilling.local_date))
.scalars()
.all()
)
assert len(records) == 1
2022-11-21 11:49:59 -05:00
assert records[0].local_date == date(2018, 1, 14)
assert records[0].billable_units == 1
assert not records[0].updated_at
2018-09-25 15:59:08 +01:00
create_notification(
created_at=datetime.now() - timedelta(days=1),
template=sample_template,
2024-02-09 12:24:09 -05:00
status=NotificationStatus.DELIVERED,
sent_by=None,
international=False,
rate_multiplier=1.0,
billable_units=1,
)
# run again, make sure create_nightly_billing() updates with no error
2023-08-29 14:54:30 -07:00
create_nightly_billing_for_day("2018-01-14")
assert len(records) == 1
assert records[0].billable_units == 2
assert records[0].updated_at
def test_create_nightly_notification_status_for_service_and_day(notify_db_session):
2023-08-29 14:54:30 -07:00
first_service = create_service(service_name="First Service")
first_template = create_template(service=first_service)
2023-08-29 14:54:30 -07:00
second_service = create_service(service_name="second Service")
2024-02-13 11:04:02 -05:00
second_template = create_template(
service=second_service,
template_type=TemplateType.EMAIL,
)
2024-05-23 13:59:51 -07:00
process_day = utc_now().date() - timedelta(days=5)
2022-11-10 16:54:48 -05:00
with freeze_time(datetime.combine(process_day, time.max)):
2024-02-13 11:04:02 -05:00
create_notification(
template=first_template,
status=NotificationStatus.DELIVERED,
)
2024-02-09 12:24:09 -05:00
create_notification(template=second_template, status=NotificationStatus.FAILED)
# team API key notifications are included
2023-08-29 14:54:30 -07:00
create_notification(
2024-02-13 11:04:02 -05:00
template=second_template,
status=NotificationStatus.SENDING,
key_type=KeyType.TEAM,
2023-08-29 14:54:30 -07:00
)
# test notifications are ignored
2023-08-29 14:54:30 -07:00
create_notification(
2024-02-13 11:04:02 -05:00
template=second_template,
status=NotificationStatus.SENDING,
key_type=KeyType.TEST,
2023-08-29 14:54:30 -07:00
)
# historical notifications are included
2024-02-13 11:04:02 -05:00
create_notification_history(
template=second_template,
status=NotificationStatus.DELIVERED,
)
# these created notifications from a different day get ignored
2024-05-23 13:59:51 -07:00
with freeze_time(datetime.combine(utc_now().date() - timedelta(days=4), time.max)):
create_notification(template=first_template)
create_notification_history(template=second_template)
2024-11-15 07:48:49 -08:00
assert len(db.session.execute(select(FactNotificationStatus)).scalars().all()) == 0
2023-08-29 14:54:30 -07:00
create_nightly_notification_status_for_service_and_day(
2024-02-13 11:04:02 -05:00
str(process_day),
first_service.id,
NotificationType.SMS,
2023-08-29 14:54:30 -07:00
)
create_nightly_notification_status_for_service_and_day(
2024-02-13 11:04:02 -05:00
str(process_day),
second_service.id,
NotificationType.EMAIL,
2023-08-29 14:54:30 -07:00
)
2024-11-15 10:43:13 -08:00
new_fact_data = (
db.session.execute(
select(FactNotificationStatus).order_by(
FactNotificationStatus.notification_type,
FactNotificationStatus.notification_status,
)
)
.scalars()
.all()
)
2023-06-15 10:45:03 -07:00
assert len(new_fact_data) == 4
2024-02-28 15:54:35 -05:00
email_delivered = (NotificationType.EMAIL, NotificationStatus.DELIVERED)
email_sending = (NotificationType.EMAIL, NotificationStatus.SENDING)
email_failure = (NotificationType.EMAIL, NotificationStatus.FAILED)
sms_delivered = (NotificationType.SMS, NotificationStatus.DELIVERED)
2023-03-02 20:20:31 -05:00
2024-02-28 15:54:35 -05:00
for row in new_fact_data:
current = (row.notification_type, row.notification_status)
if current == email_delivered:
assert row.template_id == second_template.id
assert row.service_id == second_service.id
assert row.notification_type == NotificationType.EMAIL
assert row.notification_status == NotificationStatus.DELIVERED
assert row.notification_count == 1
assert row.key_type == KeyType.NORMAL
elif current == email_failure:
assert row.template_id == second_template.id
assert row.service_id == second_service.id
assert row.notification_type == NotificationType.EMAIL
assert row.notification_status == NotificationStatus.FAILED
assert row.notification_count == 1
assert row.key_type == KeyType.NORMAL
elif current == email_sending:
assert row.local_date == process_day
assert row.template_id == second_template.id
assert row.service_id == second_service.id
assert row.job_id == UUID("00000000-0000-0000-0000-000000000000")
assert row.notification_type == NotificationType.EMAIL
assert row.notification_status == NotificationStatus.SENDING
assert row.notification_count == 1
assert row.key_type == KeyType.TEAM
elif current == sms_delivered:
assert row.template_id == first_template.id
assert row.service_id == first_service.id
assert row.notification_type == NotificationType.SMS
assert row.notification_status == NotificationStatus.DELIVERED
assert row.notification_count == 1
assert row.key_type == KeyType.NORMAL
2023-08-29 14:54:30 -07:00
def test_create_nightly_notification_status_for_service_and_day_overwrites_old_data(
notify_db_session,
):
first_service = create_service(service_name="First Service")
first_template = create_template(service=first_service)
2024-05-23 13:59:51 -07:00
process_day = utc_now().date()
2022-02-16 12:30:41 +00:00
# first run: one notification, expect one row (just one status)
2024-02-13 11:04:02 -05:00
notification = create_notification(
template=first_template, status=NotificationStatus.SENDING
)
2023-08-29 14:54:30 -07:00
create_nightly_notification_status_for_service_and_day(
2024-02-13 11:04:02 -05:00
str(process_day),
first_service.id,
NotificationType.SMS,
2023-08-29 14:54:30 -07:00
)
2024-11-15 07:48:49 -08:00
new_fact_data = db.session.execute(select(FactNotificationStatus)).scalars().all()
assert len(new_fact_data) == 1
assert new_fact_data[0].notification_count == 1
2024-02-09 12:24:09 -05:00
assert new_fact_data[0].notification_status == NotificationStatus.SENDING
2022-02-16 12:30:41 +00:00
# second run: status changed, still expect one row (one status)
2024-02-09 12:24:09 -05:00
notification.status = NotificationStatus.DELIVERED
create_notification(template=first_template, status=NotificationStatus.CREATED)
2023-08-29 14:54:30 -07:00
create_nightly_notification_status_for_service_and_day(
2024-02-13 11:04:02 -05:00
str(process_day),
first_service.id,
NotificationType.SMS,
2023-08-29 14:54:30 -07:00
)
2024-11-15 10:43:13 -08:00
updated_fact_data = (
db.session.execute(
select(FactNotificationStatus).order_by(
FactNotificationStatus.notification_status
)
)
.scalars()
.all()
)
2022-02-16 12:30:41 +00:00
assert len(updated_fact_data) == 2
assert updated_fact_data[0].notification_count == 1
2024-02-09 12:24:09 -05:00
assert updated_fact_data[0].notification_status == NotificationStatus.CREATED
2022-02-16 12:30:41 +00:00
assert updated_fact_data[1].notification_count == 1
2024-02-09 12:24:09 -05:00
assert updated_fact_data[1].notification_status == NotificationStatus.DELIVERED
2022-11-16 14:11:52 -05:00
# the job runs at 04:30am EST time.
2023-08-29 14:54:30 -07:00
@freeze_time("2019-04-02T04:30")
def test_create_nightly_notification_status_for_service_and_day_respects_bst(
sample_template,
):
create_notification(
2024-02-13 11:04:02 -05:00
sample_template,
status=NotificationStatus.DELIVERED,
created_at=datetime(2019, 4, 2, 5, 0),
2023-08-29 14:54:30 -07:00
) # too new
2023-08-29 14:54:30 -07:00
create_notification(
2024-02-13 11:04:02 -05:00
sample_template,
status=NotificationStatus.CREATED,
created_at=datetime(2019, 4, 2, 5, 59),
2023-08-29 14:54:30 -07:00
)
create_notification(
2024-02-13 11:04:02 -05:00
sample_template,
status=NotificationStatus.CREATED,
created_at=datetime(2019, 4, 1, 4, 0),
2023-08-29 14:54:30 -07:00
)
2023-08-29 14:54:30 -07:00
create_notification(
2024-02-13 11:04:02 -05:00
sample_template,
status=NotificationStatus.DELIVERED,
created_at=datetime(2019, 3, 21, 17, 59),
2023-08-29 14:54:30 -07:00
) # too old
2023-08-29 14:54:30 -07:00
create_nightly_notification_status_for_service_and_day(
2024-02-13 11:04:02 -05:00
"2019-04-01",
sample_template.service_id,
NotificationType.SMS,
2023-08-29 14:54:30 -07:00
)
2024-11-15 10:43:13 -08:00
noti_status = (
db.session.execute(
select(FactNotificationStatus).order_by(FactNotificationStatus.local_date)
)
.scalars()
.all()
)
assert len(noti_status) == 1
2022-11-21 11:49:59 -05:00
assert noti_status[0].local_date == date(2019, 4, 1)
2024-02-09 12:24:09 -05:00
assert noti_status[0].notification_status == NotificationStatus.CREATED