|
| 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_for_attachment( |
| 13 | + pdfrest_api_key: str, |
| 14 | + pdfrest_live_base_url: str, |
| 15 | +) -> PdfRestFile: |
| 16 | + resource = get_test_resource_path("report.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_attachment_file( |
| 26 | + pdfrest_api_key: str, |
| 27 | + pdfrest_live_base_url: str, |
| 28 | +) -> PdfRestFile: |
| 29 | + resource = get_test_resource_path("report.docx") |
| 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 | +def test_live_add_attachment_to_pdf_success( |
| 38 | + pdfrest_api_key: str, |
| 39 | + pdfrest_live_base_url: str, |
| 40 | + uploaded_pdf_for_attachment: PdfRestFile, |
| 41 | + uploaded_attachment_file: PdfRestFile, |
| 42 | +) -> None: |
| 43 | + with PdfRestClient( |
| 44 | + api_key=pdfrest_api_key, |
| 45 | + base_url=pdfrest_live_base_url, |
| 46 | + ) as client: |
| 47 | + response = client.add_attachment_to_pdf( |
| 48 | + uploaded_pdf_for_attachment, |
| 49 | + attachment=uploaded_attachment_file, |
| 50 | + output="with-attachment", |
| 51 | + ) |
| 52 | + |
| 53 | + assert response.output_files |
| 54 | + output_file = response.output_file |
| 55 | + assert output_file.name.startswith("with-attachment") |
| 56 | + assert output_file.type == "application/pdf" |
| 57 | + assert output_file.size > 0 |
| 58 | + assert response.warning is None |
| 59 | + assert [str(file_id) for file_id in response.input_ids] == [ |
| 60 | + str(uploaded_pdf_for_attachment.id), |
| 61 | + str(uploaded_attachment_file.id), |
| 62 | + ] |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.asyncio |
| 66 | +async def test_live_async_add_attachment_to_pdf_success( |
| 67 | + pdfrest_api_key: str, |
| 68 | + pdfrest_live_base_url: str, |
| 69 | + uploaded_pdf_for_attachment: PdfRestFile, |
| 70 | + uploaded_attachment_file: PdfRestFile, |
| 71 | +) -> None: |
| 72 | + async with AsyncPdfRestClient( |
| 73 | + api_key=pdfrest_api_key, |
| 74 | + base_url=pdfrest_live_base_url, |
| 75 | + ) as client: |
| 76 | + response = await client.add_attachment_to_pdf( |
| 77 | + uploaded_pdf_for_attachment, |
| 78 | + attachment=uploaded_attachment_file, |
| 79 | + output="async-attachment", |
| 80 | + ) |
| 81 | + |
| 82 | + assert response.output_files |
| 83 | + output_file = response.output_file |
| 84 | + assert output_file.name.startswith("async-attachment") |
| 85 | + assert output_file.type == "application/pdf" |
| 86 | + assert output_file.size > 0 |
| 87 | + assert response.warning is None |
| 88 | + assert [str(file_id) for file_id in response.input_ids] == [ |
| 89 | + str(uploaded_pdf_for_attachment.id), |
| 90 | + str(uploaded_attachment_file.id), |
| 91 | + ] |
| 92 | + |
| 93 | + |
| 94 | +def test_live_add_attachment_to_pdf_invalid_file_id( |
| 95 | + pdfrest_api_key: str, |
| 96 | + pdfrest_live_base_url: str, |
| 97 | + uploaded_pdf_for_attachment: PdfRestFile, |
| 98 | + uploaded_attachment_file: PdfRestFile, |
| 99 | +) -> None: |
| 100 | + with ( |
| 101 | + PdfRestClient( |
| 102 | + api_key=pdfrest_api_key, |
| 103 | + base_url=pdfrest_live_base_url, |
| 104 | + ) as client, |
| 105 | + pytest.raises(PdfRestApiError, match=r"(?i)(id|file)"), |
| 106 | + ): |
| 107 | + client.add_attachment_to_pdf( |
| 108 | + uploaded_pdf_for_attachment, |
| 109 | + attachment=uploaded_attachment_file, |
| 110 | + extra_body={"id": "00000000-0000-0000-0000-000000000000"}, |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.asyncio |
| 115 | +async def test_live_async_add_attachment_to_pdf_invalid_attachment_id( |
| 116 | + pdfrest_api_key: str, |
| 117 | + pdfrest_live_base_url: str, |
| 118 | + uploaded_pdf_for_attachment: PdfRestFile, |
| 119 | + uploaded_attachment_file: PdfRestFile, |
| 120 | +) -> None: |
| 121 | + async with AsyncPdfRestClient( |
| 122 | + api_key=pdfrest_api_key, |
| 123 | + base_url=pdfrest_live_base_url, |
| 124 | + ) as client: |
| 125 | + with pytest.raises(PdfRestApiError, match=r"(?i)(id|file)"): |
| 126 | + await client.add_attachment_to_pdf( |
| 127 | + uploaded_pdf_for_attachment, |
| 128 | + attachment=uploaded_attachment_file, |
| 129 | + extra_body={"id_to_attach": "ffffffff-ffff-ffff-ffff-ffffffffffff"}, |
| 130 | + ) |
0 commit comments