From c3657839e4268a4eff26da1cfb801dea6111b485 Mon Sep 17 00:00:00 2001 From: Martyn Inglis Date: Wed, 7 Sep 2016 15:36:07 +0100 Subject: [PATCH] New jobs dao method to get jobs that are older than a certain number of days. Used in deleting CSV files scheduled task --- app/dao/jobs_dao.py | 6 ++++++ tests/app/dao/test_jobs_dao.py | 20 ++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index 790b9cb18..b4cfbde4d 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -66,3 +66,9 @@ def dao_create_job(job): def dao_update_job(job): db.session.add(job) db.session.commit() + + +def dao_get_jobs_older_than(limit_days): + return Job.query.filter( + cast(Job.created_at, sql_date) < days_ago(limit_days) + ).order_by(desc(Job.created_at)).all() diff --git a/tests/app/dao/test_jobs_dao.py b/tests/app/dao/test_jobs_dao.py index 2b4728be1..ee116c8ca 100644 --- a/tests/app/dao/test_jobs_dao.py +++ b/tests/app/dao/test_jobs_dao.py @@ -1,5 +1,6 @@ from datetime import (datetime, timedelta) import uuid +from freezegun import freeze_time from app.dao.jobs_dao import ( dao_get_job_by_service_id_and_job_id, @@ -8,8 +9,8 @@ from app.dao.jobs_dao import ( dao_get_jobs_by_service_id, dao_get_scheduled_jobs, dao_get_future_scheduled_job_by_id_and_service_id, - dao_get_notification_outcomes_for_job -) + dao_get_notification_outcomes_for_job, + dao_get_jobs_older_than) from app.models import Job from tests.app.conftest import sample_notification, sample_job, sample_service @@ -255,3 +256,18 @@ def test_get_scheduled_jobs_gets_ignores_jobs_scheduled_in_the_future(sample_sch def test_get_future_scheduled_job_gets_a_job_yet_to_send(sample_scheduled_job): result = dao_get_future_scheduled_job_by_id_and_service_id(sample_scheduled_job.id, sample_scheduled_job.service_id) assert result.id == sample_scheduled_job.id + + +def test_should_get_jobs_older_than_seven_days(notify_db, notify_db_session): + one_millisecond_before_midnight = datetime(2016, 10, 9, 23, 59, 59, 999) + midnight = datetime(2016, 10, 10, 0, 0, 0, 0) + one_millisecond_past_midnight = datetime(2016, 10, 10, 0, 0, 0, 1) + + job_1 = sample_job(notify_db, notify_db_session, created_at=one_millisecond_before_midnight) + sample_job(notify_db, notify_db_session, created_at=midnight) + sample_job(notify_db, notify_db_session, created_at=one_millisecond_past_midnight) + + with freeze_time('2016-10-17T00:00:00'): + jobs = dao_get_jobs_older_than(7) + assert len(jobs) == 1 + assert jobs[0].id == job_1.id