Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate household endpoints to new API structure #2038

Merged
merged 27 commits into from
Dec 20, 2024
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
43d6a96
feat: Initial stages of migration of household endpoints
anth-volk Nov 30, 2024
5dfc1b0
feat: Migrate post_household to new structure
anth-volk Nov 30, 2024
506d233
feat: Migrate update household endpoint
anth-volk Nov 30, 2024
ffb42ec
chore: Format
anth-volk Nov 30, 2024
0d3473e
fix: Fix minor bugs
anth-volk Nov 30, 2024
2129cda
fix: Modify validate_country to return 400 on country not found and u…
anth-volk Dec 2, 2024
6bd8420
feat: Refactor household endpoints to match new structure
anth-volk Dec 4, 2024
3e2a8a8
fix: Refactor fixtures
anth-volk Dec 4, 2024
a3a78fc
fix: Adjust test to handle added validation
anth-volk Dec 4, 2024
de96431
fix: Redo blueprint generation
anth-volk Dec 12, 2024
c0fdde9
fix: Remove erroneous print statement
anth-volk Dec 12, 2024
f789559
feat: Basic Werkzeug route validators
anth-volk Dec 12, 2024
f0d2a6e
feat: Error handling module and application to routes
anth-volk Dec 13, 2024
e7737c6
fix: Properly register error routes
anth-volk Dec 13, 2024
082d7cc
fix: Remove unused comment code
anth-volk Dec 13, 2024
6487058
fix: Redefine metadata route against convention
anth-volk Dec 13, 2024
c085d1d
feat: Return household JSON in household updater func and update test
anth-volk Dec 13, 2024
ca4ad9f
test: Fix some failing tests, add tests for error handlers
anth-volk Dec 13, 2024
7c24017
fix: Fix tracer tests
anth-volk Dec 13, 2024
c668a92
fix: Fix failing simulation analysis test
anth-volk Dec 13, 2024
a40e430
fix: Fix manual raising within try-catch blocks
anth-volk Dec 13, 2024
7cc72fe
fix: Fix final household tests
anth-volk Dec 14, 2024
adbfaa9
fix: Fix tests
anth-volk Dec 14, 2024
0d931a3
test: Fix tests
anth-volk Dec 18, 2024
2e70641
feat: Use error response builder
anth-volk Dec 18, 2024
0f39c1b
chore: Refactor against new error implementation
anth-volk Dec 18, 2024
3268c82
fix: Improve error handling
anth-volk Dec 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: Fix tracer tests
anth-volk committed Dec 13, 2024
commit 7c240173b71d8ed89fe900e7271298ed5bac67c8
4 changes: 2 additions & 2 deletions policyengine_api/api.py
Original file line number Diff line number Diff line change
@@ -55,6 +55,8 @@

CORS(app)

ErrorRoutes.init_app(app)

app.route("/", methods=["GET"])(get_home)

app.register_blueprint(metadata_bp)
@@ -108,8 +110,6 @@

app.register_blueprint(tracer_analysis_bp)

ErrorRoutes.init_app(app)


@app.route("/liveness-check", methods=["GET"])
def liveness_check():
7 changes: 4 additions & 3 deletions tests/python/test_household.py
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ def test_get_household_invalid_id(self, rest_client):
"""Test getting a household with invalid ID."""
response = rest_client.get("/us/household/invalid")

assert response.status_code == 400
assert response.status_code == 404
assert b"Invalid household ID" in response.data


@@ -131,7 +131,7 @@ def test_update_household_success(

assert response.status_code == 200
assert data["status"] == "ok"
assert data["result"]["household_id"] == "1"
assert data["result"]["household_id"] == 1
assert data["result"]["household_json"] == updated_data["data"]

def test_update_nonexistent_household(self, rest_client, mock_database):
@@ -253,7 +253,8 @@ def test_get_household_invalid_id(self, rest_client, invalid_id):
print(response)
print(response.data)

assert response.status_code == 400
# Default Werkzeug validation returns 404, not 400
assert response.status_code == 404
assert b"Invalid household ID" in response.data

@pytest.mark.parametrize(
35 changes: 12 additions & 23 deletions tests/python/test_tracer.py
Original file line number Diff line number Diff line change
@@ -12,13 +12,6 @@
test_service = TracerAnalysisService()


@pytest.fixture
def app():
app = Flask(__name__)
app.config["TESTING"] = True
return app


# Test cases for parse_tracer_output function
def test_parse_tracer_output():

@@ -64,33 +57,31 @@ def test_execute_tracer_analysis_success(
# Set this to US current law
test_policy_id = 2

with app.test_request_context(
"/us/tracer_analysis",
response = rest_client.post(
"/us/tracer-analysis",
json={
"household_id": test_household_id,
"policy_id": test_policy_id,
"variable": "disposable_income",
},
):
response = execute_tracer_analysis("us")
)

assert response.status_code == 200
assert b"AI analysis result" in response.data


@patch("policyengine_api.services.tracer_analysis_service.local_database")
def test_execute_tracer_analysis_no_tracer(mock_db, app, rest_client):
def test_execute_tracer_analysis_no_tracer(mock_db, rest_client):
mock_db.query.return_value.fetchone.return_value = None

with app.test_request_context(
"/us/tracer_analysis",
response = rest_client.post(
"/us/tracer-analysis",
json={
"household_id": "test_household",
"policy_id": "test_policy",
"variable": "disposable_income",
},
):
response = execute_tracer_analysis("us")
)

assert response.status_code == 404
assert (
@@ -104,7 +95,7 @@ def test_execute_tracer_analysis_no_tracer(mock_db, app, rest_client):
"policyengine_api.services.tracer_analysis_service.TracerAnalysisService.trigger_ai_analysis"
)
def test_execute_tracer_analysis_ai_error(
mock_trigger_ai_analysis, mock_db, app, rest_client
mock_trigger_ai_analysis, mock_db, rest_client
):
mock_db.query.return_value.fetchone.return_value = {
"tracer_output": json.dumps(
@@ -114,19 +105,17 @@ def test_execute_tracer_analysis_ai_error(
mock_trigger_ai_analysis.side_effect = Exception(KeyError)

test_household_id = 1500

# Set this to US current law
test_policy_id = 2

with app.test_request_context(
"/us/tracer_analysis",
# Use the test client to make the request instead of calling the function directly
response = rest_client.post(
"/us/tracer-analysis",
json={
"household_id": test_household_id,
"policy_id": test_policy_id,
"variable": "disposable_income",
},
):
response = execute_tracer_analysis("us")
)

assert response.status_code == 500
assert "An error occurred" in json.loads(response.data)["message"]