tests: improve tesseract coverage
This commit is contained in:
@@ -260,8 +260,8 @@ def generate_hocr(
|
||||
log,
|
||||
):
|
||||
|
||||
output_hocr = next(o for o in output_files if o.endswith('.hocr'))
|
||||
output_sidecar = next(o for o in output_files if o.endswith('.txt'))
|
||||
output_hocr = next(o for o in output_files if fspath(o).endswith('.hocr'))
|
||||
output_sidecar = next(o for o in output_files if fspath(o).endswith('.txt'))
|
||||
prefix = os.path.splitext(output_hocr)[0]
|
||||
|
||||
args_tesseract = tess_base_args(language, engine_mode)
|
||||
|
||||
@@ -15,7 +15,9 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with OCRmyPDF. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
from contextlib import contextmanager
|
||||
from os import fspath
|
||||
from pathlib import Path
|
||||
@@ -81,3 +83,94 @@ def test_no_languages(tmp_path):
|
||||
|
||||
with pytest.raises(MissingDependencyError):
|
||||
tesseract.languages(tesseract_env=env)
|
||||
|
||||
|
||||
def test_image_too_large_hocr(monkeypatch, resources, outdir):
|
||||
log = logging.getLogger('test_image_too_large_hocr')
|
||||
|
||||
def dummy_run(args, *, env=None, **kwargs):
|
||||
raise subprocess.CalledProcessError(1, 'tesseract', output=b'Image too large')
|
||||
|
||||
monkeypatch.setattr(tesseract, 'run', dummy_run)
|
||||
tesseract.generate_hocr(
|
||||
input_file=resources / 'crom.png',
|
||||
output_files=[outdir / 'out.hocr', outdir / 'out.txt'],
|
||||
language=['eng'],
|
||||
engine_mode=None,
|
||||
tessconfig=[],
|
||||
timeout=180.0,
|
||||
pagesegmode=None,
|
||||
log=log,
|
||||
user_words=None,
|
||||
user_patterns=None,
|
||||
tesseract_env=None,
|
||||
)
|
||||
assert "name='ocr-capabilities'" in Path(outdir / 'out.hocr').read_text()
|
||||
|
||||
|
||||
def test_image_too_large_pdf(monkeypatch, resources, outdir):
|
||||
log = logging.getLogger('test_image_too_large_pdf')
|
||||
|
||||
def dummy_run(args, *, env=None, **kwargs):
|
||||
raise subprocess.CalledProcessError(1, 'tesseract', output=b'Image too large')
|
||||
|
||||
monkeypatch.setattr(tesseract, 'run', dummy_run)
|
||||
tesseract.generate_pdf(
|
||||
input_image=resources / 'crom.png',
|
||||
skip_pdf=resources / 'blank.pdf',
|
||||
output_pdf=outdir / 'pdf.pdf',
|
||||
output_text=outdir / 'txt.txt',
|
||||
language=['eng'],
|
||||
engine_mode=None,
|
||||
text_only=False,
|
||||
tessconfig=[],
|
||||
timeout=180.0,
|
||||
pagesegmode=None,
|
||||
log=log,
|
||||
user_words=None,
|
||||
user_patterns=None,
|
||||
tesseract_env=None,
|
||||
)
|
||||
assert Path(outdir / 'txt.txt').read_text() == '[skipped page]'
|
||||
assert Path(outdir / 'pdf.pdf').samefile(resources / 'blank.pdf')
|
||||
|
||||
|
||||
def test_timeout(caplog):
|
||||
log = logging.getLogger('test_timeout')
|
||||
tesseract.page_timedout(log, '123456.png', 5)
|
||||
assert "123456" in caplog.text
|
||||
assert "took too long" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'in_, logged',
|
||||
[
|
||||
(b'Tesseract Open Source', ''),
|
||||
(b'lots of diacritics blah blah', 'diacritics'),
|
||||
(b'Warning in pixReadMem', ''),
|
||||
(b'OSD: Weak margin', 'unsure about page orientation'),
|
||||
(b'Error in pixScanForForeground', ''),
|
||||
(b'Error in boxClipToRectangle', ''),
|
||||
(b'an unexpected error', 'an unexpected error'),
|
||||
(b'a dire warning', 'a dire warning'),
|
||||
(b'read_params_file something', 'read_params_file'),
|
||||
(b'an innocent message', 'innocent'),
|
||||
(b'\x7f\x7f\x80innocent unicode failure', 'innocent'),
|
||||
],
|
||||
)
|
||||
def test_tesseract_log_output(caplog, in_, logged):
|
||||
log = logging.getLogger('tesseract_log_output')
|
||||
log.setLevel(logging.INFO)
|
||||
|
||||
tesseract.tesseract_log_output(log, in_, 'dummy')
|
||||
if logged == '':
|
||||
assert caplog.text == ''
|
||||
else:
|
||||
assert logged in caplog.text
|
||||
|
||||
|
||||
def test_tesseract_log_output_raises(caplog):
|
||||
log = logging.getLogger('tesseract_log_output')
|
||||
with pytest.raises(tesseract.TesseractConfigError):
|
||||
tesseract.tesseract_log_output(log, b'parameter not found: moo', 'dummy')
|
||||
assert 'not found' in caplog.text
|
||||
|
||||
Reference in New Issue
Block a user