mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-16 20:48:37 -04:00
[WIP] Added dao method and rest endpoint for getting template
statistics by service. Some cosmetic changes to imports. Added fix for job rest not correctly returning errors.
This commit is contained in:
@@ -28,7 +28,8 @@ from app.dao.notifications_dao import (
|
||||
dao_get_notification_statistics_for_service_and_day,
|
||||
update_notification_status_by_id,
|
||||
update_notification_reference_by_id,
|
||||
update_notification_status_by_reference
|
||||
update_notification_status_by_reference,
|
||||
dao_get_template_statistics_for_service
|
||||
)
|
||||
|
||||
from tests.app.conftest import sample_job
|
||||
@@ -886,10 +887,132 @@ def test_successful_notification_inserts_followed_by_failure_does_not_increment_
|
||||
db.session.execute('DROP TABLE TEMPLATE_STATISTICS')
|
||||
dao_create_notification(failing_notification, sample_template.template_type)
|
||||
except Exception as e:
|
||||
|
||||
# There should be no additional notification stats or counts
|
||||
assert NotificationStatistics.query.count() == 1
|
||||
notication_stats = NotificationStatistics.query.filter(
|
||||
NotificationStatistics.service_id == sample_template.service.id
|
||||
).first()
|
||||
assert notication_stats.sms_requested == 3
|
||||
|
||||
|
||||
@freeze_time("2016-03-30")
|
||||
def test_get_template_stats_for_service_returns_stats_in_reverse_date_order(sample_template, sample_job):
|
||||
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service.id)
|
||||
assert len(template_stats) == 0
|
||||
data = {
|
||||
'to': '+44709123456',
|
||||
'job_id': sample_job.id,
|
||||
'service': sample_template.service,
|
||||
'service_id': sample_template.service.id,
|
||||
'template': sample_template,
|
||||
'template_id': sample_template.id,
|
||||
'created_at': datetime.utcnow()
|
||||
}
|
||||
|
||||
notification = Notification(**data)
|
||||
dao_create_notification(notification, sample_template.template_type)
|
||||
|
||||
# move on one day
|
||||
with freeze_time('2016-03-31'):
|
||||
new_notification = Notification(**data)
|
||||
dao_create_notification(new_notification, sample_template.template_type)
|
||||
|
||||
# move on one more day
|
||||
with freeze_time('2016-04-01'):
|
||||
new_notification = Notification(**data)
|
||||
dao_create_notification(new_notification, sample_template.template_type)
|
||||
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service_id)
|
||||
assert len(template_stats) == 3
|
||||
assert template_stats[0].day == date(2016, 4, 1)
|
||||
assert template_stats[1].day == date(2016, 3, 31)
|
||||
assert template_stats[2].day == date(2016, 3, 30)
|
||||
|
||||
|
||||
@freeze_time('2016-04-09')
|
||||
def test_get_template_stats_for_service_returns_stats_can_limit_number_of_days_returned(sample_template):
|
||||
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service.id)
|
||||
assert len(template_stats) == 0
|
||||
|
||||
# make 9 stats records from 1st to 9th April
|
||||
for i in range(1, 10):
|
||||
past_date = '2016-04-0{}'.format(i)
|
||||
with freeze_time(past_date):
|
||||
template_stats = TemplateStatistics(template_id=sample_template.id,
|
||||
service_id=sample_template.service_id)
|
||||
db.session.add(template_stats)
|
||||
db.session.commit()
|
||||
|
||||
# Retrieve last week of stats
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service_id, limit_days=7)
|
||||
assert len(template_stats) == 7
|
||||
assert template_stats[0].day == date(2016, 4, 9)
|
||||
assert template_stats[6].day == date(2016, 4, 3)
|
||||
|
||||
|
||||
@freeze_time('2016-04-09')
|
||||
def test_get_template_stats_for_service_returns_stats_returns_all_stats_if_no_limit(sample_template):
|
||||
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service.id)
|
||||
assert len(template_stats) == 0
|
||||
|
||||
# make 9 stats records from 1st to 9th April
|
||||
for i in range(1, 10):
|
||||
past_date = '2016-04-0{}'.format(i)
|
||||
with freeze_time(past_date):
|
||||
template_stats = TemplateStatistics(template_id=sample_template.id,
|
||||
service_id=sample_template.service_id)
|
||||
db.session.add(template_stats)
|
||||
db.session.commit()
|
||||
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service_id)
|
||||
assert len(template_stats) == 9
|
||||
assert template_stats[0].day == date(2016, 4, 9)
|
||||
assert template_stats[8].day == date(2016, 4, 1)
|
||||
|
||||
|
||||
@freeze_time('2016-04-30')
|
||||
def test_get_template_stats_for_service_returns_results_from_first_day_with_data(sample_template):
|
||||
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service.id)
|
||||
assert len(template_stats) == 0
|
||||
|
||||
# make 9 stats records from 1st to 9th April - no data after 10th
|
||||
for i in range(1, 10):
|
||||
past_date = '2016-04-0{}'.format(i)
|
||||
with freeze_time(past_date):
|
||||
template_stats = TemplateStatistics(template_id=sample_template.id,
|
||||
service_id=sample_template.service_id)
|
||||
db.session.add(template_stats)
|
||||
db.session.commit()
|
||||
|
||||
# Retrieve one day of stats - read date is 2016-04-30
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service_id, limit_days=1)
|
||||
assert len(template_stats) == 1
|
||||
assert template_stats[0].day == date(2016, 4, 9)
|
||||
|
||||
# Retrieve three days of stats
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service_id, limit_days=3)
|
||||
assert len(template_stats) == 3
|
||||
assert template_stats[0].day == date(2016, 4, 9)
|
||||
assert template_stats[1].day == date(2016, 4, 8)
|
||||
assert template_stats[2].day == date(2016, 4, 7)
|
||||
|
||||
# Retrieve nine days of stats
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service_id, limit_days=9)
|
||||
assert len(template_stats) == 9
|
||||
assert template_stats[0].day == date(2016, 4, 9)
|
||||
assert template_stats[8].day == date(2016, 4, 1)
|
||||
|
||||
# Retrieve with no limit
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service_id)
|
||||
assert len(template_stats) == 9
|
||||
assert template_stats[0].day == date(2016, 4, 9)
|
||||
assert template_stats[8].day == date(2016, 4, 1)
|
||||
|
||||
|
||||
def test_get_template_stats_for_service_with_limit_if_no_records_returns_empty_list(sample_template):
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service.id, limit_days=7)
|
||||
assert len(template_stats) == 0
|
||||
|
||||
0
tests/app/template_statistics/__init__.py
Normal file
0
tests/app/template_statistics/__init__.py
Normal file
136
tests/app/template_statistics/test_rest.py
Normal file
136
tests/app/template_statistics/test_rest.py
Normal file
@@ -0,0 +1,136 @@
|
||||
import json
|
||||
from freezegun import freeze_time
|
||||
|
||||
from app import db
|
||||
from app.models import TemplateStatistics
|
||||
|
||||
from tests import create_authorization_header
|
||||
|
||||
|
||||
@freeze_time('2016-04-09')
|
||||
def test_get_template_statistics_for_service_for_last_week(notify_api, sample_template):
|
||||
|
||||
# make 9 stats records from 1st to 9th April
|
||||
for i in range(1, 10):
|
||||
past_date = '2016-04-0{}'.format(i)
|
||||
with freeze_time(past_date):
|
||||
template_stats = TemplateStatistics(template_id=sample_template.id,
|
||||
service_id=sample_template.service_id)
|
||||
db.session.add(template_stats)
|
||||
db.session.commit()
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
path='/service/{}/template-statistics'.format(sample_template.service_id),
|
||||
method='GET'
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_template.service_id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
query_string={'limit_days': 7}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 7
|
||||
assert json_resp['data'][0]['day'] == '2016-04-09'
|
||||
assert json_resp['data'][6]['day'] == '2016-04-03'
|
||||
|
||||
|
||||
@freeze_time('2016-04-30')
|
||||
def test_get_template_statistics_for_service_for_last_actual_week_with_data(notify_api, sample_template):
|
||||
|
||||
# make 9 stats records from 1st to 9th April
|
||||
for i in range(1, 10):
|
||||
past_date = '2016-04-0{}'.format(i)
|
||||
with freeze_time(past_date):
|
||||
template_stats = TemplateStatistics(template_id=sample_template.id,
|
||||
service_id=sample_template.service_id)
|
||||
db.session.add(template_stats)
|
||||
db.session.commit()
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
path='/service/{}/template-statistics'.format(sample_template.service_id),
|
||||
method='GET'
|
||||
)
|
||||
|
||||
# Date is frozen at 2016-04-30 and no data written since
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_template.service_id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
query_string={'limit_days': 7}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 7
|
||||
assert json_resp['data'][0]['day'] == '2016-04-09'
|
||||
assert json_resp['data'][6]['day'] == '2016-04-03'
|
||||
|
||||
|
||||
def test_get_all_template_statistics_for_service(notify_api, sample_template):
|
||||
|
||||
# make 9 stats records from 1st to 9th April
|
||||
for i in range(1, 10):
|
||||
past_date = '2016-04-0{}'.format(i)
|
||||
with freeze_time(past_date):
|
||||
template_stats = TemplateStatistics(template_id=sample_template.id,
|
||||
service_id=sample_template.service_id)
|
||||
db.session.add(template_stats)
|
||||
db.session.commit()
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
path='/service/{}/template-statistics'.format(sample_template.service_id),
|
||||
method='GET'
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_template.service_id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header]
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 9
|
||||
assert json_resp['data'][0]['day'] == '2016-04-09'
|
||||
assert json_resp['data'][8]['day'] == '2016-04-01'
|
||||
|
||||
|
||||
def test_get_all_template_statistics_with_bad_limit_arg_returns_400(notify_api, sample_template):
|
||||
|
||||
# make 9 stats records from 1st to 9th April
|
||||
for i in range(1, 10):
|
||||
past_date = '2016-04-0{}'.format(i)
|
||||
with freeze_time(past_date):
|
||||
template_stats = TemplateStatistics(template_id=sample_template.id,
|
||||
service_id=sample_template.service_id)
|
||||
db.session.add(template_stats)
|
||||
db.session.commit()
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
path='/service/{}/template-statistics'.format(sample_template.service_id),
|
||||
method='GET'
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_template.service_id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
query_string={'limit_days': 'blurk'}
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert json_resp['result'] == 'error'
|
||||
assert json_resp['message'] == ['Limit days blurk is not an integer']
|
||||
Reference in New Issue
Block a user