forge
Explore
Log in

Don't you dare go Hollow

hezarfen_rule_based_chatbot

Clone
★ 0

0 watching0 forksPython1.2 MiBa039b60Ahmet Kaan Gümüş2 months ago64 commits

test_benchmark.py138 lines, 4.9 KiBHistoryRawDownload
1"""Aşama 3 birim testleri: benchmark yükleme ve doğrulama."""
2
3import json
4import tempfile
5import unittest
6from pathlib import Path
7
8from src import catalog
9from src.evaluation.benchmark import (
10 OOS_LABEL,
11 load_benchmark,
12 validate_benchmark,
13)
14
15
16def _known_intents() -> set[str]:
17 return {item["intent"] for item in catalog.INTENTS}
18
19
20def _example_questions() -> set[str]:
21 return {
22 q.strip().casefold()
23 for item in catalog.INTENTS
24 for q in item["example_questions"]
25 }
26
27
28class LoadBenchmarkTests(unittest.TestCase):
29 def test_loads_default_benchmark(self) -> None:
30 records = load_benchmark()
31 self.assertGreater(len(records), 0)
32 self.assertIn("question", records[0])
33 self.assertIn("expected_intent", records[0])
34
35 def test_missing_file_raises(self) -> None:
36 with self.assertRaises(FileNotFoundError):
37 load_benchmark(Path("yok_boyle_bir_dosya.jsonl"))
38
39 def test_blank_lines_skipped(self) -> None:
40 with tempfile.TemporaryDirectory() as tmp:
41 path = Path(tmp) / "b.jsonl"
42 path.write_text(
43 '{"question": "a", "expected_intent": "oos"}\n\n'
44 '{"question": "b", "expected_intent": "oos"}\n',
45 encoding="utf-8",
46 )
47 self.assertEqual(len(load_benchmark(path)), 2)
48
49 def test_invalid_json_raises(self) -> None:
50 with tempfile.TemporaryDirectory() as tmp:
51 path = Path(tmp) / "b.jsonl"
52 path.write_text("{bozuk json}\n", encoding="utf-8")
53 with self.assertRaises(ValueError):
54 load_benchmark(path)
55
56 def test_missing_field_raises(self) -> None:
57 with tempfile.TemporaryDirectory() as tmp:
58 path = Path(tmp) / "b.jsonl"
59 path.write_text('{"question": "a"}\n', encoding="utf-8")
60 with self.assertRaises(ValueError):
61 load_benchmark(path)
62
63
64class ValidateBenchmarkTests(unittest.TestCase):
65 def test_real_benchmark_is_valid(self) -> None:
66 records = load_benchmark()
67 result = validate_benchmark(
68 records,
69 known_intents=_known_intents(),
70 known_roles=set(catalog.ROLE_HIERARCHY),
71 example_questions=_example_questions(),
72 )
73 self.assertGreater(result["total"], 0)
74 self.assertGreater(result["oos"], 0)
75 self.assertGreater(result["in_scope"], 0)
76
77 def test_real_benchmark_has_no_overlap_with_examples(self) -> None:
78 # Örtüşme varsa validate_benchmark ValueError atar; burada net doğrula.
79 records = load_benchmark()
80 examples = _example_questions()
81 overlap = [
82 r["question"]
83 for r in records
84 if r["question"].strip().casefold() in examples
85 ]
86 self.assertEqual(overlap, [], f"Örtüşen sorular: {overlap}")
87
88 def test_every_intent_has_at_least_one_benchmark_question(self) -> None:
89 records = load_benchmark()
90 covered = {r["expected_intent"] for r in records if r["expected_intent"] != OOS_LABEL}
91 missing = _known_intents().difference(covered)
92 self.assertEqual(missing, set(), f"Benchmark'ta karşılığı olmayan intent: {missing}")
93
94 def test_detects_unknown_intent(self) -> None:
95 records = [{"question": "x", "expected_intent": "yok_intent"}]
96 with self.assertRaises(ValueError):
97 validate_benchmark(records, known_intents=_known_intents())
98
99 def test_detects_duplicate_question(self) -> None:
100 records = [
101 {"question": "aynı", "expected_intent": "oos"},
102 {"question": "Aynı", "expected_intent": "oos"},
103 ]
104 with self.assertRaises(ValueError):
105 validate_benchmark(records, known_intents=_known_intents())
106
107 def test_detects_overlap_with_examples(self) -> None:
108 sample_example = catalog.INTENTS[0]["example_questions"][0]
109 records = [
110 {"question": sample_example, "expected_intent": catalog.INTENTS[0]["intent"]},
111 {"question": "kapsam disi soru", "expected_intent": "oos"},
112 ]
113 with self.assertRaises(ValueError):
114 validate_benchmark(
115 records,
116 known_intents=_known_intents(),
117 example_questions=_example_questions(),
118 )
119
120 def test_requires_at_least_one_oos(self) -> None:
121 records = [{"question": "x", "expected_intent": catalog.INTENTS[0]["intent"]}]
122 with self.assertRaises(ValueError):
123 validate_benchmark(records, known_intents=_known_intents())
124
125 def test_detects_unknown_role(self) -> None:
126 records = [
127 {"question": "x", "expected_intent": "oos", "role": "kral"},
128 ]
129 with self.assertRaises(ValueError):
130 validate_benchmark(
131 records,
132 known_intents=_known_intents(),
133 known_roles=set(catalog.ROLE_HIERARCHY),
134 )
135
136
137if __name__ == "__main__":
138 unittest.main()