From f7a564a17c19b4389d5d69a5c5723af250a2306e Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 25 Feb 2020 17:47:03 +0000 Subject: [PATCH] Add more realistic test Add statsd Fix imports --- app/dao/fact_billing_dao.py | 12 +++++++----- tests/app/dao/test_ft_billing_dao.py | 6 +++--- tests/app/organisation/test_rest.py | 19 +++++++++++++++---- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 5689d4aee..594ad004e 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -542,6 +542,7 @@ def create_billing_record(data, rate, process_day): return billing_record +@statsd(namespace="dao") def fetch_letter_costs_for_organisation(organisation_id, start_date, end_date): query = db.session.query( Service.name.label("service_name"), @@ -552,7 +553,6 @@ def fetch_letter_costs_for_organisation(organisation_id, start_date, end_date): ).join( FactBilling, FactBilling.service_id == Service.id, ).filter( - FactBilling.service_id == Service.id, FactBilling.bst_date >= start_date, FactBilling.bst_date <= end_date, FactBilling.notification_type == LETTER_TYPE, @@ -567,6 +567,7 @@ def fetch_letter_costs_for_organisation(organisation_id, start_date, end_date): return query.all() +@statsd(namespace="dao") def fetch_email_usage_for_organisation(organisation_id, start_date, end_date): query = db.session.query( Service.name.label("service_name"), @@ -636,6 +637,7 @@ def fetch_sms_billing_for_organisation(organisation_id, start_date, end_date): return query.all() +@statsd(namespace="dao") def fetch_usage_year_for_organisation(organisation_id, year): year_start_datetime, year_end_datetime = get_financial_year(year) @@ -662,8 +664,8 @@ def fetch_usage_year_for_organisation(organisation_id, year): 'sms_remainder': 0, 'sms_billable_units': 0, 'chargeable_billable_sms': 0, - 'sms_cost': 0, - 'letter_cost': 0, + 'sms_cost': 0.0, + 'letter_cost': 0.0, 'emails_sent': 0 } sms_usages = fetch_sms_billing_for_organisation(organisation_id, year_start_date, year_end_date) @@ -676,9 +678,9 @@ def fetch_usage_year_for_organisation(organisation_id, year): 'free_sms_limit': usage.free_sms_fragment_limit, 'sms_remainder': usage.sms_remainder, 'sms_billable_units': usage.sms_billable_units, - 'chargeable_billable_sms': float(usage.chargeable_billable_sms), + 'chargeable_billable_sms': usage.chargeable_billable_sms, 'sms_cost': float(usage.sms_cost), - 'letter_cost': 0, + 'letter_cost': 0.0, 'emails_sent': 0 } for letter_usage in letter_usages: diff --git a/tests/app/dao/test_ft_billing_dao.py b/tests/app/dao/test_ft_billing_dao.py index c01409656..1a173d428 100644 --- a/tests/app/dao/test_ft_billing_dao.py +++ b/tests/app/dao/test_ft_billing_dao.py @@ -18,7 +18,9 @@ from app.dao.fact_billing_dao import ( get_rates_for_billing, fetch_sms_free_allowance_remainder, fetch_sms_billing_for_all_services, - fetch_letter_costs_for_all_services, fetch_letter_line_items_for_all_services, fetch_usage_year_for_organisation + fetch_letter_costs_for_all_services, + fetch_letter_line_items_for_all_services, + fetch_usage_year_for_organisation ) from app.dao.organisation_dao import dao_add_service_to_organisation from app.models import ( @@ -583,8 +585,6 @@ def test_fetch_sms_billing_for_all_services_with_remainder(notify_db_session): results = fetch_sms_billing_for_all_services(datetime(2019, 5, 1), datetime(2019, 5, 31)) assert len(results) == 3 - # (organisation_name, organisation_id, service_name, free_sms_fragment_limit, sms_rate, - # sms_remainder, sms_billable_units, chargeable_billable_sms, sms_cost) assert results[0] == (org.name, org.id, service.name, service.id, 10, Decimal('0.11'), 8, 3, 0, Decimal('0')) assert results[1] == (org_2.name, org_2.id, service_2.name, service_2.id, 10, Decimal('0.11'), 0, 3, 3, Decimal('0.33')) diff --git a/tests/app/organisation/test_rest.py b/tests/app/organisation/test_rest.py index 6a332ba0a..fc741c89d 100644 --- a/tests/app/organisation/test_rest.py +++ b/tests/app/organisation/test_rest.py @@ -15,7 +15,8 @@ from tests.app.db import ( create_service, create_user, create_template, - create_ft_billing + create_ft_billing, + create_annual_billing ) @@ -750,7 +751,9 @@ def test_get_organisation_services_usage(admin_request, notify_db_session): service = create_service() template = create_template(service=service) dao_add_service_to_organisation(service=service, organisation_id=org.id) - create_ft_billing(bst_date=datetime.utcnow().date(), template=template, billable_unit=19, notifications_sent=19) + create_annual_billing(service_id=service.id, free_sms_fragment_limit=10, financial_year_start=2019) + create_ft_billing(bst_date=datetime.utcnow().date(), template=template, billable_unit=19, rate=0.060, + notifications_sent=19) response = admin_request.get( 'organisation.get_organisation_services_usage', organisation_id=org.id, @@ -758,8 +761,16 @@ def test_get_organisation_services_usage(admin_request, notify_db_session): ) assert len(response) == 1 assert len(response['services']) == 1 - print(response) - assert response['services'][0]['service_id'] == str(service.id) + service_usage = response['services'][0] + assert service_usage['service_id'] == str(service.id) + assert service_usage['service_name'] == service.name + assert service_usage['chargeable_billable_sms'] == 9.0 + assert service_usage['emails_sent'] == 0 + assert service_usage['free_sms_limit'] == 10 + assert service_usage['letter_cost'] == 0 + assert service_usage['sms_billable_units'] == 19 + assert service_usage['sms_remainder'] == 10 + assert service_usage['sms_cost'] == 0.54 def test_get_organisation_services_usage_returns_400_if_year_is_invalid(admin_request):