Skip to content

Commit

Permalink
fix: Fix final household tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anth-volk committed Dec 14, 2024
1 parent a40e430 commit 7cc72fe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
2 changes: 0 additions & 2 deletions policyengine_api/routes/household_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ 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:
Expand Down
6 changes: 1 addition & 5 deletions policyengine_api/services/household_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ 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(
Expand Down Expand Up @@ -91,7 +87,7 @@ def create_household(
def update_household(
self,
country_id: str,
household_id: str,
household_id: int,
household_json: dict,
label: str,
) -> dict:
Expand Down
41 changes: 30 additions & 11 deletions tests/python/test_household.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from policyengine_api.routes.household_routes import household_bp
from policyengine_api.services.household_service import HouseholdService
from policyengine_api.constants import COUNTRY_PACKAGE_VERSIONS

from tests.fixtures.household_fixtures import (
SAMPLE_HOUSEHOLD_DATA,
Expand Down Expand Up @@ -38,11 +39,7 @@ 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"
Expand Down Expand Up @@ -123,9 +120,13 @@ def test_update_household_success(
mock_row.keys.return_value = SAMPLE_DB_ROW.keys()
mock_database.query().fetchone.return_value = mock_row

updated_household = {
"people": {"person1": {"age": 31, "income": 55000}}
}

updated_data = {
"data": {"people": {"person1": {"age": 31, "income": 55000}}},
"label": "Updated Test Household",
"data": updated_household,
"label": SAMPLE_HOUSEHOLD_DATA["label"],
}

response = rest_client.put(
Expand All @@ -138,7 +139,17 @@ def test_update_household_success(
assert response.status_code == 200
assert data["status"] == "ok"
assert data["result"]["household_id"] == 1
assert data["result"]["household_json"] == updated_data["data"]
# assert data["result"]["household_json"] == updated_data["data"]
mock_database.query.assert_any_call(
"UPDATE household SET household_json = ?, household_hash = ?, label = ?, api_version = ? WHERE id = ?",
(
json.dumps(updated_household),
"some-hash",
SAMPLE_HOUSEHOLD_DATA["label"],
COUNTRY_PACKAGE_VERSIONS.get("us"),
1,
),
)

def test_update_nonexistent_household(self, rest_client, mock_database):
"""Test updating a non-existent household."""
Expand Down Expand Up @@ -208,16 +219,26 @@ def test_create_household(self, mock_database, mock_hash_object):
def test_update_household(self, mock_database, mock_hash_object):
"""Test HouseholdService.update_household method."""
service = HouseholdService()
mock_database.query().fetchone.return_value = SAMPLE_DB_ROW

service.update_household(
"us",
"1",
1,
SAMPLE_HOUSEHOLD_DATA["data"],
SAMPLE_HOUSEHOLD_DATA["label"],
)

mock_database.query.assert_called()
assert mock_hash_object.called
mock_database.query.assert_any_call(
"UPDATE household SET household_json = ?, household_hash = ?, label = ?, api_version = ? WHERE id = ?",
(
json.dumps(SAMPLE_HOUSEHOLD_DATA["data"]),
"some-hash",
SAMPLE_HOUSEHOLD_DATA["label"],
COUNTRY_PACKAGE_VERSIONS.get("us"),
1,
),
)


class TestHouseholdRouteValidation:
Expand Down Expand Up @@ -256,8 +277,6 @@ def test_post_household_invalid_payload(
def test_get_household_invalid_id(self, rest_client, invalid_id):
"""Test GET endpoint with invalid household IDs."""
response = rest_client.get(f"/us/household/{invalid_id}")
print(response)
print(response.data)

# Default Werkzeug validation returns 404, not 400
assert response.status_code == 404
Expand Down
2 changes: 0 additions & 2 deletions tests/python/test_user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def test_set_and_get_record(self, rest_client):

res = rest_client.get(f"/us/user-profile?auth0_id={self.auth0_id}")
return_object = json.loads(res.text)
print(return_object)

assert res.status_code == 200
assert return_object["status"] == "ok"
Expand Down Expand Up @@ -100,6 +99,5 @@ def test_non_existent_record(self, rest_client):
f"/us/user-profile?auth0_id={non_existent_auth0_id}"
)
return_object = json.loads(res.text)
print(return_object)

assert res.status_code == 404

0 comments on commit 7cc72fe

Please sign in to comment.