Skip to content

Commit

Permalink
better test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
codereverser committed Dec 22, 2024
1 parent 6f59ec6 commit 8d3cf77
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
6 changes: 1 addition & 5 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
39 changes: 38 additions & 1 deletion tests/test_pdfminer.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

0 comments on commit 8d3cf77

Please sign in to comment.