Merge pull request #2483 from alphagov/serialize-sent-stats

Add get_live_services_data endpoint and related query
This commit is contained in:
Pea (Malgorzata Tyczynska)
2019-05-01 16:10:25 +01:00
committed by GitHub
5 changed files with 185 additions and 3 deletions

View File

@@ -12,12 +12,14 @@ from app.dao.inbound_numbers_dao import (
dao_get_available_inbound_numbers,
dao_set_inbound_number_active_flag
)
from app.dao.organisation_dao import dao_add_service_to_organisation
from app.dao.service_permissions_dao import dao_add_service_permission, dao_remove_service_permission
from app.dao.services_dao import (
dao_create_service,
dao_add_user_to_service,
dao_remove_user_from_service,
dao_fetch_all_services,
dao_fetch_live_services_data,
dao_fetch_service_by_id,
dao_fetch_all_services_by_user,
dao_update_service,
@@ -57,7 +59,9 @@ from app.models import (
user_folder_permissions,
)
from tests.app.db import (
create_ft_billing,
create_inbound_number,
create_organisation,
create_user,
create_service,
create_service_with_inbound_number,
@@ -380,6 +384,52 @@ 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, go_live_at='2014-04-20T10:00:00')
template = create_template(service=service)
service_2 = create_service(service_name='second', go_live_user=sample_user, go_live_at='2017-04-20T10:00:00')
create_service(service_name='third', go_live_at='2016-04-20T10:00:00')
create_service(service_name='not_live', count_as_live=False)
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)
# two sms billing records for 1st service within current financial year:
create_ft_billing(bst_date='2019-04-20', notification_type='sms', template=template, service=service)
create_ft_billing(bst_date='2019-04-21', notification_type='sms', template=template, service=service)
# one sms billing record for 1st service from previous financial year, should not appear in the result:
create_ft_billing(bst_date='2018-04-20', notification_type='sms', template=template, service=service)
# one email billing record for 1st service within current financial year:
create_ft_billing(bst_date='2019-04-20', notification_type='email', template=template2, service=service)
# one letter billing record for 1st service within current financial year:
create_ft_billing(bst_date='2019-04-15', notification_type='letter', template=template_letter_1, service=service)
# one letter billing record for 2nd service within current financial year:
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) == 3
# checks the results and that they are ordered by date:
assert results == [
{'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': datetime(2014, 4, 20, 10, 0), 'sms_volume_intent': None, 'email_volume_intent': None,
'letter_volume_intent': None, 'sms_totals': 2, 'email_totals': 1, 'letter_totals': 1},
{'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': datetime(2016, 4, 20, 10, 0), 'sms_volume_intent': None,
'email_volume_intent': None, 'letter_volume_intent': None,
'sms_totals': 0, 'email_totals': 0, 'letter_totals': 0},
{'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': datetime(2017, 4, 20, 10, 0), 'sms_volume_intent': None,
'email_volume_intent': None, 'letter_volume_intent': None,
'sms_totals': 0, 'email_totals': 0, 'letter_totals': 1}
]
def test_get_service_by_id_returns_none_if_no_service(notify_db):
with pytest.raises(NoResultFound) as e:
dao_fetch_service_by_id(str(uuid.uuid4()))

View File

@@ -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,6 +137,30 @@ 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(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):
json_resp = admin_request.get('service.get_service_by_id', service_id=sample_service.id)
assert json_resp['data']['name'] == sample_service.name