aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2022-02-14 14:35:29 +0300
committerFrederick Muriuki Muriithi2022-02-17 08:23:05 +0300
commit12db8134081bc679565dfff1aaa2da81f913dd9d (patch)
tree755235e6d08057cc592f269cc1749288bd71fb01
parent9879964c42a5f9373fcfa2b3685cdd74cf3ea176 (diff)
downloadgenenetwork3-12db8134081bc679565dfff1aaa2da81f913dd9d.tar.gz
Test partial correlations endpoint with missing data in POST request
Add a test for the partial correlations endpoint, with: - no data in the request - missing items in the data Fix the bugs caught by the test
-rw-r--r--gn3/api/correlation.py28
-rw-r--r--tests/integration/test_partial_correlations.py90
2 files changed, 117 insertions, 1 deletions
diff --git a/gn3/api/correlation.py b/gn3/api/correlation.py
index b46855f..57b808e 100644
--- a/gn3/api/correlation.py
+++ b/gn3/api/correlation.py
@@ -3,6 +3,7 @@ import json
from flask import jsonify
from flask import Blueprint
from flask import request
+from functools import reduce
from flask import make_response
from gn3.computations.correlations import compute_all_sample_correlation
@@ -93,6 +94,20 @@ def partial_correlation():
def trait_fullname(trait):
return f"{trait['dataset']}::{trait['name']}"
+ def __field_errors__(args):
+ def __check__(acc, field):
+ if args.get(field) is None:
+ return acc + (f"Field '{field}' missing",)
+ return acc
+ return __check__
+
+ def __errors__(request_data, fields):
+ errors = tuple()
+ if request_data is None:
+ return ("No request data",)
+
+ return reduce(__field_errors__(args), fields, errors)
+
class OutputEncoder(json.JSONEncoder):
"""
Class to encode output into JSON, for objects which the default
@@ -104,11 +119,22 @@ def partial_correlation():
return json.JSONEncoder.default(self, o)
args = request.get_json()
+ request_errors = __errors__(
+ args, ("primary_trait", "control_traits", "target_db", "method"))
+ if request_errors:
+ response = make_response(
+ json.dumps({
+ "status": "error",
+ "messages": request_errors,
+ "error_type": "Client Error"}),
+ 400)
+ response.headers["Content-Type"] = "application/json"
+ return response
conn, _cursor_object = database_connector()
corr_results = partial_correlations_entry(
conn, trait_fullname(args["primary_trait"]),
tuple(trait_fullname(trait) for trait in args["control_traits"]),
- args["method"], int(args["criteria"]), args["target_db"])
+ args["method"], int(args.get("criteria", 500)), args["target_db"])
response = make_response(
json.dumps(corr_results, cls=OutputEncoder),
400 if "error" in corr_results.keys() else 200)
diff --git a/tests/integration/test_partial_correlations.py b/tests/integration/test_partial_correlations.py
new file mode 100644
index 0000000..5b520e0
--- /dev/null
+++ b/tests/integration/test_partial_correlations.py
@@ -0,0 +1,90 @@
+"""Test partial correlations"""
+import pytest
+
+from tests.integration.conftest import client
+
+@pytest.mark.integration_test
+@pytest.mark.parametrize(
+ "post_data", (
+ None, {}, {
+ "primary_trait": None,
+ "control_traits": None,
+ "method": None,
+ "target_db": None
+ }, {
+ "primary_trait": None,
+ "control_traits": None,
+ "method": None,
+ "target_db": "a_db"
+ }, {
+ "primary_trait": None,
+ "control_traits": None,
+ "method": "a_method",
+ "target_db": None
+ }, {
+ "primary_trait": None,
+ "control_traits": None,
+ "method": "a_method",
+ "target_db": "a_db"
+ }, {
+ "primary_trait": None,
+ "control_traits": ["a_trait", "another"],
+ "method": None,
+ "target_db": None
+ }, {
+ "primary_trait": None,
+ "control_traits": ["a_trait", "another"],
+ "method": None,
+ "target_db": "a_db"
+ }, {
+ "primary_trait": None,
+ "control_traits": ["a_trait", "another"],
+ "method": "a_method",
+ "target_db": None
+ }, {
+ "primary_trait": None,
+ "control_traits": ["a_trait", "another"],
+ "method": "a_method",
+ "target_db": "a_db"
+ }, {
+ "primary_trait": "a_trait",
+ "control_traits": None,
+ "method": None,
+ "target_db": None
+ }, {
+ "primary_trait": "a_trait",
+ "control_traits": None,
+ "method": None,
+ "target_db": "a_db"
+ }, {
+ "primary_trait": "a_trait",
+ "control_traits": None,
+ "method": "a_method",
+ "target_db": None
+ }, {
+ "primary_trait": "a_trait",
+ "control_traits": None,
+ "method": "a_method",
+ "target_db": "a_db"
+ }, {
+ "primary_trait": "a_trait",
+ "control_traits": ["a_trait", "another"],
+ "method": None,
+ "target_db": None
+ }, {
+ "primary_trait": "a_trait",
+ "control_traits": ["a_trait", "another"],
+ "method": None,
+ "target_db": "a_db"
+ }, {
+ "primary_trait": "a_trait",
+ "control_traits": ["a_trait", "another"],
+ "method": "a_method",
+ "target_db": None
+ }))
+def test_partial_correlation_api_with_missing_request_data(client, post_data):
+ "Test /api/correlations/partial"
+ response = client.post("/api/correlation/partial", json=post_data)
+ assert (
+ response.status_code == 400 and response.is_json and
+ response.json.get("status") == "error")