File size: 1,087 Bytes
8c3e275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# SPDX-FileCopyrightText: 2026 Team Centurions
# SPDX-License-Identifier: AGPL-3.0-or-later

import numpy as np
import pytest

from pageparse.ocr.printed import PrintedOCR


def test_tesseract_not_found(monkeypatch: pytest.MonkeyPatch):
    monkeypatch.setattr("pageparse.ocr.printed.shutil.which", lambda _: None)
    monkeypatch.setattr("pageparse.ocr.printed.TESSERACT_CANDIDATES", [])
    with pytest.raises(RuntimeError, match="Tesseract not found"):
        PrintedOCR()


def test_printed_ocr_recognize_failure(monkeypatch: pytest.MonkeyPatch):
    monkeypatch.setattr("pageparse.ocr.printed.shutil.which", lambda _: "/usr/bin/tesseract")

    import subprocess

    def fake_run(*args, **kwargs):
        class FakeResult:
            returncode = 1
            stdout = ""
            stderr = "error message"

        return FakeResult()

    monkeypatch.setattr(subprocess, "run", fake_run)

    ocr = PrintedOCR(tesseract_cmd="tesseract")
    img = np.zeros((50, 50), dtype=np.uint8)
    with pytest.raises(RuntimeError, match="Tesseract failed"):
        ocr.recognize(img)