Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ def create_app(config_name):
from app.user.rest import user as user_blueprint
from app.template.rest import template as template_blueprint
from app.status.healthcheck import status as status_blueprint
from app.job.rest import job as job_blueprint

application.register_blueprint(service_blueprint, url_prefix='/service')
application.register_blueprint(user_blueprint, url_prefix='/user')
application.register_blueprint(template_blueprint, url_prefix="/template")
application.register_blueprint(status_blueprint, url_prefix='/status')
application.register_blueprint(job_blueprint, url_prefix='/job')

return application

Expand Down
4 changes: 2 additions & 2 deletions app/dao/jobs_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def save_job(job):
db.session.commit()


def get_job_by_id(id):
return Job.query.get(id)
def get_job_by_id(job_id):
return Job.query.filter_by(id=job_id).one()


def get_jobs_by_service(service_id):
Expand Down
51 changes: 51 additions & 0 deletions app/job/rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from flask import (
Blueprint,
jsonify,
request
)

from sqlalchemy.exc import DataError
from sqlalchemy.orm.exc import NoResultFound

from app.dao.jobs_dao import (
save_job,
get_job_by_id,
get_jobs
)

from app.schemas import (
job_schema,
jobs_schema
)

job = Blueprint('job', __name__)


@job.route('/<job_id>', methods=['GET'])
@job.route('', methods=['GET'])
def get_job(job_id=None):
if job_id:
try:
job = get_job_by_id(job_id)
data, errors = job_schema.dump(job)
return jsonify(data=data)
except DataError:
return jsonify(result="error", message="Invalid job id"), 400
except NoResultFound:
return jsonify(result="error", message="Job not found"), 404
else:
jobs = get_jobs()
data, errors = jobs_schema.dump(jobs)
return jsonify(data=data)


@job.route('', methods=['POST'])
def create_job():
job, errors = job_schema.load(request.get_json())
if errors:
return jsonify(result="error", message=errors), 400
try:
save_job(job)
except Exception as e:
return jsonify(result="error", message=str(e)), 500
return jsonify(data=job_schema.dump(job).data), 201
1 change: 1 addition & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class Job(db.Model):
service_id = db.Column(db.BigInteger, db.ForeignKey('services.id'), index=True, unique=False)
service = db.relationship('Service', backref=db.backref('jobs', lazy='dynamic'))
template_id = db.Column(db.BigInteger, db.ForeignKey('templates.id'), index=True, unique=False)
template = db.relationship('Template', backref=db.backref('jobs', lazy='dynamic'))
created_at = db.Column(
db.DateTime,
index=False,
Expand Down
9 changes: 8 additions & 1 deletion app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Meta:
class TemplateSchema(ma.ModelSchema):
class Meta:
model = models.Template
exclude = ("updated_at", "created_at", "service_id")
exclude = ("updated_at", "created_at", "service_id", "jobs")


class TokenSchema(ma.ModelSchema):
Expand All @@ -34,6 +34,11 @@ class Meta:
exclude = ["service"]


class JobSchema(ma.ModelSchema):
class Meta:
model = models.Job


user_schema = UserSchema()
users_schema = UserSchema(many=True)
service_schema = ServiceSchema()
Expand All @@ -42,3 +47,5 @@ class Meta:
templates_schema = TemplateSchema(many=True)
token_schema = TokenSchema()
tokens_schema = TokenSchema(many=True)
job_schema = JobSchema()
jobs_schema = JobSchema(many=True)
Empty file added tests/app/job/__init__.py
Empty file.
88 changes: 88 additions & 0 deletions tests/app/job/test_job_rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import json
import uuid

from flask import url_for

from tests.app.conftest import sample_job as create_job


def test_get_jobs(notify_api, notify_db, notify_db_session, sample_template):

_setup_jobs(notify_db, notify_db_session, sample_template)

with notify_api.test_request_context():
with notify_api.test_client() as client:
response = client.get(url_for('job.get_job'))
assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True))
assert len(resp_json['data']) == 5


def test_get_job_with_invalid_id_returns400(notify_api, notify_db,
notify_db_session,
sample_template):
with notify_api.test_request_context():
with notify_api.test_client() as client:
response = client.get(url_for('job.get_job', job_id='invalid_id'))
assert response.status_code == 400
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json == {'message': 'Invalid job id',
'result': 'error'}


def test_get_job_with_unknown_id_returns404(notify_api, notify_db,
notify_db_session,
sample_template):
random_id = str(uuid.uuid4())
with notify_api.test_request_context():
with notify_api.test_client() as client:
response = client.get(url_for('job.get_job', job_id=random_id))
assert response.status_code == 404
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json == {'message': 'Job not found', 'result':
'error'}


def test_get_job_by_id(notify_api, notify_db, notify_db_session,
sample_job):
job_id = str(sample_job.id)
with notify_api.test_request_context():
with notify_api.test_client() as client:
response = client.get(url_for('job.get_job', job_id=job_id))
assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['data']['id'] == job_id


def test_post_job(notify_api, notify_db, notify_db_session, sample_template):
job_id = uuid.uuid4()
template_id = sample_template.id
service_id = sample_template.service.id
original_file_name = 'thisisatest.csv'
data = {
'id': str(job_id),
'service': service_id,
'template': template_id,
'original_file_name': original_file_name
}
headers = [('Content-Type', 'application/json')]
with notify_api.test_request_context():
with notify_api.test_client() as client:
response = client.post(
url_for('job.create_job'),
data=json.dumps(data),
headers=headers)
assert response.status_code == 201

resp_json = json.loads(response.get_data(as_text=True))

assert resp_json['data']['id'] == str(job_id)
assert resp_json['data']['service'] == service_id
assert resp_json['data']['template'] == template_id
assert resp_json['data']['original_file_name'] == original_file_name


def _setup_jobs(notify_db, notify_db_session, template, number_of_jobs=5):
for i in range(number_of_jobs):
create_job(notify_db, notify_db_session, service=template.service,
template=template)