Use the template version at the time the notification is created or at the time the job is created.

Update notifications/sms|email endpoint to send the template version to the queue.
Update the process_job celery talk to send the template version to the queue.
When the send_sms|send_email task runs it will get the template by id and version.

Created a data migration script to add the template_vesion column for jobs and notifications.
The existing jobs and notifications are given the template_version of the current template.
There is a chance this is the wrong template version, but deemed okay since the application is not live.

Create unit test for the dao_get_template_versions method.
Rename /template/<id>/version to /template/<id>/versions which returns all versions for that template id and service id.
This commit is contained in:
Rebecca Law
2016-05-13 16:25:05 +01:00
parent c8c0f95dd2
commit 917110870d
12 changed files with 220 additions and 114 deletions

View File

@@ -19,6 +19,7 @@ def test_create_job(sample_template):
'id': job_id,
'service_id': sample_template.service.id,
'template_id': sample_template.id,
'template_version': sample_template.version,
'original_file_name': 'some.csv',
'notification_count': 1,
'created_by': sample_template.created_by

View File

@@ -271,7 +271,7 @@ def test_save_notification_creates_sms_and_template_stats(sample_template, sampl
assert NotificationStatistics.query.count() == 0
assert TemplateStatistics.query.count() == 0
data = _notification_json(sample_template, sample_job.id)
data = _notification_json(sample_template, job_id=sample_job.id)
notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
@@ -283,6 +283,7 @@ def test_save_notification_creates_sms_and_template_stats(sample_template, sampl
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['template_version'] == notification_from_db.template_version
assert data['created_at'] == notification_from_db.created_at
assert 'sending' == notification_from_db.status
assert Job.query.get(sample_job.id).notifications_sent == 1
@@ -308,7 +309,7 @@ def test_save_notification_and_create_email_and_template_stats(sample_email_temp
assert NotificationStatistics.query.count() == 0
assert TemplateStatistics.query.count() == 0
data = _notification_json(sample_email_template, sample_job.id)
data = _notification_json(sample_email_template, job_id=sample_job.id)
notification = Notification(**data)
dao_create_notification(notification, sample_email_template.template_type, ses_provider.identifier)
@@ -320,6 +321,7 @@ def test_save_notification_and_create_email_and_template_stats(sample_email_temp
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['template_version'] == notification_from_db.template_version
assert data['created_at'] == notification_from_db.created_at
assert 'sending' == notification_from_db.status
assert Job.query.get(sample_job.id).notifications_sent == 1
@@ -359,7 +361,7 @@ def test_save_notification_handles_midnight_properly(sample_template, sample_job
@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, sample_job.id)
data = _notification_json(sample_template, job_id=sample_job.id)
notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
@@ -375,7 +377,7 @@ def test_save_notification_handles_just_before_midnight_properly(sample_template
def test_save_notification_and_increment_email_stats(sample_email_template, sample_job, ses_provider):
assert Notification.query.count() == 0
data = _notification_json(sample_email_template, sample_job.id)
data = _notification_json(sample_email_template, job_id=sample_job.id)
notification_1 = Notification(**data)
notification_2 = Notification(**data)
@@ -435,7 +437,7 @@ def test_not_save_notification_and_not_create_stats_on_commit_error(sample_templ
random_id = str(uuid.uuid4())
assert Notification.query.count() == 0
data = _notification_json(sample_template, random_id)
data = _notification_json(sample_template, job_id=random_id)
notification = Notification(**data)
with pytest.raises(SQLAlchemyError):
@@ -449,7 +451,7 @@ def test_not_save_notification_and_not_create_stats_on_commit_error(sample_templ
def test_save_notification_and_increment_job(sample_template, sample_job, mmg_provider):
assert Notification.query.count() == 0
data = _notification_json(sample_template, sample_job.id)
data = _notification_json(sample_template, job_id=sample_job.id)
notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
@@ -461,6 +463,7 @@ def test_save_notification_and_increment_job(sample_template, sample_job, mmg_pr
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['template_version'] == notification_from_db.template_version
assert data['created_at'] == notification_from_db.created_at
assert 'sending' == notification_from_db.status
assert Job.query.get(sample_job.id).notifications_sent == 1
@@ -474,18 +477,7 @@ def test_save_notification_and_increment_job(sample_template, sample_job, mmg_pr
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 = {
'id': random_id,
'to': '+44709123456',
'job_id': sample_job.id,
'service_id': sample_template.service.id,
'service': sample_template.service,
'template': sample_template,
'template_id': sample_template.id,
'template_version': sample_template.version,
'created_at': datetime.utcnow(),
'content_char_count': 160
}
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, mmg_provider.identifier)
@@ -508,7 +500,7 @@ def test_save_notification_and_increment_correct_job(notify_db, notify_db_sessio
job_2 = sample_job(notify_db, notify_db_session, sample_template.service)
assert Notification.query.count() == 0
data = _notification_json(sample_template, job_1.id)
data = _notification_json(sample_template, job_id=job_1.id)
notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
@@ -520,6 +512,7 @@ def test_save_notification_and_increment_correct_job(notify_db, notify_db_sessio
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['template_version'] == notification_from_db.template_version
assert data['created_at'] == notification_from_db.created_at
assert 'sending' == notification_from_db.status
assert Job.query.get(job_1.id).notifications_sent == 1
@@ -539,6 +532,7 @@ def test_save_notification_with_no_job(sample_template, mmg_provider):
assert data['to'] == notification_from_db.to
assert data['service'] == notification_from_db.service
assert data['template'] == notification_from_db.template
assert data['template_version'] == notification_from_db.template_version
assert data['created_at'] == notification_from_db.created_at
assert 'sending' == notification_from_db.status
@@ -552,7 +546,6 @@ def test_get_notification(sample_notification):
def test_save_notification_no_job_id(sample_template, mmg_provider):
assert Notification.query.count() == 0
to = '+44709123456'
data = _notification_json(sample_template)
notification = Notification(**data)
@@ -564,6 +557,7 @@ def test_save_notification_no_job_id(sample_template, mmg_provider):
assert data['to'] == notification_from_db.to
assert data['service'] == notification_from_db.service
assert data['template'] == notification_from_db.template
assert data['template_version'] == notification_from_db.template_version
assert 'sending' == notification_from_db.status
@@ -643,7 +637,7 @@ def test_should_not_delete_failed_notifications_before_seven_days(notify_db, not
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, sample_job.id)
data = _notification_json(sample_template, job_id=sample_job.id)
notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
@@ -661,7 +655,7 @@ def test_save_new_notification_creates_template_stats(sample_template, sample_jo
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, sample_job.id)
data = _notification_json(sample_template, job_id=sample_job.id)
notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
@@ -697,7 +691,7 @@ def test_save_new_notification_creates_template_stats_per_day(sample_template, s
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, sample_job.id)
data = _notification_json(sample_template, job_id=sample_job.id)
notification_1 = Notification(**data)
notification_2 = Notification(**data)
@@ -725,7 +719,7 @@ def test_successful_notification_inserts_followed_by_failure_does_not_increment_
assert NotificationStatistics.query.count() == 0
assert TemplateStatistics.query.count() == 0
data = _notification_json(sample_template, sample_job.id)
data = _notification_json(sample_template, job_id=sample_job.id)
notification_1 = Notification(**data)
notification_2 = Notification(**data)
@@ -768,7 +762,7 @@ def test_get_template_stats_for_service_returns_stats_in_reverse_date_order(samp
template_stats = dao_get_template_statistics_for_service(sample_template.service.id)
assert len(template_stats) == 0
data = _notification_json(sample_template, sample_job.id)
data = _notification_json(sample_template, job_id=sample_job.id)
notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type, mmg_provider.identifier)
@@ -901,7 +895,7 @@ def test_sms_fragment_count(char_count, expected_sms_fragment_count):
assert get_sms_fragment_count(char_count) == expected_sms_fragment_count
def _notification_json(sample_template, job_id=None):
def _notification_json(sample_template, job_id=None, id=None):
data = {
'to': '+44709123456',
'service': sample_template.service,
@@ -914,4 +908,6 @@ def _notification_json(sample_template, job_id=None):
}
if job_id:
data.update({'job_id': job_id})
if id:
data.update({'id': id})
return data

View File

@@ -1,11 +1,10 @@
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.exc import IntegrityError
from app.dao.templates_dao import (
dao_create_template,
dao_get_template_by_id_and_service_id,
dao_get_all_templates_for_service,
dao_update_template
)
dao_update_template,
dao_get_template_versions)
from tests.app.conftest import sample_template as create_sample_template
from app.models import Template
import pytest
@@ -230,3 +229,19 @@ def test_get_template_history_version(sample_user, sample_service, sample_templa
'1'
)
assert old_template.content == old_content
def test_get_template_versions(sample_template):
original_content = sample_template.content
sample_template.content = 'new version'
dao_update_template(sample_template)
versions = dao_get_template_versions(service_id=sample_template.service_id, template_id=sample_template.id)
assert versions.__len__() == 2
for x in versions:
if x.version == 2:
assert x.content == 'new version'
else:
assert x.content == original_content
from app.schemas import (template_history_schema)
v = template_history_schema.load(versions, many=True)
assert v.__len__() == 2