|
| 1 | +""" |
| 2 | +Tests for utils/interview_prep.py. |
| 3 | +
|
| 4 | +Uses an in-memory SQLite session and mocks requests.post so no real |
| 5 | +Ollama instance is needed. |
| 6 | +""" |
| 7 | +import json |
| 8 | +from unittest.mock import MagicMock, call, patch |
| 9 | + |
| 10 | +import pytest |
| 11 | +import requests |
| 12 | +from sqlalchemy import create_engine |
| 13 | +from sqlalchemy.orm import sessionmaker |
| 14 | + |
| 15 | +import config |
| 16 | +from models.database import Base, InterviewPrepSheet, Job |
| 17 | +from utils.interview_prep import run_interview_prep |
| 18 | + |
| 19 | + |
| 20 | +# --------------------------------------------------------------------------- |
| 21 | +# Fixtures |
| 22 | +# --------------------------------------------------------------------------- |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def engine(): |
| 26 | + e = create_engine("sqlite:///:memory:") |
| 27 | + Base.metadata.create_all(e) |
| 28 | + yield e |
| 29 | + Base.metadata.drop_all(e) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def session(engine): |
| 34 | + S = sessionmaker(bind=engine) |
| 35 | + s = S() |
| 36 | + yield s |
| 37 | + s.close() |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture |
| 41 | +def profile(): |
| 42 | + return { |
| 43 | + "personal": {"name": "Jane Doe", "current_title": "Backend Engineer"}, |
| 44 | + "skills": ["Python", "SQL", "Docker"], |
| 45 | + "work_history": [ |
| 46 | + { |
| 47 | + "company": "Acme", |
| 48 | + "title": "Senior Engineer", |
| 49 | + "from": "2020", |
| 50 | + "to": "present", |
| 51 | + "highlights": ["Built scalable APIs", "Led team of 5"], |
| 52 | + } |
| 53 | + ], |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture |
| 58 | +def job(session): |
| 59 | + j = Job( |
| 60 | + external_id="interview-test-001", |
| 61 | + source="remotive", |
| 62 | + company="TechCorp", |
| 63 | + title="Senior Backend Engineer", |
| 64 | + location="Remote", |
| 65 | + url="https://example.com/jobs/1", |
| 66 | + description="<p>We need a Python expert with SQL and Docker skills.</p>", |
| 67 | + description_text="We need a Python expert with SQL and Docker skills.", |
| 68 | + status="shortlisted", |
| 69 | + ) |
| 70 | + session.add(j) |
| 71 | + session.commit() |
| 72 | + session.refresh(j) |
| 73 | + return j |
| 74 | + |
| 75 | + |
| 76 | +def _ollama_response(content: dict) -> MagicMock: |
| 77 | + mock = MagicMock() |
| 78 | + mock.raise_for_status.return_value = None |
| 79 | + mock.json.return_value = {"message": {"content": json.dumps(content)}} |
| 80 | + return mock |
| 81 | + |
| 82 | + |
| 83 | +CONTEXT_RESPONSE = { |
| 84 | + "company_snapshot": { |
| 85 | + "industry": "SaaS", |
| 86 | + "likely_size": "50-200", |
| 87 | + "culture_signals": ["fast-paced"], |
| 88 | + "red_flags": [], |
| 89 | + }, |
| 90 | + "role_summary": { |
| 91 | + "core_responsibilities": ["Build APIs"], |
| 92 | + "must_have_skills": ["Python", "SQL"], |
| 93 | + "nice_to_have_skills": ["Docker"], |
| 94 | + "seniority_signals": "Senior", |
| 95 | + }, |
| 96 | +} |
| 97 | + |
| 98 | +QUESTIONS_RESPONSE = { |
| 99 | + "technical_questions": ["Describe your Python API experience.", "How do you optimize SQL queries?"], |
| 100 | + "behavioral_questions": ["Tell me about a time you led a project.", "How do you handle deadlines?"], |
| 101 | +} |
| 102 | + |
| 103 | +MAPPING_RESPONSE = { |
| 104 | + "talking_points": [ |
| 105 | + { |
| 106 | + "jd_requirement": "Python expertise", |
| 107 | + "candidate_evidence": "Built scalable APIs at Acme", |
| 108 | + "suggested_story": "Discuss the API work at Acme", |
| 109 | + } |
| 110 | + ], |
| 111 | + "gaps_or_risks": [], |
| 112 | +} |
| 113 | + |
| 114 | +ACTION_PLAN_RESPONSE = { |
| 115 | + "minutes_0_10": ["Review company website"], |
| 116 | + "minutes_10_20": ["Practice Python API questions"], |
| 117 | + "minutes_20_30": ["Rehearse STAR stories"], |
| 118 | + "priority_note": "Emphasize Python API experience", |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +# --------------------------------------------------------------------------- |
| 123 | +# Tests |
| 124 | +# --------------------------------------------------------------------------- |
| 125 | + |
| 126 | +def test_run_interview_prep_success(session, job, profile): |
| 127 | + responses = [ |
| 128 | + _ollama_response(CONTEXT_RESPONSE), |
| 129 | + _ollama_response(QUESTIONS_RESPONSE), |
| 130 | + _ollama_response(MAPPING_RESPONSE), |
| 131 | + _ollama_response(ACTION_PLAN_RESPONSE), |
| 132 | + ] |
| 133 | + with patch("utils.interview_prep.requests.post", side_effect=responses): |
| 134 | + sheet = run_interview_prep(job.id, profile, session) |
| 135 | + |
| 136 | + assert sheet.status == "completed" |
| 137 | + assert sheet.job_application_id == job.id |
| 138 | + assert sheet.generated_at is not None |
| 139 | + |
| 140 | + # All 7 sections populated |
| 141 | + assert json.loads(sheet.company_snapshot)["industry"] == "SaaS" |
| 142 | + assert json.loads(sheet.role_requirements_summary)["seniority_signals"] == "Senior" |
| 143 | + assert len(json.loads(sheet.likely_technical_questions)) == 2 |
| 144 | + assert len(json.loads(sheet.likely_behavioral_questions)) == 2 |
| 145 | + assert len(json.loads(sheet.talking_points)) == 1 |
| 146 | + assert json.loads(sheet.gaps_or_risks) == [] |
| 147 | + assert json.loads(sheet.prep_plan_30_min)["priority_note"] == "Emphasize Python API experience" |
| 148 | + |
| 149 | + |
| 150 | +def test_run_interview_prep_invalid_job_id(session, profile): |
| 151 | + with pytest.raises(ValueError, match="not found"): |
| 152 | + run_interview_prep(99999, profile, session) |
| 153 | + |
| 154 | + # No sheet should have been created |
| 155 | + count = session.query(InterviewPrepSheet).count() |
| 156 | + assert count == 0 |
| 157 | + |
| 158 | + |
| 159 | +def test_run_interview_prep_missing_description(session, profile): |
| 160 | + empty_job = Job( |
| 161 | + external_id="interview-nodesc-001", |
| 162 | + source="remotive", |
| 163 | + company="TechCorp", |
| 164 | + title="Engineer", |
| 165 | + location="Remote", |
| 166 | + url="https://example.com/jobs/2", |
| 167 | + description=None, |
| 168 | + description_text=None, |
| 169 | + status="shortlisted", |
| 170 | + ) |
| 171 | + session.add(empty_job) |
| 172 | + session.commit() |
| 173 | + session.refresh(empty_job) |
| 174 | + |
| 175 | + with pytest.raises(ValueError, match="no description"): |
| 176 | + run_interview_prep(empty_job.id, profile, session) |
| 177 | + |
| 178 | + sheet = session.query(InterviewPrepSheet).filter( |
| 179 | + InterviewPrepSheet.job_application_id == empty_job.id |
| 180 | + ).first() |
| 181 | + assert sheet is not None |
| 182 | + assert sheet.status == "failed" |
| 183 | + assert "empty" in sheet.error_message.lower() |
| 184 | + |
| 185 | + |
| 186 | +def test_run_interview_prep_timeout_retries(session, job, profile): |
| 187 | + with patch("utils.interview_prep.requests.post", side_effect=requests.Timeout("timed out")): |
| 188 | + with patch("utils.interview_prep.time.sleep"): |
| 189 | + with pytest.raises(RuntimeError, match="timed out"): |
| 190 | + run_interview_prep(job.id, profile, session) |
| 191 | + |
| 192 | + sheet = session.query(InterviewPrepSheet).filter( |
| 193 | + InterviewPrepSheet.job_application_id == job.id |
| 194 | + ).first() |
| 195 | + assert sheet is not None |
| 196 | + assert sheet.status == "failed" |
| 197 | + assert "timed out" in sheet.error_message.lower() |
0 commit comments