diff --git a/tests/base.py b/tests/base.py index 892159d..2ad6351 100644 --- a/tests/base.py +++ b/tests/base.py @@ -18,7 +18,6 @@ class BaseTestClass: @classmethod def setup_class(cls): - cls.mode = "mupdf" cls.cams_file_name = os.getenv("CAMS_CAS_FILE") cls.new_cams_file_name = os.getenv("CAMS_CAS_FILE_NEW") cls.cams_summary_file_name = os.getenv("CAMS_CAS_SUMMARY") @@ -37,8 +36,7 @@ def setup_class(cls): ] def read_pdf(self, filename, password, output="dict"): - use_pdfminer = self.mode == "pdfminer" - return read_cas_pdf(filename, password, output=output, force_pdfminer=use_pdfminer) + return read_cas_pdf(filename, password, output=output) def test_output_json(self): for filename, password, num_folios, _ in self.pdf_files: @@ -81,8 +79,6 @@ def test_read_dict(self): for pdf_file, pdf_password, _, num_schemes in self.pdf_files: args = [pdf_file, "-p", pdf_password, "-a"] - if self.mode != "mupdf": - args.append("--force-pdfminer") result = runner.invoke(cli, args) assert result.exit_code == 0 clean_output = self.ansi_cleaner.sub("", result.output) diff --git a/tests/test_pdfminer.py b/tests/test_pdfminer.py index 6b8c3e0..c4a6b9b 100644 --- a/tests/test_pdfminer.py +++ b/tests/test_pdfminer.py @@ -1,18 +1,50 @@ +import sys + import pytest from pdfminer.layout import LTTextBoxHorizontal +from casparser import read_cas_pdf from casparser.exceptions import CASParseError from .base import BaseTestClass +try: + import builtins +except ImportError: + import __builtin__ as builtins + +realimport = builtins.__import__ + + +def mockimport(name, *args): + """Force ImportError on fitz and/or mupdf import and make casparser fallback to pdfminer""" + if name in ("fitz", "mupdf"): + raise ImportError + return realimport(name, *args) + + +@pytest.fixture(scope="class") +def monkeyclass(): + with pytest.MonkeyPatch.context() as mp: + yield mp + +@pytest.fixture(scope="class") +def use_pdfminer(monkeyclass): + if "fitz" in sys.modules: + del sys.modules["fitz"] + monkeyclass.setattr(builtins, "__import__", mockimport) + yield + monkeyclass.setattr(builtins, "__import__", realimport) + + +@pytest.mark.usefixtures("use_pdfminer") class TestPDFMiner(BaseTestClass): """Test pdfminer parser.""" @classmethod def setup_class(cls): BaseTestClass.setup_class() - cls.mode = "pdfminer" def test_bad_investor_info(self): from casparser.parsers.pdfminer import parse_investor_info @@ -22,3 +54,8 @@ def test_bad_investor_info(self): box.get_text() parse_investor_info([], 0, 0) assert "Unable to parse investor data" in str(exc_info) + + def test_invalid_file_type(self): + with pytest.raises(CASParseError) as exc_info: + read_cas_pdf(1, "", force_pdfminer=True) + assert "Invalid input" in str(exc_info)