| 1 | |
| 2 | |
| 3 | import json |
| 4 | import unittest |
| 5 | |
| 6 | from src.segment.rubric import DIMENSION_ORDER, DIMENSIONS, course_verbs, render_full_rubric |
| 7 | from src.segment.schema import SchemaError, extract_json, parse_label_json |
| 8 | |
| 9 | CHOICES = ["birinci sik", "ikinci sik", "ucuncu sik", "dorduncu sik"] |
| 10 | |
| 11 | |
| 12 | def payload(**over): |
| 13 | base = { |
| 14 | "boyutlar": { |
| 15 | "bilissel_talep": {"etiket": "uygulama", "guven": 0.8}, |
| 16 | "adim_sayisi": {"etiket": "tek_adim", "guven": 0.7}, |
| 17 | "dikkat_tuzagi": {"etiket": "yok", "guven": 0.9}, |
| 18 | "okuma_yuku": {"etiket": "dusuk", "guven": 0.6}, |
| 19 | }, |
| 20 | "tuzak_sik_index": None, |
| 21 | "gerekce": "Tek formullu duz hesap.", |
| 22 | } |
| 23 | base.update(over) |
| 24 | return json.dumps(base, ensure_ascii=False) |
| 25 | |
| 26 | |
| 27 | def parse(text): |
| 28 | return parse_label_json(text, question_id="q1", choices=CHOICES, |
| 29 | prompt_version="v1", model="mock", variant="baseline") |
| 30 | |
| 31 | |
| 32 | class TestSchema(unittest.TestCase): |
| 33 | |
| 34 | def test_gecerli_yanit(self): |
| 35 | seg = parse(payload()) |
| 36 | self.assertEqual(seg.label("bilissel_talep"), "uygulama") |
| 37 | self.assertEqual(seg.labels["okuma_yuku"].confidence, 0.6) |
| 38 | self.assertIsNone(seg.trap_choice_index) |
| 39 | self.assertEqual(seg.label_tuple(), |
| 40 | ("uygulama", "tek_adim", "yok", "dusuk")) |
| 41 | |
| 42 | def test_markdown_citasi_icindeki_json_cikarilir(self): |
| 43 | seg = parse("```json\n" + payload() + "\n```") |
| 44 | self.assertEqual(seg.label("adim_sayisi"), "tek_adim") |
| 45 | |
| 46 | def test_bos_yanit_reddedilir(self): |
| 47 | with self.assertRaises(SchemaError): |
| 48 | parse("") |
| 49 | |
| 50 | def test_json_olmayan_yanit_reddedilir(self): |
| 51 | with self.assertRaises(SchemaError): |
| 52 | parse("Bu soru bence uygulama duzeyindedir.") |
| 53 | |
| 54 | def test_eksik_boyut_reddedilir(self): |
| 55 | data = json.loads(payload()) |
| 56 | del data["boyutlar"]["okuma_yuku"] |
| 57 | with self.assertRaises(SchemaError) as ctx: |
| 58 | parse(json.dumps(data)) |
| 59 | self.assertIn("okuma_yuku", str(ctx.exception)) |
| 60 | |
| 61 | def test_izinsiz_etiket_reddedilir(self): |
| 62 | with self.assertRaises(SchemaError): |
| 63 | parse(payload(boyutlar={ |
| 64 | **json.loads(payload())["boyutlar"], |
| 65 | "bilissel_talep": {"etiket": "sentez", "guven": 0.9}})) |
| 66 | |
| 67 | def test_guven_yoksa_reddedilir(self): |
| 68 | b = json.loads(payload())["boyutlar"] |
| 69 | b["adim_sayisi"] = {"etiket": "tek_adim"} |
| 70 | with self.assertRaises(SchemaError): |
| 71 | parse(payload(boyutlar=b)) |
| 72 | |
| 73 | def test_yalnizca_etiket_metni_reddedilir(self): |
| 74 | b = json.loads(payload())["boyutlar"] |
| 75 | b["adim_sayisi"] = "tek_adim" |
| 76 | with self.assertRaises(SchemaError): |
| 77 | parse(payload(boyutlar=b)) |
| 78 | |
| 79 | def test_aralik_disi_guven_reddedilir(self): |
| 80 | b = json.loads(payload())["boyutlar"] |
| 81 | b["okuma_yuku"] = {"etiket": "dusuk", "guven": 1.4} |
| 82 | with self.assertRaises(SchemaError): |
| 83 | parse(payload(boyutlar=b)) |
| 84 | |
| 85 | def test_tuzak_var_ama_index_yok_reddedilir(self): |
| 86 | b = json.loads(payload())["boyutlar"] |
| 87 | b["dikkat_tuzagi"] = {"etiket": "var", "guven": 0.8} |
| 88 | with self.assertRaises(SchemaError): |
| 89 | parse(payload(boyutlar=b)) |
| 90 | |
| 91 | def test_tuzak_index_aralik_disi_reddedilir(self): |
| 92 | b = json.loads(payload())["boyutlar"] |
| 93 | b["dikkat_tuzagi"] = {"etiket": "var", "guven": 0.8} |
| 94 | with self.assertRaises(SchemaError): |
| 95 | parse(payload(boyutlar=b, tuzak_sik_index=9)) |
| 96 | |
| 97 | def test_tuzak_yok_iken_index_verilemez(self): |
| 98 | with self.assertRaises(SchemaError): |
| 99 | parse(payload(tuzak_sik_index=2)) |
| 100 | |
| 101 | def test_tuzak_var_metni_dogru_esler(self): |
| 102 | b = json.loads(payload())["boyutlar"] |
| 103 | b["dikkat_tuzagi"] = {"etiket": "var", "guven": 0.8} |
| 104 | seg = parse(payload(boyutlar=b, tuzak_sik_index=2)) |
| 105 | self.assertEqual(seg.trap_choice_index, 2) |
| 106 | self.assertEqual(seg.trap_choice_text, "ucuncu sik") |
| 107 | |
| 108 | def test_bos_gerekce_reddedilir(self): |
| 109 | with self.assertRaises(SchemaError): |
| 110 | parse(payload(gerekce=" ")) |
| 111 | |
| 112 | def test_extract_json_kok_liste_reddedilir(self): |
| 113 | with self.assertRaises(SchemaError): |
| 114 | extract_json("[1, 2, 3]") |
| 115 | |
| 116 | |
| 117 | class TestRubric(unittest.TestCase): |
| 118 | |
| 119 | def test_dort_boyut_ve_kategori_sayisi(self): |
| 120 | |
| 121 | self.assertEqual(len(DIMENSION_ORDER), 4) |
| 122 | self.assertLessEqual(max(len(DIMENSIONS[d].labels) for d in DIMENSION_ORDER), 3) |
| 123 | |
| 124 | def test_yalnizca_dikkat_tuzagi_dis_dogrulamali(self): |
| 125 | externally = [d for d in DIMENSION_ORDER if DIMENSIONS[d].externally_validated] |
| 126 | self.assertEqual(externally, ["dikkat_tuzagi"]) |
| 127 | |
| 128 | def test_her_boyutun_her_etiketi_icin_olcut_var(self): |
| 129 | for name in DIMENSION_ORDER: |
| 130 | spec = DIMENSIONS[name] |
| 131 | for label in spec.labels: |
| 132 | self.assertIn(label, spec.criteria) |
| 133 | self.assertTrue(spec.criteria[label].strip()) |
| 134 | |
| 135 | def test_derse_ozgu_fiiller(self): |
| 136 | |
| 137 | self.assertTrue(course_verbs("Matematik")) |
| 138 | self.assertEqual(course_verbs("Yok Boyle Ders"), {}) |
| 139 | text = render_full_rubric("Matematik") |
| 140 | self.assertIn("turevini al", text) |
| 141 | self.assertIn("EYLEM FIILLERI", text) |
| 142 | |
| 143 | |
| 144 | if __name__ == "__main__": |
| 145 | unittest.main() |