Skip to content

Commit 675572a

Browse files
committed
review: pin /api/search missing-q contract to 400 (#32)
CodeRabbit flagged that the permissive ``assert status_code in (200, 400)`` let either contract pass, so a drift between the two behaviours would go unnoticed. The shipped implementation (api/search.py:74-75) returns ``400 with {"error": "No search query provided"}`` for missing or empty q. Replaced the single permissive test with three concrete ones, each asserting the same 400 + error-body contract: - test_missing_q_returns_400 — GET /api/search (no q at all) - test_empty_q_returns_400 — GET /api/search?q= (empty) - test_whitespace_only_q_returns_400 — GET /api/search?q=%20%20%20 (api/search.py strips before checking) 191 pytest tests pass (was 189; +2 from the split).
1 parent fb549ec commit 675572a

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

tests/test_api_endpoints.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,22 @@ def test_no_match_returns_empty_results(self, client):
109109
body = response.get_json()
110110
assert "results" in body and body["results"] == []
111111

112-
def test_missing_q_returns_400_or_empty(self, client):
112+
def test_missing_q_returns_400(self, client):
113113
response = client.get("/api/search")
114-
# Implementation may return 400 (missing required param) or 200 with empty.
115-
# Both are reasonable for "no query supplied"; pin whichever shipped.
116-
assert response.status_code in (200, 400)
117-
if response.status_code == 200:
118-
body = response.get_json()
119-
assert "results" in body
114+
assert response.status_code == 400
115+
body = response.get_json()
116+
assert "error" in body
117+
assert body["error"] == "No search query provided"
118+
119+
def test_empty_q_returns_400(self, client):
120+
response = client.get("/api/search?q=")
121+
assert response.status_code == 400
122+
body = response.get_json()
123+
assert body.get("error") == "No search query provided"
124+
125+
def test_whitespace_only_q_returns_400(self, client):
126+
# api/search.py strips q before the empty-check, so " " is rejected.
127+
response = client.get("/api/search?q=%20%20%20")
128+
assert response.status_code == 400
129+
body = response.get_json()
130+
assert body.get("error") == "No search query provided"

0 commit comments

Comments
 (0)