From 350133e6db1a299a3e8cca444326da7eadb7360a Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Fri, 23 Jun 2017 15:56:47 +0100 Subject: [PATCH] ensure created_by_id is being persisted correctly (also make sure it's well tested :tada: ) --- app/models.py | 3 +++ app/notifications/process_notifications.py | 4 ++-- tests/app/notifications/test_process_notification.py | 8 ++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/models.py b/app/models.py index 4faa05abb..7a4b9ef3b 100644 --- a/app/models.py +++ b/app/models.py @@ -1007,6 +1007,9 @@ class NotificationHistory(db.Model, HistoryModel): phone_prefix = db.Column(db.String, nullable=True) rate_multiplier = db.Column(db.Float(asdecimal=False), nullable=True) + created_by = db.relationship('User') + created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), nullable=True) + @classmethod def from_original(cls, notification): history = super().from_original(notification) diff --git a/app/notifications/process_notifications.py b/app/notifications/process_notifications.py index 6709bc502..ca3ae0b53 100644 --- a/app/notifications/process_notifications.py +++ b/app/notifications/process_notifications.py @@ -52,7 +52,6 @@ def persist_notification( simulated=False, created_by_id=None ): - notification_created_at = created_at or datetime.utcnow() notification = Notification( @@ -70,7 +69,8 @@ def persist_notification( job_id=job_id, job_row_number=job_row_number, client_reference=client_reference, - reference=reference + reference=reference, + created_by_id=created_by_id ) if notification_type == SMS_TYPE: diff --git a/tests/app/notifications/test_process_notification.py b/tests/app/notifications/test_process_notification.py index e7f0c6eae..ac73b36da 100644 --- a/tests/app/notifications/test_process_notification.py +++ b/tests/app/notifications/test_process_notification.py @@ -77,6 +77,7 @@ def test_persist_notification_creates_and_save_to_db(sample_template, sample_api assert notification_from_db.status == notification_history_from_db.status assert notification_from_db.reference == notification_history_from_db.reference assert notification_from_db.client_reference == notification_history_from_db.client_reference + assert notification_from_db.created_by_id == notification_history_from_db.created_by_id mocked_redis.assert_called_once_with(str(sample_template.service_id) + "-2016-01-01-count") @@ -136,7 +137,8 @@ def test_persist_notification_does_not_increment_cache_if_test_key( api_key.key_type, job_id=sample_job.id, job_row_number=100, - reference="ref") + reference="ref", + ) assert Notification.query.count() == 1 @@ -166,7 +168,8 @@ def test_persist_notification_with_optionals(sample_job, sample_api_key, mocker) job_id=sample_job.id, job_row_number=10, client_reference="ref from client", - notification_id=n_id + notification_id=n_id, + created_by_id=sample_job.created_by_id ) assert Notification.query.count() == 1 assert NotificationHistory.query.count() == 1 @@ -182,6 +185,7 @@ def test_persist_notification_with_optionals(sample_job, sample_api_key, mocker) assert persisted_notification.international is False assert persisted_notification.phone_prefix == '44' assert persisted_notification.rate_multiplier == 1 + assert persisted_notification.created_by_id == sample_job.created_by_id @freeze_time("2016-01-01 11:09:00.061258")