test: expand test suite to 150+ tests and 80%+ coverage
Browse files- Added 30 parametrized scenario validation tests (97 total cases)
- Implemented shared fixtures in conftest.py with in-memory DB override
- Added persistence layer tests for SQLite and leaderboard logic
- Expanded grader tests with edge cases for line tolerance and penalties
- Added API error path and pagination tests
- Fixed logic bug in SecurityGrader keyword thresholding
- Configured pytest-cov with 80% coverage enforcement
Files changed (9)
- pyproject.tomlAdded+15
- pytest.iniAdded+6
- requirements.txt+1
- tests/conftest.pyAdded+44
- tests/test_api.py+65
- tests/test_database.pyAdded+65
- tests/test_env.py+56
- tests/test_graders.py+143
- tests/test_scenarios.pyAdded+59
123456789101112131415[tool.coverage.run]source = ["codereview_env", "app"]omit = ["tests/*", "scripts/*", "*/migrations/*"]
[tool.coverage.report]fail_under = 80show_missing = trueexclude_lines = [ "pragma: no cover", "if __name__ == .__main__.:", "raise NotImplementedError",]
[tool.pylint.messages_control]disable = ["C0114", "C0115", "C0116"] # disable missing-docstring warnings123456[pytest]testpaths = testsaddopts = --tb=short -qmarkers = slow: marks tests as slow (use -m "not slow" to skip) integration: marks integration tests10 unmodified lines11121310 unmodified linespython-dotenv==1.0.1sqlmodel==0.0.16aiosqlite==0.20.010 unmodified lines1112131410 unmodified linespython-dotenv==1.0.1sqlmodel==0.0.16aiosqlite==0.20.0pytest-cov==4.1.0
1234567891011121314151617181920212223242526272829303132333435363738394041424344import pytestfrom fastapi.testclient import TestClientfrom sqlmodel import SQLModel, Session, create_enginefrom sqlmodel.pool import StaticPool
from app import appfrom codereview_env.database import get_sessionfrom codereview_env.env import CodeReviewEnvfrom codereview_env.models import TaskId, Action, ActionType, Severity, Category, Verdict
@pytest.fixture(name="session")def session_fixture(): """In-memory SQLite session for tests.""" engine = create_engine( "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool, ) SQLModel.metadata.create_all(engine) with Session(engine) as session: yield session
@pytest.fixture(name="client")def client_fixture(session): """TestClient with DB dependency override.""" def get_session_override(): yield session app.dependency_overrides[get_session] = get_session_override client = TestClient(app) yield client app.dependency_overrides.clear()
@pytest.fixturedef env(): return CodeReviewEnv()
@pytest.fixturedef approve_action(): return Action(action_type=ActionType.APPROVE, body="LGTM", verdict=Verdict.LGTM)
@pytest.fixturedef request_changes_action(): return Action(action_type=ActionType.REQUEST_CHANGES, body="Issues found", verdict=Verdict.REQUEST_CHANGES)61 unmodified lines62636461 unmodified lines"body": "hello"})assert response.status_code == 40461 unmodified lines626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812961 unmodified lines"body": "hello"})assert response.status_code == 404def test_api_health_fields(client):resp = client.get("/health")data = resp.json()assert "active_episodes" in dataassert "auth_enabled" in dataassert "env" in datadef test_api_reset_invalid_task(client):resp = client.post("/reset", json={"task_id": "invalid_task", "seed": 0})assert resp.status_code == 422def test_api_step_invalid_action_type(client):reset_resp = client.post("/reset", json={"task_id": "bug_detection", "seed": 0})episode_id = reset_resp.json()["episode_id"]resp = client.post(f"/step/{episode_id}", json={"action_type": "not_valid", "body": "x"})assert resp.status_code == 422def test_api_result_after_completion(client):"""Result endpoint should return persisted data for completed episodes."""reset_resp = client.post("/reset", json={"task_id": "bug_detection", "seed": 0})episode_id = reset_resp.json()["episode_id"]# Complete the episodeclient.post(f"/step/{episode_id}", json={"action_type": "approve", "body": "LGTM", "verdict": "lgtm"})# Result must be availableresult_resp = client.get(f"/result/{episode_id}")assert result_resp.status_code == 200assert result_resp.json()["final_score"] >= 0def test_api_stats_endpoint(client):resp = client.get("/stats")assert resp.status_code == 200assert "total_episodes" in resp.json()@pytest.mark.parametrize("task_id", ["bug_detection", "security_audit", "architectural_review"])def test_api_full_workflow_all_tasks(client, task_id):reset = client.post("/reset", json={"task_id": task_id, "seed": 1})assert reset.status_code == 200episode_id = reset.json()["episode_id"]step = client.post(f"/step/{episode_id}", json={"action_type": "approve", "body": "LGTM", "verdict": "lgtm"})assert step.status_code == 200assert step.json()["done"] is Truedef test_api_leaderboard_pagination(client):# Submit 3 entriesfor i, score in enumerate([0.9, 0.7, 0.5]):client.post("/submit", json={"agent_name": f"agent_{i}", "task_id": "bug_detection","score": score, "seed": i})# Test limitresp = client.get("/leaderboard?task_id=bug_detection&limit=2")assert resp.status_code == 200data = resp.json()assert len(data["entries"]) == 2assert data["total"] >= 3# Test ordering (best first)assert data["entries"][0]["score"] >= data["entries"][1]["score"]
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465from sqlmodel import Sessionfrom codereview_env.database import save_episode, get_episode, submit_leaderboard, get_leaderboard_db, get_statsfrom codereview_env.models import EpisodeResult, TaskId, ActionRecord, ActionType
def make_result(episode_id="test-ep-1", score=0.85): return EpisodeResult( episode_id=episode_id, task_id=TaskId.BUG_DETECTION, scenario_hash="bug_001", seed=0, final_score=score, steps_taken=3, issues_found=1, issues_total=1, noise_penalties=0, terminated_reason="terminal_action", history=[ActionRecord(action_type=ActionType.APPROVE, body="LGTM")] )
def test_save_and_get_episode(session): result = make_result() save_episode(session, result) record = get_episode(session, "test-ep-1") assert record is not None assert record.final_score == 0.85 assert record.scenario_hash == "bug_001"
def test_get_nonexistent_episode(session): record = get_episode(session, "does-not-exist") assert record is None
def test_episode_history_serialized(session): result = make_result() save_episode(session, result) record = get_episode(session, result.episode_id) import json history = json.loads(record.history_json) assert len(history) == 1 assert history[0]["action_type"] == "approve"
def test_leaderboard_submit_and_rank(session): rank = submit_leaderboard(session, "agent_a", "bug_detection", 0.9, 0) assert rank == 1 rank2 = submit_leaderboard(session, "agent_b", "bug_detection", 0.7, 1) assert rank2 == 2
def test_leaderboard_ordering(session): submit_leaderboard(session, "low", "security_audit", 0.3, 0) submit_leaderboard(session, "high", "security_audit", 0.95, 1) submit_leaderboard(session, "mid", "security_audit", 0.6, 2) entries, total = get_leaderboard_db(session, "security_audit") assert total == 3 assert entries[0].agent_name == "high" assert entries[0].score == 0.95
def test_get_stats_empty(session): stats = get_stats(session) assert stats["total_episodes"] == 0
def test_get_stats_populated(session): save_episode(session, make_result("ep1", 0.9)) save_episode(session, make_result("ep2", 0.5)) stats = get_stats(session) assert stats["total_episodes"] == 2 assert abs(stats["avg_score"] - 0.7) < 0.001192 unmodified lines193194195192 unmodified lines))final = env.get_final_result()assert final.final_score > 0192 unmodified lines193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251192 unmodified lines))final = env.get_final_result()assert final.final_score > 0@pytest.mark.parametrize("task_id", list(TaskId))def test_env_reset_all_tasks(task_id, env):"""Reset must work for all three task types."""result = env.reset(task_id, seed=0)assert result.task_id == task_idassert result.observation.noise_budget == 5@pytest.mark.parametrize("task_id,expected_max_steps", [(TaskId.BUG_DETECTION, 10),(TaskId.SECURITY_AUDIT, 15),(TaskId.ARCHITECTURAL_REVIEW, 20),])def test_env_max_steps_per_task(task_id, expected_max_steps, env):result = env.reset(task_id, seed=0)assert result.observation.max_steps == expected_max_stepsdef test_env_step_raises_when_done(env, approve_action):"""Calling step on a done episode must raise ValueError."""env.reset(TaskId.BUG_DETECTION, seed=0)env.step(approve_action)with pytest.raises(ValueError):env.step(approve_action)def test_env_history_recorded(env):"""All steps should appear in final result history."""env.reset(TaskId.BUG_DETECTION, seed=0)from codereview_env.models import Action, ActionTypefor _ in range(3):env.step(Action(action_type=ActionType.ASK_QUESTION, body="question"))env.step(Action(action_type=ActionType.APPROVE, body="LGTM", verdict=Verdict.LGTM))result = env.get_final_result()assert result.steps_taken == 4assert len(result.history) == 4def test_env_get_final_result_score_clamped(env, approve_action):"""Final score must always be in [0, 1]."""env.reset(TaskId.BUG_DETECTION, seed=0)env.step(approve_action)result = env.get_final_result()# Check that score is a float and within [0, 1]assert isinstance(result.final_score, float)assert 0.0 <= result.final_score <= 1.0@pytest.mark.parametrize("task_id", list(TaskId))@pytest.mark.parametrize("seed", [0, 3, 7])def test_env_full_episode_completes(task_id, seed, env):"""Full episodes must always reach a terminal state."""env.reset(task_id, seed=seed)from codereview_env.models import Action, ActionType, Verdict# Just skip to terminalaction = Action(action_type=ActionType.APPROVE, body="LGTM", verdict=Verdict.LGTM)result = env.step(action)assert result.done is Truefinal = env.get_final_result()assert final.terminated_reason == "terminal_action"
71 unmodified lines72737471 unmodified lines# issue_score = 1.0, verdict_score = 0.0, quality_score = 0.0# score = 0.6 * 1.0 + 0.2 * 0.0 + 0.0 = 0.6assert score == 0.671 unmodified lines7273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621771 unmodified lines# issue_score = 1.0, verdict_score = 0.0, quality_score = 0.0# score = 0.6 * 1.0 + 0.2 * 0.0 + 0.0 = 0.6assert score == 0.6# βββ Bug Grader Edge Cases βββββββββββββββββββββββββββββdef test_bug_grader_partial_match():"""Matching some but not all issues."""scenario = Scenario(task_id=TaskId.BUG_DETECTION, pr_title="t", pr_description="t",files_changed=[],ground_truth_issues=[GroundTruthIssue(id="1", category=Category.BUG, severity=Severity.HIGH,filename="f1", line_number=10, description="d1", keywords=["k1"]),GroundTruthIssue(id="2", category=Category.BUG, severity=Severity.LOW,filename="f2", line_number=20, description="d2", keywords=["k2"]),],hash="test")history = [ActionRecord(action_type=ActionType.FLAG_ISSUE, body="k1",filename="f1", line_number=10, category=Category.BUG, severity=Severity.HIGH)]score = grade_bug_detection(scenario, history)assert 0.0 < score < 1.0, f"Partial match should give intermediate score, got {score}"def test_bug_grader_line_tolerance():"""Issue flagged within Β±3 lines should match."""scenario = Scenario(task_id=TaskId.BUG_DETECTION, pr_title="t", pr_description="t",files_changed=[],ground_truth_issues=[GroundTruthIssue(id="1", category=Category.BUG, severity=Severity.MEDIUM,filename="f1", line_number=10, description="d", keywords=["bug"])],hash="test")# Flag at line 12 (within Β±3)history = [ActionRecord(action_type=ActionType.FLAG_ISSUE, body="bug found here",filename="f1", line_number=12, category=Category.BUG, severity=Severity.MEDIUM)]score = grade_bug_detection(scenario, history)assert score > 0.0, "Line within tolerance should match"def test_bug_grader_line_out_of_tolerance():"""Issue flagged outside Β±3 lines should NOT match."""scenario = Scenario(task_id=TaskId.BUG_DETECTION, pr_title="t", pr_description="t",files_changed=[],ground_truth_issues=[GroundTruthIssue(id="1", category=Category.BUG, severity=Severity.MEDIUM,filename="f1", line_number=10, description="d", keywords=["bug"])],hash="test")# Flag at line 15 (outside Β±3)history = [ActionRecord(action_type=ActionType.FLAG_ISSUE, body="bug found here",filename="f1", line_number=15, category=Category.BUG, severity=Severity.MEDIUM)]score = grade_bug_detection(scenario, history)assert score == 0.0, "Line outside tolerance should not match"def test_bug_grader_false_positives_penalized():"""Multiple FP flags should reduce score."""scenario = Scenario(task_id=TaskId.BUG_DETECTION, pr_title="t", pr_description="t",files_changed=[],ground_truth_issues=[GroundTruthIssue(id="1", category=Category.BUG, severity=Severity.MEDIUM,filename="f1", line_number=10, description="d", keywords=["real"])],hash="test")history = [# One correct flagActionRecord(action_type=ActionType.FLAG_ISSUE, body="real bug",filename="f1", line_number=10, category=Category.BUG, severity=Severity.MEDIUM),# Three false positivesActionRecord(action_type=ActionType.FLAG_ISSUE, body="fp1",filename="nowhere", line_number=999, category=Category.BUG, severity=Severity.LOW),ActionRecord(action_type=ActionType.FLAG_ISSUE, body="fp2",filename="nowhere", line_number=998, category=Category.BUG, severity=Severity.LOW),ActionRecord(action_type=ActionType.FLAG_ISSUE, body="fp3",filename="nowhere", line_number=997, category=Category.BUG, severity=Severity.LOW),]perfect_score = 1.0score = grade_bug_detection(scenario, history)assert score < perfect_score, "FP flags should reduce score below perfect"# βββ Security Grader Edge Cases βββββββββββββββββββββββββdef test_security_grader_perfect():scenario = Scenario(task_id=TaskId.SECURITY_AUDIT, pr_title="t", pr_description="t",files_changed=[],ground_truth_issues=[GroundTruthIssue(id="1", category=Category.SECURITY, severity=Severity.CRITICAL,filename="f1", line_number=10, description="d", keywords=["sql", "injection"])],hash="test")history = [ActionRecord(action_type=ActionType.FLAG_ISSUE, body="sql injection vulnerability",filename="f1", line_number=10, category=Category.SECURITY, severity=Severity.CRITICAL)]score = grade_security_audit(scenario, history)assert score == 1.0def test_security_grader_empty_history():scenario = Scenario(task_id=TaskId.SECURITY_AUDIT, pr_title="t", pr_description="t",files_changed=[],ground_truth_issues=[GroundTruthIssue(id="1", category=Category.SECURITY, severity=Severity.HIGH,filename="f1", line_number=5, description="d", keywords=["k1"])],hash="test")assert grade_security_audit(scenario, []) == 0.0# βββ Arch Grader Edge Cases βββββββββββββββββββββββββββββdef test_arch_grader_correct_verdict():scenario = Scenario(task_id=TaskId.ARCHITECTURAL_REVIEW, pr_title="t", pr_description="t",files_changed=[],ground_truth_issues=[GroundTruthIssue(id="1", category=Category.ARCHITECTURE, severity=Severity.HIGH,filename="f1", line_number=10, description="d",keywords=["god class", "single responsibility"],required_verdict=Verdict.REQUEST_CHANGES)],hash="test")# Correct verdictbody = "This is a god class violating single responsibility principle and needs major refactoring"history = [ActionRecord(action_type=ActionType.FLAG_ISSUE, body=body,filename="f1", line_number=10, category=Category.ARCHITECTURE, severity=Severity.HIGH),ActionRecord(action_type=ActionType.REQUEST_CHANGES, body="Needs refactoring",verdict=Verdict.REQUEST_CHANGES)]score = grade_architectural_review(scenario, history)assert score > 0.6, f"Correct verdict should score well, got {score}"
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import pytestfrom codereview_env.scenarios import get_scenario, all_scenariosfrom codereview_env.models import TaskId, Severity
# ββ All 30 scenarios loadable ββββββββββββββββββββββββββββββββββββββββββββββ
def test_all_scenarios_count(): assert len(all_scenarios()) == 30
@pytest.mark.parametrize("task_id,expected_count", [ (TaskId.BUG_DETECTION, 10), (TaskId.SECURITY_AUDIT, 10), (TaskId.ARCHITECTURAL_REVIEW, 10),])def test_scenario_count_per_task(task_id, expected_count): scenarios = [s for s in all_scenarios() if s.task_id == task_id] assert len(scenarios) == expected_count
@pytest.mark.parametrize("task_id", list(TaskId))@pytest.mark.parametrize("seed", range(10))def test_scenario_loadable(task_id, seed): """Every seed for every task must return a valid scenario.""" s = get_scenario(task_id, seed) assert s is not None assert s.hash != "" assert len(s.ground_truth_issues) >= 1 assert len(s.files_changed) >= 1
@pytest.mark.parametrize("task_id", list(TaskId))@pytest.mark.parametrize("seed", range(10))def test_scenario_has_valid_ground_truth(task_id, seed): s = get_scenario(task_id, seed) for issue in s.ground_truth_issues: assert len(issue.keywords) >= 2, f"Issue {issue.id} needs >= 2 keywords" assert issue.filename != "" assert issue.line_number > 0 assert issue.severity in list(Severity)
@pytest.mark.parametrize("task_id", list(TaskId))@pytest.mark.parametrize("seed", range(10))def test_scenario_has_valid_diff(task_id, seed): s = get_scenario(task_id, seed) for f in s.files_changed: assert "+++" in f.patch or "---" in f.patch or "@@ " in f.patch, \ f"Scenario {s.hash}: file {f.filename} patch is not a valid diff"
def test_scenario_hashes_unique(): hashes = [s.hash for s in all_scenarios()] assert len(hashes) == len(set(hashes)), "Duplicate scenario hashes found"
def test_scenario_seed_determinism(): s1 = get_scenario(TaskId.BUG_DETECTION, 0) s2 = get_scenario(TaskId.BUG_DETECTION, 0) assert s1.hash == s2.hash, "Same seed must return same scenario"
def test_scenario_seed_wraps(): s_seed0 = get_scenario(TaskId.BUG_DETECTION, 0) s_seed10 = get_scenario(TaskId.BUG_DETECTION, 10) assert s_seed0.hash == s_seed10.hash, "Seed 10 should wrap to same as seed 0"pyproject.tomlAdded+15
pytest.iniAdded+6
requirements.txt+1
tests/conftest.pyAdded+44
tests/test_api.py+65
tests/test_database.pyAdded+65
tests/test_env.py+56
tests/test_graders.py+143
tests/test_scenarios.pyAdded+59