mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
New endpoint for template stats to use new query. Breaking change to the returned JSON, so adding on a different url.
This commit is contained in:
@@ -5,6 +5,7 @@ from flask import (
|
||||
)
|
||||
|
||||
from app.dao.notifications_dao import (
|
||||
dao_get_template_usage,
|
||||
dao_get_template_statistics_for_service,
|
||||
dao_get_template_statistics_for_template
|
||||
)
|
||||
@@ -36,6 +37,31 @@ def get_template_statistics_for_service(service_id):
|
||||
return jsonify(data=data)
|
||||
|
||||
|
||||
@template_statistics.route('/replacement')
|
||||
def get_template_statistics_for_service_by_day(service_id):
|
||||
if request.args.get('limit_days'):
|
||||
try:
|
||||
limit_days = int(request.args['limit_days'])
|
||||
except ValueError as e:
|
||||
error = '{} is not an integer'.format(request.args['limit_days'])
|
||||
message = {'limit_days': [error]}
|
||||
raise InvalidRequest(message, status_code=400)
|
||||
else:
|
||||
limit_days = None
|
||||
stats = dao_get_template_usage(service_id, limit_days=limit_days)
|
||||
|
||||
def serialize(row):
|
||||
return {
|
||||
'count': row.count,
|
||||
'day': str(row.day),
|
||||
'template_id': str(row.template_id),
|
||||
'template_name': row.name,
|
||||
'template_type': row.template_type
|
||||
}
|
||||
|
||||
return jsonify(data=[serialize(row) for row in stats])
|
||||
|
||||
|
||||
@template_statistics.route('/<template_id>')
|
||||
def get_template_statistics_for_template_id(service_id, template_id):
|
||||
stats = dao_get_template_statistics_for_template(template_id)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
import json
|
||||
|
||||
from freezegun import freeze_time
|
||||
@@ -6,7 +6,137 @@ from freezegun import freeze_time
|
||||
from app import db
|
||||
from app.models import TemplateStatistics
|
||||
from tests import create_authorization_header
|
||||
from tests.app.conftest import sample_template as create_sample_template
|
||||
from tests.app.conftest import sample_template as create_sample_template, sample_template, sample_notification, \
|
||||
sample_email_template
|
||||
|
||||
|
||||
def test_get_all_template_statistics_with_bad_arg_returns_400(notify_api):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics/replacement'.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']}
|
||||
|
||||
|
||||
@freeze_time('2016-08-18')
|
||||
def test_get_template_statistics_for_service(notify_db, notify_db_session, notify_api, sample_service):
|
||||
sms = sample_template(notify_db, notify_db_session, service=sample_service)
|
||||
email = sample_email_template(notify_db, notify_db_session, service=sample_service)
|
||||
today = datetime.now()
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics/replacement'.format(sample_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']) == 2
|
||||
assert json_resp['data'][0]['count'] == 2
|
||||
assert json_resp['data'][0]['template_id'] == str(email.id)
|
||||
assert json_resp['data'][0]['template_name'] == email.name
|
||||
assert json_resp['data'][0]['template_type'] == email.template_type
|
||||
assert json_resp['data'][0]['day'] == '2016-08-18'
|
||||
assert json_resp['data'][1]['count'] == 2
|
||||
assert json_resp['data'][1]['template_id'] == str(sms.id)
|
||||
assert json_resp['data'][1]['template_name'] == sms.name
|
||||
assert json_resp['data'][1]['template_type'] == sms.template_type
|
||||
assert json_resp['data'][1]['day'] == '2016-08-18'
|
||||
|
||||
|
||||
@freeze_time('2016-08-18')
|
||||
def test_get_template_statistics_for_service_by_day(notify_db, notify_db_session, notify_api, sample_service):
|
||||
sms = sample_template(notify_db, notify_db_session, service=sample_service)
|
||||
email = sample_email_template(notify_db, notify_db_session, service=sample_service)
|
||||
today = datetime.now()
|
||||
a_week_ago = datetime.now() - timedelta(days=7)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=a_week_ago, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=a_week_ago, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics/replacement'.format(sample_service.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
query_string={'limit_days': 1}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 2
|
||||
assert json_resp['data'][0]['count'] == 1
|
||||
assert json_resp['data'][0]['template_id'] == str(email.id)
|
||||
assert json_resp['data'][0]['template_name'] == email.name
|
||||
assert json_resp['data'][0]['template_type'] == email.template_type
|
||||
assert json_resp['data'][0]['day'] == '2016-08-18'
|
||||
assert json_resp['data'][1]['count'] == 1
|
||||
assert json_resp['data'][1]['template_id'] == str(sms.id)
|
||||
assert json_resp['data'][1]['template_name'] == sms.name
|
||||
assert json_resp['data'][1]['template_type'] == sms.template_type
|
||||
assert json_resp['data'][1]['day'] == '2016-08-18'
|
||||
|
||||
response_for_a_week = client.get(
|
||||
'/service/{}/template-statistics/replacement'.format(sample_service.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
query_string={'limit_days': 7}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response_for_a_week.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 4
|
||||
assert json_resp['data'][0]['count'] == 1
|
||||
assert json_resp['data'][0]['template_name'] == 'Email Template Name'
|
||||
assert json_resp['data'][0]['day'] == '2016-08-18'
|
||||
assert json_resp['data'][1]['count'] == 1
|
||||
assert json_resp['data'][1]['template_name'] == 'Template Name'
|
||||
assert json_resp['data'][1]['day'] == '2016-08-18'
|
||||
assert json_resp['data'][2]['count'] == 1
|
||||
assert json_resp['data'][2]['template_name'] == 'Email Template Name'
|
||||
assert json_resp['data'][2]['day'] == '2016-08-11'
|
||||
assert json_resp['data'][3]['count'] == 1
|
||||
assert json_resp['data'][3]['template_name'] == 'Template Name'
|
||||
assert json_resp['data'][3]['day'] == '2016-08-11'
|
||||
|
||||
|
||||
@freeze_time('2016-08-18')
|
||||
def test_returns_empty_list_if_no_templates_used(notify_api, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics/replacement'.format(sample_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']) == 0
|
||||
|
||||
|
||||
@freeze_time('2016-04-09')
|
||||
|
||||
Reference in New Issue
Block a user