From e5e049d73564a9035265ec6eb142da92a9e37ea9 Mon Sep 17 00:00:00 2001 From: Adam Shimali Date: Tue, 9 Feb 2016 12:48:27 +0000 Subject: [PATCH] Added service and template relationship to notification model. This makes it more consistent with other model classes with respect to marhmallow serialisation/deserialisation. --- app/dao/notifications_dao.py | 4 ++-- app/models.py | 2 ++ tests/app/conftest.py | 6 +++--- tests/app/dao/test_notification_dao.py | 17 ++++++++--------- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index be4e56ef6..8c42ac6ca 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -6,8 +6,8 @@ def save_notification(notification, update_dict={}): if update_dict: update_dict.pop('id', None) update_dict.pop('job', None) - update_dict.pop('service_id', None) - update_dict.pop('template_id', None) + update_dict.pop('service', None) + update_dict.pop('template', None) Notification.query.filter_by(id=notification.id).update(update_dict) else: db.session.add(notification) diff --git a/app/models.py b/app/models.py index 22337a6a5..1ce4a7dbf 100644 --- a/app/models.py +++ b/app/models.py @@ -203,7 +203,9 @@ class Notification(db.Model): job_id = db.Column(UUID(as_uuid=True), db.ForeignKey('jobs.id'), index=True, unique=False, nullable=False) job = db.relationship('Job', backref=db.backref('notifications', lazy='dynamic')) service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False) + service = db.relationship('Service') template_id = db.Column(db.BigInteger, db.ForeignKey('templates.id'), index=True, unique=False) + template = db.relationship('Template') created_at = db.Column( db.DateTime, index=False, diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 741d896eb..0f6a33461 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -202,9 +202,9 @@ def sample_notification(notify_db, data = { 'id': notificaton_id, 'to': to, - 'job_id': job.id, - 'service_id': service.id, - 'template_id': template.id + 'job': job, + 'service': service, + 'template': template } notification = Notification(**data) save_notification(notification) diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index 23ff6fe23..7d1218e74 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -15,13 +15,12 @@ def test_save_notification(notify_db, notify_db_session, sample_template, sample notification_id = uuid.uuid4() to = '+44709123456' - job_id = sample_job.id data = { 'id': notification_id, 'to': to, - 'job_id': job_id, - 'service_id': sample_template.service.id, - 'template_id': sample_template.id + 'job': sample_job, + 'service': sample_template.service, + 'template': sample_template } notification = Notification(**data) @@ -33,9 +32,9 @@ def test_save_notification(notify_db, notify_db_session, sample_template, sample assert data['id'] == notification_from_db.id assert data['to'] == notification_from_db.to - assert data['job_id'] == notification_from_db.job_id - assert data['service_id'] == notification_from_db.service_id - assert data['template_id'] == notification_from_db.template_id + assert data['job'] == notification_from_db.job + assert data['service'] == notification_from_db.service + assert data['template'] == notification_from_db.template assert 'sent' == notification_from_db.status @@ -63,8 +62,8 @@ def test_update_notification(notify_db, notify_db_session, sample_notification): update_dict = { 'id': sample_notification.id, - 'service_id': sample_notification.service_id, - 'template_id': sample_notification.template_id, + 'service': sample_notification.service, + 'template': sample_notification.template, 'job': sample_notification.job, 'status': 'failed' }