Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
Cliff Hill
2024-02-09 12:24:09 -05:00
parent dcf7885365
commit 9cc3f1c1ff
8 changed files with 104 additions and 100 deletions

View File

@@ -17,7 +17,8 @@ from app.celery.nightly_tasks import (
save_daily_notification_processing_time,
timeout_notifications,
)
from app.models import FactProcessingTime, Job, NotificationType
from app.enums import NotificationType, TemplateType
from app.models import FactProcessingTime, Job
from tests.app.db import (
create_job,
create_notification,
@@ -142,7 +143,7 @@ def test_delete_sms_notifications_older_than_retention_calls_child_task(
"app.celery.nightly_tasks._delete_notifications_older_than_retention_by_type"
)
delete_sms_notifications_older_than_retention()
mocked.assert_called_once_with("sms")
mocked.assert_called_once_with(NotificationType.SMS)
def test_delete_email_notifications_older_than_retentions_calls_child_task(
@@ -276,21 +277,21 @@ def test_delete_notifications_task_calls_task_for_services_with_data_retention_o
email_service = create_service(service_name="b")
letter_service = create_service(service_name="c")
create_service_data_retention(sms_service, notification_type="sms")
create_service_data_retention(email_service, notification_type="email")
create_service_data_retention(letter_service, notification_type="letter")
create_service_data_retention(sms_service, notification_type=NotificationType.SMS)
create_service_data_retention(email_service, notification_type=NotificationType.EMAIL)
create_service_data_retention(letter_service, notification_type=NotificationType.LETTER)
mock_subtask = mocker.patch(
"app.celery.nightly_tasks.delete_notifications_for_service_and_type"
)
_delete_notifications_older_than_retention_by_type("sms")
_delete_notifications_older_than_retention_by_type(NotificationType.SMS)
mock_subtask.apply_async.assert_called_once_with(
queue="reporting-tasks",
kwargs={
"service_id": sms_service.id,
"notification_type": "sms",
"notification_type": NotificationType.SMS,
# three days of retention, its morn of 5th, so we want to keep all messages from 4th, 3rd and 2nd.
"datetime_to_delete_before": date(2021, 6, 2),
},
@@ -310,7 +311,7 @@ def test_delete_notifications_task_calls_task_for_services_with_data_retention_b
"app.celery.nightly_tasks.delete_notifications_for_service_and_type"
)
_delete_notifications_older_than_retention_by_type("sms")
_delete_notifications_older_than_retention_by_type(NotificationType.SMS)
assert mock_subtask.apply_async.call_count == 2
mock_subtask.apply_async.assert_has_calls(
@@ -320,7 +321,7 @@ def test_delete_notifications_task_calls_task_for_services_with_data_retention_b
queue=ANY,
kwargs={
"service_id": service_14_days.id,
"notification_type": "sms",
"notification_type": NotificationType.SMS,
"datetime_to_delete_before": date(2021, 3, 21),
},
),
@@ -328,7 +329,7 @@ def test_delete_notifications_task_calls_task_for_services_with_data_retention_b
queue=ANY,
kwargs={
"service_id": service_3_days.id,
"notification_type": "sms",
"notification_type": NotificationType.SMS,
"datetime_to_delete_before": date(2021, 4, 1),
},
),
@@ -347,7 +348,7 @@ def test_delete_notifications_task_calls_task_for_services_that_have_sent_notifi
create_template(service_will_delete_1)
create_template(service_will_delete_2)
nothing_to_delete_sms_template = create_template(
service_nothing_to_delete, template_type="sms"
service_nothing_to_delete, template_type=TemplateType.SMS
)
nothing_to_delete_email_template = create_template(
service_nothing_to_delete, template_type="email"
@@ -377,7 +378,7 @@ def test_delete_notifications_task_calls_task_for_services_that_have_sent_notifi
"app.celery.nightly_tasks.delete_notifications_for_service_and_type"
)
_delete_notifications_older_than_retention_by_type("sms")
_delete_notifications_older_than_retention_by_type(NotificationType.SMS)
assert mock_subtask.apply_async.call_count == 2
mock_subtask.apply_async.assert_has_calls(
@@ -387,7 +388,7 @@ def test_delete_notifications_task_calls_task_for_services_that_have_sent_notifi
queue=ANY,
kwargs={
"service_id": service_will_delete_1.id,
"notification_type": "sms",
"notification_type": NotificationType.SMS,
"datetime_to_delete_before": date(2021, 3, 26),
},
),
@@ -395,7 +396,7 @@ def test_delete_notifications_task_calls_task_for_services_that_have_sent_notifi
queue=ANY,
kwargs={
"service_id": service_will_delete_2.id,
"notification_type": "sms",
"notification_type": NotificationType.SMS,
"datetime_to_delete_before": date(2021, 3, 26),
},
),

