refactor: use _get_db() pattern in get_research_brief, fix HTTPException style
- Replace lazy import + scripts.db.get_research with inline SQL via _get_db(), matching the pattern used by research_task_status and get_job_contacts - Exclude raw_output from SELECT instead of post-fetch pop - Change HTTPException in generate_research to positional-arg style - Update test_get_research_found/not_found to patch dev_api._get_db
This commit is contained in:
parent
dc158ba802
commit
0ef8547c99
2 changed files with 19 additions and 10 deletions
17
dev-api.py
17
dev-api.py
|
|
@ -316,12 +316,17 @@ def cover_letter_task(job_id: int):
|
|||
|
||||
@app.get("/api/jobs/{job_id}/research")
|
||||
def get_research_brief(job_id: int):
|
||||
from scripts.db import get_research as _get_research
|
||||
row = _get_research(Path(DB_PATH), job_id=job_id)
|
||||
db = _get_db()
|
||||
row = db.execute(
|
||||
"SELECT job_id, company_brief, ceo_brief, talking_points, tech_brief, "
|
||||
"funding_brief, red_flags, accessibility_brief, generated_at "
|
||||
"FROM company_research WHERE job_id = ? LIMIT 1",
|
||||
(job_id,),
|
||||
).fetchone()
|
||||
db.close()
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="No research found for this job")
|
||||
row.pop("raw_output", None)
|
||||
return row
|
||||
raise HTTPException(404, "No research found for this job")
|
||||
return dict(row)
|
||||
|
||||
|
||||
@app.post("/api/jobs/{job_id}/research/generate")
|
||||
|
|
@ -331,7 +336,7 @@ def generate_research(job_id: int):
|
|||
task_id, is_new = submit_task(db_path=Path(DB_PATH), task_type="company_research", job_id=job_id)
|
||||
return {"task_id": task_id, "is_new": is_new}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
raise HTTPException(500, str(e))
|
||||
|
||||
|
||||
@app.get("/api/jobs/{job_id}/research/task")
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ def client():
|
|||
|
||||
def test_get_research_found(client):
|
||||
"""Returns research row (minus raw_output) when present."""
|
||||
row = {
|
||||
import sqlite3
|
||||
mock_row = {
|
||||
"job_id": 1,
|
||||
"company_brief": "Acme Corp makes anvils.",
|
||||
"ceo_brief": "Wile E Coyote",
|
||||
|
|
@ -27,9 +28,10 @@ def test_get_research_found(client):
|
|||
"red_flags": None,
|
||||
"accessibility_brief": None,
|
||||
"generated_at": "2026-03-20T12:00:00",
|
||||
"raw_output": "should be stripped",
|
||||
}
|
||||
with patch("scripts.db.get_research", return_value=row):
|
||||
mock_db = MagicMock()
|
||||
mock_db.execute.return_value.fetchone.return_value = mock_row
|
||||
with patch("dev_api._get_db", return_value=mock_db):
|
||||
resp = client.get("/api/jobs/1/research")
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
|
|
@ -39,7 +41,9 @@ def test_get_research_found(client):
|
|||
|
||||
def test_get_research_not_found(client):
|
||||
"""Returns 404 when no research row exists for job."""
|
||||
with patch("scripts.db.get_research", return_value=None):
|
||||
mock_db = MagicMock()
|
||||
mock_db.execute.return_value.fetchone.return_value = None
|
||||
with patch("dev_api._get_db", return_value=mock_db):
|
||||
resp = client.get("/api/jobs/99/research")
|
||||
assert resp.status_code == 404
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue