mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-11 10:28:55 -04:00
add tests for new sched task
This commit is contained in:
@@ -325,9 +325,9 @@ def run_letter_jobs():
|
|||||||
current_app.logger.info("Queued {} ready letter job ids onto {}".format(len(job_ids), QueueNames.PROCESS_FTP))
|
current_app.logger.info("Queued {} ready letter job ids onto {}".format(len(job_ids), QueueNames.PROCESS_FTP))
|
||||||
|
|
||||||
|
|
||||||
@notify_celery.task(name="run-letter-notifications")
|
@notify_celery.task(name="run-letter-api-notifications")
|
||||||
@statsd(namespace="tasks")
|
@statsd(namespace="tasks")
|
||||||
def run_letter_notifications():
|
def run_letter_api_notifications():
|
||||||
current_time = datetime.utcnow().isoformat()
|
current_time = datetime.utcnow().isoformat()
|
||||||
|
|
||||||
notifications = dao_set_created_live_letter_api_notifications_to_pending()
|
notifications = dao_set_created_live_letter_api_notifications_to_pending()
|
||||||
|
|||||||
+2
-2
@@ -230,8 +230,8 @@ class Config(object):
|
|||||||
'schedule': crontab(hour=5, minute=30),
|
'schedule': crontab(hour=5, minute=30),
|
||||||
'options': {'queue': QueueNames.PERIODIC}
|
'options': {'queue': QueueNames.PERIODIC}
|
||||||
},
|
},
|
||||||
'run-letter-notifications': {
|
'run-letter-api-notifications': {
|
||||||
'task': 'run-letter-notifications',
|
'task': 'run-letter-api-notifications',
|
||||||
'schedule': crontab(hour=5, minute=40),
|
'schedule': crontab(hour=5, minute=40),
|
||||||
'options': {'queue': QueueNames.PERIODIC}
|
'options': {'queue': QueueNames.PERIODIC}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -586,7 +586,7 @@ def dao_set_created_live_letter_api_notifications_to_pending():
|
|||||||
the transaction so that if the task is run more than once concurrently, one task will block the other select
|
the transaction so that if the task is run more than once concurrently, one task will block the other select
|
||||||
from completing until it commits.
|
from completing until it commits.
|
||||||
"""
|
"""
|
||||||
return db.session.query(
|
notifications = db.session.query(
|
||||||
Notification
|
Notification
|
||||||
).filter(
|
).filter(
|
||||||
Notification.notification_type == LETTER_TYPE,
|
Notification.notification_type == LETTER_TYPE,
|
||||||
@@ -597,7 +597,7 @@ def dao_set_created_live_letter_api_notifications_to_pending():
|
|||||||
).all()
|
).all()
|
||||||
|
|
||||||
for notification in notifications:
|
for notification in notifications:
|
||||||
notification.notification_status = NOTIFICATION_PENDING
|
notification.status = NOTIFICATION_PENDING
|
||||||
|
|
||||||
db.session.add_all(notifications)
|
db.session.add_all(notifications)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ from app.celery.scheduled_tasks import (
|
|||||||
remove_transformed_dvla_files,
|
remove_transformed_dvla_files,
|
||||||
run_scheduled_jobs,
|
run_scheduled_jobs,
|
||||||
run_letter_jobs,
|
run_letter_jobs,
|
||||||
|
run_letter_api_notifications,
|
||||||
s3,
|
s3,
|
||||||
send_daily_performance_platform_stats,
|
send_daily_performance_platform_stats,
|
||||||
send_scheduled_notifications,
|
send_scheduled_notifications,
|
||||||
@@ -41,6 +42,11 @@ from app.models import (
|
|||||||
Service, Template,
|
Service, Template,
|
||||||
SMS_TYPE, LETTER_TYPE,
|
SMS_TYPE, LETTER_TYPE,
|
||||||
JOB_STATUS_READY_TO_SEND,
|
JOB_STATUS_READY_TO_SEND,
|
||||||
|
JOB_STATUS_IN_PROGRESS,
|
||||||
|
JOB_STATUS_SENT_TO_DVLA,
|
||||||
|
NOTIFICATION_PENDING,
|
||||||
|
NOTIFICATION_CREATED,
|
||||||
|
KEY_TYPE_TEST,
|
||||||
MonthlyBilling)
|
MonthlyBilling)
|
||||||
from app.utils import get_london_midnight_in_utc
|
from app.utils import get_london_midnight_in_utc
|
||||||
from tests.app.db import create_notification, create_service, create_template, create_job, create_rate
|
from tests.app.db import create_notification, create_service, create_template, create_job, create_rate
|
||||||
@@ -693,3 +699,73 @@ def test_run_letter_jobs(client, mocker, sample_letter_template):
|
|||||||
mock_celery.assert_called_once_with(name=TaskNames.DVLA_JOBS,
|
mock_celery.assert_called_once_with(name=TaskNames.DVLA_JOBS,
|
||||||
args=(job_ids,),
|
args=(job_ids,),
|
||||||
queue=QueueNames.PROCESS_FTP)
|
queue=QueueNames.PROCESS_FTP)
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_letter_jobs_does_nothing_if_no_ready_jobs(client, mocker, sample_letter_template):
|
||||||
|
job_ids = [
|
||||||
|
str(create_job(sample_letter_template, job_status=JOB_STATUS_IN_PROGRESS).id),
|
||||||
|
str(create_job(sample_letter_template, job_status=JOB_STATUS_SENT_TO_DVLA).id)
|
||||||
|
]
|
||||||
|
|
||||||
|
mock_celery = mocker.patch("app.celery.tasks.notify_celery.send_task")
|
||||||
|
|
||||||
|
run_letter_jobs()
|
||||||
|
|
||||||
|
assert not mock_celery.called
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_letter_api_notifications_triggers_ftp_task(client, mocker, sample_letter_notification):
|
||||||
|
file_contents_mock = mocker.patch(
|
||||||
|
'app.celery.scheduled_tasks.create_dvla_file_contents_for_notifications',
|
||||||
|
return_value='foo\nbar'
|
||||||
|
)
|
||||||
|
s3upload = mocker.patch('app.celery.scheduled_tasks.s3upload')
|
||||||
|
mock_celery = mocker.patch('app.celery.tasks.notify_celery.send_task')
|
||||||
|
filename = '2017-01-01T12:00:00-dvla-notifications.txt'
|
||||||
|
|
||||||
|
with freeze_time('2017-01-01 12:00:00'):
|
||||||
|
run_letter_api_notifications()
|
||||||
|
|
||||||
|
assert sample_letter_notification.status == NOTIFICATION_PENDING
|
||||||
|
file_contents_mock.assert_called_once_with([sample_letter_notification])
|
||||||
|
s3upload.assert_called_once_with(
|
||||||
|
# with trailing new line added
|
||||||
|
filedata='foo\nbar\n',
|
||||||
|
region='eu-west-1',
|
||||||
|
bucket_name='test-dvla-letter-api-files',
|
||||||
|
file_location=filename
|
||||||
|
)
|
||||||
|
mock_celery.assert_called_once_with(
|
||||||
|
name=TaskNames.DVLA_NOTIFICATIONS,
|
||||||
|
kwargs={'filename': filename},
|
||||||
|
queue=QueueNames.PROCESS_FTP
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_run_letter_api_notifications_does_nothing_if_no_created_notifications(
|
||||||
|
client,
|
||||||
|
mocker,
|
||||||
|
sample_letter_template,
|
||||||
|
sample_letter_job,
|
||||||
|
sample_api_key
|
||||||
|
):
|
||||||
|
letter_job_notification = create_notification(
|
||||||
|
sample_letter_template,
|
||||||
|
job=sample_letter_job
|
||||||
|
)
|
||||||
|
pending_letter_notification = create_notification(
|
||||||
|
sample_letter_template,
|
||||||
|
status=NOTIFICATION_PENDING,
|
||||||
|
api_key=sample_api_key
|
||||||
|
)
|
||||||
|
test_api_key_notification = create_notification(
|
||||||
|
sample_letter_template,
|
||||||
|
key_type=KEY_TYPE_TEST
|
||||||
|
)
|
||||||
|
|
||||||
|
mock_celery = mocker.patch('app.celery.tasks.notify_celery.send_task')
|
||||||
|
|
||||||
|
run_letter_api_notifications()
|
||||||
|
|
||||||
|
assert not mock_celery.called
|
||||||
|
assert letter_job_notification.status == NOTIFICATION_CREATED
|
||||||
|
assert test_api_key_notification.status == NOTIFICATION_CREATED
|
||||||
|
|||||||
Reference in New Issue
Block a user