From c15d7878fc9380c023b8d49117cb7dc349019c71 Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Mon, 29 Apr 2019 15:49:12 +0100 Subject: [PATCH] Only include ft_billing data for current financial year Test get_live_services_data endpoint Expand dao_fetch_live_services_data test with more ft_billing records --- app/dao/date_util.py | 12 ++++++++++-- app/dao/services_dao.py | 16 +++++++++++----- tests/app/dao/test_services_dao.py | 22 +++++++++++++++++----- tests/app/service/test_rest.py | 25 +++++++++++++++++++++++-- 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/app/dao/date_util.py b/app/dao/date_util.py index f9533a826..7a77f9562 100644 --- a/app/dao/date_util.py +++ b/app/dao/date_util.py @@ -7,8 +7,8 @@ import pytz def get_months_for_financial_year(year): return [ convert_bst_to_utc(month) for month in ( - get_months_for_year(4, 13, year) + - get_months_for_year(1, 4, year + 1) + get_months_for_year(4, 13, year) + + get_months_for_year(1, 4, year + 1) ) if convert_bst_to_utc(month) < datetime.now() ] @@ -22,6 +22,14 @@ def get_financial_year(year): return get_april_fools(year), get_april_fools(year + 1) - timedelta(microseconds=1) +def get_current_financial_year(): + now = datetime.utcnow() + current_month = int(now.strftime('%-m')) + current_year = int(now.strftime('%Y')) + year = current_year if current_month > 3 else current_year - 1 + return get_financial_year(year) + + def get_april_fools(year): """ This function converts the start of the financial year April 1, 00:00 as BST (British Standard Time) to UTC, diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index e72e359e2..da71bf595 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -7,6 +7,7 @@ from sqlalchemy.orm import joinedload from flask import current_app from app import db +from app.dao.date_util import get_current_financial_year from app.dao.dao_utils import ( transactional, version_class, @@ -75,6 +76,11 @@ def dao_count_live_services(): def dao_fetch_live_services_data(): + year_start_date, year_end_date = get_current_financial_year() + this_year_ft_billing = FactBilling.query.filter( + FactBilling.bst_date >= year_start_date, + FactBilling.bst_date <= year_end_date, + ).subquery() data = db.session.query( Service.id, Organisation.name.label("organisation_name"), @@ -89,18 +95,18 @@ def dao_fetch_live_services_data(): Service.volume_email, Service.volume_letter, case([ - (FactBilling.notification_type == 'email', func.sum(FactBilling.notifications_sent)) + (this_year_ft_billing.c.notification_type == 'email', func.sum(this_year_ft_billing.c.notifications_sent)) ], else_=0).label("email_totals"), case([ - (FactBilling.notification_type == 'sms', func.sum(FactBilling.notifications_sent)) + (this_year_ft_billing.c.notification_type == 'sms', func.sum(this_year_ft_billing.c.notifications_sent)) ], else_=0).label("sms_totals"), case([ - (FactBilling.notification_type == 'letter', func.sum(FactBilling.notifications_sent)) + (this_year_ft_billing.c.notification_type == 'letter', func.sum(this_year_ft_billing.c.notifications_sent)) ], else_=0).label("letter_totals"), ).outerjoin( Service.organisation ).outerjoin( - FactBilling, Service.id == FactBilling.service_id + this_year_ft_billing, Service.id == this_year_ft_billing.c.service_id ).outerjoin( User, Service.go_live_user_id == User.id ).group_by( @@ -116,7 +122,7 @@ def dao_fetch_live_services_data(): Service.volume_sms, Service.volume_email, Service.volume_letter, - FactBilling.notification_type + this_year_ft_billing.c.notification_type ).all() results = [] for row in data: diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index 70fd265dc..4b559a30d 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -384,28 +384,40 @@ def test_get_all_user_services_should_return_empty_list_if_no_services_for_user( assert len(dao_fetch_all_services_by_user(user.id)) == 0 +@freeze_time('2019-04-23T10:00:00') def test_dao_fetch_live_services_data(sample_user, mock): org = create_organisation() service = create_service(go_live_user=sample_user) template = create_template(service=service) - create_service(service_name='second', go_live_user=sample_user) + service_2 = create_service(service_name='second', go_live_user=sample_user) + create_service(service_name='third') template2 = create_template(service=service, template_type='email') + template_letter_1 = create_template(service=service, template_type='letter') + template_letter_2 = create_template(service=service_2, template_type='letter') dao_add_service_to_organisation(service=service, organisation_id=org.id) create_ft_billing(bst_date='2019-04-20', notification_type='sms', template=template, service=service) - create_ft_billing(bst_date='2019-04-20', notification_type='email', template=template2, - service=service) + create_ft_billing(bst_date='2019-04-21', notification_type='sms', template=template, service=service) + create_ft_billing(bst_date='2018-04-20', notification_type='sms', template=template, service=service) + create_ft_billing(bst_date='2019-04-20', notification_type='email', template=template2, service=service) + create_ft_billing(bst_date='2019-04-15', notification_type='letter', template=template_letter_1, service=service) + create_ft_billing(bst_date='2019-04-16', notification_type='letter', template=template_letter_2, service=service_2) results = dao_fetch_live_services_data() - assert len(results) == 2 + assert len(results) == 3 assert {'service_id': mock.ANY, 'service_name': 'Sample service', 'organisation_name': 'test_org_1', 'consent_to_research': None, 'contact_name': 'Test User', 'contact_email': 'notify@digital.cabinet-office.gov.uk', 'contact_mobile': '+447700900986', 'live_date': None, 'sms_volume_intent': None, 'email_volume_intent': None, - 'letter_volume_intent': None, 'sms_totals': 1, 'email_totals': 1, 'letter_totals': 0} in results + 'letter_volume_intent': None, 'sms_totals': 2, 'email_totals': 1, 'letter_totals': 1} in results assert {'service_id': mock.ANY, 'service_name': 'second', 'organisation_name': None, 'consent_to_research': None, 'contact_name': 'Test User', 'contact_email': 'notify@digital.cabinet-office.gov.uk', 'contact_mobile': '+447700900986', 'live_date': None, 'sms_volume_intent': None, 'email_volume_intent': None, 'letter_volume_intent': None, + 'sms_totals': 0, 'email_totals': 0, 'letter_totals': 1} in results + assert {'service_id': mock.ANY, 'service_name': 'third', 'organisation_name': None, 'consent_to_research': None, + 'contact_name': None, 'contact_email': None, + 'contact_mobile': None, 'live_date': None, 'sms_volume_intent': None, + 'email_volume_intent': None, 'letter_volume_intent': None, 'sms_totals': 0, 'email_totals': 0, 'letter_totals': 0} in results diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 446445ffb..4d93585d7 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -35,6 +35,7 @@ from tests.app.conftest import ( sample_notification_with_job ) from tests.app.db import ( + create_ft_billing, create_ft_notification_status, create_service, create_service_with_inbound_number, @@ -136,8 +137,28 @@ def test_get_service_list_should_return_empty_list_if_no_services(admin_request) assert len(json_resp['data']) == 0 -def test_get_live_services_data(): - pass +def test_get_live_services_data(sample_user, admin_request, mock): + org = create_organisation() + service = create_service(go_live_user=sample_user) + template = create_template(service=service) + create_service(service_name='second', go_live_user=sample_user) + template2 = create_template(service=service, template_type='email') + dao_add_service_to_organisation(service=service, organisation_id=org.id) + create_ft_billing(bst_date='2019-04-20', notification_type='sms', template=template, service=service) + create_ft_billing(bst_date='2019-04-20', notification_type='email', template=template2, + service=service) + response = admin_request.get('service.get_live_services_data')["data"] + assert len(response) == 2 + assert {'consent_to_research': None, 'contact_email': 'notify@digital.cabinet-office.gov.uk', + 'contact_mobile': '+447700900986', 'contact_name': 'Test User', 'email_totals': 0, + 'email_volume_intent': None, 'letter_totals': 0, 'letter_volume_intent': None, 'live_date': None, + 'organisation_name': None, 'service_id': mock.ANY, 'service_name': 'second', 'sms_totals': 0, + 'sms_volume_intent': None} in response + assert {'consent_to_research': None, 'contact_email': 'notify@digital.cabinet-office.gov.uk', + 'contact_mobile': '+447700900986', 'contact_name': 'Test User', 'email_totals': 1, + 'email_volume_intent': None, 'letter_totals': 0, 'letter_volume_intent': None, 'live_date': None, + 'organisation_name': 'test_org_1', 'service_id': mock.ANY, 'service_name': 'Sample service', + 'sms_totals': 1, 'sms_volume_intent': None} in response def test_get_service_by_id(admin_request, sample_service):