diff --git a/policyengine_api/routes/household_routes.py b/policyengine_api/routes/household_routes.py index 330ef489..55e9bbc9 100644 --- a/policyengine_api/routes/household_routes.py +++ b/policyengine_api/routes/household_routes.py @@ -33,6 +33,8 @@ def get_household(country_id: str, household_id: int) -> Response: household: dict | None = household_service.get_household( country_id, household_id ) + print("Household in get_household:") + print(household) if household is None: raise NotFound(f"Household #{household_id} not found.") else: @@ -47,6 +49,8 @@ def get_household(country_id: str, household_id: int) -> Response: status=200, mimetype="application/json", ) + except NotFound: + raise except Exception as e: raise InternalServerError( f"An error occurred while fetching household #{household_id}. Details: {str(e)}" @@ -92,7 +96,8 @@ def post_household(country_id: str) -> Response: status=201, mimetype="application/json", ) - + except BadRequest: + raise except Exception as e: raise InternalServerError( f"An error occurred while creating a new household. Details: {str(e)}" @@ -150,6 +155,10 @@ def update_household(country_id: str, household_id: int) -> Response: status=200, mimetype="application/json", ) + except BadRequest: + raise + except NotFound: + raise except Exception as e: raise InternalServerError( f"An error occurred while updating household #{household_id}. Details: {str(e)}" diff --git a/policyengine_api/routes/simulation_analysis_routes.py b/policyengine_api/routes/simulation_analysis_routes.py index b54c494f..b3299d72 100644 --- a/policyengine_api/routes/simulation_analysis_routes.py +++ b/policyengine_api/routes/simulation_analysis_routes.py @@ -68,6 +68,8 @@ def execute_simulation_analysis(country_id): response.headers["X-Accel-Buffering"] = "no" return response + except BadRequest: + raise except Exception as e: raise InternalServerError( f"An error occurred while executing the simulation analysis. Details: {str(e)}" diff --git a/policyengine_api/routes/tracer_analysis_routes.py b/policyengine_api/routes/tracer_analysis_routes.py index 14b055d1..95524aa3 100644 --- a/policyengine_api/routes/tracer_analysis_routes.py +++ b/policyengine_api/routes/tracer_analysis_routes.py @@ -46,6 +46,8 @@ def execute_tracer_analysis(country_id): response.headers["X-Accel-Buffering"] = "no" return response + except BadRequest: + raise except KeyError as e: """ This exception is raised when the tracer can't find a household tracer record diff --git a/policyengine_api/services/household_service.py b/policyengine_api/services/household_service.py index 5215f404..418026d7 100644 --- a/policyengine_api/services/household_service.py +++ b/policyengine_api/services/household_service.py @@ -23,10 +23,14 @@ def get_household(self, country_id: str, household_id: int) -> dict | None: f"SELECT * FROM household WHERE id = ? AND country_id = ?", (household_id, country_id), ).fetchone() + print("Row:") + print(row) + print("Dict of row:") # If row is present, we must JSON.loads the household_json household = None if row is not None: + print("Row is not None") household = dict(row) if household["household_json"]: household["household_json"] = json.loads( diff --git a/tests/python/test_household.py b/tests/python/test_household.py index 2a71279b..aa0d22f3 100644 --- a/tests/python/test_household.py +++ b/tests/python/test_household.py @@ -38,7 +38,11 @@ def test_get_nonexistent_household(self, rest_client, mock_database): mock_database.query().fetchone.return_value = None response = rest_client.get("/us/household/999") + print("Response:") + print(response) data = json.loads(response.data) + print("Data:") + print(data) assert response.status_code == 404 assert data["status"] == "error" @@ -49,7 +53,9 @@ def test_get_household_invalid_id(self, rest_client): response = rest_client.get("/us/household/invalid") assert response.status_code == 404 - assert b"Invalid household ID" in response.data + assert ( + b"The requested URL was not found on the server" in response.data + ) class TestCreateHousehold: @@ -255,7 +261,9 @@ def test_get_household_invalid_id(self, rest_client, invalid_id): # Default Werkzeug validation returns 404, not 400 assert response.status_code == 404 - assert b"Invalid household ID" in response.data + assert ( + b"The requested URL was not found on the server" in response.data + ) @pytest.mark.parametrize( "country_id",