From c3c9d1eac987cd6b443641a7d0ec9fba148c005f Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Fri, 11 Jan 2019 17:09:42 +0000 Subject: [PATCH] Add unit tests. Fix data types in result set. --- app/dao/fact_notification_status_dao.py | 4 +-- app/service/rest.py | 1 - .../dao/test_fact_notification_status_dao.py | 36 +++++++++++++++---- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/app/dao/fact_notification_status_dao.py b/app/dao/fact_notification_status_dao.py index 7231dc7d8..f5466bbec 100644 --- a/app/dao/fact_notification_status_dao.py +++ b/app/dao/fact_notification_status_dao.py @@ -351,8 +351,8 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id): all_stats_table.c.name, all_stats_table.c.is_precompiled_letter, all_stats_table.c.template_type, - all_stats_table.c.month, - all_stats_table.c.year, + func.cast(all_stats_table.c.month, Integer).label('month'), + func.cast(all_stats_table.c.year, Integer).label('year'), func.cast(func.sum(all_stats_table.c.count), Integer).label('count'), ).group_by( all_stats_table.c.template_id, diff --git a/app/service/rest.py b/app/service/rest.py index e00c99aa7..4aa0447b0 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -49,7 +49,6 @@ from app.dao.services_dao import ( dao_create_service, dao_fetch_all_services, dao_fetch_all_services_by_user, - dao_fetch_monthly_historical_usage_by_template_for_service, dao_fetch_service_by_id, dao_fetch_todays_stats_for_service, dao_fetch_todays_stats_for_all_services, diff --git a/tests/app/dao/test_fact_notification_status_dao.py b/tests/app/dao/test_fact_notification_status_dao.py index 0e541f7a9..9b1f92f63 100644 --- a/tests/app/dao/test_fact_notification_status_dao.py +++ b/tests/app/dao/test_fact_notification_status_dao.py @@ -341,10 +341,11 @@ def test_fetch_stats_for_all_services_by_date_range(notify_db_session): assert not results[4].count +@freeze_time('2018-01-04 14:00') def test_fetch_monthly_template_usage_for_service(sample_service): - template_one = create_template(service=sample_service, template_type='sms', template_name='one') - template_two = create_template(service=sample_service, template_type='email', template_name='one') - template_three = create_template(service=sample_service, template_type='letter', template_name='one') + template_one = create_template(service=sample_service, template_type='sms', template_name='1_one') + template_two = create_template(service=sample_service, template_type='email', template_name='2_two') + template_three = create_template(service=sample_service, template_type='letter', template_name='3_three') create_ft_notification_status(bst_date=date(2018, 1, 1), service=sample_service, @@ -353,15 +354,38 @@ def test_fetch_monthly_template_usage_for_service(sample_service): create_ft_notification_status(bst_date=date(2018, 2, 1), service=sample_service, template=template_two, - count=3) + count=4) create_ft_notification_status(bst_date=date(2018, 3, 1), service=sample_service, template=template_three, count=5) - + create_notification(template=template_one) results = fetch_monthly_template_usage_for_service( datetime(2017, 4, 1), datetime(2018, 3, 31), sample_service.id ) - print(results) assert len(results) == 3 + + assert results[0].template_id == template_one.id + assert results[0].name == template_one.name + assert results[0].is_precompiled_letter is False + assert results[0].template_type == template_one.template_type + assert results[0].month == 1 + assert results[0].year == 2018 + assert results[0].count == 3 + + assert results[1].template_id == template_two.id + assert results[1].name == template_two.name + assert results[1].is_precompiled_letter is False + assert results[1].template_type == template_two.template_type + assert results[1].month == 2 + assert results[1].year == 2018 + assert results[1].count == 4 + + assert results[2].template_id == template_three.id + assert results[2].name == template_three.name + assert results[2].is_precompiled_letter is False + assert results[2].template_type == template_three.template_type + assert results[2].month == 3 + assert results[2].year == 2018 + assert results[2].count == 5