forge
Explore
Log in

Don't you dare go Hollow

hezarfen_rag

Clone
★ 0

0 watching0 forksPython2.4 MiB184b84dAhmet Kaan Gümüş2 months ago189 commits

test_http_app.py67 lines, 2.6 KiBHistoryRawDownload
1"""#2/D3 HTTP servisi testleri — FastAPI TestClient + stub RagService (model yok)."""
2import unittest
3
4try:
5 from fastapi.testclient import TestClient
6 from src.service.http_app import create_app
7 _HAS_FASTAPI = True
8except Exception:
9 _HAS_FASTAPI = False
10
11
12class _StubService:
13 def __init__(self):
14 self.last = {}
15 def chat(self, req):
16 self.last["chat"] = req
17 return {"text": "Cevap [1].", "abstained": False, "reason": "",
18 "citations": [{"n": 1, "pages": [10]}], "used_source_ids": ["c1"],
19 "cost_usd": 0.001, "cache_hit": False}
20 def summarize(self, req):
21 self.last["summarize"] = req
22 return {"text": "Özet.", "abstained": False, "reason": "", "citations": [],
23 "scope_pages": [10, 11], "hierarchical": False, "cost_usd": 0.02}
24 def generate_questions(self, req):
25 self.last["questions"] = req
26 return {"items": [{"soru": "s1", "cevap": "c1", "zorluk": "orta"}],
27 "abstained": False, "reason": "", "span_ids": [], "pages": [10], "cost_usd": 0.01}
28
29
30@unittest.skipUnless(_HAS_FASTAPI, "fastapi yok")
31class HttpAppTests(unittest.TestCase):
32 def setUp(self):
33 self.svc = _StubService()
34 self.client = TestClient(create_app(self.svc))
35
36 def test_health(self):
37 r = self.client.get("/health")
38 self.assertEqual(r.status_code, 200)
39 self.assertEqual(r.json()["status"], "ok")
40
41 def test_chat(self):
42 r = self.client.post("/rag/chat", json={
43 "query": "DNA nedir", "role": {"role": "student", "sinif": "12",
44 "ders_list": ["biyoloji"]}})
45 self.assertEqual(r.status_code, 200)
46 self.assertEqual(r.json()["text"], "Cevap [1].")
47 self.assertEqual(self.svc.last["chat"]["query"], "DNA nedir")
48 self.assertEqual(self.svc.last["chat"]["role"]["role"], "student")
49
50 def test_chat_validation_missing_query(self):
51 r = self.client.post("/rag/chat", json={"role": {"role": "student"}})
52 self.assertEqual(r.status_code, 422) # query zorunlu
53
54 def test_summarize(self):
55 r = self.client.post("/rag/summarize", json={"scope": {"pages": [10, 11]}})
56 self.assertEqual(r.status_code, 200)
57 self.assertEqual(r.json()["scope_pages"], [10, 11])
58
59 def test_questions(self):
60 r = self.client.post("/rag/questions", json={"scope": {"pages": [10]}, "n": 3})
61 self.assertEqual(r.status_code, 200)
62 self.assertEqual(len(r.json()["items"]), 1)
63 self.assertEqual(self.svc.last["questions"]["n"], 3)
64
65
66if __name__ == "__main__":
67 unittest.main()