mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Remove NotificationStatistics
NotificationStatistics was added as a spike but didn't work out as expected. This is finally removing all that unused code. I'll drop the table in the next PR
This commit is contained in:
@@ -24,7 +24,6 @@ from app.models import (
|
||||
ProviderDetails,
|
||||
ProviderDetailsHistory,
|
||||
ProviderRates,
|
||||
NotificationStatistics,
|
||||
ScheduledNotification,
|
||||
ServiceWhitelist,
|
||||
KEY_TYPE_NORMAL, KEY_TYPE_TEST, KEY_TYPE_TEAM,
|
||||
@@ -840,35 +839,6 @@ def sample_provider_statistics(notify_db,
|
||||
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
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_firetext_client(mocker, statsd_client=None):
|
||||
client = FiretextClient()
|
||||
|
||||
@@ -13,7 +13,6 @@ from app.models import (
|
||||
NotificationEmailReplyTo,
|
||||
NotificationHistory,
|
||||
NotificationSmsSender,
|
||||
NotificationStatistics,
|
||||
ScheduledNotification,
|
||||
EMAIL_TYPE,
|
||||
SMS_TYPE,
|
||||
@@ -38,8 +37,6 @@ from app.dao.notifications_dao import (
|
||||
dao_get_notification_email_reply_for_notification,
|
||||
dao_get_notifications_by_to_field,
|
||||
dao_get_notification_sms_sender_mapping,
|
||||
dao_get_notification_statistics_for_service_and_day,
|
||||
dao_get_potential_notification_statistics_for_day,
|
||||
dao_get_scheduled_notifications,
|
||||
dao_get_template_usage,
|
||||
dao_timeout_notifications,
|
||||
@@ -79,7 +76,6 @@ from tests.app.conftest import (
|
||||
def test_should_have_decorated_notifications_dao_functions():
|
||||
assert dao_get_last_template_usage.__wrapped__.__name__ == 'dao_get_last_template_usage' # noqa
|
||||
assert dao_get_template_usage.__wrapped__.__name__ == 'dao_get_template_usage' # noqa
|
||||
assert dao_get_potential_notification_statistics_for_day.__wrapped__.__name__ == 'dao_get_potential_notification_statistics_for_day' # noqa
|
||||
assert dao_create_notification.__wrapped__.__name__ == 'dao_create_notification' # noqa
|
||||
assert update_notification_status_by_id.__wrapped__.__name__ == 'update_notification_status_by_id' # noqa
|
||||
assert dao_update_notification.__wrapped__.__name__ == 'dao_update_notification' # noqa
|
||||
@@ -581,31 +577,10 @@ def test_should_return_zero_count_if_no_notification_with_reference():
|
||||
assert not update_notification_status_by_reference('something', 'delivered')
|
||||
|
||||
|
||||
def test_should_return_none_if_no_statistics_for_a_service_for_a_day(sample_template, mmg_provider):
|
||||
data = _notification_json(sample_template)
|
||||
|
||||
notification = Notification(**data)
|
||||
dao_create_notification(notification)
|
||||
assert not dao_get_notification_statistics_for_service_and_day(
|
||||
sample_template.service.id, (datetime.utcnow() - timedelta(days=1)).date())
|
||||
|
||||
|
||||
def test_should_be_able_to_get_all_statistics_for_a_service(sample_template, mmg_provider):
|
||||
data = _notification_json(sample_template)
|
||||
|
||||
notification_1 = Notification(**data)
|
||||
notification_2 = Notification(**data)
|
||||
notification_3 = Notification(**data)
|
||||
dao_create_notification(notification_1)
|
||||
dao_create_notification(notification_2)
|
||||
dao_create_notification(notification_3)
|
||||
|
||||
|
||||
def test_create_notification_creates_notification_with_personalisation(notify_db, notify_db_session,
|
||||
sample_template_with_placeholders,
|
||||
sample_job, mmg_provider):
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationStatistics.query.count() == 0
|
||||
|
||||
data = sample_notification(notify_db=notify_db, notify_db_session=notify_db_session,
|
||||
template=sample_template_with_placeholders,
|
||||
@@ -628,7 +603,6 @@ def test_create_notification_creates_notification_with_personalisation(notify_db
|
||||
|
||||
def test_save_notification_creates_sms(sample_template, sample_job, mmg_provider):
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationStatistics.query.count() == 0
|
||||
|
||||
data = _notification_json(sample_template, job_id=sample_job.id)
|
||||
|
||||
@@ -649,7 +623,6 @@ def test_save_notification_creates_sms(sample_template, sample_job, mmg_provider
|
||||
|
||||
def test_save_notification_and_create_email(sample_email_template, sample_job):
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationStatistics.query.count() == 0
|
||||
|
||||
data = _notification_json(sample_email_template, job_id=sample_job.id)
|
||||
|
||||
@@ -773,7 +746,6 @@ def test_not_save_notification_and_not_create_stats_on_commit_error(sample_templ
|
||||
|
||||
assert Notification.query.count() == 0
|
||||
assert Job.query.get(sample_job.id).notifications_sent == 0
|
||||
assert NotificationStatistics.query.count() == 0
|
||||
|
||||
|
||||
def test_save_notification_and_increment_job(sample_template, sample_job, mmg_provider):
|
||||
|
||||
@@ -35,7 +35,6 @@ from app.dao.services_dao import (
|
||||
from app.dao.service_permissions_dao import dao_add_service_permission, dao_remove_service_permission
|
||||
from app.dao.users_dao import save_model_user
|
||||
from app.models import (
|
||||
NotificationStatistics,
|
||||
ProviderStatistics,
|
||||
VerifyCode,
|
||||
ApiKey,
|
||||
@@ -447,7 +446,6 @@ def test_delete_service_and_associated_objects(notify_db,
|
||||
assert ServicePermission.query.count() == 3
|
||||
|
||||
delete_service_and_all_associated_db_objects(sample_service)
|
||||
assert NotificationStatistics.query.count() == 0
|
||||
assert ProviderStatistics.query.count() == 0
|
||||
assert VerifyCode.query.count() == 0
|
||||
assert ApiKey.query.count() == 0
|
||||
|
||||
@@ -9,7 +9,6 @@ import app.celery.tasks
|
||||
from app.dao.notifications_dao import (
|
||||
get_notification_by_id
|
||||
)
|
||||
from app.models import NotificationStatistics
|
||||
from tests.app.notifications.test_notifications_ses_callback import ses_confirmation_callback
|
||||
from tests.app.conftest import sample_notification as create_sample_notification
|
||||
|
||||
@@ -431,10 +430,6 @@ def test_firetext_callback_should_record_statsd(client, notify_db, notify_db_ses
|
||||
app.statsd_client.incr.assert_any_call("callback.firetext.delivered")
|
||||
|
||||
|
||||
def get_notification_stats(service_id):
|
||||
return NotificationStatistics.query.filter_by(service_id=service_id).one()
|
||||
|
||||
|
||||
def _sample_sns_s3_callback():
|
||||
return json.dumps({
|
||||
"SigningCertURL": "foo.pem",
|
||||
|
||||
@@ -1,225 +0,0 @@
|
||||
from datetime import date, timedelta
|
||||
|
||||
from flask import json
|
||||
from freezegun import freeze_time
|
||||
from datetime import datetime
|
||||
|
||||
from tests import create_authorization_header
|
||||
from tests.app.conftest import (
|
||||
sample_notification_statistics as create_sample_notification_statistics,
|
||||
sample_service as create_sample_service
|
||||
)
|
||||
|
||||
|
||||
def test_get_notification_statistics(notify_api, sample_notification_statistics):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_notification_statistics.service_id
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/notifications/statistics?day={}'.format(date.today().isoformat()),
|
||||
headers=[auth_header]
|
||||
)
|
||||
|
||||
notifications = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert len(notifications['data']) == 1
|
||||
stats = notifications['data'][0]
|
||||
assert stats['emails_requested'] == 2
|
||||
assert stats['emails_delivered'] == 1
|
||||
assert stats['emails_failed'] == 1
|
||||
assert stats['sms_requested'] == 2
|
||||
assert stats['sms_delivered'] == 1
|
||||
assert stats['sms_failed'] == 1
|
||||
assert stats['service'] == str(sample_notification_statistics.service_id)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
@freeze_time('1955-11-05T12:00:00')
|
||||
def test_get_notification_statistics_only_returns_today(notify_api, notify_db, notify_db_session, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
yesterdays_notification_statistics = create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
day=date.today() - timedelta(days=1)
|
||||
)
|
||||
todays_notification_statistics = create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
day=date.today()
|
||||
)
|
||||
tomorrows_notification_statistics = create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
day=date.today() + timedelta(days=1)
|
||||
)
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_service.id
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/notifications/statistics?day={}'.format(date.today().isoformat()),
|
||||
headers=[auth_header]
|
||||
)
|
||||
|
||||
notifications = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert len(notifications['data']) == 1
|
||||
assert notifications['data'][0]['day'] == date.today().isoformat()
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_get_notification_statistics_fails_if_no_date(notify_api, sample_notification_statistics):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_notification_statistics.service_id
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/notifications/statistics',
|
||||
headers=[auth_header]
|
||||
)
|
||||
|
||||
resp = json.loads(response.get_data(as_text=True))
|
||||
assert resp['result'] == 'error'
|
||||
assert resp['message'] == {'day': ['Missing data for required field.']}
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
def test_get_notification_statistics_fails_if_invalid_date(notify_api, sample_notification_statistics):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_notification_statistics.service_id
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/notifications/statistics?day=2016-99-99',
|
||||
headers=[auth_header]
|
||||
)
|
||||
|
||||
resp = json.loads(response.get_data(as_text=True))
|
||||
assert resp['result'] == 'error'
|
||||
assert resp['message'] == {'day': ['Not a valid date.']}
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
def test_get_notification_statistics_returns_zeros_if_not_in_db(notify_api, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_service.id
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/notifications/statistics?day={}'.format(date.today().isoformat()),
|
||||
headers=[auth_header]
|
||||
)
|
||||
|
||||
notifications = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert len(notifications['data']) == 1
|
||||
stats = notifications['data'][0]
|
||||
assert stats['emails_requested'] == 0
|
||||
assert stats['emails_delivered'] == 0
|
||||
assert stats['emails_failed'] == 0
|
||||
assert stats['sms_requested'] == 0
|
||||
assert stats['sms_delivered'] == 0
|
||||
assert stats['sms_failed'] == 0
|
||||
assert stats['service'] == str(sample_service.id)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_get_notification_statistics_returns_both_existing_stats_and_generated_zeros(
|
||||
notify_api,
|
||||
notify_db,
|
||||
notify_db_session
|
||||
):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
service_with_stats = create_sample_service(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service_name='service_with_stats',
|
||||
email_from='service_with_stats'
|
||||
)
|
||||
service_without_stats = create_sample_service(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service_name='service_without_stats',
|
||||
email_from='service_without_stats'
|
||||
)
|
||||
notification_statistics = create_sample_notification_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=service_with_stats,
|
||||
day=date.today()
|
||||
)
|
||||
auth_header = create_authorization_header(
|
||||
service_id=service_with_stats.id
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/notifications/statistics?day={}'.format(date.today().isoformat()),
|
||||
headers=[auth_header]
|
||||
)
|
||||
|
||||
notifications = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert len(notifications['data']) == 2
|
||||
retrieved_stats = notifications['data'][0]
|
||||
generated_stats = notifications['data'][1]
|
||||
|
||||
assert retrieved_stats['emails_requested'] == 2
|
||||
assert retrieved_stats['emails_delivered'] == 1
|
||||
assert retrieved_stats['emails_failed'] == 1
|
||||
assert retrieved_stats['sms_requested'] == 2
|
||||
assert retrieved_stats['sms_delivered'] == 1
|
||||
assert retrieved_stats['sms_failed'] == 1
|
||||
assert retrieved_stats['service'] == str(service_with_stats.id)
|
||||
|
||||
assert generated_stats['emails_requested'] == 0
|
||||
assert generated_stats['emails_delivered'] == 0
|
||||
assert generated_stats['emails_failed'] == 0
|
||||
assert generated_stats['sms_requested'] == 0
|
||||
assert generated_stats['sms_delivered'] == 0
|
||||
assert generated_stats['sms_failed'] == 0
|
||||
assert generated_stats['service'] == str(service_without_stats.id)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_get_notification_statistics_returns_zeros_when_only_stats_for_different_date(
|
||||
notify_api,
|
||||
sample_notification_statistics
|
||||
):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
with freeze_time('1985-10-26T00:06:00'):
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_notification_statistics.service_id
|
||||
)
|
||||
response = client.get(
|
||||
'/notifications/statistics?day={}'.format(datetime.utcnow().isoformat()),
|
||||
headers=[auth_header]
|
||||
)
|
||||
|
||||
notifications = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert len(notifications['data']) == 1
|
||||
assert notifications['data'][0]['emails_requested'] == 0
|
||||
assert notifications['data'][0]['emails_delivered'] == 0
|
||||
assert notifications['data'][0]['emails_failed'] == 0
|
||||
assert notifications['data'][0]['sms_requested'] == 0
|
||||
assert notifications['data'][0]['sms_delivered'] == 0
|
||||
assert notifications['data'][0]['sms_failed'] == 0
|
||||
assert notifications['data'][0]['service'] == str(sample_notification_statistics.service_id)
|
||||
Reference in New Issue
Block a user