tests n stuff

This commit is contained in:
Leo Hemsted
2016-10-07 12:55:48 +01:00
parent d22d055e21
commit bdb4da4976
4 changed files with 31 additions and 11 deletions

View File

@@ -29,9 +29,7 @@ def run_scheduled_jobs():
try: try:
for job in dao_set_scheduled_jobs_to_pending(): for job in dao_set_scheduled_jobs_to_pending():
process_job.apply_async([str(job.id)], queue="process-job") process_job.apply_async([str(job.id)], queue="process-job")
current_app.logger.info( current_app.logger.info("Job ID {} added to process job queue".format(job.id))
"Job ID {} added to process job queue".format(job.id)
)
except SQLAlchemyError as e: except SQLAlchemyError as e:
current_app.logger.exception("Failed to run scheduled jobs", e) current_app.logger.exception("Failed to run scheduled jobs", e)
raise raise

View File

@@ -71,7 +71,6 @@ def dao_set_scheduled_jobs_to_pending():
return jobs return jobs
def dao_get_future_scheduled_job_by_id_and_service_id(job_id, service_id): def dao_get_future_scheduled_job_by_id_and_service_id(job_id, service_id):
return Job.query \ return Job.query \
.filter( .filter(

View File

@@ -221,6 +221,18 @@ def test_should_not_process_email_job_if_would_exceed_send_limits(notify_db, not
assert tasks.send_email.apply_async.called is False assert tasks.send_email.apply_async.called is False
def test_should_not_process_job_if_already_pending(notify_db, notify_db_session, mocker):
job = sample_job(notify_db, notify_db_session, job_status='scheduled')
mocker.patch('app.celery.tasks.s3.get_job_from_s3')
mocker.patch('app.celery.tasks.send_sms.apply_async')
process_job(job.id)
assert s3.get_job_from_s3.called is False
assert tasks.send_sms.apply_async.called is False
@freeze_time("2016-01-01 11:09:00.061258") @freeze_time("2016-01-01 11:09:00.061258")
def test_should_process_email_job_if_exactly_on_send_limits(notify_db, def test_should_process_email_job_if_exactly_on_send_limits(notify_db,
notify_db_session, notify_db_session,

View File

@@ -8,7 +8,7 @@ from app.dao.jobs_dao import (
dao_create_job, dao_create_job,
dao_update_job, dao_update_job,
dao_get_jobs_by_service_id, dao_get_jobs_by_service_id,
dao_get_scheduled_jobs, dao_set_scheduled_jobs_to_pending,
dao_get_future_scheduled_job_by_id_and_service_id, 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 dao_get_jobs_older_than
@@ -223,31 +223,42 @@ def test_update_job(sample_job):
assert job_from_db.job_status == 'in progress' assert job_from_db.job_status == 'in progress'
def test_get_scheduled_jobs_gets_all_jobs_in_scheduled_state_scheduled_before_now(notify_db, notify_db_session): def test_set_scheduled_jobs_to_pending_gets_all_jobs_in_scheduled_state_before_now(notify_db, notify_db_session):
one_minute_ago = datetime.utcnow() - timedelta(minutes=1) one_minute_ago = datetime.utcnow() - timedelta(minutes=1)
one_hour_ago = datetime.utcnow() - timedelta(minutes=60) one_hour_ago = datetime.utcnow() - timedelta(minutes=60)
job_new = create_job(notify_db, notify_db_session, scheduled_for=one_minute_ago, job_status='scheduled') job_new = create_job(notify_db, notify_db_session, scheduled_for=one_minute_ago, job_status='scheduled')
job_old = create_job(notify_db, notify_db_session, scheduled_for=one_hour_ago, job_status='scheduled') job_old = create_job(notify_db, notify_db_session, scheduled_for=one_hour_ago, job_status='scheduled')
jobs = dao_get_scheduled_jobs() jobs = dao_set_scheduled_jobs_to_pending()
assert len(jobs) == 2 assert len(jobs) == 2
assert jobs[0].id == job_old.id assert jobs[0].id == job_old.id
assert jobs[1].id == job_new.id assert jobs[1].id == job_new.id
def test_get_scheduled_jobs_gets_ignores_jobs_not_scheduled(notify_db, notify_db_session): def test_set_scheduled_jobs_to_pending_gets_ignores_jobs_not_scheduled(notify_db, notify_db_session):
one_minute_ago = datetime.utcnow() - timedelta(minutes=1) one_minute_ago = datetime.utcnow() - timedelta(minutes=1)
create_job(notify_db, notify_db_session) create_job(notify_db, notify_db_session)
job_scheduled = create_job(notify_db, notify_db_session, scheduled_for=one_minute_ago, job_status='scheduled') job_scheduled = create_job(notify_db, notify_db_session, scheduled_for=one_minute_ago, job_status='scheduled')
jobs = dao_get_scheduled_jobs() jobs = dao_set_scheduled_jobs_to_pending()
assert len(jobs) == 1 assert len(jobs) == 1
assert jobs[0].id == job_scheduled.id assert jobs[0].id == job_scheduled.id
def test_get_scheduled_jobs_gets_ignores_jobs_scheduled_in_the_future(sample_scheduled_job): def test_set_scheduled_jobs_to_pending_gets_ignores_jobs_scheduled_in_the_future(sample_scheduled_job):
jobs = dao_get_scheduled_jobs() jobs = dao_set_scheduled_jobs_to_pending()
assert len(jobs) == 0 assert len(jobs) == 0
def test_set_scheduled_jobs_to_pending_updates_rows(notify_db, notify_db_session):
one_minute_ago = datetime.utcnow() - timedelta(minutes=1)
one_hour_ago = datetime.utcnow() - timedelta(minutes=60)
create_job(notify_db, notify_db_session, scheduled_for=one_minute_ago, job_status='scheduled')
create_job(notify_db, notify_db_session, scheduled_for=one_hour_ago, job_status='scheduled')
jobs = dao_set_scheduled_jobs_to_pending()
assert len(jobs) == 2
assert jobs[0].job_status == 'pending'
assert jobs[1].job_status == 'pending'
def test_get_future_scheduled_job_gets_a_job_yet_to_send(sample_scheduled_job): 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) 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 assert result.id == sample_scheduled_job.id