Skip to content

Commit

Permalink
add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
heyitsmebev committed Jan 13, 2025
1 parent b224065 commit 88c3da5
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,4 @@ class StatisticsType(StrEnum):
REQUESTED = "requested"
DELIVERED = "delivered"
FAILURE = "failure"
PENDING = "pending"
11 changes: 7 additions & 4 deletions app/service/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ def format_statistics(statistics, total_notifications=None):
sms_dict = counts[NotificationType.SMS]
delivered_count = sms_dict[StatisticsType.DELIVERED]
failed_count = sms_dict[StatisticsType.FAILURE]
pending_count = total_notifications - (delivered_count + failed_count)
sms_dict[StatisticsType.PENDING] = calculate_pending_stats(
delivered_count, failed_count, total_notifications
)

pending_count = max(0, pending_count)
return counts

sms_dict[StatisticsType.PENDING] = pending_count

return counts
def calculate_pending_stats(delivered_count, failed_count, total_notifications):
pending_count = total_notifications - (delivered_count + failed_count)
return max(0, pending_count)


def format_admin_stats(statistics):
Expand Down
38 changes: 37 additions & 1 deletion tests/app/dao/test_services_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -1638,59 +1638,69 @@ def test_get_live_services_with_organization(sample_organization):
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 0,
StatisticsType.PENDING: 0,
},
TemplateType.SMS: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 2,
StatisticsType.PENDING: 2,
},
},
(_this_date.date() + timedelta(days=1)).strftime("%Y-%m-%d"): {
TemplateType.EMAIL: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 0,
StatisticsType.PENDING: 0,
},
TemplateType.SMS: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 1,
StatisticsType.PENDING: 0,
},
},
(_this_date.date() + timedelta(days=2)).strftime("%Y-%m-%d"): {
TemplateType.EMAIL: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 0,
StatisticsType.PENDING: 0,
},
TemplateType.SMS: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 1,
StatisticsType.PENDING: 0,
},
},
(_this_date.date() + timedelta(days=3)).strftime("%Y-%m-%d"): {
TemplateType.EMAIL: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 0,
StatisticsType.PENDING: 0,
},
TemplateType.SMS: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 0,
StatisticsType.PENDING: 0,
},
},
(_this_date.date() + timedelta(days=4)).strftime("%Y-%m-%d"): {
TemplateType.EMAIL: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 0,
StatisticsType.PENDING: 0,
},
TemplateType.SMS: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 1,
StatisticsType.PENDING: 0,
},
},
},
Expand All @@ -1713,59 +1723,69 @@ def test_get_live_services_with_organization(sample_organization):
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 0,
StatisticsType.PENDING: 0,
},
TemplateType.SMS: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 2,
StatisticsType.PENDING: 2,
},
},
(_this_date.date() + timedelta(days=1)).strftime("%Y-%m-%d"): {
TemplateType.EMAIL: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 0,
StatisticsType.PENDING: 0,
},
TemplateType.SMS: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 1,
StatisticsType.PENDING: 0,
},
},
(_this_date.date() + timedelta(days=2)).strftime("%Y-%m-%d"): {
TemplateType.EMAIL: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 0,
StatisticsType.PENDING: 0,
},
TemplateType.SMS: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 1,
StatisticsType.PENDING: 0,
},
},
(_this_date.date() + timedelta(days=3)).strftime("%Y-%m-%d"): {
TemplateType.EMAIL: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 0,
StatisticsType.PENDING: 0,
},
TemplateType.SMS: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 0,
StatisticsType.PENDING: 0,
},
},
(_this_date.date() + timedelta(days=4)).strftime("%Y-%m-%d"): {
TemplateType.EMAIL: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 0,
StatisticsType.PENDING: 0,
},
TemplateType.SMS: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.REQUESTED: 1,
StatisticsType.PENDING: 0,
},
},
},
Expand All @@ -1786,5 +1806,21 @@ def test_get_specific_days(data, start_date, days, end_date, expected, is_error)
new_line.count = 1
new_line.something = line["something"]
new_data.append(new_line)
results = get_specific_days_stats(new_data, start_date, days, end_date)

total_notifications = None

date_key = _this_date.date().strftime("%Y-%m-%d")
if expected and date_key in expected:
sms_stats = expected[date_key].get(TemplateType.SMS, {})
requested = sms_stats.get(StatisticsType.REQUESTED, 0)
if requested > 0:
total_notifications = {_this_date: requested}

results = get_specific_days_stats(
new_data,
start_date,
days,
end_date,
total_notifications=total_notifications,
)
assert results == expected
5 changes: 2 additions & 3 deletions tests/app/service/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2463,14 +2463,13 @@ def test_get_detailed_services_only_includes_todays_notifications(sample_templat
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.PENDING: 0,
StatisticsType.REQUESTED: 0

StatisticsType.REQUESTED: 0,
},
NotificationType.SMS: {
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.PENDING: 0,
StatisticsType.REQUESTED: 3
StatisticsType.REQUESTED: 3,
},
}

