From 9310d9e6a3b69a491eaf5b19a2f60745991f2b89 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Fri, 20 May 2022 12:45:16 +0100 Subject: [PATCH] split out org sms usage into separate function this'll allow us to reuse the complex sql query for the platform admin report, which doesn't care about organisations --- app/dao/fact_billing_dao.py | 7 +++- tests/app/dao/test_fact_billing_dao.py | 47 ++++++++++++++++++++------ 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 06aeafab4..3375e0de6 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -764,6 +764,12 @@ def fetch_sms_billing_for_organisation(organisation_id, financial_year): def query_organisation_sms_usage_for_year(organisation_id, year): + return query_sms_usage_for_year_per_service(year).filter( + Service.organisation_id == organisation_id + ) + + +def query_sms_usage_for_year_per_service(year): """ See docstring for query_service_sms_usage_for_year() """ @@ -815,7 +821,6 @@ def query_organisation_sms_usage_for_year(organisation_id, year): FactBilling.notification_type == SMS_TYPE, ) ).filter( - Service.organisation_id == organisation_id, AnnualBilling.financial_year_start == year, ) diff --git a/tests/app/dao/test_fact_billing_dao.py b/tests/app/dao/test_fact_billing_dao.py index a84bffaa4..04812316b 100644 --- a/tests/app/dao/test_fact_billing_dao.py +++ b/tests/app/dao/test_fact_billing_dao.py @@ -22,6 +22,7 @@ from app.dao.fact_billing_dao import ( get_rate, get_rates_for_billing, query_organisation_sms_usage_for_year, + query_sms_usage_for_year_per_service, ) from app.dao.organisation_dao import dao_add_service_to_organisation from app.models import NOTIFICATION_STATUS_TYPES, FactBilling @@ -1026,15 +1027,12 @@ def test_fetch_usage_year_for_organisation_only_returns_data_for_live_services(n @freeze_time('2022-04-27 13:30') -def test_query_organisation_sms_usage_for_year_handles_multiple_services(notify_db_session): +def test_query_sms_usage_for_year_per_service_handles_multiple_services(notify_db_session): today = datetime.utcnow().date() yesterday = datetime.utcnow().date() - timedelta(days=1) current_year = datetime.utcnow().year - org = create_organisation(name='Organisation 1') - service_1 = create_service(restricted=False, service_name="Service 1") - dao_add_service_to_organisation(service=service_1, organisation_id=org.id) sms_template_1 = create_template(service=service_1) create_ft_billing( bst_date=yesterday, template=sms_template_1, rate=1, @@ -1047,7 +1045,6 @@ def test_query_organisation_sms_usage_for_year_handles_multiple_services(notify_ create_annual_billing(service_id=service_1.id, free_sms_fragment_limit=5, financial_year_start=current_year) service_2 = create_service(restricted=False, service_name="Service 2") - dao_add_service_to_organisation(service=service_2, organisation_id=org.id) sms_template_2 = create_template(service=service_2) create_ft_billing( bst_date=yesterday, template=sms_template_2, rate=1, @@ -1061,7 +1058,7 @@ def test_query_organisation_sms_usage_for_year_handles_multiple_services(notify_ # ---------- - result = query_organisation_sms_usage_for_year(org.id, 2022).all() + result = query_sms_usage_for_year_per_service(2022).all() service_1_rows = [row for row in result if row.service_id == service_1.id] service_2_rows = [row for row in result if row.service_id == service_2.id] @@ -1095,15 +1092,12 @@ def test_query_organisation_sms_usage_for_year_handles_multiple_services(notify_ @freeze_time('2022-05-01 13:30') -def test_query_organisation_sms_usage_for_year_handles_multiple_rates(notify_db_session): +def test_query_sms_usage_for_year_per_service_handles_multiple_rates(notify_db_session): old_rate_date = date(2022, 4, 29) new_rate_date = date(2022, 5, 1) current_year = datetime.utcnow().year - org = create_organisation(name='Organisation 1') - service_1 = create_service(restricted=False, service_name="Service 1") - dao_add_service_to_organisation(service=service_1, organisation_id=org.id) sms_template_1 = create_template(service=service_1) create_ft_billing( bst_date=old_rate_date, template=sms_template_1, rate=2, @@ -1115,7 +1109,7 @@ def test_query_organisation_sms_usage_for_year_handles_multiple_rates(notify_db_ ) create_annual_billing(service_id=service_1.id, free_sms_fragment_limit=3, financial_year_start=current_year) - result = query_organisation_sms_usage_for_year(org.id, 2022).all() + result = query_sms_usage_for_year_per_service(2022).all() # al lthe free allowance is used on the first day assert result[0]['bst_date'] == date(2022, 4, 29) @@ -1127,6 +1121,37 @@ def test_query_organisation_sms_usage_for_year_handles_multiple_rates(notify_db_ assert result[1]['cost'] == 6 +@freeze_time('2022-05-01 13:30') +def test_query_organisation_sms_usage_for_year_filters_on_organisation(notify_db_session): + bst_date = date(2022, 5, 1) + current_year = datetime.utcnow().year + + service_1 = create_service(restricted=False, service_name="Service 1") + service_2 = create_service(restricted=False, service_name="Service 2") + + org_1 = create_organisation(name="Org 1") + org_2 = create_organisation(name="Org 2") + + dao_add_service_to_organisation(service=service_1, organisation_id=org_1.id) + dao_add_service_to_organisation(service=service_2, organisation_id=org_2.id) + + sms_template_1 = create_template(service=service_1) + sms_template_2 = create_template(service=service_2) + + create_ft_billing(bst_date=bst_date, template=sms_template_1, billable_unit=2) + create_ft_billing(bst_date=bst_date, template=sms_template_2, billable_unit=4) + + create_annual_billing(service_id=service_1.id, free_sms_fragment_limit=0, financial_year_start=current_year) + create_annual_billing(service_id=service_2.id, free_sms_fragment_limit=0, financial_year_start=current_year) + + result = query_organisation_sms_usage_for_year(org_1.id, 2022).all() + + assert len(result) == 1 + assert result[0]['bst_date'] == date(2022, 5, 1) + assert result[0]['charged_units'] == 2 + assert result[0]['service_id'] == service_1.id + + def test_fetch_daily_volumes_for_platform( notify_db_session, sample_template, sample_email_template, sample_letter_template ):