| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | import json |
| 16 | import os |
| 17 | import unittest |
| 18 | from unittest import mock |
| 19 | |
| 20 | from src.eval import runner as R |
| 21 | |
| 22 | |
| 23 | def _it(item_id, kategori="orta", senaryo=None, unite="U1", |
| 24 | critical=False, beklenen="cevapla"): |
| 25 | d = {"id": item_id, "kategori": kategori, "unite": unite, |
| 26 | "critical": critical, "beklenen_davranis": beklenen} |
| 27 | if senaryo: |
| 28 | d["senaryo"] = senaryo |
| 29 | return d |
| 30 | |
| 31 | |
| 32 | class StratifiedSamplingTests(unittest.TestCase): |
| 33 | def test_all_critical_items_are_mandatory(self): |
| 34 | items = [_it("c1", critical=True), _it("c2", critical=True), |
| 35 | _it("n1"), _it("n2")] |
| 36 | ids = R._select_judge_ids(items, sample_n=2) |
| 37 | self.assertIn("c1", ids) |
| 38 | self.assertIn("c2", ids) |
| 39 | |
| 40 | def test_every_category_gets_at_least_one(self): |
| 41 | |
| 42 | items = ([_it(f"k{i}", kategori="kolay") for i in range(20)] |
| 43 | + [_it(f"o{i}", kategori="orta") for i in range(20)] |
| 44 | + [_it("z1", kategori="zor", critical=True)]) |
| 45 | ids = R._select_judge_ids(items, sample_n=5) |
| 46 | cats = {it["kategori"] for it in items if it["id"] in ids} |
| 47 | self.assertEqual(cats, {"kolay", "orta", "zor"}) |
| 48 | |
| 49 | def test_every_unit_gets_at_least_one(self): |
| 50 | items = [_it("a", unite="U1", critical=True), _it("b", unite="U2"), |
| 51 | _it("c", unite="U3")] |
| 52 | ids = R._select_judge_ids(items, sample_n=1) |
| 53 | units = {it["unite"] for it in items if it["id"] in ids} |
| 54 | self.assertEqual(units, {"U1", "U2", "U3"}) |
| 55 | |
| 56 | def test_senaryo_is_part_of_the_stratum(self): |
| 57 | items = ([_it(f"d{i}", senaryo="direct") for i in range(10)] |
| 58 | + [_it(f"m{i}", senaryo="multi_hop") for i in range(10)] |
| 59 | + [_it(f"v{i}", senaryo="variant") for i in range(10)]) |
| 60 | ids = R._select_judge_ids(items, sample_n=3) |
| 61 | sen = {it.get("senaryo") for it in items if it["id"] in ids} |
| 62 | self.assertEqual(sen, {"direct", "multi_hop", "variant"}) |
| 63 | |
| 64 | def test_only_answerable_items_are_sampled(self): |
| 65 | items = [_it("a"), _it("r1", beklenen="red", critical=True), |
| 66 | _it("e1", beklenen="cekimser", critical=True)] |
| 67 | ids = R._select_judge_ids(items, sample_n=10) |
| 68 | self.assertEqual(ids, {"a"}) |
| 69 | |
| 70 | def test_deterministic_for_fixed_seed(self): |
| 71 | items = [_it(f"i{i}", kategori=("kolay" if i % 2 else "orta")) |
| 72 | for i in range(40)] |
| 73 | self.assertEqual(R._select_judge_ids(items, sample_n=12), |
| 74 | R._select_judge_ids(items, sample_n=12)) |
| 75 | |
| 76 | def test_different_seed_gives_different_sample(self): |
| 77 | items = [_it(f"i{i}") for i in range(40)] |
| 78 | a = R._select_judge_ids(items, sample_n=10, seed=1) |
| 79 | b = R._select_judge_ids(items, sample_n=10, seed=2) |
| 80 | self.assertNotEqual(a, b) |
| 81 | |
| 82 | def test_sample_size_is_respected_approximately(self): |
| 83 | items = [_it(f"i{i}", kategori=("kolay" if i % 3 else "orta")) |
| 84 | for i in range(100)] |
| 85 | ids = R._select_judge_ids(items, sample_n=25) |
| 86 | |
| 87 | self.assertGreaterEqual(len(ids), 25) |
| 88 | self.assertLess(len(ids), 50) |
| 89 | |
| 90 | def test_sample_larger_than_pool(self): |
| 91 | items = [_it("a"), _it("b")] |
| 92 | self.assertEqual(R._select_judge_ids(items, sample_n=99), {"a", "b"}) |
| 93 | |
| 94 | |
| 95 | class FrozenJudgeSetTests(unittest.TestCase): |
| 96 | def test_frozen_ids_loaded_from_file(self): |
| 97 | |
| 98 | import tempfile |
| 99 | items = [_it("a"), _it("b"), _it("c")] |
| 100 | with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False, |
| 101 | encoding="utf-8") as fh: |
| 102 | json.dump(["a", "c"], fh) |
| 103 | path = fh.name |
| 104 | try: |
| 105 | with mock.patch.object(R, "JUDGE_IDS_PATH", path): |
| 106 | self.assertEqual(R._select_judge_ids(items), {"a", "c"}) |
| 107 | finally: |
| 108 | os.unlink(path) |
| 109 | |
| 110 | def test_frozen_ids_missing_from_set_are_dropped_with_warning(self): |
| 111 | import tempfile |
| 112 | items = [_it("a")] |
| 113 | with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False, |
| 114 | encoding="utf-8") as fh: |
| 115 | json.dump(["a", "yok1", "yok2"], fh) |
| 116 | path = fh.name |
| 117 | try: |
| 118 | with mock.patch.object(R, "JUDGE_IDS_PATH", path): |
| 119 | self.assertEqual(R._select_judge_ids(items), {"a"}) |
| 120 | finally: |
| 121 | os.unlink(path) |
| 122 | |
| 123 | |
| 124 | class CompositionReportTests(unittest.TestCase): |
| 125 | def test_composition_exposes_coverage_and_breakdown(self): |
| 126 | items = ([_it(f"k{i}", kategori="kolay") for i in range(10)] |
| 127 | + [_it(f"z{i}", kategori="zor", critical=True) for i in range(3)]) |
| 128 | ids = R._select_judge_ids(items, sample_n=6) |
| 129 | comp = R._judge_composition(items, ids) |
| 130 | self.assertEqual(comp["n_answerable"], 13) |
| 131 | self.assertEqual(comp["n_selected"], len(ids)) |
| 132 | self.assertAlmostEqual(comp["coverage"], len(ids) / 13) |
| 133 | self.assertIn("kolay", comp["by_kategori"]) |
| 134 | self.assertIn("zor", comp["by_kategori"]) |
| 135 | self.assertEqual(comp["kategoriler_hakemsiz"], []) |
| 136 | |
| 137 | def test_composition_flags_uncovered_categories(self): |
| 138 | items = [_it("a", kategori="kolay"), _it("b", kategori="zor")] |
| 139 | comp = R._judge_composition(items, {"a"}) |
| 140 | self.assertEqual(comp["kategoriler_hakemsiz"], ["zor"]) |
| 141 | |
| 142 | |
| 143 | class RealGoldenSetTests(unittest.TestCase): |
| 144 | |
| 145 | |
| 146 | def _items(self, name): |
| 147 | path = os.path.join("tests", "golden", name) |
| 148 | if not os.path.exists(path): |
| 149 | self.skipTest(f"{name} yok") |
| 150 | return json.load(open(path, encoding="utf-8"))["items"] |
| 151 | |
| 152 | def test_bio_covers_kolay_and_orta_now(self): |
| 153 | items = self._items("golden_12bio_v1.json") |
| 154 | comp = R._judge_composition(items, R._select_judge_ids(items)) |
| 155 | self.assertIn("kolay", comp["by_kategori"]) |
| 156 | self.assertIn("orta", comp["by_kategori"]) |
| 157 | self.assertGreater(comp["coverage"], 0.25) |
| 158 | self.assertEqual(comp["kategoriler_hakemsiz"], []) |
| 159 | |
| 160 | def test_non_bio_sets_no_longer_get_zero_judges(self): |
| 161 | |
| 162 | for name in ("golden_12kimya_v1.json", "golden_12fizik_v1.json"): |
| 163 | items = self._items(name) |
| 164 | comp = R._judge_composition(items, R._select_judge_ids(items)) |
| 165 | with self.subTest(name): |
| 166 | self.assertGreater(comp["n_selected"], 0) |
| 167 | |
| 168 | |
| 169 | if __name__ == "__main__": |
| 170 | unittest.main() |