|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from pdfrest import AsyncPdfRestClient, PdfRestApiError, PdfRestClient |
| 6 | +from pdfrest.models import PdfRestFile |
| 7 | + |
| 8 | +from ..resources import get_test_resource_path |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(scope="module") |
| 12 | +def uploaded_pdf_with_forms( |
| 13 | + pdfrest_api_key: str, |
| 14 | + pdfrest_live_base_url: str, |
| 15 | +) -> PdfRestFile: |
| 16 | + resource = get_test_resource_path("form_with_data.pdf") |
| 17 | + with PdfRestClient( |
| 18 | + api_key=pdfrest_api_key, |
| 19 | + base_url=pdfrest_live_base_url, |
| 20 | + ) as client: |
| 21 | + return client.files.create_from_paths([resource])[0] |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="module") |
| 25 | +def uploaded_form_data_file( |
| 26 | + pdfrest_api_key: str, |
| 27 | + pdfrest_live_base_url: str, |
| 28 | +) -> PdfRestFile: |
| 29 | + resource = get_test_resource_path("form_data.xml") |
| 30 | + with PdfRestClient( |
| 31 | + api_key=pdfrest_api_key, |
| 32 | + base_url=pdfrest_live_base_url, |
| 33 | + ) as client: |
| 34 | + return client.files.create_from_paths([resource])[0] |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize( |
| 38 | + "output_name", |
| 39 | + [ |
| 40 | + pytest.param(None, id="default-output"), |
| 41 | + pytest.param("imported-form", id="custom-output"), |
| 42 | + ], |
| 43 | +) |
| 44 | +def test_live_import_form_data( |
| 45 | + pdfrest_api_key: str, |
| 46 | + pdfrest_live_base_url: str, |
| 47 | + uploaded_pdf_with_forms: PdfRestFile, |
| 48 | + uploaded_form_data_file: PdfRestFile, |
| 49 | + output_name: str | None, |
| 50 | +) -> None: |
| 51 | + kwargs: dict[str, str] = {} |
| 52 | + if output_name is not None: |
| 53 | + kwargs["output"] = output_name |
| 54 | + |
| 55 | + with PdfRestClient( |
| 56 | + api_key=pdfrest_api_key, |
| 57 | + base_url=pdfrest_live_base_url, |
| 58 | + ) as client: |
| 59 | + response = client.import_form_data( |
| 60 | + uploaded_pdf_with_forms, |
| 61 | + uploaded_form_data_file, |
| 62 | + **kwargs, |
| 63 | + ) |
| 64 | + |
| 65 | + assert response.output_files |
| 66 | + output_file = response.output_file |
| 67 | + assert output_file.type == "application/pdf" |
| 68 | + assert str(response.input_id) == str(uploaded_pdf_with_forms.id) |
| 69 | + if output_name is not None: |
| 70 | + assert output_file.name.startswith(output_name) |
| 71 | + else: |
| 72 | + assert output_file.name.endswith(".pdf") |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.asyncio |
| 76 | +async def test_live_async_import_form_data_success( |
| 77 | + pdfrest_api_key: str, |
| 78 | + pdfrest_live_base_url: str, |
| 79 | + uploaded_pdf_with_forms: PdfRestFile, |
| 80 | + uploaded_form_data_file: PdfRestFile, |
| 81 | +) -> None: |
| 82 | + async with AsyncPdfRestClient( |
| 83 | + api_key=pdfrest_api_key, |
| 84 | + base_url=pdfrest_live_base_url, |
| 85 | + ) as client: |
| 86 | + response = await client.import_form_data( |
| 87 | + uploaded_pdf_with_forms, |
| 88 | + uploaded_form_data_file, |
| 89 | + output="async-imported", |
| 90 | + ) |
| 91 | + |
| 92 | + assert response.output_files |
| 93 | + output_file = response.output_file |
| 94 | + assert output_file.name.startswith("async-imported") |
| 95 | + assert output_file.type == "application/pdf" |
| 96 | + assert str(response.input_id) == str(uploaded_pdf_with_forms.id) |
| 97 | + |
| 98 | + |
| 99 | +def test_live_import_form_data_invalid_data_file_id( |
| 100 | + pdfrest_api_key: str, |
| 101 | + pdfrest_live_base_url: str, |
| 102 | + uploaded_pdf_with_forms: PdfRestFile, |
| 103 | + uploaded_form_data_file: PdfRestFile, |
| 104 | +) -> None: |
| 105 | + with ( |
| 106 | + PdfRestClient( |
| 107 | + api_key=pdfrest_api_key, |
| 108 | + base_url=pdfrest_live_base_url, |
| 109 | + ) as client, |
| 110 | + pytest.raises(PdfRestApiError, match=r"(?i)(data|id)"), |
| 111 | + ): |
| 112 | + client.import_form_data( |
| 113 | + uploaded_pdf_with_forms, |
| 114 | + uploaded_form_data_file, |
| 115 | + extra_body={"data_file_id": "ffffffff-ffff-ffff-ffff-ffffffffffff"}, |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.asyncio |
| 120 | +async def test_live_async_import_form_data_invalid_data_file_id( |
| 121 | + pdfrest_api_key: str, |
| 122 | + pdfrest_live_base_url: str, |
| 123 | + uploaded_pdf_with_forms: PdfRestFile, |
| 124 | + uploaded_form_data_file: PdfRestFile, |
| 125 | +) -> None: |
| 126 | + async with AsyncPdfRestClient( |
| 127 | + api_key=pdfrest_api_key, |
| 128 | + base_url=pdfrest_live_base_url, |
| 129 | + ) as client: |
| 130 | + with pytest.raises(PdfRestApiError, match=r"(?i)(data|id)"): |
| 131 | + await client.import_form_data( |
| 132 | + uploaded_pdf_with_forms, |
| 133 | + uploaded_form_data_file, |
| 134 | + extra_body={"data_file_id": "00000000-0000-0000-0000-000000000000"}, |
| 135 | + ) |
0 commit comments