From 4ff4717fdfa2699e6b362c97131f07e0ef8eebc2 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Tue, 9 May 2017 11:09:16 +0100 Subject: [PATCH] make sure we update both statuses when updating notification history --- app/models.py | 4 ++++ tests/app/dao/test_notification_dao.py | 27 +++++++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/app/models.py b/app/models.py index 605b7bab8..1b07a6fc5 100644 --- a/app/models.py +++ b/app/models.py @@ -895,6 +895,10 @@ class NotificationHistory(db.Model, HistoryModel): history.status = notification.status return history + def update_from_original(self, original): + super().update_from_original(original) + self.status = original.status + @hybrid_property def status(self): return self._status_enum diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index 9c5006de7..68618a272 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -839,22 +839,35 @@ def test_update_notification(sample_notification): def test_update_notification_with_no_notification_status(sample_notification): # specifically, it has an old enum status, but not a new status (because the upgrade script has just run) - sample_notification._status_fkey = None - sample_notification._enum_status = 'created' - dao_update_notification(sample_notification) - - assert sample_notification.status == 'created' - assert sample_notification._enum_status == 'created' - assert sample_notification._status_fkey == None + update_dict = {'_status_enum': 'created', '_status_fkey': None} + Notification.query.filter(Notification.id == sample_notification.id).update(update_dict) + # now lets update the status to failed - both columns should now be populated sample_notification.status = 'failed' dao_update_notification(sample_notification) + notification_from_db = Notification.query.get(sample_notification.id) assert notification_from_db.status == 'failed' assert notification_from_db._status_enum == 'failed' assert notification_from_db._status_fkey == 'failed' +def test_updating_notification_with_no_notification_status_updates_notification_history(sample_notification): + # same as above, but with notification history + update_dict = {'_status_enum': 'created', '_status_fkey': None} + Notification.query.filter(Notification.id == sample_notification.id).update(update_dict) + NotificationHistory.query.filter(NotificationHistory.id == sample_notification.id).update(update_dict) + + # now lets update the notification's status to failed - both columns should now be populated on the history object + sample_notification.status = 'failed' + dao_update_notification(sample_notification) + + hist_from_db = NotificationHistory.query.get(sample_notification.id) + assert hist_from_db.status == 'failed' + assert hist_from_db._status_enum == 'failed' + assert hist_from_db._status_fkey == 'failed' + + @freeze_time("2016-01-10 12:00:00.000000") def test_should_delete_notifications_after_seven_days(notify_db, notify_db_session): assert len(Notification.query.all()) == 0