From db6668eb6105bed51e6e33ade9460e8cd5259033 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Mon, 30 Oct 2017 15:17:01 +0000 Subject: [PATCH] Added the notification_to_sms_sender mapping table to the purge notifications query --- app/dao/notifications_dao.py | 14 +++++--- app/dao/services_dao.py | 2 +- .../notification_dao/test_notification_dao.py | 34 ++++++++++++++++++- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 4de97170e..56794c394 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -33,6 +33,7 @@ from app.models import ( ServiceEmailReplyTo, Template, EMAIL_TYPE, + SMS_TYPE, KEY_TYPE_NORMAL, KEY_TYPE_TEST, LETTER_TYPE, @@ -374,17 +375,22 @@ def delete_notifications_created_more_than_a_week_ago_by_type(notification_type) seven_days_ago = date.today() - timedelta(days=7) # Following could be refactored when NotificationSmsReplyTo and NotificationLetterContact in models.py - if notification_type == EMAIL_TYPE: + if notification_type in [EMAIL_TYPE, SMS_TYPE]: subq = db.session.query(Notification.id).filter( func.date(Notification.created_at) < seven_days_ago, Notification.notification_type == notification_type ).subquery() - deleted = db.session.query( - NotificationEmailReplyTo + if notification_type == EMAIL_TYPE: + notification_sender_mapping_table = NotificationEmailReplyTo + if notification_type == SMS_TYPE: + notification_sender_mapping_table = NotificationSmsSender + db.session.query( + notification_sender_mapping_table ).filter( - NotificationEmailReplyTo.notification_id.in_(subq) + notification_sender_mapping_table.notification_id.in_(subq) ).delete(synchronize_session='fetch') + deleted = db.session.query(Notification).filter( func.date(Notification.created_at) < seven_days_ago, Notification.notification_type == notification_type, diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index f9347b6b7..986a0b7bb 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -10,7 +10,7 @@ from app.dao.dao_utils import ( transactional, version_class ) -from app.dao.notifications_dao import get_financial_year +from app.dao.date_util import get_financial_year from app.dao.service_sms_sender_dao import insert_service_sms_sender from app.models import ( NotificationStatistics, diff --git a/tests/app/dao/notification_dao/test_notification_dao.py b/tests/app/dao/notification_dao/test_notification_dao.py index 0cf5fff7b..cfbc2dd77 100644 --- a/tests/app/dao/notification_dao/test_notification_dao.py +++ b/tests/app/dao/notification_dao/test_notification_dao.py @@ -22,7 +22,7 @@ from app.models import ( KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST, - JOB_STATUS_IN_PROGRESS, NotificationSmsSender) + JOB_STATUS_IN_PROGRESS, NotificationSmsSender, SMS_TYPE) from app.dao.notifications_dao import ( dao_create_notification, @@ -1044,6 +1044,38 @@ def test_should_delete_notification_to_email_reply_to_after_seven_days( assert notification.created_at.date() >= date(2016, 1, 3) +@freeze_time("2016-01-10 12:00:00.000000") +def test_should_delete_notification_to_sms_sender_after_seven_days( + sample_template +): + assert len(Notification.query.all()) == 0 + + sms_sender = create_service_sms_sender(service=sample_template.service, sms_sender='123456', is_default=False) + + # create one notification a day between 1st and 10th from 11:00 to 19:00 of each type + for i in range(1, 11): + past_date = '2016-01-{0:02d} {0:02d}:00:00.000000'.format(i) + with freeze_time(past_date): + notification = create_notification(template=sample_template, sms_sender_id=sms_sender.id) + + all_notifications = Notification.query.all() + assert len(all_notifications) == 10 + + all_notification_sms_senders = NotificationSmsSender.query.all() + assert len(all_notification_sms_senders) == 10 + + # Records before 3rd should be deleted + delete_notifications_created_more_than_a_week_ago_by_type(SMS_TYPE) + remaining_notifications = Notification.query.filter_by(notification_type=SMS_TYPE).all() + remaining_notification_to_sms_sender = NotificationSmsSender.query.filter_by().all() + + assert len(remaining_notifications) == 8 + assert len(remaining_notification_to_sms_sender) == 8 + + for notification in remaining_notifications: + assert notification.created_at.date() >= date(2016, 1, 3) + + @pytest.mark.parametrize('notification_type', ['sms', 'email', 'letter']) @freeze_time("2016-01-10 12:00:00.000000") def test_should_not_delete_notification_history(notify_db, notify_db_session, sample_service, notification_type):