More cleanup.

Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
Cliff Hill
2024-02-13 11:04:02 -05:00
parent acecf9792d
commit 63aa12e4ef
15 changed files with 662 additions and 203 deletions

View File

@@ -22,7 +22,7 @@ from app.dao.fact_billing_dao import (
)
from app.dao.organization_dao import dao_add_service_to_organization
from app.enums import NotificationStatus, NotificationType, TemplateType
from app.models import FactBilling, Notification
from app.models import FactBilling
from tests.app.db import (
create_annual_billing,
create_ft_billing,
@@ -95,7 +95,11 @@ def test_fetch_billing_data_for_today_includes_data_with_the_right_key_type(
service = create_service()
template = create_template(service=service, template_type=TemplateType.EMAIL)
for key_type in ["normal", "test", "team"]:
create_notification(template=template, status=NotificationStatus.DELIVERED, key_type=key_type,)
create_notification(
template=template,
status=NotificationStatus.DELIVERED,
key_type=key_type,
)
today = datetime.utcnow()
results = fetch_billing_data_for_day(today.date())
@@ -103,7 +107,9 @@ def test_fetch_billing_data_for_today_includes_data_with_the_right_key_type(
assert results[0].notifications_sent == 2
@pytest.mark.parametrize("notification_type", [NotificationType.EMAIL, NotificationType.SMS])
@pytest.mark.parametrize(
"notification_type", [NotificationType.EMAIL, NotificationType.SMS]
)
def test_fetch_billing_data_for_day_only_calls_query_for_permission_type(
notify_db_session, notification_type
):
@@ -119,7 +125,10 @@ def test_fetch_billing_data_for_day_only_calls_query_for_permission_type(
assert len(results) == 1
@pytest.mark.parametrize("notification_type", [NotificationType.EMAIL, NotificationType.SMS],)
@pytest.mark.parametrize(
"notification_type",
[NotificationType.EMAIL, NotificationType.SMS],
)
def test_fetch_billing_data_for_day_only_calls_query_for_all_channels(
notify_db_session, notification_type
):
@@ -130,7 +139,8 @@ def test_fetch_billing_data_for_day_only_calls_query_for_all_channels(
create_notification(template=sms_template, status=NotificationStatus.DELIVERED)
today = datetime.utcnow()
results = fetch_billing_data_for_day(
process_day=today.date(), check_permissions=False,
process_day=today.date(),
check_permissions=False,
)
assert len(results) == 2
@@ -142,7 +152,11 @@ def test_fetch_billing_data_for_today_includes_data_with_the_right_date(
process_day = datetime(2018, 4, 1, 13, 30, 0)
service = create_service()
template = create_template(service=service, template_type=TemplateType.EMAIL)
create_notification(template=template, status=NotificationStatus.DELIVERED, created_at=process_day,)
create_notification(
template=template,
status=NotificationStatus.DELIVERED,
created_at=process_day,
)
create_notification(
template=template,
status=NotificationStatus.DELIVERED,
@@ -155,7 +169,12 @@ def test_fetch_billing_data_for_today_includes_data_with_the_right_date(
created_at=datetime(2018, 4, 1, 0, 23, 23),
)
create_notification(
template=template, status=NotificationStatus.SENDING, created_at=process_day + timedelta(days=1,)
template=template,
status=NotificationStatus.SENDING,
created_at=process_day
+ timedelta(
days=1,
),
)
day_under_test = process_day
@@ -198,8 +217,16 @@ def test_fetch_billing_data_for_day_is_grouped_by_service(notify_db_session):
def test_fetch_billing_data_for_day_is_grouped_by_provider(notify_db_session):
service = create_service()
template = create_template(service=service)
create_notification(template=template, status=NotificationStatus.DELIVERED, sent_by="sns",)
create_notification(template=template, status=NotificationStatus.DELIVERED, sent_by="sns",)
create_notification(
template=template,
status=NotificationStatus.DELIVERED,
sent_by="sns",
)
create_notification(
template=template,
status=NotificationStatus.DELIVERED,
sent_by="sns",
)
today = datetime.utcnow()
results = fetch_billing_data_for_day(today.date())
@@ -211,8 +238,16 @@ def test_fetch_billing_data_for_day_is_grouped_by_provider(notify_db_session):
def test_fetch_billing_data_for_day_is_grouped_by_rate_mulitplier(notify_db_session):
service = create_service()
template = create_template(service=service)
create_notification(template=template, status=NotificationStatus.DELIVERED, rate_multiplier=1,)
create_notification(template=template, status=NotificationStatus.DELIVERED, rate_multiplier=2,)
create_notification(
template=template,
status=NotificationStatus.DELIVERED,
rate_multiplier=1,
)
create_notification(
template=template,
status=NotificationStatus.DELIVERED,
rate_multiplier=2,
)
today = datetime.utcnow()
results = fetch_billing_data_for_day(today.date())
@@ -224,8 +259,16 @@ def test_fetch_billing_data_for_day_is_grouped_by_rate_mulitplier(notify_db_sess
def test_fetch_billing_data_for_day_is_grouped_by_international(notify_db_session):
service = create_service()
sms_template = create_template(service=service)
create_notification(template=sms_template, status=NotificationStatus.DELIVERED, international=True,)
create_notification(template=sms_template, status=NotificationStatus.DELIVERED, international=False,)
create_notification(
template=sms_template,
status=NotificationStatus.DELIVERED,
international=True,
)
create_notification(
template=sms_template,
status=NotificationStatus.DELIVERED,
international=False,
)
today = datetime.utcnow()
results = fetch_billing_data_for_day(today.date())
@@ -266,10 +309,14 @@ def test_fetch_billing_data_for_day_uses_correct_table(notify_db_session):
five_days_ago = datetime.utcnow() - timedelta(days=5)
create_notification(
template=sms_template, status=NotificationStatus.DELIVERED, created_at=five_days_ago,
template=sms_template,
status=NotificationStatus.DELIVERED,
created_at=five_days_ago,
)
create_notification_history(
template=email_template, status=NotificationStatus.DELIVERED, created_at=five_days_ago,
template=email_template,
status=NotificationStatus.DELIVERED,
created_at=five_days_ago,
)
results = fetch_billing_data_for_day(
@@ -311,16 +358,24 @@ def test_fetch_billing_data_for_day_bills_correctly_for_status(notify_db_session
)
sms_results = [x for x in results if x.notification_type == NotificationType.SMS]
email_results = [x for x in results if x.notification_type == NotificationType.EMAIL]
email_results = [
x for x in results if x.notification_type == NotificationType.EMAIL
]
# we expect as many rows as we check for notification types
assert 6 == sms_results[0].notifications_sent
assert 4 == email_results[0].notifications_sent
def test_get_rates_for_billing(notify_db_session):
create_rate(start_date=datetime.utcnow(), value=12, notification_type=NotificationType.EMAIL)
create_rate(start_date=datetime.utcnow(), value=22, notification_type=NotificationType.SMS)
create_rate(start_date=datetime.utcnow(), value=33, notification_type=NotificationType.EMAIL)
create_rate(
start_date=datetime.utcnow(), value=12, notification_type=NotificationType.EMAIL
)
create_rate(
start_date=datetime.utcnow(), value=22, notification_type=NotificationType.SMS
)
create_rate(
start_date=datetime.utcnow(), value=33, notification_type=NotificationType.EMAIL
)
rates = get_rates_for_billing()
assert len(rates) == 3
@@ -329,17 +384,25 @@ def test_get_rates_for_billing(notify_db_session):
@freeze_time("2017-06-01 12:00")
def test_get_rate(notify_db_session):
create_rate(
start_date=datetime(2017, 5, 30, 23, 0), value=1.2, notification_type=NotificationType.EMAIL,
start_date=datetime(2017, 5, 30, 23, 0),
value=1.2,
notification_type=NotificationType.EMAIL,
)
create_rate(
start_date=datetime(2017, 5, 30, 23, 0), value=2.2, notification_type=NotificationType.SMS,
start_date=datetime(2017, 5, 30, 23, 0),
value=2.2,
notification_type=NotificationType.SMS,
)
create_rate(
start_date=datetime(2017, 5, 30, 23, 0), value=3.3, notification_type=NotificationType.EMAIL,
start_date=datetime(2017, 5, 30, 23, 0),
value=3.3,
notification_type=NotificationType.EMAIL,
)
rates = get_rates_for_billing()
rate = get_rate(rates, notification_type=NotificationType.SMS, date=date(2017, 6, 1))
rate = get_rate(
rates, notification_type=NotificationType.SMS, date=date(2017, 6, 1)
)
assert rate == 2.2
@@ -351,10 +414,14 @@ def test_get_rate_chooses_right_rate_depending_on_date(
notify_db_session, date, expected_rate
):
create_rate(
start_date=datetime(2016, 1, 1, 0, 0), value=1.2, notification_type=NotificationType.SMS,
start_date=datetime(2016, 1, 1, 0, 0),
value=1.2,
notification_type=NotificationType.SMS,
)
create_rate(
start_date=datetime(2018, 9, 30, 23, 0), value=2.2, notification_type=NotificationType.SMS,
start_date=datetime(2018, 9, 30, 23, 0),
value=2.2,
notification_type=NotificationType.SMS,
)
rates = get_rates_for_billing()
@@ -397,7 +464,9 @@ def test_fetch_monthly_billing_for_year(notify_db_session):
def test_fetch_monthly_billing_for_year_variable_rates(notify_db_session):
service = set_up_yearly_data_variable_rates()
create_annual_billing(
service_id=service.id, free_sms_fragment_limit=6, financial_year_start=2018,
service_id=service.id,
free_sms_fragment_limit=6,
financial_year_start=2018,
)
results = fetch_monthly_billing_for_year(service.id, 2018)
@@ -501,7 +570,9 @@ def test_fetch_billing_totals_for_year_uses_current_annual_billing(notify_db_ses
def test_fetch_billing_totals_for_year_variable_rates(notify_db_session):
service = set_up_yearly_data_variable_rates()
create_annual_billing(
service_id=service.id, free_sms_fragment_limit=6, financial_year_start=2018,
service_id=service.id,
free_sms_fragment_limit=6,
financial_year_start=2018,
)
results = fetch_billing_totals_for_year(service_id=service.id, year=2018)
@@ -558,13 +629,21 @@ def test_fetch_sms_free_allowance_remainder_until_date_with_two_services(
org = create_organization(name="Org for {}".format(service.name))
dao_add_service_to_organization(service=service, organization_id=org.id)
create_annual_billing(
service_id=service.id, free_sms_fragment_limit=10, financial_year_start=2016,
service_id=service.id,
free_sms_fragment_limit=10,
financial_year_start=2016,
)
create_ft_billing(
template=template, local_date=datetime(2016, 4, 20), billable_unit=2, rate=0.11,
template=template,
local_date=datetime(2016, 4, 20),
billable_unit=2,
rate=0.11,
)
create_ft_billing(
template=template, local_date=datetime(2016, 5, 20), billable_unit=3, rate=0.11,
template=template,
local_date=datetime(2016, 5, 20),
billable_unit=3,
rate=0.11,
)
service_2 = create_service(service_name="used free allowance")
@@ -693,7 +772,9 @@ def test_fetch_sms_billing_for_all_services_with_remainder(notify_db_session):
)
service_4 = create_service(service_name="d - email only")
email_template = create_template(service=service_4, template_type=TemplateType.EMAIL)
email_template = create_template(
service=service_4, template_type=TemplateType.EMAIL
)
org_4 = create_organization(name="Org for {}".format(service_4.name))
dao_add_service_to_organization(service=service_4, organization_id=org_4.id)
create_annual_billing(
@@ -827,13 +908,17 @@ def test_fetch_usage_year_for_organization(notify_db_session):
financial_year_start=2019,
)
dao_add_service_to_organization(
service=service_with_emails_for_org, organization_id=fixtures["org_1"].id,
service=service_with_emails_for_org,
organization_id=fixtures["org_1"].id,
)
template = create_template(
service=service_with_emails_for_org, template_type=TemplateType.EMAIL,
service=service_with_emails_for_org,
template_type=TemplateType.EMAIL,
)
create_ft_billing(
local_date=datetime(2019, 5, 1), template=template, notifications_sent=1100,
local_date=datetime(2019, 5, 1),
template=template,
notifications_sent=1100,
)
results = fetch_usage_year_for_organization(fixtures["org_1"].id, 2019)
@@ -1039,8 +1124,12 @@ def test_fetch_usage_year_for_organization_only_returns_data_for_live_services(
live_service = create_service(restricted=False)
sms_template = create_template(service=live_service)
trial_service = create_service(restricted=True, service_name="trial_service")
email_template = create_template(service=trial_service, template_type=TemplateType.EMAIL)
trial_sms_template = create_template(service=trial_service, template_type=TemplateType.SMS)
email_template = create_template(
service=trial_service, template_type=TemplateType.EMAIL
)
trial_sms_template = create_template(
service=trial_service, template_type=TemplateType.SMS
)
dao_add_service_to_organization(service=live_service, organization_id=org.id)
dao_add_service_to_organization(service=trial_service, organization_id=org.id)
create_ft_billing(