2023-05-08 13:21:24 -04:00
|
|
|
import os
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2023-07-07 23:41:37 -05:00
|
|
|
from unstructured.partition import pdf, strategies
|
2023-05-08 13:21:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_validate_strategy_validates():
|
|
|
|
# Nothing should raise for a valid strategy
|
|
|
|
strategies.validate_strategy("hi_res", "pdf")
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_strategy_raises_for_bad_filetype():
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
strategies.validate_strategy("fast", "image")
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_strategy_raises_for_bad_strategy():
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
strategies.validate_strategy("totally_guess_the_text", "image")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("filename", "from_file", "expected"),
|
|
|
|
[
|
|
|
|
("layout-parser-paper-fast.pdf", True, True),
|
2023-07-07 23:41:37 -05:00
|
|
|
("copy-protected.pdf", True, True),
|
|
|
|
("loremipsum-flat.pdf", True, False),
|
2023-05-08 13:21:24 -04:00
|
|
|
("layout-parser-paper-fast.pdf", False, True),
|
2023-07-07 23:41:37 -05:00
|
|
|
("copy-protected.pdf", False, True),
|
|
|
|
("loremipsum-flat.pdf", False, False),
|
2023-05-08 13:21:24 -04:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_is_pdf_text_extractable(filename, from_file, expected):
|
|
|
|
filename = os.path.join("example-docs", filename)
|
|
|
|
|
|
|
|
if from_file:
|
|
|
|
with open(filename, "rb") as f:
|
2023-07-07 23:41:37 -05:00
|
|
|
extractable = pdf.extractable_elements(file=f)
|
2023-05-08 13:21:24 -04:00
|
|
|
else:
|
2023-07-07 23:41:37 -05:00
|
|
|
extractable = pdf.extractable_elements(filename=filename)
|
2023-05-08 13:21:24 -04:00
|
|
|
|
2023-07-07 23:41:37 -05:00
|
|
|
assert bool(extractable) is expected
|
2023-05-12 13:45:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("infer_table_structure", "expected"),
|
|
|
|
[
|
|
|
|
(True, "hi_res"),
|
|
|
|
(False, "ocr_only"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_determine_image_auto_strategy(infer_table_structure, expected):
|
|
|
|
strategy = strategies._determine_image_auto_strategy(
|
|
|
|
infer_table_structure=infer_table_structure,
|
|
|
|
)
|
|
|
|
assert strategy is expected
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("pdf_text_extractable", "infer_table_structure", "expected"),
|
|
|
|
[
|
|
|
|
(True, True, "hi_res"),
|
|
|
|
(False, True, "hi_res"),
|
|
|
|
(True, False, "fast"),
|
|
|
|
(False, False, "ocr_only"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_determine_image_pdf_strategy(pdf_text_extractable, infer_table_structure, expected):
|
|
|
|
strategy = strategies._determine_pdf_auto_strategy(
|
|
|
|
pdf_text_extractable=pdf_text_extractable,
|
|
|
|
infer_table_structure=infer_table_structure,
|
|
|
|
)
|
|
|
|
assert strategy is expected
|
2023-06-16 10:59:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_determine_pdf_or_image_strategy_fallback_ocr():
|
|
|
|
strategy = strategies.determine_pdf_or_image_strategy(
|
|
|
|
strategy="fast",
|
|
|
|
is_image=True,
|
|
|
|
)
|
|
|
|
assert strategy == "ocr_only"
|