From 1c3793c5acca0157d588e637890208b639732e35 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Wed, 27 Apr 2022 17:03:39 +0100 Subject: [PATCH] Ensure org billing tests have AnnualBilling for all services we add a row in AnnualBilling table whenever a new service is created, and our billing code assumes this is done. Yet when we were writing some of the tests, this was not a thing yet, so now we are updating those tests so they reflect our system well. Co-authored-by: Pea Tyczynska --- tests/app/dao/test_fact_billing_dao.py | 44 ++++++++++++++------------ tests/app/db.py | 7 ++++ tests/app/platform_stats/test_rest.py | 8 +++-- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/tests/app/dao/test_fact_billing_dao.py b/tests/app/dao/test_fact_billing_dao.py index e90cfa5b2..a84bffaa4 100644 --- a/tests/app/dao/test_fact_billing_dao.py +++ b/tests/app/dao/test_fact_billing_dao.py @@ -21,7 +21,7 @@ from app.dao.fact_billing_dao import ( fetch_volumes_by_service, get_rate, get_rates_for_billing, - query_organisation_sms_usage_for_year + query_organisation_sms_usage_for_year, ) from app.dao.organisation_dao import dao_add_service_to_organisation from app.models import NOTIFICATION_STATUS_TYPES, FactBilling @@ -845,6 +845,7 @@ def test_fetch_letter_line_items_for_all_service(notify_db_session): def test_fetch_usage_year_for_organisation(notify_db_session): fixtures = set_up_usage_data(datetime(2019, 5, 1)) service_with_emails_for_org = create_service(service_name='Service with emails for org') + create_annual_billing(service_with_emails_for_org.id, free_sms_fragment_limit=0, financial_year_start=2019) dao_add_service_to_organisation( service=service_with_emails_for_org, organisation_id=fixtures["org_1"].id @@ -1014,6 +1015,8 @@ def test_fetch_usage_year_for_organisation_only_returns_data_for_live_services(n notifications_sent=100) create_ft_billing(bst_date=datetime.utcnow().date(), template=trial_letter_template, billable_unit=40, rate=0.30, notifications_sent=20) + create_annual_billing(service_id=live_service.id, free_sms_fragment_limit=0, financial_year_start=2019) + create_annual_billing(service_id=trial_service.id, free_sms_fragment_limit=0, financial_year_start=2019) results = fetch_usage_year_for_organisation(organisation_id=org.id, year=2019) @@ -1247,7 +1250,8 @@ def test_fetch_volumes_by_service(notify_db_session): results = fetch_volumes_by_service(start_date=datetime(2022, 2, 1), end_date=datetime(2022, 2, 28)) - assert len(results) == 4 + # since we are using a pre-set up fixture, we only care about some of the results + assert len(results) == 7 assert results[0].service_name == 'a - with sms and letter' assert results[0].organisation_name == 'Org for a - with sms and letter' assert results[0].free_allowance == 10 @@ -1268,22 +1272,22 @@ def test_fetch_volumes_by_service(notify_db_session): assert results[1].letter_sheet_totals == 0 assert float(results[1].letter_cost) == 0 - assert results[2].service_name == 'b - chargeable sms' - assert not results[2].organisation_name - assert results[2].free_allowance == 10 - assert results[2].sms_notifications == 2 - assert results[2].sms_chargeable_units == 3 - assert results[2].email_totals == 0 - assert results[2].letter_totals == 0 - assert results[2].letter_sheet_totals == 0 - assert float(results[2].letter_cost) == 0 + assert results[4].service_name == 'b - chargeable sms' + assert not results[4].organisation_name + assert results[4].free_allowance == 10 + assert results[4].sms_notifications == 2 + assert results[4].sms_chargeable_units == 3 + assert results[4].email_totals == 0 + assert results[4].letter_totals == 0 + assert results[4].letter_sheet_totals == 0 + assert float(results[4].letter_cost) == 0 - assert results[3].service_name == 'e - sms within allowance' - assert not results[3].organisation_name - assert results[3].free_allowance == 10 - assert results[3].sms_notifications == 1 - assert results[3].sms_chargeable_units == 2 - assert results[3].email_totals == 0 - assert results[3].letter_totals == 0 - assert results[3].letter_sheet_totals == 0 - assert float(results[3].letter_cost) == 0 + assert results[6].service_name == 'e - sms within allowance' + assert not results[6].organisation_name + assert results[6].free_allowance == 10 + assert results[6].sms_notifications == 1 + assert results[6].sms_chargeable_units == 2 + assert results[6].email_totals == 0 + assert results[6].letter_totals == 0 + assert results[6].letter_sheet_totals == 0 + assert float(results[6].letter_cost) == 0 diff --git a/tests/app/db.py b/tests/app/db.py index ac7b976db..30864e876 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -998,6 +998,7 @@ def set_up_usage_data(start_date): name='Org for {}'.format(service_with_emails.name), ) dao_add_service_to_organisation(service=service_with_emails, organisation_id=org_2.id) + create_annual_billing(service_id=service_with_emails.id, free_sms_fragment_limit=0, financial_year_start=year) create_ft_billing(bst_date=start_date, template=email_template, notifications_sent=10) @@ -1012,6 +1013,7 @@ def set_up_usage_data(start_date): billing_reference="org3 billing reference" ) dao_add_service_to_organisation(service=service_with_letters, organisation_id=org_for_service_with_letters.id) + create_annual_billing(service_id=service_with_letters.id, free_sms_fragment_limit=0, financial_year_start=year) create_ft_billing(bst_date=start_date, template=letter_template_3, notifications_sent=2, billable_unit=3, rate=.50, postage='first') @@ -1023,6 +1025,11 @@ def set_up_usage_data(start_date): # service with letters, without an organisation: service_with_letters_without_org = create_service(service_name='d - service without org') letter_template_4 = create_template(service=service_with_letters_without_org, template_type='letter') + create_annual_billing( + service_id=service_with_letters_without_org.id, + free_sms_fragment_limit=0, + financial_year_start=year + ) create_ft_billing(bst_date=two_days_later, template=letter_template_4, notifications_sent=7, billable_unit=4, rate=1.55, postage='rest-of-world') diff --git a/tests/app/platform_stats/test_rest.py b/tests/app/platform_stats/test_rest.py index eda62b1dd..2bd3847c0 100644 --- a/tests/app/platform_stats/test_rest.py +++ b/tests/app/platform_stats/test_rest.py @@ -214,7 +214,9 @@ def test_volumes_by_service_report( end_date='2022-03-01' ) - assert len(response) == 4 + assert len(response) == 7 + + # since we are using a pre-set up fixture, we only care about some of the results assert response[0] == {'email_totals': 0, 'free_allowance': 10, 'letter_cost': 0.0, 'letter_sheet_totals': 0, 'letter_totals': 0, 'organisation_id': str(fixture['org_1'].id), @@ -228,12 +230,12 @@ def test_volumes_by_service_report( 'service_id': str(fixture['service_with_out_ft_billing_this_year'].id), 'service_name': fixture['service_with_out_ft_billing_this_year'].name, 'sms_chargeable_units': 0, 'sms_notifications': 0} - assert response[2] == {'email_totals': 0, 'free_allowance': 10, 'letter_cost': 0.0, 'letter_sheet_totals': 0, + assert response[4] == {'email_totals': 0, 'free_allowance': 10, 'letter_cost': 0.0, 'letter_sheet_totals': 0, 'letter_totals': 0, 'organisation_id': '', 'organisation_name': '', 'service_id': str(fixture['service_with_sms_without_org'].id), 'service_name': fixture['service_with_sms_without_org'].name, 'sms_chargeable_units': 0, 'sms_notifications': 0} - assert response[3] == {'email_totals': 0, 'free_allowance': 10, 'letter_cost': 0.0, 'letter_sheet_totals': 0, + assert response[6] == {'email_totals': 0, 'free_allowance': 10, 'letter_cost': 0.0, 'letter_sheet_totals': 0, 'letter_totals': 0, 'organisation_id': '', 'organisation_name': '', 'service_id': str(fixture['service_with_sms_within_allowance'].id), 'service_name': fixture['service_with_sms_within_allowance'].name,