diff --git a/app/celery/tasks.py b/app/celery/tasks.py index d41271e06..f07e3fe09 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -32,8 +32,7 @@ from app.dao.invited_user_dao import delete_invitations_created_more_than_two_da from app.dao.notifications_dao import ( dao_create_notification, dao_update_notification, - delete_failed_notifications_created_more_than_a_week_ago, - delete_successful_notifications_created_more_than_a_day_ago, + delete_notifications_created_more_than_a_week_ago, dao_get_notification_statistics_for_service_and_day, update_notification_reference_by_id ) @@ -67,7 +66,7 @@ def delete_verify_codes(): def delete_successful_notifications(): try: start = datetime.utcnow() - deleted = delete_successful_notifications_created_more_than_a_day_ago() + deleted = delete_notifications_created_more_than_a_week_ago('sent') current_app.logger.info( "Delete job started {} finished {} deleted {} successful notifications".format( start, @@ -84,7 +83,7 @@ def delete_successful_notifications(): def delete_failed_notifications(): try: start = datetime.utcnow() - deleted = delete_failed_notifications_created_more_than_a_week_ago() + deleted = delete_notifications_created_more_than_a_week_ago('failed') current_app.logger.info( "Delete job started {} finished {} deleted {} failed notifications".format( start, diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index a4e657b41..6196a7842 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -236,19 +236,19 @@ def filter_query(query, filter_dict=None): return query -def delete_successful_notifications_created_more_than_a_day_ago(): +def delete_notifications_created_more_than_a_day_ago(status): deleted = db.session.query(Notification).filter( Notification.created_at < datetime.utcnow() - timedelta(days=1), - Notification.status == 'sent' + Notification.status == status ).delete() db.session.commit() return deleted -def delete_failed_notifications_created_more_than_a_week_ago(): +def delete_notifications_created_more_than_a_week_ago(status): deleted = db.session.query(Notification).filter( Notification.created_at < datetime.utcnow() - timedelta(days=7), - Notification.status == 'failed' + Notification.status == status ).delete() db.session.commit() return deleted diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index 39a755a71..7554c6bc0 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -46,16 +46,16 @@ def firetext_error(): return {'code': 0, 'description': 'error'} -def test_should_call_delete_successful_notifications_in_task(notify_api, mocker): - mocker.patch('app.celery.tasks.delete_successful_notifications_created_more_than_a_day_ago') +def test_should_call_delete_notifications_more_than_week_in_task(notify_api, mocker): + mocker.patch('app.celery.tasks.delete_notifications_created_more_than_a_week_ago') delete_successful_notifications() - assert tasks.delete_successful_notifications_created_more_than_a_day_ago.call_count == 1 + assert tasks.delete_notifications_created_more_than_a_week_ago.call_count == 1 -def test_should_call_delete_failed_notifications_in_task(notify_api, mocker): - mocker.patch('app.celery.tasks.delete_failed_notifications_created_more_than_a_week_ago') +def test_should_call_delete_notifications_more_than_week_in_task(notify_api, mocker): + mocker.patch('app.celery.tasks.delete_notifications_created_more_than_a_week_ago') delete_failed_notifications() - assert tasks.delete_failed_notifications_created_more_than_a_week_ago.call_count == 1 + assert tasks.delete_notifications_created_more_than_a_week_ago.call_count == 1 def test_should_call_delete_codes_on_delete_verify_codes_task(notify_api, mocker): diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index f042e52e3..5f7d80cd0 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -23,8 +23,8 @@ from app.dao.notifications_dao import ( get_notification_for_job, get_notifications_for_job, dao_get_notification_statistics_for_service, - delete_successful_notifications_created_more_than_a_day_ago, - delete_failed_notifications_created_more_than_a_week_ago, + delete_notifications_created_more_than_a_day_ago, + delete_notifications_created_more_than_a_week_ago, dao_get_notification_statistics_for_service_and_day, update_notification_status_by_id, update_notification_reference_by_id, @@ -701,21 +701,21 @@ def test_update_notification(sample_notification, sample_template): assert notification_from_db.status == 'failed' -def test_should_delete_sent_notifications_after_one_day(notify_db, notify_db_session): +def test_should_delete_notifications_after_one_day(notify_db, notify_db_session): created_at = datetime.utcnow() - timedelta(hours=24) sample_notification(notify_db, notify_db_session, created_at=created_at) sample_notification(notify_db, notify_db_session, created_at=created_at) assert len(Notification.query.all()) == 2 - delete_successful_notifications_created_more_than_a_day_ago() + delete_notifications_created_more_than_a_day_ago('sent') assert len(Notification.query.all()) == 0 -def test_should_delete_failed_notifications_after_seven_days(notify_db, notify_db_session): +def test_should_delete_notifications_after_seven_days(notify_db, notify_db_session): created_at = datetime.utcnow() - timedelta(hours=24 * 7) sample_notification(notify_db, notify_db_session, created_at=created_at, status="failed") sample_notification(notify_db, notify_db_session, created_at=created_at, status="failed") assert len(Notification.query.all()) == 2 - delete_failed_notifications_created_more_than_a_week_ago() + delete_notifications_created_more_than_a_week_ago('failed') assert len(Notification.query.all()) == 0 @@ -726,7 +726,7 @@ def test_should_not_delete_sent_notifications_before_one_day(notify_db, notify_d sample_notification(notify_db, notify_db_session, created_at=valid, to_field="valid") assert len(Notification.query.all()) == 2 - delete_successful_notifications_created_more_than_a_day_ago() + delete_notifications_created_more_than_a_day_ago('sent') assert len(Notification.query.all()) == 1 assert Notification.query.first().to == 'valid' @@ -737,7 +737,7 @@ def test_should_not_delete_failed_notifications_before_seven_days(notify_db, not sample_notification(notify_db, notify_db_session, created_at=expired, status="failed", to_field="expired") sample_notification(notify_db, notify_db_session, created_at=valid, status="failed", to_field="valid") assert len(Notification.query.all()) == 2 - delete_failed_notifications_created_more_than_a_week_ago() + delete_notifications_created_more_than_a_week_ago('failed') assert len(Notification.query.all()) == 1 assert Notification.query.first().to == 'valid'