mirror of
https://github.com/GSA/notifications-api.git
synced 2026-05-17 07:20:55 -04:00
Merge pull request #287 from alphagov/statistics_group_by_week_backup
Group by Notification Statistics added and all tests working.
This commit is contained in:
@@ -13,7 +13,8 @@ from app.models import (
|
||||
MMG_PROVIDER,
|
||||
SES_PROVIDER,
|
||||
TWILIO_PROVIDER,
|
||||
ProviderStatistics)
|
||||
ProviderStatistics,
|
||||
NotificationStatistics)
|
||||
from app.dao.users_dao import (save_model_user, create_user_code, create_secret_code)
|
||||
from app.dao.services_dao import (dao_create_service, dao_add_user_to_service)
|
||||
from app.dao.templates_dao import dao_create_template
|
||||
@@ -493,3 +494,32 @@ def sample_provider_statistics(notify_db,
|
||||
notify_db.session.add(stats)
|
||||
notify_db.session.commit()
|
||||
return stats
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def sample_notification_statistics(notify_db,
|
||||
notify_db_session,
|
||||
service=None,
|
||||
day=None,
|
||||
emails_requested=2,
|
||||
emails_delivered=1,
|
||||
emails_failed=1,
|
||||
sms_requested=2,
|
||||
sms_delivered=1,
|
||||
sms_failed=1):
|
||||
if service is None:
|
||||
service = sample_service(notify_db, notify_db_session)
|
||||
if day is None:
|
||||
day = date.today()
|
||||
stats = NotificationStatistics(
|
||||
service=service,
|
||||
day=day,
|
||||
emails_requested=emails_requested,
|
||||
emails_delivered=emails_delivered,
|
||||
emails_failed=emails_failed,
|
||||
sms_requested=sms_requested,
|
||||
sms_delivered=sms_delivered,
|
||||
sms_failed=sms_failed)
|
||||
notify_db.session.add(stats)
|
||||
notify_db.session.commit()
|
||||
return stats
|
||||
|
||||
122
tests/app/dao/test_notification_statistics_dao.py
Normal file
122
tests/app/dao/test_notification_statistics_dao.py
Normal file
@@ -0,0 +1,122 @@
|
||||
from datetime import (date, timedelta)
|
||||
|
||||
from app.models import NotificationStatistics
|
||||
from tests.app.conftest import sample_notification_statistics as create_sample_notification_statistics
|
||||
from app.dao.notifications_dao import dao_get_7_day_agg_notification_statistics_for_service
|
||||
|
||||
|
||||
def test_display_weekly_notification_statistics_sum_over_week(notify_db,
|
||||
notify_db_session,
|
||||
sample_service):
|
||||
fools = date(2016, 4, 1)
|
||||
create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
day=fools
|
||||
)
|
||||
create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
day=fools + timedelta(days=1)
|
||||
)
|
||||
assert dao_get_7_day_agg_notification_statistics_for_service(
|
||||
sample_service.id,
|
||||
fools
|
||||
).all() == [(0, 4, 2, 2, 4, 2, 2)]
|
||||
|
||||
|
||||
def test_display_weekly_notification_statistics_separate_over_weeks(notify_db,
|
||||
notify_db_session,
|
||||
sample_service):
|
||||
fools = date(2016, 4, 1)
|
||||
next_week = fools + timedelta(days=7)
|
||||
create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
day=fools
|
||||
)
|
||||
create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
day=next_week
|
||||
)
|
||||
assert dao_get_7_day_agg_notification_statistics_for_service(
|
||||
sample_service.id,
|
||||
fools
|
||||
).all() == [(1, 2, 1, 1, 2, 1, 1), (0, 2, 1, 1, 2, 1, 1)]
|
||||
|
||||
|
||||
def test_display_weekly_notification_statistics_7_days_from_date_from(notify_db,
|
||||
notify_db_session,
|
||||
sample_service):
|
||||
fools = date(2016, 4, 1)
|
||||
eow_fools = fools + timedelta(days=6)
|
||||
next_week = fools + timedelta(days=7)
|
||||
two_weeks_later = fools + timedelta(days=14)
|
||||
create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
day=fools
|
||||
)
|
||||
create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
day=eow_fools
|
||||
)
|
||||
create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
day=next_week
|
||||
)
|
||||
create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
day=two_weeks_later
|
||||
)
|
||||
assert dao_get_7_day_agg_notification_statistics_for_service(
|
||||
sample_service.id,
|
||||
fools
|
||||
).all() == [(2, 2, 1, 1, 2, 1, 1), (1, 2, 1, 1, 2, 1, 1), (0, 4, 2, 2, 4, 2, 2)]
|
||||
|
||||
|
||||
def test_display_weekly_notification_statistics_week_number_misses_week(notify_db,
|
||||
notify_db_session,
|
||||
sample_service):
|
||||
fools = date(2016, 4, 1)
|
||||
two_weeks_later = fools + timedelta(days=14)
|
||||
create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
day=fools
|
||||
)
|
||||
create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
day=two_weeks_later
|
||||
)
|
||||
assert dao_get_7_day_agg_notification_statistics_for_service(
|
||||
sample_service.id,
|
||||
fools
|
||||
).all() == [(2, 2, 1, 1, 2, 1, 1), (0, 2, 1, 1, 2, 1, 1)]
|
||||
|
||||
|
||||
def test_display_weekly_notification_statistics_week_limit(notify_db,
|
||||
notify_db_session,
|
||||
sample_service):
|
||||
fools = date(2016, 4, 1)
|
||||
two_weeks_later = fools + timedelta(days=14)
|
||||
create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
day=fools
|
||||
)
|
||||
create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
day=two_weeks_later
|
||||
)
|
||||
assert dao_get_7_day_agg_notification_statistics_for_service(
|
||||
sample_service.id,
|
||||
fools,
|
||||
1
|
||||
).all() == [(0, 2, 1, 1, 2, 1, 1)]
|
||||
122
tests/app/notifications/test_notification_statistics_rest.py
Normal file
122
tests/app/notifications/test_notification_statistics_rest.py
Normal file
@@ -0,0 +1,122 @@
|
||||
import json
|
||||
from datetime import (date, timedelta)
|
||||
from flask import url_for
|
||||
from tests import create_authorization_header
|
||||
from tests.app.conftest import sample_notification_statistics as create_sample_notification_statistics
|
||||
|
||||
|
||||
def test_get_notification_statistics(
|
||||
notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_template,
|
||||
sample_email_template
|
||||
):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
path = '/service/{}/notifications-statistics'.format(sample_email_template.service)
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_email_template.service_id)
|
||||
|
||||
response = client.get(path, headers=[auth_header])
|
||||
assert response.status_code == 404
|
||||
|
||||
stats = json.loads(response.get_data(as_text=True))
|
||||
assert stats['result'] == 'error'
|
||||
assert stats['message'] == 'No result found'
|
||||
|
||||
|
||||
def test_get_week_aggregate_statistics(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service):
|
||||
with notify_api.test_request_context():
|
||||
sample_notification_statistics = create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
day=date(date.today().year, 4, 1))
|
||||
with notify_api.test_client() as client:
|
||||
endpoint = url_for(
|
||||
'notifications-statistics.get_notification_statistics_for_service_seven_day_aggregate',
|
||||
service_id=sample_service.id)
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_service.id)
|
||||
|
||||
resp = client.get(endpoint, headers=[auth_header])
|
||||
assert resp.status_code == 200
|
||||
json_resp = json.loads(resp.get_data(as_text=True))
|
||||
week_len_index = len(json_resp['data']) - 1
|
||||
assert json_resp['data'][week_len_index]['emails_requested'] == 2
|
||||
assert json_resp['data'][week_len_index]['sms_requested'] == 2
|
||||
assert json_resp['data'][week_len_index]['week_start'] == date(date.today().year, 4, 1).strftime('%Y-%m-%d')
|
||||
assert json_resp['data'][week_len_index]['week_end'] == date(date.today().year, 4, 7).strftime('%Y-%m-%d')
|
||||
|
||||
|
||||
def test_get_week_aggregate_statistics_date_from(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service):
|
||||
with notify_api.test_request_context():
|
||||
sample_notification_statistics = create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
day=date(date.today().year, 4, 1))
|
||||
date_from_str = date(date.today().year, 4, 1).strftime('%Y-%m-%d')
|
||||
with notify_api.test_client() as client:
|
||||
endpoint = url_for(
|
||||
'notifications-statistics.get_notification_statistics_for_service_seven_day_aggregate',
|
||||
service_id=sample_service.id,
|
||||
date_from=date_from_str)
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_service.id)
|
||||
|
||||
resp = client.get(endpoint, headers=[auth_header])
|
||||
assert resp.status_code == 200
|
||||
json_resp = json.loads(resp.get_data(as_text=True))
|
||||
week_len_index = len(json_resp['data']) - 1
|
||||
assert json_resp['data'][week_len_index]['emails_requested'] == 2
|
||||
assert json_resp['data'][week_len_index]['sms_requested'] == 2
|
||||
assert json_resp['data'][week_len_index]['week_start'] == date_from_str
|
||||
assert json_resp['data'][week_len_index]['week_end'] == date(date.today().year, 4, 7).strftime('%Y-%m-%d')
|
||||
|
||||
|
||||
def test_get_week_aggregate_statistics_date_in_future(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
endpoint = url_for(
|
||||
'notifications-statistics.get_notification_statistics_for_service_seven_day_aggregate',
|
||||
service_id=sample_service.id,
|
||||
date_from=(date.today() + timedelta(days=1)).strftime('%Y-%m-%d'))
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_service.id)
|
||||
|
||||
resp = client.get(endpoint, headers=[auth_header])
|
||||
assert resp.status_code == 400
|
||||
json_resp = json.loads(resp.get_data(as_text=True))
|
||||
assert json_resp['result'] == 'error'
|
||||
assert json_resp['message']['date_from'][0] == 'Date cannot be in the future'
|
||||
|
||||
|
||||
def test_get_week_aggregate_statistics_invalid_week_count(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
endpoint = url_for(
|
||||
'notifications-statistics.get_notification_statistics_for_service_seven_day_aggregate',
|
||||
service_id=sample_service.id,
|
||||
week_count=-1)
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_service.id)
|
||||
|
||||
resp = client.get(endpoint, headers=[auth_header])
|
||||
assert resp.status_code == 400
|
||||
json_resp = json.loads(resp.get_data(as_text=True))
|
||||
assert json_resp['result'] == 'error'
|
||||
assert json_resp['message']['week_count'][0] == 'Not a positive integer'
|
||||
@@ -442,28 +442,6 @@ def test_filter_by_status_and_template_type(notify_api,
|
||||
assert notifications['notifications'][0]['status'] == 'delivered'
|
||||
|
||||
|
||||
def test_get_notification_statistics(
|
||||
notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_template,
|
||||
sample_email_template
|
||||
):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
path = '/service/{}/notifications-statistics'.format(sample_email_template.service)
|
||||
|
||||
auth_header = create_authorization_header(service_id=sample_email_template.service_id)
|
||||
|
||||
response = client.get(path, headers=[auth_header])
|
||||
assert response.status_code == 404
|
||||
|
||||
stats = json.loads(response.get_data(as_text=True))
|
||||
assert stats['result'] == 'error'
|
||||
assert stats['message'] == 'No result found'
|
||||
|
||||
|
||||
def test_create_sms_should_reject_if_missing_required_fields(notify_api, sample_api_key, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
Reference in New Issue
Block a user