2016-01-15 11:12:05 +00:00
|
|
|
import uuid
|
2016-02-04 20:55:09 +00:00
|
|
|
import json
|
2016-01-15 11:12:05 +00:00
|
|
|
|
|
|
|
|
from app.dao.jobs_dao import (
|
|
|
|
|
save_job,
|
2016-01-18 09:57:04 +00:00
|
|
|
get_job,
|
2016-01-15 11:12:05 +00:00
|
|
|
get_jobs_by_service,
|
2016-01-18 09:57:04 +00:00
|
|
|
_get_jobs
|
2016-01-15 11:12:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
from app.models import Job
|
|
|
|
|
|
|
|
|
|
|
2016-01-15 12:16:07 +00:00
|
|
|
def test_save_job(notify_db, notify_db_session, sample_template):
|
2016-01-15 11:12:05 +00:00
|
|
|
|
|
|
|
|
assert Job.query.count() == 0
|
2016-01-16 10:14:48 +00:00
|
|
|
|
2016-01-15 11:12:05 +00:00
|
|
|
job_id = uuid.uuid4()
|
2016-01-16 10:14:48 +00:00
|
|
|
bucket_name = 'service-{}-notify'.format(sample_template.service.id)
|
|
|
|
|
file_name = '{}.csv'.format(job_id)
|
2016-01-15 11:12:05 +00:00
|
|
|
data = {
|
|
|
|
|
'id': job_id,
|
2016-01-16 10:14:48 +00:00
|
|
|
'service_id': sample_template.service.id,
|
2016-01-15 12:16:07 +00:00
|
|
|
'template_id': sample_template.id,
|
2016-01-16 10:14:48 +00:00
|
|
|
'bucket_name': bucket_name,
|
|
|
|
|
'file_name': file_name,
|
2016-02-22 14:56:09 +00:00
|
|
|
'original_file_name': 'some.csv',
|
|
|
|
|
'notification_count': 1
|
2016-01-15 11:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
job = Job(**data)
|
|
|
|
|
save_job(job)
|
|
|
|
|
|
|
|
|
|
assert Job.query.count() == 1
|
|
|
|
|
job_from_db = Job.query.get(job_id)
|
|
|
|
|
assert job == job_from_db
|
|
|
|
|
|
|
|
|
|
|
2016-01-16 10:14:48 +00:00
|
|
|
def test_get_job_by_id(notify_db, notify_db_session, sample_job):
|
2016-01-18 09:57:04 +00:00
|
|
|
job_from_db = get_job(sample_job.service.id, sample_job.id)
|
2016-01-16 10:14:48 +00:00
|
|
|
assert sample_job == job_from_db
|
2016-01-15 11:12:05 +00:00
|
|
|
|
|
|
|
|
|
2016-01-18 09:57:04 +00:00
|
|
|
def test_get_jobs_for_service(notify_db, notify_db_session, sample_template):
|
2016-01-15 11:12:05 +00:00
|
|
|
|
2016-01-18 09:57:04 +00:00
|
|
|
from tests.app.conftest import sample_job as create_job
|
|
|
|
|
from tests.app.conftest import sample_service as create_service
|
|
|
|
|
from tests.app.conftest import sample_template as create_template
|
|
|
|
|
from tests.app.conftest import sample_user as create_user
|
|
|
|
|
|
|
|
|
|
one_job = create_job(notify_db, notify_db_session, sample_template.service,
|
|
|
|
|
sample_template)
|
|
|
|
|
|
|
|
|
|
other_user = create_user(notify_db, notify_db_session,
|
|
|
|
|
email="test@digital.cabinet-office.gov.uk")
|
|
|
|
|
other_service = create_service(notify_db, notify_db_session,
|
2016-01-22 14:43:30 +00:00
|
|
|
user=other_user, service_name="other service")
|
2016-01-18 09:57:04 +00:00
|
|
|
other_template = create_template(notify_db, notify_db_session,
|
|
|
|
|
service=other_service)
|
|
|
|
|
other_job = create_job(notify_db, notify_db_session, service=other_service,
|
|
|
|
|
template=other_template)
|
|
|
|
|
|
|
|
|
|
one_job_from_db = get_jobs_by_service(one_job.service_id)
|
|
|
|
|
other_job_from_db = get_jobs_by_service(other_job.service_id)
|
|
|
|
|
|
|
|
|
|
assert len(one_job_from_db) == 1
|
|
|
|
|
assert one_job == one_job_from_db[0]
|
|
|
|
|
|
|
|
|
|
assert len(other_job_from_db) == 1
|
|
|
|
|
assert other_job == other_job_from_db[0]
|
2016-01-15 11:12:05 +00:00
|
|
|
|
2016-01-18 09:57:04 +00:00
|
|
|
assert one_job_from_db != other_job_from_db
|
2016-01-15 11:12:05 +00:00
|
|
|
|
|
|
|
|
|
2016-01-15 12:16:07 +00:00
|
|
|
def test_get_all_jobs(notify_db, notify_db_session, sample_template):
|
|
|
|
|
from tests.app.conftest import sample_job as create_job
|
2016-01-15 11:12:05 +00:00
|
|
|
for i in range(5):
|
2016-01-15 12:16:07 +00:00
|
|
|
create_job(notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
sample_template.service,
|
|
|
|
|
sample_template)
|
2016-01-18 09:57:04 +00:00
|
|
|
jobs_from_db = _get_jobs()
|
2016-01-15 12:16:07 +00:00
|
|
|
assert len(jobs_from_db) == 5
|
2016-02-04 20:55:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_job(notify_db, notify_db_session, sample_job):
|
|
|
|
|
assert sample_job.status == 'pending'
|
|
|
|
|
|
|
|
|
|
update_dict = {
|
|
|
|
|
'id': sample_job.id,
|
|
|
|
|
'service': sample_job.service.id,
|
|
|
|
|
'template': sample_job.template.id,
|
|
|
|
|
'bucket_name': sample_job.bucket_name,
|
|
|
|
|
'file_name': sample_job.file_name,
|
|
|
|
|
'original_file_name': sample_job.original_file_name,
|
|
|
|
|
'status': 'in progress'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
save_job(sample_job, update_dict=update_dict)
|
|
|
|
|
|
|
|
|
|
job_from_db = Job.query.get(sample_job.id)
|
|
|
|
|
|
|
|
|
|
assert job_from_db.status == 'in progress'
|