| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | import os |
| 7 | import unittest |
| 8 | |
| 9 | from src.ingest import ocr as ocr_mod |
| 10 | from src.ingest.pdf_parse import Block, PARAGRAPH, HEADING, _page_needs_ocr, _ocr_blocks |
| 11 | |
| 12 | |
| 13 | def _b(text, block_no, kind=PARAGRAPH): |
| 14 | return Block(page=1, bbox=(0, 0, 400, 560), text=text, block_no=block_no, kind=kind) |
| 15 | |
| 16 | |
| 17 | class PageNeedsOcrTests(unittest.TestCase): |
| 18 | def test_empty_text_with_image_needs_ocr(self): |
| 19 | |
| 20 | self.assertTrue(_page_needs_ocr([], has_images=True, min_chars=20)) |
| 21 | |
| 22 | def test_empty_text_no_image_skips(self): |
| 23 | |
| 24 | self.assertFalse(_page_needs_ocr([], has_images=False, min_chars=20)) |
| 25 | |
| 26 | def test_healthy_text_layer_skips(self): |
| 27 | blocks = [_b("Bu sayfada bol miktarda öğretici gövde metni bulunuyor.", 0)] |
| 28 | self.assertFalse(_page_needs_ocr(blocks, has_images=True, min_chars=20)) |
| 29 | |
| 30 | def test_only_label_noise_still_needs_ocr(self): |
| 31 | |
| 32 | blocks = [_b("3,4 nm", 0)] |
| 33 | self.assertTrue(_page_needs_ocr(blocks, has_images=True, min_chars=20)) |
| 34 | |
| 35 | |
| 36 | class OcrBlocksTests(unittest.TestCase): |
| 37 | def test_splits_paragraphs(self): |
| 38 | text = "Birinci paragraf.\n\nİkinci paragraf.\n\n \n\nÜçüncü." |
| 39 | blocks = _ocr_blocks(text, page_no=5, width=400, height=560) |
| 40 | self.assertEqual([b.text for b in blocks], |
| 41 | ["Birinci paragraf.", "İkinci paragraf.", "Üçüncü."]) |
| 42 | self.assertTrue(all(b.ocr for b in blocks)) |
| 43 | self.assertTrue(all(b.is_body for b in blocks)) |
| 44 | self.assertTrue(all(b.page == 5 for b in blocks)) |
| 45 | self.assertEqual([b.block_no for b in blocks], [9000, 9001, 9002]) |
| 46 | self.assertTrue(all(b.bbox == (0.0, 0.0, 400, 560) for b in blocks)) |
| 47 | |
| 48 | def test_empty_text_no_blocks(self): |
| 49 | self.assertEqual(_ocr_blocks("", 1, 400, 560), []) |
| 50 | self.assertEqual(_ocr_blocks(" \n\n ", 1, 400, 560), []) |
| 51 | |
| 52 | |
| 53 | class GracefulDegradationTests(unittest.TestCase): |
| 54 | def test_unavailable_when_pytesseract_missing(self): |
| 55 | orig = ocr_mod._pt |
| 56 | ocr_mod._pt = lambda: None |
| 57 | try: |
| 58 | self.assertFalse(ocr_mod.ocr_available("tur")) |
| 59 | self.assertEqual(ocr_mod.ocr_page(None, lang="tur"), "") |
| 60 | finally: |
| 61 | ocr_mod._pt = orig |
| 62 | |
| 63 | def test_ocr_page_swallows_errors(self): |
| 64 | |
| 65 | class _Boom: |
| 66 | class pytesseract: |
| 67 | tesseract_cmd = "" |
| 68 | def image_to_string(self, *a, **k): |
| 69 | raise RuntimeError("motor patladı") |
| 70 | orig = ocr_mod._pt |
| 71 | ocr_mod._pt = lambda: _Boom() |
| 72 | try: |
| 73 | class _FakePage: |
| 74 | def get_pixmap(self, *a, **k): |
| 75 | raise RuntimeError("render patladı") |
| 76 | self.assertEqual(ocr_mod.ocr_page(_FakePage(), lang="tur"), "") |
| 77 | finally: |
| 78 | ocr_mod._pt = orig |
| 79 | |
| 80 | |
| 81 | class ResolveLangTests(unittest.TestCase): |
| 82 | def test_local_tur_preferred(self): |
| 83 | |
| 84 | if ocr_mod._local_tessdata("tur") is None: |
| 85 | self.skipTest("yerel tur.traineddata yok") |
| 86 | lang, tessdata_dir = ocr_mod._resolve_lang(object(), "tur") |
| 87 | self.assertEqual(lang, "tur") |
| 88 | |
| 89 | self.assertTrue(tessdata_dir.endswith("tessdata")) |
| 90 | self.assertTrue(os.path.isdir(tessdata_dir)) |
| 91 | |
| 92 | def test_falls_back_to_eng_when_lang_absent(self): |
| 93 | class _FakePt: |
| 94 | def get_languages(self, config=""): |
| 95 | return ["eng", "osd"] |
| 96 | |
| 97 | lang, tessdata_dir = ocr_mod._resolve_lang(_FakePt(), "deu") |
| 98 | self.assertEqual(lang, "eng") |
| 99 | self.assertEqual(tessdata_dir, "") |
| 100 | |
| 101 | |
| 102 | if __name__ == "__main__": |
| 103 | unittest.main() |