mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
Add a limit days query param for get all jobs.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from datetime import date, timedelta
|
||||
|
||||
from sqlalchemy import desc
|
||||
from app import db
|
||||
from app.models import Job
|
||||
@@ -7,8 +9,11 @@ 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()
|
||||
|
||||
|
||||
def dao_get_jobs_by_service_id(service_id):
|
||||
return Job.query.filter_by(service_id=service_id).order_by(desc(Job.created_at)).all()
|
||||
def dao_get_jobs_by_service_id(service_id, limit_days=None):
|
||||
query_filter = [Job.service_id == service_id]
|
||||
if limit_days is not None:
|
||||
query_filter.append(Job.created_at >= _days_ago(limit_days))
|
||||
return Job.query.filter(*query_filter).order_by(desc(Job.created_at)).all()
|
||||
|
||||
|
||||
def dao_get_job_by_id(job_id):
|
||||
@@ -23,3 +28,7 @@ def dao_create_job(job):
|
||||
def dao_update_job(job):
|
||||
db.session.add(job)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def _days_ago(number_of_days):
|
||||
return date.today() - timedelta(days=number_of_days)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
from flask import (
|
||||
Blueprint,
|
||||
jsonify,
|
||||
request
|
||||
)
|
||||
request,
|
||||
current_app)
|
||||
|
||||
from app.dao.jobs_dao import (
|
||||
dao_create_job,
|
||||
@@ -36,7 +36,17 @@ def get_job_by_service_and_job_id(service_id, job_id):
|
||||
|
||||
@job.route('', methods=['GET'])
|
||||
def get_jobs_by_service(service_id):
|
||||
jobs = dao_get_jobs_by_service_id(service_id)
|
||||
if request.args.get('limit_days'):
|
||||
try:
|
||||
limit_days = int(request.args['limit_days'])
|
||||
except ValueError as e:
|
||||
error = '{} is not an integer'.format(request.args['limit_days'])
|
||||
current_app.logger.error(error)
|
||||
return jsonify(result="error", message={'limit_days': [error]}), 400
|
||||
else:
|
||||
limit_days = None
|
||||
|
||||
jobs = dao_get_jobs_by_service_id(service_id, limit_days)
|
||||
data, errors = job_schema.dump(jobs, many=True)
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from datetime import datetime
|
||||
from datetime import (datetime, timedelta)
|
||||
import uuid
|
||||
|
||||
from app.dao.jobs_dao import (
|
||||
@@ -64,6 +64,25 @@ def test_get_jobs_for_service(notify_db, notify_db_session, sample_template):
|
||||
assert one_job_from_db != other_job_from_db
|
||||
|
||||
|
||||
def test_get_jobs_for_service(notify_db, notify_db_session, sample_template):
|
||||
from tests.app.conftest import sample_job as create_job
|
||||
|
||||
one_job = create_job(notify_db, notify_db_session, sample_template.service, sample_template)
|
||||
old_job = create_job(notify_db, notify_db_session, sample_template.service, sample_template,
|
||||
created_at=datetime.now() - timedelta(days=8))
|
||||
|
||||
jobs = dao_get_jobs_by_service_id(one_job.service_id)
|
||||
|
||||
assert len(jobs) == 2
|
||||
assert one_job in jobs
|
||||
assert old_job in jobs
|
||||
|
||||
jobs_limit_days = dao_get_jobs_by_service_id(one_job.service_id, limit_days=7)
|
||||
assert len(jobs_limit_days) == 1
|
||||
assert one_job in jobs_limit_days
|
||||
assert old_job not in jobs_limit_days
|
||||
|
||||
|
||||
def test_get_jobs_for_service_in_created_at_order(notify_db, notify_db_session, sample_template):
|
||||
from tests.app.conftest import sample_job as create_job
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import json
|
||||
import uuid
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import app.celery.tasks
|
||||
|
||||
from tests import create_authorization_header
|
||||
@@ -22,6 +24,32 @@ def test_get_jobs(notify_api, notify_db, notify_db_session, sample_template):
|
||||
assert len(resp_json['data']) == 5
|
||||
|
||||
|
||||
def test_get_jobs_with_limit_days(notify_api, notify_db, notify_db_session, sample_template):
|
||||
create_job(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_template.service,
|
||||
template=sample_template,
|
||||
)
|
||||
create_job(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_template.service,
|
||||
template=sample_template,
|
||||
created_at=datetime.now() - timedelta(days=7))
|
||||
|
||||
service_id = sample_template.service.id
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
path = '/service/{}/job'.format(service_id)
|
||||
auth_header = create_authorization_header(service_id=service_id)
|
||||
response = client.get(path, headers=[auth_header], query_string={'limit_days': 5})
|
||||
assert response.status_code == 200
|
||||
resp_json = json.loads(response.get_data(as_text=True))
|
||||
assert len(resp_json['data']) == 1
|
||||
|
||||
|
||||
def test_get_job_with_invalid_service_id_returns404(notify_api, sample_api_key, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
Reference in New Issue
Block a user