From 83a7aa7533f8f4ecac049dc0e93aff6429e6e5ae Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 18 Feb 2022 07:17:50 +0300 Subject: Test partial correlations endpoint with non-existent primary traits Test that the partial correlations endpoint responds with an appropriate "not-found" message and the corresponding 404 status code in the case where a request is made and the primary trait requested for does not exist in the database. Summary of the changes in each file: * gn3/api/correlation.py: generalise the building of the response * gn3/computations/partial_correlations.py: return with a "not-found" if the primary trait does not exist in the database * gn3/db/partial_correlations.py: Fix a number of bugs that led to exceptions in the case that the primary trait did not exist * pytest.ini: register a `slow` pytest marker * tests/integration/test_partial_correlations.py: Add a new test to check for an appropriate 404 response in case of a primary trait that does not exist in the database. --- gn3/api/correlation.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'gn3/api') diff --git a/gn3/api/correlation.py b/gn3/api/correlation.py index 57b808e..cbe01d8 100644 --- a/gn3/api/correlation.py +++ b/gn3/api/correlation.py @@ -118,25 +118,25 @@ def partial_correlation(): return str(o, encoding="utf-8") return json.JSONEncoder.default(self, o) + def __build_response__(data): + status_codes = {"error": 400, "not-found": 404, "success": 200} + response = make_response( + json.dumps(data, cls=OutputEncoder), + status_codes[data["status"]]) + response.headers["Content-Type"] = "application/json" + return response + 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 + return __build_response__({ + "status": "error", + "messages": request_errors, + "error_type": "Client Error"}) 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.get("criteria", 500)), args["target_db"]) - response = make_response( - json.dumps(corr_results, cls=OutputEncoder), - 400 if "error" in corr_results.keys() else 200) - response.headers["Content-Type"] = "application/json" - return response + return __build_response__(corr_results) -- cgit v1.2.3