Merge pull request #11 from DsThakurRawat/add-test-graders
Browse files- tests/test_graders.py +74 -0
tests/test_graders.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from codereview_env.models import Scenario, ActionRecord, Category, Severity, TaskId, GroundTruthIssue, ActionType, Verdict
|
| 2 |
+
from codereview_env.graders.bug_grader import grade_bug_detection
|
| 3 |
+
from codereview_env.graders.security_grader import grade_security_audit
|
| 4 |
+
from codereview_env.graders.arch_grader import grade_architectural_review
|
| 5 |
+
|
| 6 |
+
def test_bug_grader_perfect():
|
| 7 |
+
scenario = Scenario(
|
| 8 |
+
task_id=TaskId.BUG_DETECTION,
|
| 9 |
+
pr_title="test", pr_description="test",
|
| 10 |
+
files_changed=[],
|
| 11 |
+
ground_truth_issues=[
|
| 12 |
+
GroundTruthIssue(id="1", category=Category.BUG, severity=Severity.MEDIUM, filename="f1", line_number=10, description="d1", keywords=["k1", "k2"])
|
| 13 |
+
],
|
| 14 |
+
hash="h1"
|
| 15 |
+
)
|
| 16 |
+
history = [
|
| 17 |
+
ActionRecord(action_type=ActionType.FLAG_ISSUE, body="found k1 k2", filename="f1", line_number=10, category=Category.BUG, severity=Severity.MEDIUM)
|
| 18 |
+
]
|
| 19 |
+
score = grade_bug_detection(scenario, history)
|
| 20 |
+
assert score == 1.0
|
| 21 |
+
|
| 22 |
+
def test_bug_grader_none():
|
| 23 |
+
scenario = Scenario(
|
| 24 |
+
task_id=TaskId.BUG_DETECTION,
|
| 25 |
+
pr_title="test", pr_description="test",
|
| 26 |
+
files_changed=[],
|
| 27 |
+
ground_truth_issues=[
|
| 28 |
+
GroundTruthIssue(id="1", category=Category.BUG, severity=Severity.MEDIUM, filename="f1", line_number=10, description="d1", keywords=["k1", "k2"])
|
| 29 |
+
],
|
| 30 |
+
hash="h1"
|
| 31 |
+
)
|
| 32 |
+
history = []
|
| 33 |
+
score = grade_bug_detection(scenario, history)
|
| 34 |
+
assert score == 0.0
|
| 35 |
+
|
| 36 |
+
def test_security_grader_severity_mismatch():
|
| 37 |
+
scenario = Scenario(
|
| 38 |
+
task_id=TaskId.SECURITY_AUDIT,
|
| 39 |
+
pr_title="test", pr_description="test",
|
| 40 |
+
files_changed=[],
|
| 41 |
+
ground_truth_issues=[
|
| 42 |
+
GroundTruthIssue(id="1", category=Category.SECURITY, severity=Severity.CRITICAL, filename="f1", line_number=10, description="d1", keywords=["k1"])
|
| 43 |
+
],
|
| 44 |
+
hash="h1"
|
| 45 |
+
)
|
| 46 |
+
# Low severity flagged when it was critical
|
| 47 |
+
history = [
|
| 48 |
+
ActionRecord(action_type=ActionType.FLAG_ISSUE, body="k1", filename="f1", line_number=10, category=Category.SECURITY, severity=Severity.LOW)
|
| 49 |
+
]
|
| 50 |
+
score = grade_security_audit(scenario, history)
|
| 51 |
+
# sev_diff = 3, sev_score = max(0, 1 - 3*0.3) = 0.1
|
| 52 |
+
# threshold = max(4, 1*0.6) = 4. kw_score = 1/4 = 0.25
|
| 53 |
+
# total_score = 0.7 * 0.1 + 0.3 * 0.25 = 0.07 + 0.075 = 0.145
|
| 54 |
+
assert score == 0.145
|
| 55 |
+
|
| 56 |
+
def test_arch_grader_verdict():
|
| 57 |
+
scenario = Scenario(
|
| 58 |
+
task_id=TaskId.ARCHITECTURAL_REVIEW,
|
| 59 |
+
pr_title="test", pr_description="test",
|
| 60 |
+
files_changed=[],
|
| 61 |
+
ground_truth_issues=[
|
| 62 |
+
GroundTruthIssue(id="1", category=Category.ARCHITECTURE, severity=Severity.HIGH, filename="f1", line_number=10, description="d1", keywords=["k1"], required_verdict=Verdict.REQUEST_CHANGES)
|
| 63 |
+
],
|
| 64 |
+
hash="h1"
|
| 65 |
+
)
|
| 66 |
+
# Flagged issue but approved (wrong verdict)
|
| 67 |
+
history = [
|
| 68 |
+
ActionRecord(action_type=ActionType.FLAG_ISSUE, body="k1", filename="f1", line_number=10, category=Category.ARCHITECTURE, severity=Severity.HIGH),
|
| 69 |
+
ActionRecord(action_type=ActionType.APPROVE, body="lgtm", verdict=Verdict.LGTM)
|
| 70 |
+
]
|
| 71 |
+
score = grade_architectural_review(scenario, history)
|
| 72 |
+
# issue_score = 1.0, verdict_score = 0.0, quality_score = 0.0
|
| 73 |
+
# score = 0.6 * 1.0 + 0.2 * 0.0 + 0.0 = 0.6
|
| 74 |
+
assert score == 0.6
|