Expand Down
59 changes: 48 additions & 11 deletions tests/app/service/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from app.enums import KeyType, NotificationStatus, NotificationType, StatisticsType
from app.service.statistics import (
add_monthly_notification_status_stats,
calculate_pending_stats,
create_empty_monthly_notification_status_stats_dict,
create_stats_dict,
create_zeroed_stats_dicts,
Expand All @@ -27,22 +28,22 @@
@pytest.mark.idparametrize(
"stats, email_counts, sms_counts",
{
"empty": ([], [0, 0, 0], [0, 0, 0]),
"empty": ([], [0, 0, 0, 0], [0, 0, 0, 0]),
"always_increment_requested": (
[
StatsRow(NotificationType.EMAIL, NotificationStatus.DELIVERED, 1),
StatsRow(NotificationType.EMAIL, NotificationStatus.FAILED, 1),
],
[2, 1, 1],
[0, 0, 0],
[2, 1, 1, 0],
[0, 0, 0, 0],
),
"dont_mix_template_types": (
[
StatsRow(NotificationType.EMAIL, NotificationStatus.DELIVERED, 1),
StatsRow(NotificationType.SMS, NotificationStatus.DELIVERED, 1),
],
[1, 1, 0],
[1, 1, 0],
[1, 1, 0, 0],
[1, 1, 0, 0],
),
"convert_fail_statuses_to_failed": (
[
Expand All @@ -57,25 +58,25 @@
NotificationType.EMAIL, NotificationStatus.PERMANENT_FAILURE, 1
),
],
[4, 0, 4],
[0, 0, 0],
[4, 0, 4, 0],
[0, 0, 0, 0],
),
"convert_sent_to_delivered": (
[
StatsRow(NotificationType.SMS, NotificationStatus.SENDING, 1),
StatsRow(NotificationType.SMS, NotificationStatus.DELIVERED, 1),
StatsRow(NotificationType.SMS, NotificationStatus.SENT, 1),
],
[0, 0, 0],
[3, 2, 0],
[0, 0, 0, 0],
[3, 2, 0, 0],
),
"handles_none_rows": (
[
StatsRow(NotificationType.SMS, NotificationStatus.SENDING, 1),
StatsRow(None, None, None),
],
[0, 0, 0],
[1, 0, 0],
[0, 0, 0, 0],
[1, 0, 0, 0],
),
},
)
Expand All @@ -89,6 +90,7 @@ def test_format_statistics(stats, email_counts, sms_counts):
StatisticsType.REQUESTED,
StatisticsType.DELIVERED,
StatisticsType.FAILURE,
StatisticsType.PENDING,
],
email_counts,
)
Expand All @@ -101,23 +103,58 @@ def test_format_statistics(stats, email_counts, sms_counts):
StatisticsType.REQUESTED,
StatisticsType.DELIVERED,
StatisticsType.FAILURE,
StatisticsType.PENDING,
],
sms_counts,
)
}


def test_format_statistics_with_pending():
stats = [
StatsRow(NotificationType.SMS, NotificationStatus.DELIVERED, 10),
StatsRow(NotificationType.SMS, NotificationStatus.FAILED, 2),
]

total_notifications_for_sms = 20

result = format_statistics(stats, total_notifications=total_notifications_for_sms)

expected_sms_counts = {
StatisticsType.REQUESTED: 12,
StatisticsType.DELIVERED: 10,
StatisticsType.FAILURE: 2,
StatisticsType.PENDING: 8,
}

assert result[NotificationType.SMS] == expected_sms_counts


@pytest.mark.parametrize(
"delivered, failed, total, expected",
[
(10, 2, 20, 8),
(10, 10, 20, 0),
(15, 10, 20, 0),
],
)
def test_calculate_pending(delivered, failed, total, expected):
assert calculate_pending_stats(delivered, failed, total) == expected


def test_create_zeroed_stats_dicts():
assert create_zeroed_stats_dicts() == {
NotificationType.SMS: {
StatisticsType.REQUESTED: 0,
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.PENDING: 0,
},
NotificationType.EMAIL: {
StatisticsType.REQUESTED: 0,
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.PENDING: 0,
},
}

Expand Down
4 changes: 4 additions & 0 deletions tests/app/service/test_statistics_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def test_get_template_usage_by_month_returns_two_templates(
StatisticsType.REQUESTED: 2,
StatisticsType.DELIVERED: 1,
StatisticsType.FAILURE: 0,
StatisticsType.PENDING: 0,
},
),
(
Expand All @@ -127,6 +128,7 @@ def test_get_template_usage_by_month_returns_two_templates(
StatisticsType.REQUESTED: 1,
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.PENDING: 0,
},
),
],
Expand Down Expand Up @@ -163,11 +165,13 @@ def test_get_service_notification_statistics_with_unknown_service(admin_request)
StatisticsType.REQUESTED: 0,
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.PENDING: 0,
},
NotificationType.EMAIL: {
StatisticsType.REQUESTED: 0,
StatisticsType.DELIVERED: 0,
StatisticsType.FAILURE: 0,
StatisticsType.PENDING: 0,
},
}

Expand Down

0 comments on commit 88c3da5

Please sign in to comment.