| 1 | |
| 2 | import unittest |
| 3 | |
| 4 | from src.ingest.canonical import CanonicalUnit, CanonicalDoc |
| 5 | from src.ingest.pdf_parse import HEADING, PARAGRAPH |
| 6 | from src.chunk import chunk_document, approx_tokens |
| 7 | |
| 8 | WORDS40 = " ".join(f"kelime{i}" for i in range(40)) |
| 9 | |
| 10 | |
| 11 | def _u(n, text, kind=PARAGRAPH, page=1, vis="mixed", disi=False): |
| 12 | return CanonicalUnit(span_id=f"doc#{page}.{n}", doc_id="doc", sinif="12", |
| 13 | ders="biyoloji", kaynak_turu="ders_kitabi", page=page, |
| 14 | bbox=(0, 0, 100, 20), block_no=n, kind=kind, text=text, |
| 15 | page_visual=vis, retrieval_disi=disi) |
| 16 | |
| 17 | |
| 18 | def _doc(units): |
| 19 | return CanonicalDoc(source_path="x", doc_id="doc", source_version="v", sinif="12", |
| 20 | ders="biyoloji", kaynak_turu="ders_kitabi", page_count=1, units=units) |
| 21 | |
| 22 | |
| 23 | class TokenTests(unittest.TestCase): |
| 24 | def test_approx(self): |
| 25 | self.assertEqual(approx_tokens("a b c d"), round(4 * 1.4)) |
| 26 | self.assertEqual(approx_tokens(""), 0) |
| 27 | |
| 28 | |
| 29 | class ChunkTests(unittest.TestCase): |
| 30 | def setUp(self): |
| 31 | units = [_u(i, WORDS40) for i in range(8)] |
| 32 | self.doc = _doc(units) |
| 33 | self.chunks = chunk_document(self.doc) |
| 34 | self.children = [c for c in self.chunks if c.level == "child"] |
| 35 | self.parents = [c for c in self.chunks if c.level == "parent"] |
| 36 | |
| 37 | def test_children_created_and_sized(self): |
| 38 | self.assertGreaterEqual(len(self.children), 2) |
| 39 | |
| 40 | for c in self.children: |
| 41 | self.assertGreater(c.approx_tokens, 0) |
| 42 | |
| 43 | def test_all_span_ids_preserved(self): |
| 44 | got = {sid for c in self.children for sid in c.span_ids} |
| 45 | want = {u.span_id for u in self.doc.units} |
| 46 | self.assertEqual(got, want) |
| 47 | |
| 48 | def test_heading_starts_new_child_when_buffer_full(self): |
| 49 | |
| 50 | units = [_u(0, WORDS40), _u(1, WORDS40), _u(2, WORDS40), |
| 51 | _u(3, "1.2 Yeni Bölüm", HEADING), _u(4, WORDS40)] |
| 52 | chunks = [c for c in chunk_document(_doc(units)) if c.level == "child"] |
| 53 | heading_chunk = next(c for c in chunks if "doc#1.3" in c.span_ids) |
| 54 | self.assertNotIn("doc#1.0", heading_chunk.span_ids) |
| 55 | |
| 56 | def test_small_heading_does_not_split(self): |
| 57 | |
| 58 | units = [_u(0, "kısa"), _u(1, "1.2 Yeni Bölüm", HEADING), _u(2, WORDS40)] |
| 59 | chunks = [c for c in chunk_document(_doc(units)) if c.level == "child"] |
| 60 | self.assertEqual(len(chunks), 1) |
| 61 | |
| 62 | def test_parents_link_children(self): |
| 63 | self.assertGreaterEqual(len(self.parents), 1) |
| 64 | for p in self.parents: |
| 65 | self.assertTrue(p.child_ids) |
| 66 | for cid in p.child_ids: |
| 67 | child = next(c for c in self.children if c.chunk_id == cid) |
| 68 | self.assertEqual(child.parent_id, p.chunk_id) |
| 69 | |
| 70 | def test_parent_span_ids_union_of_children(self): |
| 71 | p = self.parents[0] |
| 72 | child_spans = {sid for cid in p.child_ids |
| 73 | for c in self.children if c.chunk_id == cid for sid in c.span_ids} |
| 74 | self.assertEqual(set(p.span_ids), child_spans) |
| 75 | |
| 76 | def test_excluded_units_not_chunked(self): |
| 77 | units = [_u(0, WORDS40), _u(1, "Değerlendirme Soruları " + WORDS40, disi=True)] |
| 78 | chunks = chunk_document(_doc(units)) |
| 79 | allspans = {sid for c in chunks for sid in c.span_ids} |
| 80 | self.assertIn("doc#1.0", allspans) |
| 81 | self.assertNotIn("doc#1.1", allspans) |
| 82 | |
| 83 | def test_metadata_propagated(self): |
| 84 | c = self.children[0] |
| 85 | self.assertEqual((c.doc_id, c.sinif, c.ders, c.kaynak_turu), |
| 86 | ("doc", "12", "biyoloji", "ders_kitabi")) |
| 87 | self.assertLessEqual(c.page_start, c.page_end) |
| 88 | |
| 89 | |
| 90 | class PageAlignedChildTests(unittest.TestCase): |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | def test_child_never_spans_pages(self): |
| 102 | units = [_u(0, WORDS40, page=1), _u(1, WORDS40, page=1), |
| 103 | _u(2, WORDS40, page=2), _u(3, WORDS40, page=2), |
| 104 | _u(4, WORDS40, page=3)] |
| 105 | kids = [c for c in chunk_document(_doc(units), page_aligned=True) |
| 106 | if c.level == "child"] |
| 107 | for c in kids: |
| 108 | self.assertEqual(c.page_start, c.page_end, f"{c.chunk_id} sayfa aşıyor") |
| 109 | |
| 110 | def test_old_behaviour_did_span_pages(self): |
| 111 | |
| 112 | units = [_u(i, WORDS40, page=1 + i // 2) for i in range(6)] |
| 113 | kids = [c for c in chunk_document(_doc(units), page_aligned=False) |
| 114 | if c.level == "child"] |
| 115 | self.assertTrue(any(c.page_start != c.page_end for c in kids)) |
| 116 | |
| 117 | def test_page_flush_is_unconditional(self): |
| 118 | |
| 119 | |
| 120 | units = [_u(0, "kısa", page=1), _u(1, WORDS40, page=2)] |
| 121 | kids = [c for c in chunk_document(_doc(units), page_aligned=True) |
| 122 | if c.level == "child"] |
| 123 | self.assertEqual(len(kids), 2) |
| 124 | self.assertEqual(kids[0].span_ids, ["doc#1.0"]) |
| 125 | |
| 126 | def test_no_unit_is_lost(self): |
| 127 | units = [_u(i, WORDS40, page=1 + i % 4) for i in range(12)] |
| 128 | kids = [c for c in chunk_document(_doc(units), page_aligned=True) |
| 129 | if c.level == "child"] |
| 130 | self.assertEqual({sid for c in kids for sid in c.span_ids}, |
| 131 | {u.span_id for u in units}) |
| 132 | |
| 133 | def test_parents_may_still_span_pages(self): |
| 134 | |
| 135 | |
| 136 | units = [_u(i, WORDS40, page=1 + i // 2) for i in range(20)] |
| 137 | pars = [c for c in chunk_document(_doc(units), page_aligned=True) |
| 138 | if c.level == "parent"] |
| 139 | self.assertTrue(pars) |
| 140 | self.assertTrue(any(p.page_start != p.page_end for p in pars)) |
| 141 | |
| 142 | def test_flag_defaults_from_env(self): |
| 143 | import importlib |
| 144 | from unittest import mock |
| 145 | from src.chunk import chunker |
| 146 | with mock.patch.dict("os.environ", {"RAG_CHUNK_PAGE_ALIGNED": "0"}): |
| 147 | m = importlib.reload(chunker) |
| 148 | try: |
| 149 | self.assertFalse(m.PAGE_ALIGNED_DEFAULT) |
| 150 | finally: |
| 151 | importlib.reload(chunker) |
| 152 | |
| 153 | def test_eval_and_production_read_the_same_switch(self): |
| 154 | |
| 155 | |
| 156 | import inspect |
| 157 | from src.eval import runner |
| 158 | from src.service import http_app |
| 159 | for mod in (runner, http_app): |
| 160 | src = inspect.getsource(mod) |
| 161 | self.assertIn("chunk_document(doc)", src) |
| 162 | |
| 163 | |
| 164 | if __name__ == "__main__": |
| 165 | unittest.main() |