|
|
|
|
@@ -33,8 +33,7 @@ from app.dao.notifications_dao import (
|
|
|
|
|
get_notifications_for_job,
|
|
|
|
|
get_notifications_for_service,
|
|
|
|
|
update_notification_status_by_id,
|
|
|
|
|
update_notification_status_by_reference,
|
|
|
|
|
update_provider_stats,
|
|
|
|
|
update_notification_status_by_reference
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
from notifications_utils.template import get_sms_fragment_count
|
|
|
|
|
@@ -49,7 +48,6 @@ def test_should_have_decorated_notifications_dao_functions():
|
|
|
|
|
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
|
|
|
|
|
assert update_provider_stats.__wrapped__.__name__ == 'update_provider_stats' # noqa
|
|
|
|
|
assert update_notification_status_by_reference.__wrapped__.__name__ == 'update_notification_status_by_reference' # noqa
|
|
|
|
|
assert get_notification_for_job.__wrapped__.__name__ == 'get_notification_for_job' # noqa
|
|
|
|
|
assert get_notifications_for_job.__wrapped__.__name__ == 'get_notifications_for_job' # noqa
|
|
|
|
|
@@ -232,33 +230,28 @@ def test_should_by_able_to_update_status_by_reference(sample_email_template, ses
|
|
|
|
|
data = _notification_json(sample_email_template, status='sending')
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(
|
|
|
|
|
notification,
|
|
|
|
|
sample_email_template.template_type)
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.get(notification.id).status == "sending"
|
|
|
|
|
notification.reference = 'reference'
|
|
|
|
|
dao_update_notification(notification)
|
|
|
|
|
|
|
|
|
|
update_notification_status_by_reference('reference', 'delivered', 'delivered')
|
|
|
|
|
update_notification_status_by_reference('reference', 'delivered')
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'delivered'
|
|
|
|
|
_assert_notification_stats(notification.service_id, emails_delivered=1, emails_requested=1, emails_failed=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_by_able_to_update_status_by_id(sample_template, sample_job, mmg_provider):
|
|
|
|
|
with freeze_time('2000-01-01 12:00:00'):
|
|
|
|
|
data = _notification_json(sample_template, job_id=sample_job.id, status='sending')
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'sending'
|
|
|
|
|
|
|
|
|
|
with freeze_time('2000-01-02 12:00:00'):
|
|
|
|
|
assert update_notification_status_by_id(notification.id, 'delivered', 'delivered')
|
|
|
|
|
assert update_notification_status_by_id(notification.id, 'delivered')
|
|
|
|
|
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'delivered'
|
|
|
|
|
_assert_notification_stats(notification.service_id, sms_delivered=1, sms_requested=1, sms_failed=0)
|
|
|
|
|
_assert_job_stats(notification.job_id, sent=1, count=1, delivered=1, failed=0)
|
|
|
|
|
assert notification.updated_at == datetime(2000, 1, 2, 12, 0, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -266,7 +259,7 @@ def test_should_not_update_status_by_id_if_not_sending_and_does_not_update_job(n
|
|
|
|
|
notification = sample_notification(notify_db, notify_db_session, status='delivered')
|
|
|
|
|
job = Job.query.get(notification.job_id)
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'delivered'
|
|
|
|
|
assert not update_notification_status_by_id(notification.id, 'failed', 'failure')
|
|
|
|
|
assert not update_notification_status_by_id(notification.id, 'failed')
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'delivered'
|
|
|
|
|
assert job == Job.query.get(notification.job_id)
|
|
|
|
|
|
|
|
|
|
@@ -274,58 +267,46 @@ def test_should_not_update_status_by_id_if_not_sending_and_does_not_update_job(n
|
|
|
|
|
def test_should_update_status_if_created(notify_db, notify_db_session):
|
|
|
|
|
notification = sample_notification(notify_db, notify_db_session, status='created')
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'created'
|
|
|
|
|
assert update_notification_status_by_id(notification.id, 'failed', 'failure')
|
|
|
|
|
assert update_notification_status_by_id(notification.id, 'failed')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_by_able_to_update_status_by_id_from_pending_to_delivered(sample_template, sample_job):
|
|
|
|
|
data = _notification_json(sample_template, job_id=sample_job.id, status='sending')
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'sending'
|
|
|
|
|
assert update_notification_status_by_id(notification_id=notification.id, status='pending')
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'pending'
|
|
|
|
|
_assert_notification_stats(notification.service_id, sms_requested=1, sms_delivered=0, sms_failed=0)
|
|
|
|
|
_assert_job_stats(sample_job.id, sent=1, count=1, delivered=0, failed=0)
|
|
|
|
|
|
|
|
|
|
assert update_notification_status_by_id(notification.id, 'delivered', 'delivered')
|
|
|
|
|
assert update_notification_status_by_id(notification.id, 'delivered')
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'delivered'
|
|
|
|
|
_assert_notification_stats(notification.service_id, sms_requested=1, sms_delivered=1, sms_failed=0)
|
|
|
|
|
_assert_job_stats(notification.job_id, sent=1, count=1, delivered=1, failed=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_by_able_to_update_status_by_id_from_pending_to_temporary_failure(sample_template, sample_job):
|
|
|
|
|
data = _notification_json(sample_template, job_id=sample_job.id, status='sending')
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'sending'
|
|
|
|
|
assert update_notification_status_by_id(notification_id=notification.id, status='pending')
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'pending'
|
|
|
|
|
_assert_notification_stats(notification.service_id, sms_requested=1, sms_delivered=0, sms_failed=0)
|
|
|
|
|
_assert_job_stats(notification.job_id, sent=1, count=1, delivered=0, failed=0)
|
|
|
|
|
|
|
|
|
|
assert update_notification_status_by_id(
|
|
|
|
|
notification.id,
|
|
|
|
|
status='permanent-failure',
|
|
|
|
|
notification_statistics_status='failure')
|
|
|
|
|
status='permanent-failure')
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'temporary-failure'
|
|
|
|
|
_assert_notification_stats(notification.service_id, sms_delivered=0, sms_requested=1, sms_failed=1)
|
|
|
|
|
_assert_job_stats(sample_job.id, sent=1, count=1, delivered=0, failed=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_by_able_to_update_status_by_id_from_sending_to_permanent_failure(sample_template, sample_job):
|
|
|
|
|
data = _notification_json(sample_template, job_id=sample_job.id, status='sending')
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'sending'
|
|
|
|
|
|
|
|
|
|
assert update_notification_status_by_id(
|
|
|
|
|
notification.id,
|
|
|
|
|
status='permanent-failure',
|
|
|
|
|
notification_statistics_status='failure'
|
|
|
|
|
status='permanent-failure'
|
|
|
|
|
)
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'permanent-failure'
|
|
|
|
|
_assert_notification_stats(notification.service_id, sms_requested=1, sms_delivered=0, sms_failed=1)
|
|
|
|
|
_assert_job_stats(sample_job.id, sent=1, count=1, delivered=0, failed=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_not_update_status_one_notification_status_is_delivered(notify_db, notify_db_session,
|
|
|
|
|
@@ -336,90 +317,28 @@ def test_should_not_update_status_one_notification_status_is_delivered(notify_db
|
|
|
|
|
status='sending')
|
|
|
|
|
assert Notification.query.get(notification.id).status == "sending"
|
|
|
|
|
|
|
|
|
|
update_provider_stats(
|
|
|
|
|
notification.id,
|
|
|
|
|
'email',
|
|
|
|
|
ses_provider.identifier)
|
|
|
|
|
notification.reference = 'reference'
|
|
|
|
|
dao_update_notification(notification)
|
|
|
|
|
update_notification_status_by_reference('reference', 'delivered', 'delivered')
|
|
|
|
|
update_notification_status_by_reference('reference', 'delivered')
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'delivered'
|
|
|
|
|
|
|
|
|
|
update_notification_status_by_reference('reference', 'failed', 'temporary-failure')
|
|
|
|
|
update_notification_status_by_reference('reference', 'failed')
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'delivered'
|
|
|
|
|
_assert_notification_stats(notification.service_id, emails_requested=1, emails_delivered=1, emails_failed=0)
|
|
|
|
|
_assert_job_stats(notification.job_id, sent=1, count=1, delivered=1, failed=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_be_able_to_record_statistics_failure_for_sms(notify_db, notify_db_session, ):
|
|
|
|
|
notification = sample_notification(notify_db=notify_db, notify_db_session=notify_db_session, status='sending')
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'sending'
|
|
|
|
|
|
|
|
|
|
assert update_notification_status_by_id(notification.id, 'permanent-failure', 'failure')
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'permanent-failure'
|
|
|
|
|
_assert_notification_stats(notification.service_id, sms_requested=1, sms_delivered=0, sms_failed=1)
|
|
|
|
|
_assert_job_stats(notification.job_id, sent=1, count=1, delivered=0, failed=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_be_able_to_record_statistics_failure_for_email(sample_email_template, sample_job, ses_provider):
|
|
|
|
|
data = _notification_json(sample_email_template, job_id=sample_job.id, status='sending')
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_email_template.template_type)
|
|
|
|
|
|
|
|
|
|
update_provider_stats(
|
|
|
|
|
notification.id,
|
|
|
|
|
'email',
|
|
|
|
|
ses_provider.identifier)
|
|
|
|
|
notification.reference = 'reference'
|
|
|
|
|
dao_update_notification(notification)
|
|
|
|
|
count = update_notification_status_by_reference('reference', 'failed', 'failure')
|
|
|
|
|
assert count == 1
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'failed'
|
|
|
|
|
_assert_notification_stats(notification.service_id, emails_requested=1, emails_delivered=0, emails_failed=1)
|
|
|
|
|
_assert_job_stats(notification.job_id, sent=1, count=1, delivered=0, failed=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_return_zero_count_if_no_notification_with_id():
|
|
|
|
|
assert not update_notification_status_by_id(str(uuid.uuid4()), 'delivered', 'delivered')
|
|
|
|
|
assert not update_notification_status_by_id(str(uuid.uuid4()), 'delivered')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_return_zero_count_if_no_notification_with_reference():
|
|
|
|
|
assert not update_notification_status_by_reference('something', 'delivered', 'delivered')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_be_able_to_get_statistics_for_a_service(sample_template, mmg_provider):
|
|
|
|
|
data = _notification_json(sample_template)
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
_assert_notification_stats(notification.service_id, sms_requested=1,
|
|
|
|
|
notification_created_at=notification.created_at.date())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_be_able_to_get_statistics_for_a_service_for_a_day(sample_template, mmg_provider):
|
|
|
|
|
now = datetime.utcnow()
|
|
|
|
|
data = _notification_json(sample_template)
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
stat = dao_get_notification_statistics_for_service_and_day(
|
|
|
|
|
sample_template.service.id, now.date()
|
|
|
|
|
)
|
|
|
|
|
assert stat.emails_requested == 0
|
|
|
|
|
assert stat.emails_failed == 0
|
|
|
|
|
assert stat.emails_delivered == 0
|
|
|
|
|
assert stat.sms_requested == 1
|
|
|
|
|
assert stat.sms_failed == 0
|
|
|
|
|
assert stat.sms_delivered == 0
|
|
|
|
|
assert stat.day == notification.created_at.date()
|
|
|
|
|
assert stat.service_id == notification.service_id
|
|
|
|
|
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, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
assert not dao_get_notification_statistics_for_service_and_day(
|
|
|
|
|
sample_template.service.id, (datetime.utcnow() - timedelta(days=1)).date())
|
|
|
|
|
|
|
|
|
|
@@ -430,11 +349,9 @@ def test_should_be_able_to_get_all_statistics_for_a_service(sample_template, mmg
|
|
|
|
|
notification_1 = Notification(**data)
|
|
|
|
|
notification_2 = Notification(**data)
|
|
|
|
|
notification_3 = Notification(**data)
|
|
|
|
|
dao_create_notification(notification_1, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification_2, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification_3, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
_assert_notification_stats(sample_template.service.id, sms_requested=3)
|
|
|
|
|
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,
|
|
|
|
|
@@ -461,22 +378,9 @@ def test_create_notification_creates_notification_with_personalisation(notify_db
|
|
|
|
|
assert data.created_at == notification_from_db.created_at
|
|
|
|
|
assert notification_from_db.status == 'created'
|
|
|
|
|
assert {'name': 'Jo'} == notification_from_db.personalisation
|
|
|
|
|
_assert_job_stats(sample_job.id, sent=1, count=1, delivered=0, failed=0)
|
|
|
|
|
|
|
|
|
|
stats = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_template_with_placeholders.service.id).first()
|
|
|
|
|
assert stats.emails_requested == 0
|
|
|
|
|
assert stats.sms_requested == 1
|
|
|
|
|
|
|
|
|
|
template_stats = TemplateStatistics.query.filter(
|
|
|
|
|
TemplateStatistics.service_id == sample_template_with_placeholders.service.id,
|
|
|
|
|
TemplateStatistics.template_id == sample_template_with_placeholders.id).first()
|
|
|
|
|
assert template_stats.service_id == sample_template_with_placeholders.service.id
|
|
|
|
|
assert template_stats.template_id == sample_template_with_placeholders.id
|
|
|
|
|
assert template_stats.usage_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_notification_creates_sms_and_template_stats(sample_template, sample_job, mmg_provider):
|
|
|
|
|
def test_save_notification_creates_sms(sample_template, sample_job, mmg_provider):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
assert NotificationStatistics.query.count() == 0
|
|
|
|
|
assert TemplateStatistics.query.count() == 0
|
|
|
|
|
@@ -484,7 +388,7 @@ def test_save_notification_creates_sms_and_template_stats(sample_template, sampl
|
|
|
|
|
data = _notification_json(sample_template, job_id=sample_job.id)
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
notification_from_db = Notification.query.all()[0]
|
|
|
|
|
@@ -496,20 +400,9 @@ def test_save_notification_creates_sms_and_template_stats(sample_template, sampl
|
|
|
|
|
assert data['template_version'] == notification_from_db.template_version
|
|
|
|
|
assert data['created_at'] == notification_from_db.created_at
|
|
|
|
|
assert notification_from_db.status == 'created'
|
|
|
|
|
_assert_job_stats(sample_job.id, sent=1, count=1, delivered=0, failed=0)
|
|
|
|
|
|
|
|
|
|
stats = NotificationStatistics.query.filter(NotificationStatistics.service_id == sample_template.service.id).first()
|
|
|
|
|
assert stats.emails_requested == 0
|
|
|
|
|
assert stats.sms_requested == 1
|
|
|
|
|
|
|
|
|
|
template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id,
|
|
|
|
|
TemplateStatistics.template_id == sample_template.id).first()
|
|
|
|
|
assert template_stats.service_id == sample_template.service.id
|
|
|
|
|
assert template_stats.template_id == sample_template.id
|
|
|
|
|
assert template_stats.usage_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_notification_and_create_email_and_template_stats(sample_email_template, sample_job, ses_provider):
|
|
|
|
|
def test_save_notification_and_create_email(sample_email_template, sample_job, ses_provider):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
assert NotificationStatistics.query.count() == 0
|
|
|
|
|
assert TemplateStatistics.query.count() == 0
|
|
|
|
|
@@ -517,7 +410,7 @@ def test_save_notification_and_create_email_and_template_stats(sample_email_temp
|
|
|
|
|
data = _notification_json(sample_email_template, job_id=sample_job.id)
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_email_template.template_type)
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
notification_from_db = Notification.query.all()[0]
|
|
|
|
|
@@ -529,109 +422,37 @@ def test_save_notification_and_create_email_and_template_stats(sample_email_temp
|
|
|
|
|
assert data['template_version'] == notification_from_db.template_version
|
|
|
|
|
assert data['created_at'] == notification_from_db.created_at
|
|
|
|
|
assert notification_from_db.status == 'created'
|
|
|
|
|
_assert_job_stats(sample_job.id, sent=1, count=1, delivered=0, failed=0)
|
|
|
|
|
|
|
|
|
|
stats = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_email_template.service.id).first()
|
|
|
|
|
|
|
|
|
|
assert stats.emails_requested == 1
|
|
|
|
|
assert stats.sms_requested == 0
|
|
|
|
|
|
|
|
|
|
template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_email_template.service.id,
|
|
|
|
|
TemplateStatistics.template_id == sample_email_template.id).first() # noqa
|
|
|
|
|
|
|
|
|
|
assert template_stats.service_id == sample_email_template.service.id
|
|
|
|
|
assert template_stats.template_id == sample_email_template.id
|
|
|
|
|
assert template_stats.usage_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2016-01-01 00:00:00.000000")
|
|
|
|
|
def test_save_notification_handles_midnight_properly(sample_template, sample_job, mmg_provider):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
data = _notification_json(sample_template, sample_job.id)
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
|
|
|
|
|
stats = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_template.service.id).first()
|
|
|
|
|
|
|
|
|
|
assert stats.day == date(2016, 1, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2016-01-01 23:59:59.999999")
|
|
|
|
|
def test_save_notification_handles_just_before_midnight_properly(sample_template, sample_job, mmg_provider):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
data = _notification_json(sample_template, job_id=sample_job.id)
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
|
|
|
|
|
stats = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_template.service.id).first()
|
|
|
|
|
|
|
|
|
|
assert stats.day == date(2016, 1, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_notification_and_increment_email_stats(sample_email_template, sample_job, ses_provider):
|
|
|
|
|
def test_save_notification(sample_email_template, sample_job, ses_provider):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
data = _notification_json(sample_email_template, job_id=sample_job.id)
|
|
|
|
|
|
|
|
|
|
notification_1 = Notification(**data)
|
|
|
|
|
notification_2 = Notification(**data)
|
|
|
|
|
dao_create_notification(notification_1, sample_email_template.template_type)
|
|
|
|
|
dao_create_notification(notification_1)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
|
|
|
|
|
stats1 = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_email_template.service.id).first()
|
|
|
|
|
|
|
|
|
|
assert stats1.emails_requested == 1
|
|
|
|
|
assert stats1.sms_requested == 0
|
|
|
|
|
|
|
|
|
|
dao_create_notification(notification_2, sample_email_template.template_type)
|
|
|
|
|
dao_create_notification(notification_2)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 2
|
|
|
|
|
|
|
|
|
|
stats2 = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_email_template.service.id).first()
|
|
|
|
|
|
|
|
|
|
assert stats2.emails_requested == 2
|
|
|
|
|
assert stats2.sms_requested == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_notification_and_increment_sms_stats(sample_template, sample_job, mmg_provider):
|
|
|
|
|
def test_save_notification(sample_template, sample_job, mmg_provider):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
data = _notification_json(sample_template, sample_job.id)
|
|
|
|
|
|
|
|
|
|
notification_1 = Notification(**data)
|
|
|
|
|
notification_2 = Notification(**data)
|
|
|
|
|
dao_create_notification(notification_1, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification_1)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
|
|
|
|
|
stats1 = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_template.service.id
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
assert stats1.emails_requested == 0
|
|
|
|
|
assert stats1.sms_requested == 1
|
|
|
|
|
|
|
|
|
|
dao_create_notification(notification_2, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification_2)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 2
|
|
|
|
|
|
|
|
|
|
stats2 = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_template.service.id
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
assert stats2.emails_requested == 0
|
|
|
|
|
assert stats2.sms_requested == 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_not_save_notification_and_not_create_stats_on_commit_error(sample_template, sample_job, mmg_provider):
|
|
|
|
|
random_id = str(uuid.uuid4())
|
|
|
|
|
@@ -641,7 +462,7 @@ def test_not_save_notification_and_not_create_stats_on_commit_error(sample_templ
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
with pytest.raises(SQLAlchemyError):
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
assert Job.query.get(sample_job.id).notifications_sent == 0
|
|
|
|
|
@@ -654,7 +475,7 @@ def test_save_notification_and_increment_job(sample_template, sample_job, mmg_pr
|
|
|
|
|
data = _notification_json(sample_template, job_id=sample_job.id)
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
notification_from_db = Notification.query.all()[0]
|
|
|
|
|
@@ -666,34 +487,10 @@ def test_save_notification_and_increment_job(sample_template, sample_job, mmg_pr
|
|
|
|
|
assert data['template_version'] == notification_from_db.template_version
|
|
|
|
|
assert data['created_at'] == notification_from_db.created_at
|
|
|
|
|
assert notification_from_db.status == 'created'
|
|
|
|
|
assert Job.query.get(sample_job.id).notifications_sent == 1
|
|
|
|
|
|
|
|
|
|
notification_2 = Notification(**data)
|
|
|
|
|
dao_create_notification(notification_2, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification_2)
|
|
|
|
|
assert Notification.query.count() == 2
|
|
|
|
|
# count is off because the count is defaulted to 1 in the sample_job
|
|
|
|
|
_assert_job_stats(sample_job.id, sent=2, count=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_not_increment_job_if_notification_fails_to_persist(sample_template, sample_job, mmg_provider):
|
|
|
|
|
random_id = str(uuid.uuid4())
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
data = _notification_json(sample_template, job_id=sample_job.id, id=random_id)
|
|
|
|
|
|
|
|
|
|
notification_1 = Notification(**data)
|
|
|
|
|
dao_create_notification(notification_1, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
assert Job.query.get(sample_job.id).notifications_sent == 1
|
|
|
|
|
job_last_updated_at = Job.query.get(sample_job.id).updated_at
|
|
|
|
|
|
|
|
|
|
notification_2 = Notification(**data)
|
|
|
|
|
with pytest.raises(SQLAlchemyError):
|
|
|
|
|
dao_create_notification(notification_2, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
assert Job.query.get(sample_job.id).notifications_sent == 1
|
|
|
|
|
assert Job.query.get(sample_job.id).updated_at == job_last_updated_at
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_notification_and_increment_correct_job(notify_db, notify_db_session, sample_template, mmg_provider):
|
|
|
|
|
@@ -705,7 +502,7 @@ def test_save_notification_and_increment_correct_job(notify_db, notify_db_sessio
|
|
|
|
|
data = _notification_json(sample_template, job_id=job_1.id)
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
notification_from_db = Notification.query.all()[0]
|
|
|
|
|
@@ -718,8 +515,6 @@ def test_save_notification_and_increment_correct_job(notify_db, notify_db_sessio
|
|
|
|
|
assert data['created_at'] == notification_from_db.created_at
|
|
|
|
|
assert notification_from_db.status == 'created'
|
|
|
|
|
assert job_1.id != job_2.id
|
|
|
|
|
_assert_job_stats(job_id=job_1.id, sent=1, count=1)
|
|
|
|
|
_assert_job_stats(job_id=job_2.id, sent=0, count=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_notification_with_no_job(sample_template, mmg_provider):
|
|
|
|
|
@@ -727,7 +522,7 @@ def test_save_notification_with_no_job(sample_template, mmg_provider):
|
|
|
|
|
data = _notification_json(sample_template)
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
notification_from_db = Notification.query.all()[0]
|
|
|
|
|
@@ -754,7 +549,7 @@ def test_save_notification_no_job_id(sample_template, mmg_provider):
|
|
|
|
|
data = _notification_json(sample_template)
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
notification_from_db = Notification.query.all()[0]
|
|
|
|
|
@@ -788,7 +583,6 @@ def test_get_all_notifications_for_job(notify_db, notify_db_session, sample_job)
|
|
|
|
|
|
|
|
|
|
notifications_from_db = get_notifications_for_job(sample_job.service.id, sample_job.id).items
|
|
|
|
|
assert len(notifications_from_db) == 5
|
|
|
|
|
_assert_notification_stats(sample_job.service.id, sms_requested=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_all_notifications_for_job_by_status(notify_db, notify_db_session, sample_job):
|
|
|
|
|
@@ -870,120 +664,6 @@ def test_should_not_delete_failed_notifications_before_seven_days(notify_db, not
|
|
|
|
|
assert Notification.query.first().to == 'do_not_delete'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2016-03-30")
|
|
|
|
|
def test_save_new_notification_creates_template_stats(sample_template, sample_job, mmg_provider):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
assert TemplateStatistics.query.count() == 0
|
|
|
|
|
data = _notification_json(sample_template, job_id=sample_job.id)
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert TemplateStatistics.query.count() == 1
|
|
|
|
|
template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id,
|
|
|
|
|
TemplateStatistics.template_id == sample_template.id).first()
|
|
|
|
|
assert template_stats.service_id == sample_template.service.id
|
|
|
|
|
assert template_stats.template_id == sample_template.id
|
|
|
|
|
assert template_stats.usage_count == 1
|
|
|
|
|
assert template_stats.day == date(2016, 3, 30)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2016-03-30")
|
|
|
|
|
def test_save_new_notification_creates_template_stats_per_day(sample_template, sample_job, mmg_provider):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
assert TemplateStatistics.query.count() == 0
|
|
|
|
|
data = _notification_json(sample_template, job_id=sample_job.id)
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert TemplateStatistics.query.count() == 1
|
|
|
|
|
template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id,
|
|
|
|
|
TemplateStatistics.template_id == sample_template.id).first()
|
|
|
|
|
assert template_stats.service_id == sample_template.service.id
|
|
|
|
|
assert template_stats.template_id == sample_template.id
|
|
|
|
|
assert template_stats.usage_count == 1
|
|
|
|
|
assert template_stats.day == date(2016, 3, 30)
|
|
|
|
|
|
|
|
|
|
# move on one day
|
|
|
|
|
with freeze_time('2016-03-31'):
|
|
|
|
|
assert TemplateStatistics.query.count() == 1
|
|
|
|
|
new_notification = Notification(**data)
|
|
|
|
|
dao_create_notification(new_notification, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert TemplateStatistics.query.count() == 2
|
|
|
|
|
first_stats = TemplateStatistics.query.filter(TemplateStatistics.day == datetime(2016, 3, 30)).first()
|
|
|
|
|
second_stats = TemplateStatistics.query.filter(TemplateStatistics.day == datetime(2016, 3, 31)).first()
|
|
|
|
|
|
|
|
|
|
assert first_stats.template_id == second_stats.template_id
|
|
|
|
|
assert first_stats.service_id == second_stats.service_id
|
|
|
|
|
|
|
|
|
|
assert first_stats.day == date(2016, 3, 30)
|
|
|
|
|
assert first_stats.usage_count == 1
|
|
|
|
|
|
|
|
|
|
assert second_stats.day == date(2016, 3, 31)
|
|
|
|
|
assert second_stats.usage_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_another_notification_increments_template_stats(sample_template, sample_job, mmg_provider):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
assert TemplateStatistics.query.count() == 0
|
|
|
|
|
data = _notification_json(sample_template, job_id=sample_job.id)
|
|
|
|
|
|
|
|
|
|
notification_1 = Notification(**data)
|
|
|
|
|
notification_2 = Notification(**data)
|
|
|
|
|
dao_create_notification(notification_1, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert TemplateStatistics.query.count() == 1
|
|
|
|
|
template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id,
|
|
|
|
|
TemplateStatistics.template_id == sample_template.id).first()
|
|
|
|
|
assert template_stats.service_id == sample_template.service.id
|
|
|
|
|
assert template_stats.template_id == sample_template.id
|
|
|
|
|
assert template_stats.usage_count == 1
|
|
|
|
|
|
|
|
|
|
dao_create_notification(notification_2, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert TemplateStatistics.query.count() == 1
|
|
|
|
|
template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id,
|
|
|
|
|
TemplateStatistics.template_id == sample_template.id).first()
|
|
|
|
|
assert template_stats.usage_count == 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_successful_notification_inserts_followed_by_failure_does_not_increment_template_stats(sample_template,
|
|
|
|
|
sample_job,
|
|
|
|
|
mmg_provider):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
assert NotificationStatistics.query.count() == 0
|
|
|
|
|
assert TemplateStatistics.query.count() == 0
|
|
|
|
|
|
|
|
|
|
data = _notification_json(sample_template, job_id=sample_job.id)
|
|
|
|
|
|
|
|
|
|
notification_1 = Notification(**data)
|
|
|
|
|
notification_2 = Notification(**data)
|
|
|
|
|
notification_3 = Notification(**data)
|
|
|
|
|
dao_create_notification(notification_1, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification_2, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification_3, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
_assert_notification_stats(sample_template.service.id, sms_requested=3)
|
|
|
|
|
|
|
|
|
|
assert TemplateStatistics.query.count() == 1
|
|
|
|
|
template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id,
|
|
|
|
|
TemplateStatistics.template_id == sample_template.id).first()
|
|
|
|
|
assert template_stats.service_id == sample_template.service.id
|
|
|
|
|
assert template_stats.template_id == sample_template.id
|
|
|
|
|
assert template_stats.usage_count == 3
|
|
|
|
|
|
|
|
|
|
failing_notification = Notification(**data)
|
|
|
|
|
try:
|
|
|
|
|
# Mess up db in really bad way
|
|
|
|
|
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_notification_stats(sample_template.service.id, sms_requested=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2016-01-10")
|
|
|
|
|
def test_should_limit_notifications_return_by_day_limit_plus_one(notify_db, notify_db_session, sample_service):
|
|
|
|
|
assert len(Notification.query.all()) == 0
|
|
|
|
|
@@ -1024,7 +704,7 @@ def test_creating_notification_adds_to_notification_history(sample_template):
|
|
|
|
|
data = _notification_json(sample_template)
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
dao_create_notification(notification)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
|
|
|
|
|
@@ -1069,26 +749,3 @@ def _notification_json(sample_template, job_id=None, id=None, status=None):
|
|
|
|
|
if status:
|
|
|
|
|
data.update({'status': status})
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _assert_notification_stats(service_id,
|
|
|
|
|
emails_delivered=0, emails_requested=0, emails_failed=0,
|
|
|
|
|
sms_delivered=0, sms_requested=0, sms_failed=0,
|
|
|
|
|
notification_created_at=None):
|
|
|
|
|
stats = NotificationStatistics.query.filter_by(service_id=service_id).all()
|
|
|
|
|
assert len(stats) == 1
|
|
|
|
|
assert stats[0].emails_delivered == emails_delivered
|
|
|
|
|
assert stats[0].emails_requested == emails_requested
|
|
|
|
|
assert stats[0].emails_failed == emails_failed
|
|
|
|
|
assert stats[0].sms_delivered == sms_delivered
|
|
|
|
|
assert stats[0].sms_requested == sms_requested
|
|
|
|
|
assert stats[0].sms_failed == sms_failed
|
|
|
|
|
assert stats[0].day == notification_created_at if notification_created_at else True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _assert_job_stats(job_id, sent=0, count=0, delivered=0, failed=0):
|
|
|
|
|
job = Job.query.get(job_id)
|
|
|
|
|
assert job.notifications_sent == sent
|
|
|
|
|
assert job.notification_count == count
|
|
|
|
|
assert job.notifications_delivered == delivered
|
|
|
|
|
assert job.notifications_failed == failed
|
|
|
|
|
|