tests: assert that most patched functions are called
We were not actually checking if functions we patched we called when expected.
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import patch
|
||||
|
||||
from ocrmypdf import hookimpl
|
||||
from ocrmypdf.builtin_plugins import ghostscript
|
||||
@@ -37,10 +37,8 @@ def run_append_stderr(*args, **kwargs):
|
||||
|
||||
@hookimpl
|
||||
def generate_pdfa(pdf_pages, pdfmark, output_file, compression, pdf_version, pdfa_part):
|
||||
m = Mock()
|
||||
m.side_effect = run_append_stderr
|
||||
|
||||
with patch('ocrmypdf._exec.ghostscript.run_polling_stderr', m):
|
||||
with patch('ocrmypdf._exec.ghostscript.run_polling_stderr') as mock:
|
||||
mock.side_effect = run_append_stderr
|
||||
ghostscript.generate_pdfa(
|
||||
pdf_pages=pdf_pages,
|
||||
pdfmark=pdfmark,
|
||||
@@ -50,5 +48,5 @@ def generate_pdfa(pdf_pages, pdfmark, output_file, compression, pdf_version, pdf
|
||||
pdfa_part=pdfa_part,
|
||||
progressbar_class=None,
|
||||
)
|
||||
m.assert_called_once()
|
||||
mock.assert_called_once()
|
||||
return output_file
|
||||
|
||||
@@ -39,7 +39,8 @@ def run_rig_args(args, **kwargs):
|
||||
|
||||
@hookimpl
|
||||
def generate_pdfa(pdf_pages, pdfmark, output_file, compression, pdf_version, pdfa_part):
|
||||
with patch('ocrmypdf._exec.ghostscript.run_polling_stderr', new=run_rig_args):
|
||||
with patch('ocrmypdf._exec.ghostscript.run_polling_stderr') as mock:
|
||||
mock.side_effect = run_rig_args
|
||||
ghostscript.generate_pdfa(
|
||||
pdf_pages=pdf_pages,
|
||||
pdfmark=pdfmark,
|
||||
@@ -49,4 +50,5 @@ def generate_pdfa(pdf_pages, pdfmark, output_file, compression, pdf_version, pdf
|
||||
pdfa_part=pdfa_part,
|
||||
progressbar_class=None,
|
||||
)
|
||||
mock.assert_called()
|
||||
return output_file
|
||||
|
||||
@@ -44,7 +44,8 @@ def rasterize_pdf_page(
|
||||
rotation=None,
|
||||
filter_vector=False,
|
||||
) -> Path:
|
||||
with patch('ocrmypdf._exec.ghostscript.run', new=raise_gs_fail):
|
||||
with patch('ocrmypdf._exec.ghostscript.run') as mock:
|
||||
mock.side_effect = raise_gs_fail
|
||||
ghostscript.rasterize_pdf_page(
|
||||
input_file=input_file,
|
||||
output_file=output_file,
|
||||
@@ -55,4 +56,5 @@ def rasterize_pdf_page(
|
||||
rotation=rotation,
|
||||
filter_vector=filter_vector,
|
||||
)
|
||||
mock.assert_called()
|
||||
return output_file
|
||||
|
||||
@@ -34,7 +34,8 @@ def raise_gs_fail(*args, **kwargs):
|
||||
|
||||
@hookimpl
|
||||
def generate_pdfa(pdf_pages, pdfmark, output_file, compression, pdf_version, pdfa_part):
|
||||
with patch('ocrmypdf._exec.ghostscript.run_polling_stderr', new=raise_gs_fail):
|
||||
with patch('ocrmypdf._exec.ghostscript.run_polling_stderr') as mock:
|
||||
mock.side_effect = raise_gs_fail
|
||||
ghostscript.generate_pdfa(
|
||||
pdf_pages=pdf_pages,
|
||||
pdfmark=pdfmark,
|
||||
@@ -44,4 +45,5 @@ def generate_pdfa(pdf_pages, pdfmark, output_file, compression, pdf_version, pdf
|
||||
pdfa_part=pdfa_part,
|
||||
progressbar_class=None,
|
||||
)
|
||||
mock.assert_called()
|
||||
return output_file
|
||||
|
||||
@@ -26,6 +26,7 @@ that is not UTF-8 compatible, so we are forced to check that we can convert it
|
||||
and present it to the user.
|
||||
"""
|
||||
|
||||
from contextlib import contextmanager
|
||||
from subprocess import CalledProcessError
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -42,17 +43,25 @@ def bad_utf8(*args, **kwargs):
|
||||
)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def patch_tesseract_run():
|
||||
with patch('ocrmypdf._exec.tesseract.run') as mock:
|
||||
mock.side_effect = bad_utf8
|
||||
yield
|
||||
mock.assert_called()
|
||||
|
||||
|
||||
class BadUtf8OcrEngine(TesseractOcrEngine):
|
||||
@staticmethod
|
||||
def generate_hocr(input_file, output_hocr, output_text, options):
|
||||
with patch('ocrmypdf._exec.tesseract.run', new=bad_utf8):
|
||||
with patch_tesseract_run():
|
||||
TesseractOcrEngine.generate_hocr(
|
||||
input_file, output_hocr, output_text, options
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def generate_pdf(input_file, output_pdf, output_text, options):
|
||||
with patch('ocrmypdf._exec.tesseract.run', new=bad_utf8):
|
||||
with patch_tesseract_run():
|
||||
TesseractOcrEngine.generate_pdf(
|
||||
input_file, output_pdf, output_text, options
|
||||
)
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
from contextlib import contextmanager
|
||||
from subprocess import CalledProcessError
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -35,22 +36,30 @@ def raise_size_exception(*args, **kwargs):
|
||||
)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def patch_tesseract_run():
|
||||
with patch('ocrmypdf._exec.tesseract.run') as mock:
|
||||
mock.side_effect = raise_size_exception
|
||||
yield
|
||||
mock.assert_called()
|
||||
|
||||
|
||||
class BigImageErrorOcrEngine(TesseractOcrEngine):
|
||||
@staticmethod
|
||||
def get_orientation(input_file, options):
|
||||
with patch('ocrmypdf._exec.tesseract.run', new=raise_size_exception):
|
||||
with patch_tesseract_run():
|
||||
return TesseractOcrEngine.get_orientation(input_file, options)
|
||||
|
||||
@staticmethod
|
||||
def generate_hocr(input_file, output_hocr, output_text, options):
|
||||
with patch('ocrmypdf._exec.tesseract.run', new=raise_size_exception):
|
||||
with patch_tesseract_run():
|
||||
TesseractOcrEngine.generate_hocr(
|
||||
input_file, output_hocr, output_text, options
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def generate_pdf(input_file, output_pdf, output_text, options):
|
||||
with patch('ocrmypdf._exec.tesseract.run', new=raise_size_exception):
|
||||
with patch_tesseract_run():
|
||||
TesseractOcrEngine.generate_pdf(
|
||||
input_file, output_pdf, output_text, options
|
||||
)
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
import signal
|
||||
from contextlib import contextmanager
|
||||
from subprocess import CalledProcessError
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -37,22 +38,30 @@ def raise_crash(*args, **kwargs):
|
||||
)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def patch_tesseract_run():
|
||||
with patch('ocrmypdf._exec.tesseract.run') as mock:
|
||||
mock.side_effect = raise_crash
|
||||
yield
|
||||
mock.assert_called()
|
||||
|
||||
|
||||
class CrashOcrEngine(TesseractOcrEngine):
|
||||
@staticmethod
|
||||
def get_orientation(input_file, options):
|
||||
with patch('ocrmypdf._exec.tesseract.run', new=raise_crash):
|
||||
with patch_tesseract_run():
|
||||
return TesseractOcrEngine.get_orientation(input_file, options)
|
||||
|
||||
@staticmethod
|
||||
def generate_hocr(input_file, output_hocr, output_text, options):
|
||||
with patch('ocrmypdf._exec.tesseract.run', new=raise_crash):
|
||||
with patch_tesseract_run():
|
||||
TesseractOcrEngine.generate_hocr(
|
||||
input_file, output_hocr, output_text, options
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def generate_pdf(input_file, output_pdf, output_text, options):
|
||||
with patch('ocrmypdf._exec.tesseract.run', new=raise_crash):
|
||||
with patch_tesseract_run():
|
||||
TesseractOcrEngine.generate_pdf(
|
||||
input_file, output_pdf, output_text, options
|
||||
)
|
||||
|
||||
@@ -36,12 +36,17 @@ class TestSafeSymlink:
|
||||
|
||||
|
||||
def test_no_cpu_count(monkeypatch):
|
||||
invoked = False
|
||||
|
||||
def cpu_count_raises():
|
||||
nonlocal invoked
|
||||
invoked = True
|
||||
raise NotImplementedError()
|
||||
|
||||
monkeypatch.setattr(multiprocessing, 'cpu_count', cpu_count_raises)
|
||||
with pytest.warns(expected_warning=UserWarning):
|
||||
assert helpers.available_cpu_count() == 1
|
||||
assert invoked, "Patched function called during test"
|
||||
|
||||
|
||||
def test_deprecated():
|
||||
|
||||
@@ -65,11 +65,12 @@ def test_cmyk_no_icc(caplog, resources, no_outpdf):
|
||||
def test_img2pdf_fails(resources, no_outpdf):
|
||||
with patch(
|
||||
'ocrmypdf._pipeline.img2pdf.convert', side_effect=img2pdf.ImageOpenError()
|
||||
):
|
||||
) as mock:
|
||||
rc = run_ocrmypdf_api(
|
||||
resources / 'baiona_gray.png', no_outpdf, '--image-dpi', '200'
|
||||
)
|
||||
assert rc == ocrmypdf.ExitCode.input_file
|
||||
mock.assert_called()
|
||||
|
||||
|
||||
def test_jpeg_in_jpeg_out(resources, outpdf):
|
||||
|
||||
@@ -318,7 +318,7 @@ def test_metadata_fixup_warning(resources, outdir, caplog):
|
||||
)
|
||||
metadata_fixup(working_file=outdir / 'graph.pdf', context=context)
|
||||
for record in caplog.records:
|
||||
assert record.levelname != 'WARNING'
|
||||
assert record.levelname != 'WARNING', "Unexpected warning"
|
||||
|
||||
# Now add some metadata that will not be copyable
|
||||
graph = pikepdf.open(outdir / 'graph.pdf')
|
||||
|
||||
@@ -141,7 +141,8 @@ def test_multiple_pngs(resources, outdir):
|
||||
draw.rectangle((0, 0, im.width, im.height), fill=128)
|
||||
im.save(output_file)
|
||||
|
||||
with patch('ocrmypdf.optimize.pngquant.quantize', new=mockquant):
|
||||
with patch('ocrmypdf.optimize.pngquant.quantize') as mock:
|
||||
mock.side_effect = mockquant
|
||||
check_ocrmypdf(
|
||||
outdir / 'in.pdf',
|
||||
outdir / 'out.pdf',
|
||||
@@ -155,6 +156,7 @@ def test_multiple_pngs(resources, outdir):
|
||||
'--plugin',
|
||||
'tests/plugins/tesseract_noop.py',
|
||||
)
|
||||
mock.assert_called()
|
||||
|
||||
with pikepdf.open(outdir / 'in.pdf') as inpdf, pikepdf.open(
|
||||
outdir / 'out.pdf'
|
||||
|
||||
@@ -28,11 +28,12 @@ def test_no_unpaper(resources, no_outpdf):
|
||||
output = fspath(no_outpdf)
|
||||
|
||||
_parser, options, pm = get_parser_options_plugins(["--clean", input_, output])
|
||||
with patch("ocrmypdf._exec.unpaper.version") as mock_unpaper_version:
|
||||
mock_unpaper_version.side_effect = FileNotFoundError("unpaper")
|
||||
with patch("ocrmypdf._exec.unpaper.version") as mock:
|
||||
mock.side_effect = FileNotFoundError("unpaper")
|
||||
|
||||
with pytest.raises(MissingDependencyError):
|
||||
check_options(options, pm)
|
||||
mock.assert_called()
|
||||
|
||||
|
||||
def test_old_unpaper(resources, no_outpdf):
|
||||
@@ -40,11 +41,12 @@ def test_old_unpaper(resources, no_outpdf):
|
||||
output = fspath(no_outpdf)
|
||||
|
||||
_parser, options, pm = get_parser_options_plugins(["--clean", input_, output])
|
||||
with patch("ocrmypdf._exec.unpaper.version") as mock_unpaper_version:
|
||||
mock_unpaper_version.return_value = '0.5'
|
||||
with patch("ocrmypdf._exec.unpaper.version") as mock:
|
||||
mock.return_value = '0.5'
|
||||
|
||||
with pytest.raises(MissingDependencyError):
|
||||
check_options(options, pm)
|
||||
mock.assert_called()
|
||||
|
||||
|
||||
@pytest.mark.skipif(not have_unpaper(), reason="requires unpaper")
|
||||
|
||||
@@ -188,18 +188,20 @@ def test_language_warning(caplog):
|
||||
caplog.set_level(logging.DEBUG)
|
||||
with patch(
|
||||
'ocrmypdf._validation.locale.getlocale', return_value=('en_US', 'UTF-8')
|
||||
):
|
||||
) as mock:
|
||||
vd.check_options_languages(opts, {'eng'})
|
||||
assert opts.languages == {'eng'}
|
||||
assert '' in caplog.text
|
||||
mock.assert_called_once()
|
||||
|
||||
opts = make_opts(language=None)
|
||||
with patch(
|
||||
'ocrmypdf._validation.locale.getlocale', return_value=('fr_FR', 'UTF-8')
|
||||
):
|
||||
) as mock:
|
||||
vd.check_options_languages(opts, {'eng'})
|
||||
assert opts.languages == {'eng'}
|
||||
assert 'assuming --language' in caplog.text
|
||||
mock.assert_called_once()
|
||||
|
||||
|
||||
def test_version_comparison():
|
||||
@@ -265,7 +267,8 @@ def test_pagesegmode_warning(caplog):
|
||||
|
||||
|
||||
def test_two_languages():
|
||||
with patch('ocrmypdf._exec.tesseract.has_textonly_pdf', return_value=True):
|
||||
with patch('ocrmypdf._exec.tesseract.has_textonly_pdf', return_value=True) as mock:
|
||||
vd._check_options(
|
||||
*make_opts_pm(language='fakelang1+fakelang2'), {'fakelang1', 'fakelang2'}
|
||||
)
|
||||
mock.assert_called()
|
||||
|
||||
Reference in New Issue
Block a user