2016-03-31 15:57:50 +01:00
|
|
|
from datetime import datetime, timedelta, date
|
2016-03-08 15:23:19 +00:00
|
|
|
import uuid
|
2016-03-31 15:57:50 +01:00
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from app import DATE_FORMAT
|
2016-03-08 15:23:19 +00:00
|
|
|
from freezegun import freeze_time
|
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
|
2016-03-31 15:57:50 +01:00
|
|
|
|
|
|
|
|
from app.models import (
|
|
|
|
|
Notification,
|
|
|
|
|
Job,
|
|
|
|
|
NotificationStatistics,
|
|
|
|
|
TemplateStatistics
|
|
|
|
|
)
|
|
|
|
|
|
2016-02-09 12:01:17 +00:00
|
|
|
from app.dao.notifications_dao import (
|
2016-02-25 09:59:50 +00:00
|
|
|
dao_create_notification,
|
|
|
|
|
dao_update_notification,
|
2016-02-09 12:01:17 +00:00
|
|
|
get_notification,
|
2016-02-16 11:22:44 +00:00
|
|
|
get_notification_for_job,
|
2016-03-08 16:34:03 +00:00
|
|
|
get_notifications_for_job,
|
2016-03-09 17:46:01 +00:00
|
|
|
dao_get_notification_statistics_for_service,
|
|
|
|
|
delete_successful_notifications_created_more_than_a_day_ago,
|
2016-03-10 09:48:29 +00:00
|
|
|
delete_failed_notifications_created_more_than_a_week_ago,
|
2016-03-10 17:29:17 +00:00
|
|
|
dao_get_notification_statistics_for_service_and_day,
|
|
|
|
|
update_notification_status_by_id,
|
2016-03-11 10:19:40 +00:00
|
|
|
update_notification_reference_by_id,
|
|
|
|
|
update_notification_status_by_reference
|
2016-02-09 12:01:17 +00:00
|
|
|
)
|
2016-03-31 15:57:50 +01:00
|
|
|
|
2016-03-04 15:54:43 +00:00
|
|
|
from tests.app.conftest import sample_job
|
2016-03-09 17:46:01 +00:00
|
|
|
from tests.app.conftest import sample_notification
|
2016-02-09 12:01:17 +00:00
|
|
|
|
|
|
|
|
|
2016-03-11 09:40:35 +00:00
|
|
|
def test_should_by_able_to_update_reference_by_id(sample_notification):
|
|
|
|
|
assert not Notification.query.get(sample_notification.id).reference
|
|
|
|
|
count = update_notification_reference_by_id(sample_notification.id, 'reference')
|
|
|
|
|
assert count == 1
|
|
|
|
|
assert Notification.query.get(sample_notification.id).reference == 'reference'
|
|
|
|
|
|
|
|
|
|
|
2016-03-21 13:24:37 +00:00
|
|
|
def test_should_by_able_to_update_status_by_reference(sample_email_template):
|
|
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'service': sample_email_template.service,
|
|
|
|
|
'service_id': sample_email_template.service.id,
|
|
|
|
|
'template': sample_email_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_email_template.id,
|
2016-03-21 13:24:37 +00:00
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_email_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.get(notification.id).status == "sent"
|
|
|
|
|
update_notification_reference_by_id(notification.id, 'reference')
|
|
|
|
|
update_notification_status_by_reference('reference', 'delivered', 'delivered')
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'delivered'
|
|
|
|
|
assert NotificationStatistics.query.filter_by(
|
|
|
|
|
service_id=sample_email_template.service.id
|
|
|
|
|
).one().emails_delivered == 1
|
|
|
|
|
assert NotificationStatistics.query.filter_by(
|
|
|
|
|
service_id=sample_email_template.service.id
|
|
|
|
|
).one().emails_requested == 1
|
|
|
|
|
assert NotificationStatistics.query.filter_by(
|
|
|
|
|
service_id=sample_email_template.service.id
|
|
|
|
|
).one().emails_error == 0
|
2016-03-11 10:19:40 +00:00
|
|
|
|
|
|
|
|
|
2016-03-10 17:29:17 +00:00
|
|
|
def test_should_by_able_to_update_status_by_id(sample_notification):
|
|
|
|
|
assert Notification.query.get(sample_notification.id).status == 'sent'
|
2016-03-21 13:24:37 +00:00
|
|
|
count = update_notification_status_by_id(sample_notification.id, 'delivered', 'delivered')
|
2016-03-10 17:29:17 +00:00
|
|
|
assert count == 1
|
|
|
|
|
assert Notification.query.get(sample_notification.id).status == 'delivered'
|
2016-03-21 13:24:37 +00:00
|
|
|
assert NotificationStatistics.query.filter_by(
|
|
|
|
|
service_id=sample_notification.service.id
|
|
|
|
|
).one().sms_delivered == 1
|
|
|
|
|
assert NotificationStatistics.query.filter_by(
|
|
|
|
|
service_id=sample_notification.service.id
|
|
|
|
|
).one().sms_requested == 1
|
|
|
|
|
assert NotificationStatistics.query.filter_by(
|
|
|
|
|
service_id=sample_notification.service.id
|
|
|
|
|
).one().sms_error == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_be_able_to_record_statistics_failure_for_sms(sample_notification):
|
|
|
|
|
assert Notification.query.get(sample_notification.id).status == 'sent'
|
|
|
|
|
count = update_notification_status_by_id(sample_notification.id, 'delivered', 'failure')
|
|
|
|
|
assert count == 1
|
|
|
|
|
assert Notification.query.get(sample_notification.id).status == 'delivered'
|
|
|
|
|
assert NotificationStatistics.query.filter_by(
|
|
|
|
|
service_id=sample_notification.service.id
|
|
|
|
|
).one().sms_delivered == 0
|
|
|
|
|
assert NotificationStatistics.query.filter_by(
|
|
|
|
|
service_id=sample_notification.service.id
|
|
|
|
|
).one().sms_requested == 1
|
|
|
|
|
assert NotificationStatistics.query.filter_by(
|
|
|
|
|
service_id=sample_notification.service.id
|
|
|
|
|
).one().sms_error == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_be_able_to_record_statistics_failure_for_email(sample_email_template):
|
|
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'service': sample_email_template.service,
|
|
|
|
|
'service_id': sample_email_template.service.id,
|
|
|
|
|
'template': sample_email_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_email_template.id,
|
2016-03-21 13:24:37 +00:00
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_email_template.template_type)
|
|
|
|
|
|
|
|
|
|
update_notification_reference_by_id(notification.id, 'reference')
|
|
|
|
|
count = update_notification_status_by_reference('reference', 'bounce', 'failure')
|
|
|
|
|
assert count == 1
|
|
|
|
|
assert Notification.query.get(notification.id).status == 'bounce'
|
|
|
|
|
assert NotificationStatistics.query.filter_by(
|
|
|
|
|
service_id=notification.service.id
|
|
|
|
|
).one().emails_delivered == 0
|
|
|
|
|
assert NotificationStatistics.query.filter_by(
|
|
|
|
|
service_id=notification.service.id
|
|
|
|
|
).one().emails_requested == 1
|
|
|
|
|
assert NotificationStatistics.query.filter_by(
|
|
|
|
|
service_id=notification.service.id
|
|
|
|
|
).one().emails_error == 1
|
2016-03-10 17:29:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_return_zero_count_if_no_notification_with_id():
|
2016-03-21 13:24:37 +00:00
|
|
|
assert update_notification_status_by_id(str(uuid.uuid4()), 'delivered', 'delivered') == 0
|
2016-03-10 17:29:17 +00:00
|
|
|
|
|
|
|
|
|
2016-03-11 10:19:40 +00:00
|
|
|
def test_should_return_zero_count_if_no_notification_with_reference():
|
2016-03-21 13:24:37 +00:00
|
|
|
assert update_notification_status_by_reference('something', 'delivered', 'delivered') == 0
|
2016-03-10 17:29:17 +00:00
|
|
|
|
|
|
|
|
|
2016-03-08 16:34:03 +00:00
|
|
|
def test_should_be_able_to_get_statistics_for_a_service(sample_template):
|
|
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'service': sample_template.service,
|
|
|
|
|
'service_id': sample_template.service.id,
|
|
|
|
|
'template': sample_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_template.id,
|
2016-03-08 16:34:03 +00:00
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
stats = dao_get_notification_statistics_for_service(sample_template.service.id)
|
|
|
|
|
assert len(stats) == 1
|
|
|
|
|
assert stats[0].emails_requested == 0
|
|
|
|
|
assert stats[0].sms_requested == 1
|
2016-03-17 11:32:55 +00:00
|
|
|
assert stats[0].sms_delivered == 0
|
|
|
|
|
assert stats[0].sms_error == 0
|
2016-03-09 11:06:37 +00:00
|
|
|
assert stats[0].day == notification.created_at.strftime(DATE_FORMAT)
|
2016-03-08 16:34:03 +00:00
|
|
|
assert stats[0].service_id == notification.service_id
|
2016-03-17 11:32:55 +00:00
|
|
|
assert stats[0].emails_requested == 0
|
|
|
|
|
assert stats[0].emails_delivered == 0
|
|
|
|
|
assert stats[0].emails_error == 0
|
2016-03-08 16:34:03 +00:00
|
|
|
|
|
|
|
|
|
2016-03-09 11:06:37 +00:00
|
|
|
def test_should_be_able_to_get_statistics_for_a_service_for_a_day(sample_template):
|
|
|
|
|
now = datetime.utcnow()
|
|
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'service': sample_template.service,
|
|
|
|
|
'service_id': sample_template.service.id,
|
|
|
|
|
'template': sample_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_template.id,
|
2016-03-09 11:06:37 +00:00
|
|
|
'created_at': now
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.strftime(DATE_FORMAT)
|
|
|
|
|
)
|
|
|
|
|
assert stat.emails_requested == 0
|
2016-03-17 11:32:55 +00:00
|
|
|
assert stat.emails_error == 0
|
|
|
|
|
assert stat.emails_delivered == 0
|
2016-03-09 11:06:37 +00:00
|
|
|
assert stat.sms_requested == 1
|
2016-03-17 11:32:55 +00:00
|
|
|
assert stat.sms_error == 0
|
|
|
|
|
assert stat.sms_delivered == 0
|
2016-03-09 11:06:37 +00:00
|
|
|
assert stat.day == notification.created_at.strftime(DATE_FORMAT)
|
|
|
|
|
assert stat.service_id == notification.service_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_return_none_if_no_statistics_for_a_service_for_a_day(sample_template):
|
|
|
|
|
now = datetime.utcnow()
|
|
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'service': sample_template.service,
|
|
|
|
|
'service_id': sample_template.service.id,
|
|
|
|
|
'template': sample_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_template.id,
|
2016-03-09 11:06:37 +00:00
|
|
|
'created_at': now
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
assert not dao_get_notification_statistics_for_service_and_day(
|
|
|
|
|
sample_template.service.id, (datetime.utcnow() - timedelta(days=1)).strftime(DATE_FORMAT)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2016-03-08 16:34:03 +00:00
|
|
|
def test_should_be_able_to_get_all_statistics_for_a_service(sample_template):
|
|
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'service': sample_template.service,
|
|
|
|
|
'service_id': sample_template.service.id,
|
|
|
|
|
'template': sample_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_template.id,
|
2016-03-08 16:34:03 +00:00
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
stats = dao_get_notification_statistics_for_service(sample_template.service.id)
|
|
|
|
|
assert len(stats) == 1
|
|
|
|
|
assert stats[0].emails_requested == 0
|
|
|
|
|
assert stats[0].sms_requested == 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_be_able_to_get_all_statistics_for_a_service_for_several_days(sample_template):
|
|
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'service': sample_template.service,
|
|
|
|
|
'service_id': sample_template.service.id,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template': sample_template,
|
|
|
|
|
'template_id': sample_template.id,
|
2016-03-08 16:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
today = datetime.utcnow()
|
|
|
|
|
yesterday = datetime.utcnow() - timedelta(days=1)
|
|
|
|
|
two_days_ago = datetime.utcnow() - timedelta(days=2)
|
|
|
|
|
data.update({
|
|
|
|
|
'created_at': today
|
|
|
|
|
})
|
|
|
|
|
notification_1 = Notification(**data)
|
|
|
|
|
data.update({
|
|
|
|
|
'created_at': yesterday
|
|
|
|
|
})
|
|
|
|
|
notification_2 = Notification(**data)
|
|
|
|
|
data.update({
|
|
|
|
|
'created_at': two_days_ago
|
|
|
|
|
})
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
stats = dao_get_notification_statistics_for_service(sample_template.service.id)
|
|
|
|
|
assert len(stats) == 3
|
|
|
|
|
assert stats[0].emails_requested == 0
|
|
|
|
|
assert stats[0].sms_requested == 1
|
2016-03-09 11:06:37 +00:00
|
|
|
assert stats[0].day == today.strftime(DATE_FORMAT)
|
2016-03-08 16:34:03 +00:00
|
|
|
assert stats[1].emails_requested == 0
|
|
|
|
|
assert stats[1].sms_requested == 1
|
2016-03-09 11:06:37 +00:00
|
|
|
assert stats[1].day == yesterday.strftime(DATE_FORMAT)
|
2016-03-08 16:34:03 +00:00
|
|
|
assert stats[2].emails_requested == 0
|
|
|
|
|
assert stats[2].sms_requested == 1
|
2016-03-09 11:06:37 +00:00
|
|
|
assert stats[2].day == two_days_ago.strftime(DATE_FORMAT)
|
2016-03-08 16:34:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_be_empty_list_if_no_statistics_for_a_service(sample_service):
|
|
|
|
|
assert len(dao_get_notification_statistics_for_service(sample_service.id)) == 0
|
|
|
|
|
|
|
|
|
|
|
2016-03-31 15:57:50 +01:00
|
|
|
def test_save_notification_creates_sms_and_template_stats(sample_template, sample_job):
|
2016-03-08 15:23:19 +00:00
|
|
|
assert Notification.query.count() == 0
|
2016-03-31 15:57:50 +01:00
|
|
|
assert NotificationStatistics.query.count() == 0
|
|
|
|
|
assert TemplateStatistics.query.count() == 0
|
|
|
|
|
|
2016-03-08 15:23:19 +00:00
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'job_id': sample_job.id,
|
|
|
|
|
'service': sample_template.service,
|
|
|
|
|
'service_id': sample_template.service.id,
|
|
|
|
|
'template': sample_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_template.id,
|
2016-03-08 15:23:19 +00:00
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
notification_from_db = Notification.query.all()[0]
|
|
|
|
|
assert notification_from_db.id
|
|
|
|
|
assert data['to'] == notification_from_db.to
|
|
|
|
|
assert data['job_id'] == notification_from_db.job_id
|
|
|
|
|
assert data['service'] == notification_from_db.service
|
|
|
|
|
assert data['template'] == notification_from_db.template
|
|
|
|
|
assert data['created_at'] == notification_from_db.created_at
|
|
|
|
|
assert 'sent' == notification_from_db.status
|
|
|
|
|
assert Job.query.get(sample_job.id).notifications_sent == 1
|
|
|
|
|
|
2016-03-08 16:34:03 +00:00
|
|
|
stats = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_template.service.id
|
2016-03-08 15:23:19 +00:00
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
assert stats.emails_requested == 0
|
|
|
|
|
assert stats.sms_requested == 1
|
|
|
|
|
|
2016-03-31 15:57:50 +01:00
|
|
|
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):
|
2016-03-08 15:23:19 +00:00
|
|
|
|
|
|
|
|
assert Notification.query.count() == 0
|
2016-03-31 15:57:50 +01:00
|
|
|
assert NotificationStatistics.query.count() == 0
|
|
|
|
|
assert TemplateStatistics.query.count() == 0
|
|
|
|
|
|
2016-03-08 15:23:19 +00:00
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'job_id': sample_job.id,
|
|
|
|
|
'service': sample_email_template.service,
|
|
|
|
|
'service_id': sample_email_template.service.id,
|
|
|
|
|
'template': sample_email_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_email_template.id,
|
2016-03-08 15:23:19 +00:00
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_email_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
notification_from_db = Notification.query.all()[0]
|
|
|
|
|
assert notification_from_db.id
|
|
|
|
|
assert data['to'] == notification_from_db.to
|
|
|
|
|
assert data['job_id'] == notification_from_db.job_id
|
|
|
|
|
assert data['service'] == notification_from_db.service
|
|
|
|
|
assert data['template'] == notification_from_db.template
|
|
|
|
|
assert data['created_at'] == notification_from_db.created_at
|
|
|
|
|
assert 'sent' == notification_from_db.status
|
|
|
|
|
assert Job.query.get(sample_job.id).notifications_sent == 1
|
|
|
|
|
|
2016-03-08 16:34:03 +00:00
|
|
|
stats = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_email_template.service.id
|
2016-03-08 15:23:19 +00:00
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
assert stats.emails_requested == 1
|
|
|
|
|
assert stats.sms_requested == 0
|
|
|
|
|
|
2016-03-31 15:57:50 +01:00
|
|
|
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
|
|
|
|
|
|
2016-03-08 15:23:19 +00:00
|
|
|
|
|
|
|
|
@freeze_time("2016-01-01 00:00:00.000000")
|
|
|
|
|
def test_save_notification_handles_midnight_properly(sample_template, sample_job):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'job_id': sample_job.id,
|
|
|
|
|
'service': sample_template.service,
|
|
|
|
|
'service_id': sample_template.service.id,
|
|
|
|
|
'template': sample_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_template.id,
|
2016-03-08 15:23:19 +00:00
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
|
2016-03-08 16:34:03 +00:00
|
|
|
stats = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_template.service.id
|
2016-03-08 15:23:19 +00:00
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
assert stats.day == '2016-01-01'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2016-01-01 23:59:59.999999")
|
|
|
|
|
def test_save_notification_handles_just_before_midnight_properly(sample_template, sample_job):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'job_id': sample_job.id,
|
|
|
|
|
'service': sample_template.service,
|
|
|
|
|
'service_id': sample_template.service.id,
|
|
|
|
|
'template': sample_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_template.id,
|
2016-03-08 15:23:19 +00:00
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
|
2016-03-08 16:34:03 +00:00
|
|
|
stats = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_template.service.id
|
2016-03-08 15:23:19 +00:00
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
assert stats.day == '2016-01-01'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_notification_and_increment_email_stats(sample_email_template, sample_job):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'job_id': sample_job.id,
|
|
|
|
|
'service': sample_email_template.service,
|
|
|
|
|
'service_id': sample_email_template.service.id,
|
|
|
|
|
'template': sample_email_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_email_template.id,
|
2016-03-08 15:23:19 +00:00
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification_1 = Notification(**data)
|
|
|
|
|
notification_2 = Notification(**data)
|
|
|
|
|
dao_create_notification(notification_1, sample_email_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
|
2016-03-08 16:34:03 +00:00
|
|
|
stats1 = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_email_template.service.id
|
2016-03-08 15:23:19 +00:00
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
assert stats1.emails_requested == 1
|
|
|
|
|
assert stats1.sms_requested == 0
|
|
|
|
|
|
2016-03-15 14:40:42 +00:00
|
|
|
dao_create_notification(notification_2, sample_email_template.template_type)
|
2016-03-08 15:23:19 +00:00
|
|
|
|
|
|
|
|
assert Notification.query.count() == 2
|
|
|
|
|
|
2016-03-08 16:34:03 +00:00
|
|
|
stats2 = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_email_template.service.id
|
2016-03-08 15:23:19 +00:00
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
assert stats2.emails_requested == 2
|
|
|
|
|
assert stats2.sms_requested == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_notification_and_increment_sms_stats(sample_template, sample_job):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'job_id': sample_job.id,
|
|
|
|
|
'service': sample_template.service,
|
|
|
|
|
'service_id': sample_template.service.id,
|
|
|
|
|
'template': sample_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_template.id,
|
2016-03-08 15:23:19 +00:00
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification_1 = Notification(**data)
|
|
|
|
|
notification_2 = Notification(**data)
|
|
|
|
|
dao_create_notification(notification_1, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
|
2016-03-08 16:34:03 +00:00
|
|
|
stats1 = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_template.service.id
|
2016-03-08 15:23:19 +00:00
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
assert stats1.emails_requested == 0
|
|
|
|
|
assert stats1.sms_requested == 1
|
|
|
|
|
|
|
|
|
|
dao_create_notification(notification_2, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 2
|
|
|
|
|
|
2016-03-08 16:34:03 +00:00
|
|
|
stats2 = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_template.service.id
|
2016-03-08 15:23:19 +00:00
|
|
|
).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):
|
|
|
|
|
random_id = str(uuid.uuid4())
|
2016-03-04 14:25:28 +00:00
|
|
|
|
2016-03-08 15:23:19 +00:00
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'job_id': random_id,
|
|
|
|
|
'service': sample_template.service,
|
|
|
|
|
'service_id': sample_template.service.id,
|
|
|
|
|
'template': sample_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_template.id,
|
2016-03-08 15:23:19 +00:00
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
|
|
|
|
with pytest.raises(SQLAlchemyError):
|
|
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
assert Job.query.get(sample_job.id).notifications_sent == 0
|
2016-03-08 16:34:03 +00:00
|
|
|
assert NotificationStatistics.query.count() == 0
|
2016-03-31 15:57:50 +01:00
|
|
|
assert TemplateStatistics.query.count() == 0
|
2016-03-08 15:23:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_save_notification_and_increment_job(sample_template, sample_job):
|
2016-03-04 14:25:28 +00:00
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'job_id': sample_job.id,
|
|
|
|
|
'service': sample_template.service,
|
2016-03-08 15:23:19 +00:00
|
|
|
'service_id': sample_template.service.id,
|
2016-03-04 14:25:28 +00:00
|
|
|
'template': sample_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_template.id,
|
2016-03-04 14:25:28 +00:00
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
2016-03-08 15:23:19 +00:00
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
2016-03-04 14:25:28 +00:00
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
notification_from_db = Notification.query.all()[0]
|
|
|
|
|
assert notification_from_db.id
|
|
|
|
|
assert data['to'] == notification_from_db.to
|
|
|
|
|
assert data['job_id'] == notification_from_db.job_id
|
|
|
|
|
assert data['service'] == notification_from_db.service
|
|
|
|
|
assert data['template'] == notification_from_db.template
|
|
|
|
|
assert data['created_at'] == notification_from_db.created_at
|
|
|
|
|
assert 'sent' == notification_from_db.status
|
|
|
|
|
assert Job.query.get(sample_job.id).notifications_sent == 1
|
|
|
|
|
|
2016-03-08 15:23:19 +00:00
|
|
|
notification_2 = Notification(**data)
|
2016-03-15 14:40:42 +00:00
|
|
|
dao_create_notification(notification_2, sample_template.template_type)
|
2016-03-08 15:23:19 +00:00
|
|
|
assert Notification.query.count() == 2
|
|
|
|
|
assert Job.query.get(sample_job.id).notifications_sent == 2
|
2016-03-04 14:25:28 +00:00
|
|
|
|
2016-03-04 15:54:43 +00:00
|
|
|
|
2016-03-08 15:23:19 +00:00
|
|
|
def test_should_not_increment_job_if_notification_fails_to_persist(sample_template, sample_job):
|
|
|
|
|
random_id = str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
data = {
|
|
|
|
|
'id': random_id,
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'job_id': sample_job.id,
|
|
|
|
|
'service_id': sample_template.service.id,
|
|
|
|
|
'service': sample_template.service,
|
|
|
|
|
'template': sample_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_template.id,
|
2016-03-08 15:23:19 +00:00
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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):
|
2016-03-04 15:54:43 +00:00
|
|
|
job_1 = sample_job(notify_db, notify_db_session, sample_template.service)
|
|
|
|
|
job_2 = sample_job(notify_db, notify_db_session, sample_template.service)
|
|
|
|
|
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'job_id': job_1.id,
|
2016-03-08 15:23:19 +00:00
|
|
|
'service_id': sample_template.service.id,
|
2016-03-04 15:54:43 +00:00
|
|
|
'service': sample_template.service,
|
|
|
|
|
'template': sample_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_template.id,
|
2016-03-04 15:54:43 +00:00
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
2016-03-08 15:23:19 +00:00
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
2016-03-04 15:54:43 +00:00
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
notification_from_db = Notification.query.all()[0]
|
|
|
|
|
assert notification_from_db.id
|
|
|
|
|
assert data['to'] == notification_from_db.to
|
|
|
|
|
assert data['job_id'] == notification_from_db.job_id
|
|
|
|
|
assert data['service'] == notification_from_db.service
|
|
|
|
|
assert data['template'] == notification_from_db.template
|
|
|
|
|
assert data['created_at'] == notification_from_db.created_at
|
|
|
|
|
assert 'sent' == notification_from_db.status
|
|
|
|
|
assert Job.query.get(job_1.id).notifications_sent == 1
|
|
|
|
|
assert Job.query.get(job_2.id).notifications_sent == 0
|
|
|
|
|
|
|
|
|
|
|
2016-03-04 14:25:28 +00:00
|
|
|
def test_save_notification_with_no_job(sample_template):
|
2016-02-09 12:01:17 +00:00
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
data = {
|
2016-02-25 09:59:50 +00:00
|
|
|
'to': '+44709123456',
|
2016-03-08 15:23:19 +00:00
|
|
|
'service_id': sample_template.service.id,
|
2016-02-09 12:48:27 +00:00
|
|
|
'service': sample_template.service,
|
2016-02-25 09:59:50 +00:00
|
|
|
'template': sample_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_template.id,
|
2016-02-25 09:59:50 +00:00
|
|
|
'created_at': datetime.utcnow()
|
2016-02-09 12:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
2016-03-08 15:23:19 +00:00
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
2016-02-09 12:01:17 +00:00
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
2016-02-09 18:28:10 +00:00
|
|
|
notification_from_db = Notification.query.all()[0]
|
|
|
|
|
assert notification_from_db.id
|
2016-02-09 12:01:17 +00:00
|
|
|
assert data['to'] == notification_from_db.to
|
2016-02-09 12:48:27 +00:00
|
|
|
assert data['service'] == notification_from_db.service
|
|
|
|
|
assert data['template'] == notification_from_db.template
|
2016-02-25 09:59:50 +00:00
|
|
|
assert data['created_at'] == notification_from_db.created_at
|
2016-02-09 12:01:17 +00:00
|
|
|
assert 'sent' == notification_from_db.status
|
|
|
|
|
|
|
|
|
|
|
2016-02-25 09:59:50 +00:00
|
|
|
def test_get_notification(sample_notification):
|
2016-02-16 11:22:44 +00:00
|
|
|
notifcation_from_db = get_notification(
|
|
|
|
|
sample_notification.service.id,
|
|
|
|
|
sample_notification.id)
|
|
|
|
|
assert sample_notification == notifcation_from_db
|
|
|
|
|
|
|
|
|
|
|
2016-02-25 09:59:50 +00:00
|
|
|
def test_save_notification_no_job_id(sample_template):
|
2016-02-10 11:08:24 +00:00
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
to = '+44709123456'
|
|
|
|
|
data = {
|
|
|
|
|
'to': to,
|
2016-03-08 15:23:19 +00:00
|
|
|
'service_id': sample_template.service.id,
|
2016-02-10 11:08:24 +00:00
|
|
|
'service': sample_template.service,
|
2016-02-25 09:59:50 +00:00
|
|
|
'template': sample_template,
|
2016-03-31 15:57:50 +01:00
|
|
|
'template_id': sample_template.id,
|
2016-02-25 09:59:50 +00:00
|
|
|
'created_at': datetime.utcnow()
|
2016-02-10 11:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification = Notification(**data)
|
2016-03-08 15:23:19 +00:00
|
|
|
dao_create_notification(notification, sample_template.template_type)
|
2016-02-10 11:08:24 +00:00
|
|
|
|
|
|
|
|
assert Notification.query.count() == 1
|
|
|
|
|
notification_from_db = Notification.query.all()[0]
|
|
|
|
|
assert notification_from_db.id
|
|
|
|
|
assert data['to'] == notification_from_db.to
|
|
|
|
|
assert data['service'] == notification_from_db.service
|
|
|
|
|
assert data['template'] == notification_from_db.template
|
|
|
|
|
assert 'sent' == notification_from_db.status
|
|
|
|
|
|
|
|
|
|
|
2016-02-25 09:59:50 +00:00
|
|
|
def test_get_notification_for_job(sample_notification):
|
2016-02-16 11:22:44 +00:00
|
|
|
notifcation_from_db = get_notification_for_job(
|
|
|
|
|
sample_notification.service.id,
|
|
|
|
|
sample_notification.job_id,
|
|
|
|
|
sample_notification.id)
|
2016-02-09 12:01:17 +00:00
|
|
|
assert sample_notification == notifcation_from_db
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_all_notifications_for_job(notify_db, notify_db_session, sample_job):
|
|
|
|
|
from tests.app.conftest import sample_notification
|
|
|
|
|
for i in range(0, 5):
|
2016-03-08 15:23:19 +00:00
|
|
|
try:
|
|
|
|
|
sample_notification(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
service=sample_job.service,
|
|
|
|
|
template=sample_job.template,
|
|
|
|
|
job=sample_job)
|
|
|
|
|
except IntegrityError:
|
|
|
|
|
pass
|
2016-02-09 12:01:17 +00:00
|
|
|
|
2016-03-01 13:30:10 +00:00
|
|
|
notifcations_from_db = get_notifications_for_job(sample_job.service.id, sample_job.id).items
|
2016-02-09 12:01:17 +00:00
|
|
|
assert len(notifcations_from_db) == 5
|
2016-03-08 16:34:03 +00:00
|
|
|
stats = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_job.service.id
|
2016-03-08 15:23:19 +00:00
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
assert stats.emails_requested == 0
|
|
|
|
|
assert stats.sms_requested == 5
|
2016-02-09 12:01:17 +00:00
|
|
|
|
|
|
|
|
|
2016-03-08 15:23:19 +00:00
|
|
|
def test_update_notification(sample_notification, sample_template):
|
2016-02-09 12:01:17 +00:00
|
|
|
assert sample_notification.status == 'sent'
|
2016-02-25 09:59:50 +00:00
|
|
|
sample_notification.status = 'failed'
|
|
|
|
|
dao_update_notification(sample_notification)
|
2016-02-09 12:01:17 +00:00
|
|
|
notification_from_db = Notification.query.get(sample_notification.id)
|
|
|
|
|
assert notification_from_db.status == 'failed'
|
2016-03-09 17:46:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_delete_sent_notifications_after_one_day(notify_db, notify_db_session):
|
|
|
|
|
created_at = datetime.utcnow() - timedelta(hours=24)
|
|
|
|
|
sample_notification(notify_db, notify_db_session, created_at=created_at)
|
|
|
|
|
sample_notification(notify_db, notify_db_session, created_at=created_at)
|
|
|
|
|
assert len(Notification.query.all()) == 2
|
|
|
|
|
delete_successful_notifications_created_more_than_a_day_ago()
|
|
|
|
|
assert len(Notification.query.all()) == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_delete_failed_notifications_after_seven_days(notify_db, notify_db_session):
|
|
|
|
|
created_at = datetime.utcnow() - timedelta(hours=24 * 7)
|
|
|
|
|
sample_notification(notify_db, notify_db_session, created_at=created_at, status="failed")
|
|
|
|
|
sample_notification(notify_db, notify_db_session, created_at=created_at, status="failed")
|
|
|
|
|
assert len(Notification.query.all()) == 2
|
|
|
|
|
delete_failed_notifications_created_more_than_a_week_ago()
|
|
|
|
|
assert len(Notification.query.all()) == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_not_delete_sent_notifications_before_one_day(notify_db, notify_db_session):
|
|
|
|
|
expired = datetime.utcnow() - timedelta(hours=24)
|
|
|
|
|
valid = datetime.utcnow() - timedelta(hours=23, minutes=59, seconds=59)
|
|
|
|
|
sample_notification(notify_db, notify_db_session, created_at=expired, to_field="expired")
|
|
|
|
|
sample_notification(notify_db, notify_db_session, created_at=valid, to_field="valid")
|
|
|
|
|
|
|
|
|
|
assert len(Notification.query.all()) == 2
|
|
|
|
|
delete_successful_notifications_created_more_than_a_day_ago()
|
|
|
|
|
assert len(Notification.query.all()) == 1
|
|
|
|
|
assert Notification.query.first().to == 'valid'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_not_delete_failed_notifications_before_seven_days(notify_db, notify_db_session):
|
|
|
|
|
expired = datetime.utcnow() - timedelta(hours=24 * 7)
|
2016-03-10 17:29:17 +00:00
|
|
|
valid = datetime.utcnow() - timedelta(hours=(24 * 6) + 23, minutes=59, seconds=59)
|
2016-03-09 17:46:01 +00:00
|
|
|
sample_notification(notify_db, notify_db_session, created_at=expired, status="failed", to_field="expired")
|
|
|
|
|
sample_notification(notify_db, notify_db_session, created_at=valid, status="failed", to_field="valid")
|
|
|
|
|
assert len(Notification.query.all()) == 2
|
|
|
|
|
delete_failed_notifications_created_more_than_a_week_ago()
|
|
|
|
|
assert len(Notification.query.all()) == 1
|
|
|
|
|
assert Notification.query.first().to == 'valid'
|
2016-03-31 15:57:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2016-03-30")
|
|
|
|
|
def test_save_new_notification_creates_template_stats(sample_template, sample_job):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
assert TemplateStatistics.query.count() == 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)
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
assert TemplateStatistics.query.count() == 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)
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
assert TemplateStatistics.query.count() == 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_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
|
2016-03-31 16:53:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_successful_notification_inserts_followed_by_failure_does_not_increment_template_stats(sample_template,
|
|
|
|
|
sample_job):
|
|
|
|
|
assert Notification.query.count() == 0
|
|
|
|
|
assert NotificationStatistics.query.count() == 0
|
|
|
|
|
assert TemplateStatistics.query.count() == 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_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 NotificationStatistics.query.count() == 1
|
|
|
|
|
notication_stats = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_template.service.id
|
|
|
|
|
).first()
|
|
|
|
|
assert notication_stats.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
|
|
|
|
|
|
|
|
|
|
broken_data = {
|
|
|
|
|
'to': '+44709123456',
|
|
|
|
|
'job_id': sample_job.id,
|
|
|
|
|
'service': None,
|
|
|
|
|
'service_id': None,
|
|
|
|
|
'template': sample_template,
|
|
|
|
|
'template_id': sample_template.id,
|
|
|
|
|
'created_at': datetime.utcnow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
broken_notification = Notification(**broken_data)
|
|
|
|
|
try:
|
|
|
|
|
dao_create_notification(broken_notification, sample_template.template_type)
|
|
|
|
|
except:
|
|
|
|
|
assert TemplateStatistics.query.count() == 1
|
|
|
|
|
template_stats = TemplateStatistics.query.filter(TemplateStatistics.service_id == sample_template.service.id,
|
|
|
|
|
TemplateStatistics.template_id == sample_template.id).first() # noqa
|
|
|
|
|
assert template_stats.service_id == sample_template.service.id
|
|
|
|
|
assert template_stats.template_id == sample_template.id
|
|
|
|
|
assert template_stats.usage_count == 3
|
|
|
|
|
|
|
|
|
|
assert NotificationStatistics.query.count() == 1
|
|
|
|
|
notication_stats = NotificationStatistics.query.filter(
|
|
|
|
|
NotificationStatistics.service_id == sample_template.service.id
|
|
|
|
|
).first()
|
|
|
|
|
assert notication_stats.sms_requested == 3
|