From 559faf3034a9afa5b68cc24259257412086cdad7 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 7 Nov 2019 10:57:31 +0000 Subject: [PATCH] Fix the query. Missing the where clause to join the two tables.... OOPS --- app/celery/scheduled_tasks.py | 2 +- app/dao/jobs_dao.py | 5 +++-- tests/app/dao/test_jobs_dao.py | 23 +++++++++++++++-------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/app/celery/scheduled_tasks.py b/app/celery/scheduled_tasks.py index 520567618..57978c602 100644 --- a/app/celery/scheduled_tasks.py +++ b/app/celery/scheduled_tasks.py @@ -245,5 +245,5 @@ def check_for_missing_rows_in_completed_jobs(): recipient_csv, template = get_recipient_csv_and_template(job) for row in recipient_csv.get_rows(): if row.index == row_to_process.missing_row: - current_app.logger.info("Processing row: {} for job: {}") + current_app.logger.info("Processing missing row: {} for job: {}") process_row(row, template, job, job.service) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index 2f549a4de..a5a7411f1 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -195,11 +195,12 @@ def find_jobs_with_missing_rows(): # Using 10 minutes as a condition seems reasonable. ten_minutes_ago = datetime.utcnow() - timedelta(minutes=10) jobs_with_rows_missing = db.session.query( - func.count(Notification.id).label('count_notifications'), + func.count(Notification.id).label('actual_count'), Job ).filter( Job.job_status == JOB_STATUS_FINISHED, - Job.processing_finished < ten_minutes_ago + Job.processing_finished < ten_minutes_ago, + Job.id == Notification.job_id ).group_by( Job ).having( diff --git a/tests/app/dao/test_jobs_dao.py b/tests/app/dao/test_jobs_dao.py index 8a4c86f8d..704973bfe 100644 --- a/tests/app/dao/test_jobs_dao.py +++ b/tests/app/dao/test_jobs_dao.py @@ -416,19 +416,26 @@ def test_can_letter_job_be_cancelled_returns_false_and_error_message_if_notifica def test_find_jobs_with_missing_rows(sample_email_template): - job = create_job(template=sample_email_template, - notification_count=5, - job_status=JOB_STATUS_FINISHED, - processing_finished=datetime.utcnow() - timedelta(minutes=11) - ) + healthy_job = create_job(template=sample_email_template, + notification_count=3, + job_status=JOB_STATUS_FINISHED, + processing_finished=datetime.utcnow() - timedelta(minutes=11) + ) + for i in range(0, 3): + create_notification(job=healthy_job, job_row_number=i) + job_with_missing_rows = create_job(template=sample_email_template, + notification_count=5, + job_status=JOB_STATUS_FINISHED, + processing_finished=datetime.utcnow() - timedelta(minutes=11) + ) for i in range(0, 4): - create_notification(job=job, job_row_number=i) + create_notification(job=job_with_missing_rows, job_row_number=i) results = find_jobs_with_missing_rows() assert len(results) == 1 - assert results[0][0] == 4 - assert results[0][1] == job + assert results[0].actual_count == 4 + assert results[0][1] == job_with_missing_rows def test_find_jobs_with_missing_rows_returns_nothing_for_a_job_completed_less_than_10_minutes_ago(