mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-09 17:45:41 -04:00
Avoid assigning notification.template when creating test objects
`Notification.template` changed from being a Template relationship to TemplateHistory. When a relationship attribute is being assigned, SQLAlchemy checks that the assigned value type matches the relationship type. Since most tests at the moment create a notification using a Template instance this check fails. Rewriting the tests to use TemplateHistory objects would require changes to the majority of tests. Instead, when creating a notification objects we assign the foreign key attributes directly. This skips the SQLAlchemy type check, but we still get the constraint check on the foreign keys, so a matching TemplateHistory object needs to exist in the database.
This commit is contained in:
@@ -544,8 +544,7 @@ def sample_notification(
|
|||||||
'job': job,
|
'job': job,
|
||||||
'service_id': service.id,
|
'service_id': service.id,
|
||||||
'service': service,
|
'service': service,
|
||||||
'template_id': template.id if template else None,
|
'template_id': template.id,
|
||||||
'template': template,
|
|
||||||
'template_version': template.version,
|
'template_version': template.version,
|
||||||
'status': status,
|
'status': status,
|
||||||
'reference': reference,
|
'reference': reference,
|
||||||
@@ -624,7 +623,7 @@ def sample_email_notification(notify_db, notify_db_session):
|
|||||||
'job': job,
|
'job': job,
|
||||||
'service_id': service.id,
|
'service_id': service.id,
|
||||||
'service': service,
|
'service': service,
|
||||||
'template': template,
|
'template_id': template.id,
|
||||||
'template_version': template.version,
|
'template_version': template.version,
|
||||||
'status': 'created',
|
'status': 'created',
|
||||||
'reference': None,
|
'reference': None,
|
||||||
|
|||||||
@@ -615,7 +615,7 @@ def test_save_notification_creates_sms(sample_template, sample_job, mmg_provider
|
|||||||
assert data['to'] == notification_from_db.to
|
assert data['to'] == notification_from_db.to
|
||||||
assert data['job_id'] == notification_from_db.job_id
|
assert data['job_id'] == notification_from_db.job_id
|
||||||
assert data['service'] == notification_from_db.service
|
assert data['service'] == notification_from_db.service
|
||||||
assert data['template'] == notification_from_db.template
|
assert data['template_id'] == notification_from_db.template_id
|
||||||
assert data['template_version'] == notification_from_db.template_version
|
assert data['template_version'] == notification_from_db.template_version
|
||||||
assert data['created_at'] == notification_from_db.created_at
|
assert data['created_at'] == notification_from_db.created_at
|
||||||
assert notification_from_db.status == 'created'
|
assert notification_from_db.status == 'created'
|
||||||
@@ -635,7 +635,7 @@ def test_save_notification_and_create_email(sample_email_template, sample_job):
|
|||||||
assert data['to'] == notification_from_db.to
|
assert data['to'] == notification_from_db.to
|
||||||
assert data['job_id'] == notification_from_db.job_id
|
assert data['job_id'] == notification_from_db.job_id
|
||||||
assert data['service'] == notification_from_db.service
|
assert data['service'] == notification_from_db.service
|
||||||
assert data['template'] == notification_from_db.template
|
assert data['template_id'] == notification_from_db.template_id
|
||||||
assert data['template_version'] == notification_from_db.template_version
|
assert data['template_version'] == notification_from_db.template_version
|
||||||
assert data['created_at'] == notification_from_db.created_at
|
assert data['created_at'] == notification_from_db.created_at
|
||||||
assert notification_from_db.status == 'created'
|
assert notification_from_db.status == 'created'
|
||||||
@@ -761,7 +761,7 @@ def test_save_notification_and_increment_job(sample_template, sample_job, mmg_pr
|
|||||||
assert data['to'] == notification_from_db.to
|
assert data['to'] == notification_from_db.to
|
||||||
assert data['job_id'] == notification_from_db.job_id
|
assert data['job_id'] == notification_from_db.job_id
|
||||||
assert data['service'] == notification_from_db.service
|
assert data['service'] == notification_from_db.service
|
||||||
assert data['template'] == notification_from_db.template
|
assert data['template_id'] == notification_from_db.template_id
|
||||||
assert data['template_version'] == notification_from_db.template_version
|
assert data['template_version'] == notification_from_db.template_version
|
||||||
assert data['created_at'] == notification_from_db.created_at
|
assert data['created_at'] == notification_from_db.created_at
|
||||||
assert notification_from_db.status == 'created'
|
assert notification_from_db.status == 'created'
|
||||||
@@ -788,7 +788,7 @@ def test_save_notification_and_increment_correct_job(notify_db, notify_db_sessio
|
|||||||
assert data['to'] == notification_from_db.to
|
assert data['to'] == notification_from_db.to
|
||||||
assert data['job_id'] == notification_from_db.job_id
|
assert data['job_id'] == notification_from_db.job_id
|
||||||
assert data['service'] == notification_from_db.service
|
assert data['service'] == notification_from_db.service
|
||||||
assert data['template'] == notification_from_db.template
|
assert data['template_id'] == notification_from_db.template_id
|
||||||
assert data['template_version'] == notification_from_db.template_version
|
assert data['template_version'] == notification_from_db.template_version
|
||||||
assert data['created_at'] == notification_from_db.created_at
|
assert data['created_at'] == notification_from_db.created_at
|
||||||
assert notification_from_db.status == 'created'
|
assert notification_from_db.status == 'created'
|
||||||
@@ -807,7 +807,7 @@ def test_save_notification_with_no_job(sample_template, mmg_provider):
|
|||||||
assert notification_from_db.id
|
assert notification_from_db.id
|
||||||
assert data['to'] == notification_from_db.to
|
assert data['to'] == notification_from_db.to
|
||||||
assert data['service'] == notification_from_db.service
|
assert data['service'] == notification_from_db.service
|
||||||
assert data['template'] == notification_from_db.template
|
assert data['template_id'] == notification_from_db.template_id
|
||||||
assert data['template_version'] == notification_from_db.template_version
|
assert data['template_version'] == notification_from_db.template_version
|
||||||
assert data['created_at'] == notification_from_db.created_at
|
assert data['created_at'] == notification_from_db.created_at
|
||||||
assert notification_from_db.status == 'created'
|
assert notification_from_db.status == 'created'
|
||||||
@@ -852,7 +852,7 @@ def test_save_notification_no_job_id(sample_template, mmg_provider):
|
|||||||
assert notification_from_db.id
|
assert notification_from_db.id
|
||||||
assert data['to'] == notification_from_db.to
|
assert data['to'] == notification_from_db.to
|
||||||
assert data['service'] == notification_from_db.service
|
assert data['service'] == notification_from_db.service
|
||||||
assert data['template'] == notification_from_db.template
|
assert data['template_id'] == notification_from_db.template_id
|
||||||
assert data['template_version'] == notification_from_db.template_version
|
assert data['template_version'] == notification_from_db.template_version
|
||||||
assert notification_from_db.status == 'created'
|
assert notification_from_db.status == 'created'
|
||||||
assert data.get('job_id') is None
|
assert data.get('job_id') is None
|
||||||
@@ -1251,7 +1251,6 @@ def _notification_json(sample_template, job_id=None, id=None, status=None):
|
|||||||
'to': '+44709123456',
|
'to': '+44709123456',
|
||||||
'service': sample_template.service,
|
'service': sample_template.service,
|
||||||
'service_id': sample_template.service.id,
|
'service_id': sample_template.service.id,
|
||||||
'template': sample_template,
|
|
||||||
'template_id': sample_template.id,
|
'template_id': sample_template.id,
|
||||||
'template_version': sample_template.version,
|
'template_version': sample_template.version,
|
||||||
'created_at': datetime.utcnow(),
|
'created_at': datetime.utcnow(),
|
||||||
|
|||||||
@@ -160,8 +160,7 @@ def create_notification(
|
|||||||
'job': job,
|
'job': job,
|
||||||
'service_id': template.service.id,
|
'service_id': template.service.id,
|
||||||
'service': template.service,
|
'service': template.service,
|
||||||
'template_id': template and template.id,
|
'template_id': template.id,
|
||||||
'template': template,
|
|
||||||
'template_version': template.version,
|
'template_version': template.version,
|
||||||
'status': status,
|
'status': status,
|
||||||
'reference': reference,
|
'reference': reference,
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ def test_create_letter_notification_creates_notification(sample_letter_template,
|
|||||||
assert notification == Notification.query.one()
|
assert notification == Notification.query.one()
|
||||||
assert notification.job is None
|
assert notification.job is None
|
||||||
assert notification.status == NOTIFICATION_CREATED
|
assert notification.status == NOTIFICATION_CREATED
|
||||||
assert notification.template == sample_letter_template
|
assert notification.template_id == sample_letter_template.id
|
||||||
|
assert notification.template_version == sample_letter_template.version
|
||||||
assert notification.api_key == sample_api_key
|
assert notification.api_key == sample_api_key
|
||||||
assert notification.notification_type == LETTER_TYPE
|
assert notification.notification_type == LETTER_TYPE
|
||||||
assert notification.key_type == sample_api_key.key_type
|
assert notification.key_type == sample_api_key.key_type
|
||||||
|
|||||||
@@ -202,8 +202,8 @@ def test_notification_subject_fills_in_placeholders_for_email(sample_email_templ
|
|||||||
|
|
||||||
|
|
||||||
def test_notification_subject_fills_in_placeholders_for_letter(sample_letter_template):
|
def test_notification_subject_fills_in_placeholders_for_letter(sample_letter_template):
|
||||||
sample_letter_template.subject = '((name))'
|
|
||||||
noti = create_notification(sample_letter_template, personalisation={'name': 'hello'})
|
noti = create_notification(sample_letter_template, personalisation={'name': 'hello'})
|
||||||
|
noti.template.subject = '((name))'
|
||||||
assert noti.subject == 'hello'
|
assert noti.subject == 'hello'
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user