| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | import os |
| 16 | import tempfile |
| 17 | import unittest |
| 18 | |
| 19 | import numpy as np |
| 20 | |
| 21 | from src.index import disk_cache |
| 22 | |
| 23 | |
| 24 | class DiskCacheTests(unittest.TestCase): |
| 25 | def setUp(self): |
| 26 | self.d = tempfile.TemporaryDirectory() |
| 27 | self.root = self.d.name |
| 28 | self.addCleanup(self.d.cleanup) |
| 29 | |
| 30 | @staticmethod |
| 31 | def _ornek(n=3, dim=4): |
| 32 | ids = [f"doc#{i}" for i in range(n)] |
| 33 | vecs = np.arange(n * dim, dtype=np.float32).reshape(n, dim) |
| 34 | sparse = [{"1": 0.5}, {}, {"2": 1.0}][:n] |
| 35 | return ids, vecs, sparse |
| 36 | |
| 37 | def test_round_trip_is_exact(self): |
| 38 | ids, vecs, sparse = self._ornek() |
| 39 | key = disk_cache.cache_key("doc", ["a", "b", "c"], "sig") |
| 40 | self.assertIsNotNone(disk_cache.save(self.root, "doc", key, ids, vecs, sparse)) |
| 41 | got = disk_cache.load(self.root, "doc", key, dim=4, n=3) |
| 42 | self.assertIsNotNone(got) |
| 43 | g_ids, g_vecs, g_sparse = got |
| 44 | self.assertEqual(g_ids, ids) |
| 45 | self.assertTrue(np.array_equal(g_vecs, vecs)) |
| 46 | self.assertEqual(g_sparse, sparse) |
| 47 | |
| 48 | def test_key_is_content_and_model_derived(self): |
| 49 | a = disk_cache.cache_key("doc", ["bir metin"], "voyage|m|1024") |
| 50 | self.assertNotEqual(a, disk_cache.cache_key("doc", ["başka metin"], "voyage|m|1024")) |
| 51 | self.assertNotEqual(a, disk_cache.cache_key("doc", ["bir metin"], "cohere|m|1024")) |
| 52 | self.assertNotEqual(a, disk_cache.cache_key("baska", ["bir metin"], "voyage|m|1024")) |
| 53 | self.assertEqual(a, disk_cache.cache_key("doc", ["bir metin"], "voyage|m|1024")) |
| 54 | |
| 55 | def test_empty_root_disables_cache(self): |
| 56 | ids, vecs, sparse = self._ornek() |
| 57 | key = disk_cache.cache_key("doc", ["x"], "sig") |
| 58 | self.assertIsNone(disk_cache.save("", "doc", key, ids, vecs, sparse)) |
| 59 | self.assertIsNone(disk_cache.load("", "doc", key, dim=4, n=3)) |
| 60 | self.assertEqual(disk_cache.cache_root({}), "") |
| 61 | |
| 62 | def test_missing_and_corrupt_are_misses_not_errors(self): |
| 63 | key = disk_cache.cache_key("doc", ["x"], "sig") |
| 64 | self.assertIsNone(disk_cache.load(self.root, "doc", key, dim=4, n=3)) |
| 65 | with open(os.path.join(self.root, "doc." + key + ".npz"), "wb") as fh: |
| 66 | fh.write(b"bu bir npz degil") |
| 67 | self.assertIsNone(disk_cache.load(self.root, "doc", key, dim=4, n=3)) |
| 68 | |
| 69 | def test_shape_mismatch_is_a_miss(self): |
| 70 | |
| 71 | ids, vecs, sparse = self._ornek() |
| 72 | key = disk_cache.cache_key("doc", ["a", "b", "c"], "sig") |
| 73 | disk_cache.save(self.root, "doc", key, ids, vecs, sparse) |
| 74 | self.assertIsNone(disk_cache.load(self.root, "doc", key, dim=1024, n=3)) |
| 75 | self.assertIsNone(disk_cache.load(self.root, "doc", key, dim=4, n=2)) |
| 76 | |
| 77 | |
| 78 | try: |
| 79 | from fastapi.testclient import TestClient |
| 80 | from src.service.http_app import _LazyService, create_app |
| 81 | _HAS_FASTAPI = True |
| 82 | except Exception: |
| 83 | _HAS_FASTAPI = False |
| 84 | |
| 85 | |
| 86 | class _Gercek: |
| 87 | generator = object() |
| 88 | doc = object() |
| 89 | question_gen = object() |
| 90 | summarizer = object() |
| 91 | |
| 92 | def chat(self, req): |
| 93 | return {"text": "x", "abstained": False, "reason": ""} |
| 94 | |
| 95 | def summarize(self, req): |
| 96 | return {"text": "x", "abstained": False, "reason": ""} |
| 97 | |
| 98 | def generate_questions(self, req): |
| 99 | return {"items": [], "abstained": False, "reason": ""} |
| 100 | |
| 101 | |
| 102 | @unittest.skipUnless(_HAS_FASTAPI, "fastapi yok") |
| 103 | class ReadinessShapeTests(unittest.TestCase): |
| 104 | def test_before_corpus_build_readiness_declares_provider_only(self): |
| 105 | lazy = _LazyService() |
| 106 | c = TestClient(create_app(lazy)) |
| 107 | r = c.get("/ready") |
| 108 | b = r.json() |
| 109 | self.assertEqual(r.status_code, 503) |
| 110 | self.assertFalse(b["korpus_hazir"]) |
| 111 | self.assertEqual(b["hazir_tur"], "yok") |
| 112 | |
| 113 | def test_after_corpus_build_readiness_is_full(self): |
| 114 | lazy = _LazyService() |
| 115 | c = TestClient(create_app(lazy)) |
| 116 | lazy.ata(_Gercek()) |
| 117 | r = c.get("/ready") |
| 118 | b = r.json() |
| 119 | self.assertEqual(r.status_code, 200) |
| 120 | self.assertTrue(b["korpus_hazir"]) |
| 121 | self.assertEqual(b["hazir_tur"], "tam") |
| 122 | |
| 123 | |
| 124 | class MultiCorpusReadinessTests(unittest.TestCase): |
| 125 | def test_generator_is_none_until_a_corpus_is_loaded(self): |
| 126 | |
| 127 | from src.service.multi import MultiCorpusService |
| 128 | s = MultiCorpusService(["10/biyoloji"], school="okul-a", shared=object()) |
| 129 | s._specs = {k: __file__ for k in s._specs} |
| 130 | self.assertFalse(s.korpus_hazir) |
| 131 | self.assertIsNone(s.generator) |
| 132 | |
| 133 | |
| 134 | if __name__ == "__main__": |
| 135 | unittest.main() |