From b4e28046fc428b79f652c17714482da3c630b012 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 16 Mar 2026 09:18:09 +0000 Subject: [PATCH] Add error response tests for create_saved_search endpoint Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com> --- tests/test_api_advanced_filters.py | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/test_api_advanced_filters.py b/tests/test_api_advanced_filters.py index 03bd7b24..f6a97879 100644 --- a/tests/test_api_advanced_filters.py +++ b/tests/test_api_advanced_filters.py @@ -269,6 +269,53 @@ class TestSavedSearchesCRUD: response2 = client.post("/api/saved-searches", json=payload) assert response2.status_code == 409 + def test_create_saved_search_db_error(self, client: TestClient, monkeypatch): + """POST /api/saved-searches returns 500 on DB exception.""" + # Mock db.add or db.commit to raise an exception + # We can monkeypatch the route's dependency or the models + # It's easier to mock the SavedSearch model's __init__ or db's add + # Since we use db: DbSession, it's an instance of sqlalchemy.orm.Session + from sqlalchemy.orm import Session + + original_commit = Session.commit + + def mock_commit(*args, **kwargs): + raise Exception("Simulated DB error") + + monkeypatch.setattr(Session, "commit", mock_commit) + + payload = { + "name": "DB Error Search", + "filters": {"status": "completed"}, + } + response = client.post("/api/saved-searches", json=payload) + assert response.status_code == 500 + assert "Failed to save search" in response.json()["detail"] + + def test_create_saved_search_limit_reached(self, client: TestClient, monkeypatch): + """POST /api/saved-searches returns 409 if max limit is reached.""" + monkeypatch.setattr("app.api.saved_searches.MAX_SAVED_SEARCHES_PER_USER", 1) + + # Create first one + payload1 = {"name": "Search 1", "filters": {"status": "completed"}} + response1 = client.post("/api/saved-searches", json=payload1) + assert response1.status_code == 201 + + # Creating second one should fail due to limit + payload2 = {"name": "Search 2", "filters": {"status": "pending"}} + response2 = client.post("/api/saved-searches", json=payload2) + assert response2.status_code == 409 + assert "Maximum of 1 saved searches reached" in response2.json()["detail"] + + def test_create_saved_search_invalid_name_type(self, client: TestClient): + """POST /api/saved-searches with non-string name returns 422.""" + payload = { + "name": 12345, + "filters": {"status": "completed"}, + } + response = client.post("/api/saved-searches", json=payload) + assert response.status_code == 422 + def test_update_saved_search(self, client: TestClient): """PUT /api/saved-searches/{id} updates the saved search.""" # Create