Skip to content

Commit

Permalink
started with the query endpoint and request's errors
Browse files Browse the repository at this point in the history
  • Loading branch information
northwestwitch committed Apr 23, 2020
1 parent f50ca56 commit cd7c7be
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 3 deletions.
1 change: 1 addition & 0 deletions cgbeacon2/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .consent_codes import CONSENT_CODES
from .variant_constants import CHROMOSOMES
from .response_objs import MISSING_PARAMS_ERROR
7 changes: 7 additions & 0 deletions cgbeacon2/constants/response_objs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Error objects
MISSING_PARAMS_ERROR = dict(
error=dict(
errorCode=400,
errorMessage="Missing one or more mandatory parameters (referenceName, referenceBases, assemblyId)"
)
)
34 changes: 34 additions & 0 deletions cgbeacon2/server/blueprints/api_v1/controllers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
import logging
from flask import request
from cgbeacon2.constants import MISSING_PARAMS_ERROR

LOG = logging.getLogger(__name__)

def set_query(req):
"""Create a a query dictionary from GET or POST requests sent to the query endpoint.
Accepts:
req(flask.request)
Returns:
query(dict)
"""
data = None
query = {}
if request.method == "GET":
data = req.args
else: # POST method
data = req.data

# check if the minimal required params were provided in query
if None in [data.get("referenceName"), data.get("referenceBases"), data.get("assemblyId")]:
# return a bad request 400 error with explanation message
return MISSING_PARAMS_ERROR


query = dict(

)

return data
23 changes: 20 additions & 3 deletions cgbeacon2/server/blueprints/api_v1/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# -*- coding: utf-8 -*-
import logging
from flask import Blueprint, current_app, jsonify
from flask import Blueprint, current_app, jsonify, request
from cgbeacon2.models import Beacon
from .controllers import set_query

LOG = logging.getLogger(__name__)

api1_bp = Blueprint("api_v1", __name__,)


@api1_bp.route("/apiv1.0/", methods=["GET"])
def info():
"""Returns Beacon info data as a json object"""
Expand All @@ -18,3 +17,21 @@ def info():
resp = jsonify(beacon_obj.__dict__)
resp.status_code = 200
return resp


@api1_bp.route("/apiv1.0/query", methods=["GET", "POST"])
def query():
"""Create a query from params provided in the request and return a response with eventual results"""

resp_obj = {}
# test query: http://127.0.0.1:5000/apiv1.0/query?assemblyId=GRCh37&referenceName=1&start=1138913&alternateBases=T&includeDatasetResponses=true
query = set_query(request)

if "error" in query:
# query was not valid return error response
resp_obj["message"] = dict(query)
resp = jsonify(resp_obj)
resp.status_code = query["error"]["errorCode"]
return resp

return "HELLO THERE"
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ def mock_app(database):
return app


@pytest.fixture
def query_snv():
"""A dictionary containing test query params"""
query = dict(
assembly = "GRCh37",
chrom = "1",
start = 235850063,
ref = "C",
alt = "A",
include_ds_resp = "ALL"
)
return query


@pytest.fixture
def test_dataset_cli():
"""A test dataset dictionary"""
Expand Down
26 changes: 26 additions & 0 deletions tests/server/blueprints/api_v1/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import json
from cgbeacon2.constants import MISSING_PARAMS_ERROR


def test_info(mock_app):
Expand All @@ -14,3 +15,28 @@ def test_info(mock_app):
fields = ["id", "name", "apiVersion", "organisation", "datasets"]
for field in fields:
assert data[field] is not None


def test_query_get_request_missing_params(mock_app):
"""Test the query endpoint by sending a request without required params"""

# When a request missing one or more required params is sent to the server
response = mock_app.test_client().get("/apiv1.0/query?")

# Then it should return error
assert response.status_code == 400
data = json.loads(response.data)
assert data["message"]["error"] == MISSING_PARAMS_ERROR["error"]



"""
def test_query_get_request(mock_app, query_snv):
Test the query endpoint by sending a GET request
query_string = f'query?assemblyId={query_snv["assembly"]}&referenceName={query_snv["chrom"]}&start={query_snv["start"]}&alternateBases={query_snv["alt"]}&includeDatasetResponses={query_snv["include_ds_resp"]}'
response = mock_app.test_client().get("".join(["/apiv1.0/", query_string]))
assert response.status_code == 200
"""

0 comments on commit cd7c7be

Please sign in to comment.