|
| 1 | +import io |
| 2 | + |
| 3 | +import pypdfium2 as pdfium |
| 4 | +import pytest |
| 5 | + |
| 6 | +from mindee.error import MindeeError |
| 7 | +from mindee.input.page_options import KEEP_ONLY, REMOVE, PageOptions |
| 8 | +from mindee.input.sources import ( |
| 9 | + Base64Input, |
| 10 | + BytesInput, |
| 11 | + FileInput, |
| 12 | + LocalInputSource, |
| 13 | + PathInput, |
| 14 | +) |
| 15 | +from tests.utils import FILE_TYPES_DIR, PRODUCT_DATA_DIR |
| 16 | + |
| 17 | + |
| 18 | +def _assert_page_options(input_source: LocalInputSource, numb_pages: int): |
| 19 | + assert input_source.is_pdf() is True |
| 20 | + # Currently the least verbose way of comparing pages with pypdfium2 |
| 21 | + # I.e., each page is read and rendered as a rasterized image. |
| 22 | + # These images are then compared as raw byte sequences. |
| 23 | + cut_pdf = pdfium.PdfDocument(input_source.file_object) |
| 24 | + pdf = pdfium.PdfDocument(FILE_TYPES_DIR / "pdf" / f"multipage_cut-{numb_pages}.pdf") |
| 25 | + for idx in range(len(pdf)): |
| 26 | + pdf_page = pdf.get_page(idx) |
| 27 | + pdf_page_render = pdfium.PdfPage.render(pdf_page) |
| 28 | + cut_pdf_page = cut_pdf.get_page(idx) |
| 29 | + cut_pdf_page_render = pdfium.PdfPage.render(cut_pdf_page) |
| 30 | + |
| 31 | + assert bytes(pdf_page_render.buffer) == bytes(cut_pdf_page_render.buffer) |
| 32 | + cut_pdf.close() |
| 33 | + pdf.close() |
| 34 | + |
| 35 | + |
| 36 | +def test_pdf_reconstruct_ok(): |
| 37 | + input_source = PathInput(FILE_TYPES_DIR / "pdf" / "multipage.pdf") |
| 38 | + input_source.process_pdf(behavior=KEEP_ONLY, on_min_pages=2, page_indexes=range(5)) |
| 39 | + assert isinstance(input_source.file_object, io.BytesIO) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize("numb_pages", [1, 2, 3]) |
| 43 | +def test_process_pdf_cut_n_pages(numb_pages: int): |
| 44 | + input_source = PathInput(FILE_TYPES_DIR / "pdf" / "multipage.pdf") |
| 45 | + input_source.process_pdf( |
| 46 | + behavior=KEEP_ONLY, on_min_pages=2, page_indexes=[0, -2, -1][:numb_pages] |
| 47 | + ) |
| 48 | + assert input_source.page_count == numb_pages |
| 49 | + _assert_page_options(input_source, numb_pages) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize("numb_pages", [1, 2, 3]) |
| 53 | +def test_apply_pages_pdf_cut_n_pages(numb_pages: int): |
| 54 | + input_source = PathInput(FILE_TYPES_DIR / "pdf" / "multipage.pdf") |
| 55 | + input_source.apply_page_options( |
| 56 | + PageOptions(on_min_pages=2, page_indexes=[0, -2, -1][:numb_pages]) |
| 57 | + ) |
| 58 | + assert input_source.count_doc_pages() == numb_pages |
| 59 | + _assert_page_options(input_source, numb_pages) |
| 60 | + |
| 61 | + |
| 62 | +def test_pdf_keep_5_first_pages(): |
| 63 | + input_source = PathInput(FILE_TYPES_DIR / "pdf" / "multipage.pdf") |
| 64 | + assert input_source.is_pdf() is True |
| 65 | + input_source.process_pdf( |
| 66 | + behavior=KEEP_ONLY, on_min_pages=2, page_indexes=[0, 1, 2, 3, 4] |
| 67 | + ) |
| 68 | + assert input_source.count_doc_pages() == 5 |
| 69 | + |
| 70 | + |
| 71 | +def test_pdf_keep_invalid_pages(): |
| 72 | + input_source = PathInput(FILE_TYPES_DIR / "pdf" / "multipage.pdf") |
| 73 | + assert input_source.is_pdf() is True |
| 74 | + input_source.process_pdf( |
| 75 | + behavior=KEEP_ONLY, on_min_pages=2, page_indexes=[0, 1, 17] |
| 76 | + ) |
| 77 | + assert input_source.count_doc_pages() == 2 |
| 78 | + |
| 79 | + |
| 80 | +def test_pdf_remove_5_last_pages(): |
| 81 | + input_source = PathInput(FILE_TYPES_DIR / "pdf" / "multipage.pdf") |
| 82 | + assert input_source.is_pdf() is True |
| 83 | + input_source.process_pdf( |
| 84 | + behavior=REMOVE, on_min_pages=2, page_indexes=[-5, -4, -3, -2, -1] |
| 85 | + ) |
| 86 | + assert input_source.count_doc_pages() == 7 |
| 87 | + |
| 88 | + |
| 89 | +def test_pdf_remove_5_first_pages(): |
| 90 | + input_source = PathInput(FILE_TYPES_DIR / "pdf" / "multipage.pdf") |
| 91 | + assert input_source.is_pdf() is True |
| 92 | + input_source.process_pdf( |
| 93 | + behavior=REMOVE, on_min_pages=2, page_indexes=list(range(5)) |
| 94 | + ) |
| 95 | + assert input_source.count_doc_pages() == 7 |
| 96 | + |
| 97 | + |
| 98 | +def test_pdf_remove_invalid_pages(): |
| 99 | + input_source = PathInput(FILE_TYPES_DIR / "pdf" / "multipage.pdf") |
| 100 | + assert input_source.is_pdf() is True |
| 101 | + input_source.process_pdf(behavior=REMOVE, on_min_pages=2, page_indexes=[16]) |
| 102 | + assert input_source.count_doc_pages() == 12 |
| 103 | + |
| 104 | + |
| 105 | +def test_pdf_keep_no_pages(): |
| 106 | + input_source = PathInput(FILE_TYPES_DIR / "pdf" / "multipage.pdf") |
| 107 | + assert input_source.is_pdf() is True |
| 108 | + # empty page indexes |
| 109 | + with pytest.raises(RuntimeError): |
| 110 | + input_source.process_pdf(behavior=KEEP_ONLY, on_min_pages=2, page_indexes=[]) |
| 111 | + # all invalid pages |
| 112 | + with pytest.raises(RuntimeError): |
| 113 | + input_source.process_pdf( |
| 114 | + behavior=KEEP_ONLY, on_min_pages=2, page_indexes=[16, 17] |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +def test_pdf_remove_all_pages(): |
| 119 | + input_source = PathInput(FILE_TYPES_DIR / "pdf" / "multipage.pdf") |
| 120 | + assert input_source.is_pdf() is True |
| 121 | + with pytest.raises(RuntimeError): |
| 122 | + input_source.process_pdf( |
| 123 | + behavior=REMOVE, on_min_pages=2, page_indexes=list(range(15)) |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | +def test_pdf_input_from_file(): |
| 128 | + with open(FILE_TYPES_DIR / "pdf" / "multipage.pdf", "rb") as fp: |
| 129 | + input_source = FileInput(fp) |
| 130 | + assert input_source.is_pdf() is True |
| 131 | + input_source.process_pdf(behavior=KEEP_ONLY, on_min_pages=2, page_indexes=[0]) |
| 132 | + assert input_source.count_doc_pages() == 1 |
| 133 | + |
| 134 | + |
| 135 | +def test_pdf_input_from_base64(): |
| 136 | + with open(PRODUCT_DATA_DIR / "invoices" / "invoice_10p.txt", "rt") as fp: |
| 137 | + input_source = Base64Input(fp.read(), filename="invoice_10p.pdf") |
| 138 | + assert input_source.is_pdf() is True |
| 139 | + input_source.process_pdf(behavior=KEEP_ONLY, on_min_pages=2, page_indexes=[0]) |
| 140 | + assert input_source.count_doc_pages() == 1 |
| 141 | + |
| 142 | + |
| 143 | +def test_pdf_input_from_bytes(): |
| 144 | + with open(PRODUCT_DATA_DIR / "invoices" / "invoice_10p.pdf", "rb") as fp: |
| 145 | + input_source = BytesInput(fp.read(), filename="invoice_10p.pdf") |
| 146 | + assert input_source.is_pdf() is True |
| 147 | + input_source.process_pdf(behavior=KEEP_ONLY, on_min_pages=2, page_indexes=[0]) |
| 148 | + assert input_source.count_doc_pages() == 1 |
| 149 | + |
| 150 | + |
| 151 | +def test_pdf_blank_check(): |
| 152 | + with pytest.raises(MindeeError): |
| 153 | + input_source = PathInput(FILE_TYPES_DIR / "pdf" / "blank.pdf") |
| 154 | + input_source.process_pdf(behavior=KEEP_ONLY, on_min_pages=2, page_indexes=[0]) |
| 155 | + |
| 156 | + with pytest.raises(MindeeError): |
| 157 | + input_source = PathInput(FILE_TYPES_DIR / "pdf" / "blank_1.pdf") |
| 158 | + input_source.process_pdf(behavior=KEEP_ONLY, on_min_pages=2, page_indexes=[0]) |
| 159 | + |
| 160 | + input_not_blank = PathInput(FILE_TYPES_DIR / "pdf" / "not_blank_image_only.pdf") |
| 161 | + assert input_not_blank.count_doc_pages() == 1 |
0 commit comments