diff --git a/app/dao/fact_notification_status_dao.py b/app/dao/fact_notification_status_dao.py index ee2e7c626..27e881191 100644 --- a/app/dao/fact_notification_status_dao.py +++ b/app/dao/fact_notification_status_dao.py @@ -146,7 +146,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days( stats_for_today = ( db.session.query( - Notification.notification_type.cast(db.Text), + Notification.notification_type, Notification.status, *([Notification.template_id] if by_template else []), func.count().label("count"), @@ -212,14 +212,14 @@ def fetch_notification_status_totals_for_all_services(start_date, end_date): if start_date <= datetime.utcnow().date() <= end_date: stats_for_today = ( db.session.query( - Notification.notification_type.cast(db.Text).label("notification_type"), + Notification.notification_type.label("notification_type"), Notification.status, Notification.key_type, func.count().label("count"), ) .filter(Notification.created_at >= today) .group_by( - Notification.notification_type.cast(db.Text), + Notification.notification_type, Notification.status, Notification.key_type, ) @@ -297,7 +297,7 @@ def fetch_stats_for_all_services_by_date_range( today = get_midnight_in_utc(datetime.utcnow()) subquery = ( db.session.query( - Notification.notification_type.cast(db.Text).label("notification_type"), + Notification.notification_type.label("notification_type"), Notification.status.label("status"), Notification.service_id.label("service_id"), func.count(Notification.id).label("count"), @@ -450,7 +450,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id): def get_total_notifications_for_date_range(start_date, end_date): query = ( db.session.query( - FactNotificationStatus.local_date.cast(db.Text).label("local_date"), + FactNotificationStatus.local_date.label("local_date"), func.sum( case( [ diff --git a/app/enums.py b/app/enums.py index 31d05c13c..e2c0966ed 100644 --- a/app/enums.py +++ b/app/enums.py @@ -1,5 +1,6 @@ -from strenum import StrEnum # In 3.11 this is in the enum library. We will not need - # this external library any more. +from strenum import StrEnum # In 3.11 this is in the enum library. We will not need + +# this external library any more. class TemplateType(StrEnum): diff --git a/app/models.py b/app/models.py index 6eb81e482..e5a436df5 100644 --- a/app/models.py +++ b/app/models.py @@ -1973,10 +1973,7 @@ class Rate(db.Model): notification_type = enum_column(NotificationType, index=True, nullable=False) def __str__(self): - the_string = "{}".format(self.rate) - the_string += " {}".format(self.notification_type) - the_string += " {}".format(self.valid_from) - return the_string + return f"{self.rate} {self.notification_type} {self.valid_from}" class InboundSms(db.Model): @@ -2140,7 +2137,11 @@ class FactNotificationStatus(db.Model): job_id = db.Column(UUID(as_uuid=True), primary_key=True, index=True, nullable=False) notification_type = enum_column(NotificationType, primary_key=True, nullable=False) key_type = enum_column(KeyType, primary_key=True, nullable=False) - notification_status = enum_column(NotificationStatus, primary_key=True, nullable=False) + notification_status = enum_column( + NotificationStatus, + primary_key=True, + nullable=False, + ) notification_count = db.Column(db.Integer(), nullable=False) created_at = db.Column( db.DateTime, diff --git a/migrations/versions/0410_enums_for_everything.py b/migrations/versions/0410_enums_for_everything.py index 1ea6fc4d7..879e3c772 100644 --- a/migrations/versions/0410_enums_for_everything.py +++ b/migrations/versions/0410_enums_for_everything.py @@ -578,7 +578,7 @@ def upgrade(): ) op.alter_column( "ft_notification_status", - "notification_type", + "notification_status", existing_type=sa.TEXT(), type_=enum_type(NotificationStatus), existing_nullable=False, @@ -613,7 +613,7 @@ def downgrade(): # Alter columns back op.alter_column( "ft_notification_status", - "notification_type", + "notification_status", existing_type=enum_type(NotificationStatus), type_=sa.TEXT(), existing_nullable=False, diff --git a/tests/app/dao/test_fact_notification_status_dao.py b/tests/app/dao/test_fact_notification_status_dao.py index 68ab57808..cd9c5bedd 100644 --- a/tests/app/dao/test_fact_notification_status_dao.py +++ b/tests/app/dao/test_fact_notification_status_dao.py @@ -17,8 +17,8 @@ from app.dao.fact_notification_status_dao import ( get_total_notifications_for_date_range, update_fact_notification_status, ) -from app.enums import KeyType, NotificationStatus -from app.models import FactNotificationStatus, NotificationType, TemplateType +from app.enums import KeyType, NotificationStatus, NotificationType, TemplateType +from app.models import FactNotificationStatus from tests.app.db import ( create_ft_notification_status, create_job, @@ -32,24 +32,31 @@ def test_fetch_notification_status_for_service_by_month(notify_db_session): service_1 = create_service(service_name="service_1") service_2 = create_service(service_name="service_2") - create_ft_notification_status(date(2018, 1, 1), "sms", service_1, count=4) - create_ft_notification_status(date(2018, 1, 2), "sms", service_1, count=10) create_ft_notification_status( - date(2018, 1, 2), "sms", service_1, notification_status="created" + date(2018, 1, 1), NotificationType.SMS, service_1, count=4 ) - create_ft_notification_status(date(2018, 1, 3), "email", service_1) + create_ft_notification_status( + date(2018, 1, 2), NotificationType.SMS, service_1, count=10 + ) + create_ft_notification_status( + date(2018, 1, 2), + NotificationType.SMS, + service_1, + notification_status=NotificationStatus.CREATED, + ) + create_ft_notification_status(date(2018, 1, 3), NotificationType.EMAIL, service_1) - create_ft_notification_status(date(2018, 2, 2), "sms", service_1) + create_ft_notification_status(date(2018, 2, 2), NotificationType.SMS, service_1) # not included - too early - create_ft_notification_status(date(2017, 12, 31), "sms", service_1) + create_ft_notification_status(date(2017, 12, 31), NotificationType.SMS, service_1) # not included - too late - create_ft_notification_status(date(2017, 3, 1), "sms", service_1) + create_ft_notification_status(date(2017, 3, 1), NotificationType.SMS, service_1) # not included - wrong service - create_ft_notification_status(date(2018, 1, 3), "sms", service_2) + create_ft_notification_status(date(2018, 1, 3), NotificationType.SMS, service_2) # not included - test keys create_ft_notification_status( - date(2018, 1, 3), "sms", service_1, key_type=KeyType.TEST + date(2018, 1, 3), NotificationType.SMS, service_1, key_type=KeyType.TEST ) results = sorted( @@ -62,23 +69,23 @@ def test_fetch_notification_status_for_service_by_month(notify_db_session): assert len(results) == 4 assert results[0].month.date() == date(2018, 1, 1) - assert results[0].notification_type == "email" - assert results[0].notification_status == "delivered" + assert results[0].notification_type == NotificationType.EMAIL + assert results[0].notification_status == NotificationStatus.DELIVERED assert results[0].count == 1 assert results[1].month.date() == date(2018, 1, 1) - assert results[1].notification_type == "sms" - assert results[1].notification_status == "created" + assert results[1].notification_type == NotificationType.SMS + assert results[1].notification_status == NotificationStatus.CREATED assert results[1].count == 1 assert results[2].month.date() == date(2018, 1, 1) - assert results[2].notification_type == "sms" - assert results[2].notification_status == "delivered" + assert results[2].notification_type == NotificationType.SMS + assert results[2].notification_status == NotificationStatus.DELIVERED assert results[2].count == 14 assert results[3].month.date() == date(2018, 2, 1) - assert results[3].notification_type == "sms" - assert results[3].notification_status == "delivered" + assert results[3].notification_type == NotificationType.SMS + assert results[3].notification_status == NotificationStatus.DELIVERED assert results[3].count == 1 @@ -109,7 +116,7 @@ def test_fetch_notification_status_for_service_for_day(notify_db_session): create_notification( service_1.templates[0], created_at=datetime(2018, 6, 1, 12, 0, 0), - status="delivered", + status=NotificationStatus.DELIVERED, ) # test key @@ -138,13 +145,13 @@ def test_fetch_notification_status_for_service_for_day(notify_db_session): assert len(results) == 2 assert results[0].month == datetime(2018, 6, 1, 0, 0) - assert results[0].notification_type == "sms" - assert results[0].notification_status == "created" + assert results[0].notification_type == NotificationType.SMS + assert results[0].notification_status == NotificationStatus.CREATED assert results[0].count == 3 assert results[1].month == datetime(2018, 6, 1, 0, 0) - assert results[1].notification_type == "sms" - assert results[1].notification_status == "delivered" + assert results[1].notification_type == NotificationType.SMS + assert results[1].notification_status == NotificationStatus.DELIVERED assert results[1].count == 1 @@ -159,20 +166,42 @@ def test_fetch_notification_status_for_service_for_today_and_7_previous_days( service=service_1, template_type=TemplateType.EMAIL ) - create_ft_notification_status(date(2018, 10, 29), NotificationType.SMS, service_1, count=10,) - create_ft_notification_status(date(2018, 10, 25), NotificationType.SMS, service_1, count=8,) create_ft_notification_status( - date(2018, 10, 29), NotificationType.SMS, service_1, notification_status=NotificationStatus.CREATED, + date(2018, 10, 29), + NotificationType.SMS, + service_1, + count=10, + ) + create_ft_notification_status( + date(2018, 10, 25), + NotificationType.SMS, + service_1, + count=8, + ) + create_ft_notification_status( + date(2018, 10, 29), + NotificationType.SMS, + service_1, + notification_status=NotificationStatus.CREATED, + ) + create_ft_notification_status( + date(2018, 10, 29), + NotificationType.EMAIL, + service_1, + count=3, ) - create_ft_notification_status(date(2018, 10, 29), NotificationType.EMAIL, service_1, count=3,) create_notification(sms_template, created_at=datetime(2018, 10, 31, 11, 0, 0)) create_notification(sms_template_2, created_at=datetime(2018, 10, 31, 11, 0, 0)) create_notification( - sms_template, created_at=datetime(2018, 10, 31, 12, 0, 0), status=NotificationStatus.DELIVERED, + sms_template, + created_at=datetime(2018, 10, 31, 12, 0, 0), + status=NotificationStatus.DELIVERED, ) create_notification( - email_template, created_at=datetime(2018, 10, 31, 13, 0, 0), status=NotificationStatus.DELIVERED, + email_template, + created_at=datetime(2018, 10, 31, 13, 0, 0), + status=NotificationStatus.DELIVERED, ) # too early, shouldn't be included @@ -299,16 +328,16 @@ def test_fetch_notification_status_totals_for_all_services( assert len(results) == 3 - assert results[0].notification_type == "email" - assert results[0].status == "delivered" + assert results[0].notification_type == NotificationType.EMAIL + assert results[0].status == NotificationStatus.DELIVERED assert results[0].count == expected_email - assert results[1].notification_type == "sms" - assert results[1].status == "created" + assert results[1].notification_type == NotificationType.SMS + assert results[1].status == NotificationStatus.CREATED assert results[1].count == expected_created_sms - assert results[2].notification_type == "sms" - assert results[2].status == "delivered" + assert results[2].notification_type == NotificationType.SMS + assert results[2].status == NotificationStatus.DELIVERED assert results[2].count == expected_sms diff --git a/tests/app/db.py b/tests/app/db.py index 553b5c562..5fdc70b3c 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -929,7 +929,9 @@ def set_up_usage_data(start_date): # service with emails only: service_with_emails = create_service(service_name="b - emails") - email_template = create_template(service=service_with_emails, template_type=TemplateType.EMAIL) + email_template = create_template( + service=service_with_emails, template_type=TemplateType.EMAIL + ) org_2 = create_organization( name="Org for {}".format(service_with_emails.name), )