View File

@@ -13,7 +13,7 @@ from app.celery.reporting_tasks import (
)
from app.config import QueueNames
from app.dao.fact_billing_dao import get_rate
from app.enums import KeyType, NotificationType
from app.enums import KeyType, NotificationStatus, NotificationType, TemplateType
from app.models import FactBilling, FactNotificationStatus, Notification
from tests.app.db import (
create_notification,
@@ -122,13 +122,13 @@ def test_create_nightly_billing_for_day_checks_history(
create_notification(
created_at=yesterday,
template=sample_template,
status="sending",
status=NotificationStatus.SENDING,
)
create_notification_history(
created_at=yesterday,
template=sample_template,
status="delivered",
status=NotificationStatus.DELIVERED,
)
records = FactBilling.query.all()
@@ -164,7 +164,7 @@ def test_create_nightly_billing_for_day_sms_rate_multiplier(
create_notification(
created_at=yesterday,
template=sample_template,
status="delivered",
status=NotificationStatus.DELIVERED,
sent_by="sns",
international=False,
rate_multiplier=1.0,
@@ -173,7 +173,7 @@ def test_create_nightly_billing_for_day_sms_rate_multiplier(
create_notification(
created_at=yesterday,
template=sample_template,
status="delivered",
status=NotificationStatus.DELIVERED,
sent_by="sns",
international=False,
rate_multiplier=second_rate,
@@ -204,7 +204,7 @@ def test_create_nightly_billing_for_day_different_templates(
create_notification(
created_at=yesterday,
template=sample_template,
status="delivered",
status=NotificationStatus.DELIVERED,
sent_by="sns",
international=False,
rate_multiplier=1.0,
@@ -213,7 +213,7 @@ def test_create_nightly_billing_for_day_different_templates(
create_notification(
created_at=yesterday,
template=sample_email_template,
status="delivered",
status=NotificationStatus.DELIVERED,
sent_by="sns",
international=False,
rate_multiplier=0,
@@ -248,7 +248,7 @@ def test_create_nightly_billing_for_day_same_sent_by(
create_notification(
created_at=yesterday,
template=sample_template,
status="delivered",
status=NotificationStatus.DELIVERED,
sent_by="sns",
international=False,
rate_multiplier=1.0,
@@ -257,7 +257,7 @@ def test_create_nightly_billing_for_day_same_sent_by(
create_notification(
created_at=yesterday,
template=sample_template,
status="delivered",
status=NotificationStatus.DELIVERED,
sent_by="sns",
international=False,
rate_multiplier=1.0,
@@ -288,7 +288,7 @@ def test_create_nightly_billing_for_day_null_sent_by_sms(
create_notification(
created_at=yesterday,
template=sample_template,
status="delivered",
status=NotificationStatus.DELIVERED,
sent_by=None,
international=False,
rate_multiplier=1.0,
@@ -334,7 +334,7 @@ def test_create_nightly_billing_for_day_use_BST(
create_notification(
created_at=datetime(2018, 3, 26, 4, 1),
template=sample_template,
status="delivered",
status=NotificationStatus.DELIVERED,
rate_multiplier=1.0,
billable_units=1,
)
@@ -342,7 +342,7 @@ def test_create_nightly_billing_for_day_use_BST(
create_notification(
created_at=datetime(2018, 3, 25, 23, 59),
template=sample_template,
status="delivered",
status=NotificationStatus.DELIVERED,
rate_multiplier=1.0,
billable_units=2,
)
@@ -351,7 +351,7 @@ def test_create_nightly_billing_for_day_use_BST(
create_notification(
created_at=datetime(2018, 3, 24, 23, 59),
template=sample_template,
status="delivered",
status=NotificationStatus.DELIVERED,
rate_multiplier=1.0,
billable_units=4,
)
@@ -376,7 +376,7 @@ def test_create_nightly_billing_for_day_update_when_record_exists(
create_notification(
created_at=datetime.now() - timedelta(days=1),
template=sample_template,
status="delivered",
status=NotificationStatus.DELIVERED,
sent_by=None,
international=False,
rate_multiplier=1.0,
@@ -397,7 +397,7 @@ def test_create_nightly_billing_for_day_update_when_record_exists(
create_notification(
created_at=datetime.now() - timedelta(days=1),
template=sample_template,
status="delivered",
status=NotificationStatus.DELIVERED,
sent_by=None,
international=False,
rate_multiplier=1.0,
@@ -415,25 +415,25 @@ def test_create_nightly_notification_status_for_service_and_day(notify_db_sessio
first_service = create_service(service_name="First Service")
first_template = create_template(service=first_service)
second_service = create_service(service_name="second Service")
second_template = create_template(service=second_service, template_type="email")
second_template = create_template(service=second_service, template_type=TemplateType.EMAIL,)
process_day = datetime.utcnow().date() - timedelta(days=5)
with freeze_time(datetime.combine(process_day, time.max)):
create_notification(template=first_template, status="delivered")
create_notification(template=second_template, status="failed")
create_notification(template=first_template, status=NotificationStatus.DELIVERED,)
create_notification(template=second_template, status=NotificationStatus.FAILED)
# team API key notifications are included
create_notification(
template=second_template, status="sending", key_type=KeyType.TEAM
template=second_template, status=NotificationStatus.SENDING, key_type=KeyType.TEAM,
)
# test notifications are ignored
create_notification(
template=second_template, status="sending", key_type=KeyType.TEST
template=second_template, status=NotificationStatus.SENDING, key_type=KeyType.TEST,
)
# historical notifications are included
create_notification_history(template=second_template, status="delivered")
create_notification_history(template=second_template, status=NotificationStatus.DELIVERED,)
# these created notifications from a different day get ignored
with freeze_time(
@@ -445,10 +445,10 @@ def test_create_nightly_notification_status_for_service_and_day(notify_db_sessio
assert len(FactNotificationStatus.query.all()) == 0
create_nightly_notification_status_for_service_and_day(
str(process_day), first_service.id, "sms"
str(process_day), first_service.id, NotificationType.SMS,
)
create_nightly_notification_status_for_service_and_day(
str(process_day), second_service.id, "email"
str(process_day), second_service.id, NotificationType.EMAIL,
)
new_fact_data = FactNotificationStatus.query.order_by(
@@ -461,16 +461,16 @@ def test_create_nightly_notification_status_for_service_and_day(notify_db_sessio
email_delivered_row = new_fact_data[0]
assert email_delivered_row.template_id == second_template.id
assert email_delivered_row.service_id == second_service.id
assert email_delivered_row.notification_type == "email"
assert email_delivered_row.notification_status == "delivered"
assert email_delivered_row.notification_type == NotificationType.EMAIL
assert email_delivered_row.notification_status == NotificationStatus.DELIVERED
assert email_delivered_row.notification_count == 1
assert email_delivered_row.key_type == KeyType.NORMAL
email_sending_row = new_fact_data[1]
assert email_sending_row.template_id == second_template.id
assert email_sending_row.service_id == second_service.id
assert email_sending_row.notification_type == "email"
assert email_sending_row.notification_status == "failed"
assert email_sending_row.notification_type == NotificationType.EMAIL
assert email_sending_row.notification_status == NotificationStatus.FAILED
assert email_sending_row.notification_count == 1
assert email_sending_row.key_type == KeyType.NORMAL
@@ -479,16 +479,16 @@ def test_create_nightly_notification_status_for_service_and_day(notify_db_sessio
assert email_failure_row.template_id == second_template.id
assert email_failure_row.service_id == second_service.id
assert email_failure_row.job_id == UUID("00000000-0000-0000-0000-000000000000")
assert email_failure_row.notification_type == "email"
assert email_failure_row.notification_status == "sending"
assert email_failure_row.notification_type == NotificationType.EMAIL
assert email_failure_row.notification_status == NotificationStatus.SENDING
assert email_failure_row.notification_count == 1
assert email_failure_row.key_type == KeyType.TEAM
sms_delivered_row = new_fact_data[3]
assert sms_delivered_row.template_id == first_template.id
assert sms_delivered_row.service_id == first_service.id
assert sms_delivered_row.notification_type == "sms"
assert sms_delivered_row.notification_status == "delivered"
assert sms_delivered_row.notification_type == NotificationType.SMS
assert sms_delivered_row.notification_status == NotificationStatus.DELIVERED
assert sms_delivered_row.notification_count == 1
assert sms_delivered_row.key_type == KeyType.NORMAL
@@ -501,22 +501,22 @@ def test_create_nightly_notification_status_for_service_and_day_overwrites_old_d
process_day = datetime.utcnow().date()
# first run: one notification, expect one row (just one status)
notification = create_notification(template=first_template, status="sending")
notification = create_notification(template=first_template, status=NotificationStatus.SENDING)
create_nightly_notification_status_for_service_and_day(
str(process_day), first_service.id, "sms"
str(process_day), first_service.id, NotificationType.SMS,
)
new_fact_data = FactNotificationStatus.query.all()
assert len(new_fact_data) == 1
assert new_fact_data[0].notification_count == 1
assert new_fact_data[0].notification_status == "sending"
assert new_fact_data[0].notification_status == NotificationStatus.SENDING
# second run: status changed, still expect one row (one status)
notification.status = "delivered"
create_notification(template=first_template, status="created")
notification.status = NotificationStatus.DELIVERED
create_notification(template=first_template, status=NotificationStatus.CREATED)
create_nightly_notification_status_for_service_and_day(
str(process_day), first_service.id, "sms"
str(process_day), first_service.id, NotificationType.SMS,
)
updated_fact_data = FactNotificationStatus.query.order_by(
@@ -525,9 +525,9 @@ def test_create_nightly_notification_status_for_service_and_day_overwrites_old_d
assert len(updated_fact_data) == 2
assert updated_fact_data[0].notification_count == 1
assert updated_fact_data[0].notification_status == "created"
assert updated_fact_data[0].notification_status == NotificationStatus.CREATED
assert updated_fact_data[1].notification_count == 1
assert updated_fact_data[1].notification_status == "delivered"
assert updated_fact_data[1].notification_status == NotificationStatus.DELIVERED
# the job runs at 04:30am EST time.
@@ -536,22 +536,22 @@ def test_create_nightly_notification_status_for_service_and_day_respects_bst(
sample_template,
):
create_notification(
sample_template, status="delivered", created_at=datetime(2019, 4, 2, 5, 0)
sample_template, status=NotificationStatus.DELIVERED, created_at=datetime(2019, 4, 2, 5, 0),
) # too new
create_notification(
sample_template, status="created", created_at=datetime(2019, 4, 2, 5, 59)
sample_template, status=NotificationStatus.CREATED, created_at=datetime(2019, 4, 2, 5, 59),
)
create_notification(
sample_template, status="created", created_at=datetime(2019, 4, 1, 4, 0)
sample_template, status=NotificationStatus.CREATED, created_at=datetime(2019, 4, 1, 4, 0),
)
create_notification(
sample_template, status="delivered", created_at=datetime(2019, 3, 21, 17, 59)
sample_template, status=NotificationStatus.DELIVERED, created_at=datetime(2019, 3, 21, 17, 59),
) # too old
create_nightly_notification_status_for_service_and_day(
"2019-04-01", sample_template.service_id, "sms"
"2019-04-01", sample_template.service_id, NotificationType.SMS,
)
noti_status = FactNotificationStatus.query.order_by(
@@ -560,4 +560,4 @@ def test_create_nightly_notification_status_for_service_and_day_respects_bst(
assert len(noti_status) == 1
assert noti_status[0].local_date == date(2019, 4, 1)
assert noti_status[0].notification_status == "created"
assert noti_status[0].notification_status == NotificationStatus.CREATED

View File

@@ -19,7 +19,7 @@ from app.celery.scheduled_tasks import (
)
from app.config import QueueNames, Test
from app.dao.jobs_dao import dao_get_job_by_id
from app.enums import JobStatus
from app.enums import JobStatus, NotificationStatus, TemplateType
from tests.app import load_example_csv
from tests.app.db import create_job, create_notification, create_template
@@ -101,13 +101,13 @@ def test_should_update_scheduled_jobs_and_put_on_queue(mocker, sample_template):
one_minute_in_the_past = datetime.utcnow() - timedelta(minutes=1)
job = create_job(
sample_template, job_status="scheduled", scheduled_for=one_minute_in_the_past
sample_template, job_status=JobStatus.SCHEDULED, scheduled_for=one_minute_in_the_past
)
run_scheduled_jobs()
updated_job = dao_get_job_by_id(job.id)
assert updated_job.job_status == "pending"
assert updated_job.job_status == JobStatus.PENDING
mocked.assert_called_with([str(job.id)], queue="job-tasks")
@@ -118,22 +118,22 @@ def test_should_update_all_scheduled_jobs_and_put_on_queue(sample_template, mock
ten_minutes_in_the_past = datetime.utcnow() - timedelta(minutes=10)
twenty_minutes_in_the_past = datetime.utcnow() - timedelta(minutes=20)
job_1 = create_job(
sample_template, job_status="scheduled", scheduled_for=one_minute_in_the_past
sample_template, job_status=JobStatus.SCHEDULED, scheduled_for=one_minute_in_the_past,
)
job_2 = create_job(
sample_template, job_status="scheduled", scheduled_for=ten_minutes_in_the_past
sample_template, job_status=JobStatus.SCHEDULED, scheduled_for=ten_minutes_in_the_past,
)
job_3 = create_job(
sample_template,
job_status="scheduled",
job_status=JobStatus.SCHEDULED,
scheduled_for=twenty_minutes_in_the_past,
)
run_scheduled_jobs()
assert dao_get_job_by_id(job_1.id).job_status == "pending"
assert dao_get_job_by_id(job_2.id).job_status == "pending"
assert dao_get_job_by_id(job_2.id).job_status == "pending"
assert dao_get_job_by_id(job_1.id).job_status == JobStatus.PENDING
assert dao_get_job_by_id(job_2.id).job_status == JobStatus.PENDING
assert dao_get_job_by_id(job_2.id).job_status == JobStatus.PENDING
mocked.assert_has_calls(
[
@@ -299,36 +299,36 @@ def test_replay_created_notifications(notify_db_session, sample_service, mocker)
"app.celery.provider_tasks.deliver_sms.apply_async"
)
sms_template = create_template(service=sample_service, template_type="sms")
email_template = create_template(service=sample_service, template_type="email")
sms_template = create_template(service=sample_service, template_type=TemplateType.SMS)
email_template = create_template(service=sample_service, template_type=TemplateType.EMAIL)
older_than = (60 * 60) + (60 * 15) # 1 hour 15 minutes
# notifications expected to be resent
old_sms = create_notification(
template=sms_template,
created_at=datetime.utcnow() - timedelta(seconds=older_than),
status="created",
status=NotificationStatus.CREATED,
)
old_email = create_notification(
template=email_template,
created_at=datetime.utcnow() - timedelta(seconds=older_than),
status="created",
status=NotificationStatus.CREATED,
)
# notifications that are not to be resent
create_notification(
template=sms_template,
created_at=datetime.utcnow() - timedelta(seconds=older_than),
status="sending",
status=NotificationStatus.SENDING,
)
create_notification(
template=email_template,
created_at=datetime.utcnow() - timedelta(seconds=older_than),
status="delivered",
status=NotificationStatus.DELIVERED,
)
create_notification(
template=sms_template, created_at=datetime.utcnow(), status="created"
template=sms_template, created_at=datetime.utcnow(), status=NotificationStatus.CREATED,
)
create_notification(
template=email_template, created_at=datetime.utcnow(), status="created"
template=email_template, created_at=datetime.utcnow(), status=NotificationStatus.CREATED,
)
replay_created_notifications()