Added a jobs_dao method to answer if the all the notifications have been created in the database for the given job.

This commit is contained in:
Rebecca Law
2017-03-14 10:50:09 +00:00
parent 28249fb4e2
commit ea4214c7d5
2 changed files with 38 additions and 3 deletions

View File

@@ -5,7 +5,11 @@ from sqlalchemy import func, desc, asc, cast, Date as sql_date
from app import db
from app.dao import days_ago
from app.models import Job, NotificationHistory, JOB_STATUS_SCHEDULED, JOB_STATUS_PENDING
from app.models import (Job,
Notification,
NotificationHistory,
JOB_STATUS_SCHEDULED,
JOB_STATUS_PENDING)
from app.statsd_decorators import statsd
@@ -24,6 +28,20 @@ def dao_get_notification_outcomes_for_job(service_id, job_id):
.all()
@statsd(namespace="dao")
def are_all_notifications_created_for_job(job_id):
query = db.session.query(func.count(Notification.id))\
.join(Job)\
.filter(Job.id == job_id)\
.group_by(Job.id)\
.having(func.count(Notification.id) == Job.notification_count).first()
if query:
return True
else:
return False
def dao_get_job_by_service_id_and_job_id(service_id, job_id):
return Job.query.filter_by(service_id=service_id, id=job_id).one()

View File

@@ -12,8 +12,8 @@ from app.dao.jobs_dao import (
dao_set_scheduled_jobs_to_pending,
dao_get_future_scheduled_job_by_id_and_service_id,
dao_get_notification_outcomes_for_job,
dao_get_jobs_older_than
)
dao_get_jobs_older_than,
are_all_notifications_created_for_job)
from app.models import Job
from tests.app.conftest import sample_notification as create_notification
@@ -314,3 +314,20 @@ def test_get_jobs_for_service_doesnt_return_test_messages(notify_db, notify_db_s
jobs = dao_get_jobs_by_service_id(sample_job.service_id).items
assert jobs == [sample_job]
def test_are_all_notifications_created_for_job_returns_true(notify_db, notify_db_session, sample_job):
create_notification(notify_db=notify_db, notify_db_session=notify_db_session, job=sample_job)
job_is_complete = are_all_notifications_created_for_job(sample_job.id)
assert job_is_complete
def test_are_all_notifications_created_for_job_returns_false(notify_db, notify_db_session):
job = create_job(notify_db=notify_db, notify_db_session=notify_db_session, notification_count=2)
job_is_complete = are_all_notifications_created_for_job(job.id)
assert not job_is_complete
def test_are_all_notifications_created_for_job_returns_false_when_job_does_not_exist(notify_db, notify_db_session):
job_is_complete = are_all_notifications_created_for_job(uuid.uuid4())
assert not job_is_complete