|
| 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_docx_for_pdf( |
| 13 | + pdfrest_api_key: str, |
| 14 | + pdfrest_live_base_url: str, |
| 15 | +) -> PdfRestFile: |
| 16 | + resource = get_test_resource_path("report.docx") |
| 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_html_url_for_pdf( |
| 26 | + pdfrest_api_key: str, |
| 27 | + pdfrest_live_base_url: str, |
| 28 | +) -> str: |
| 29 | + resource = get_test_resource_path("sample.html") |
| 30 | + with PdfRestClient( |
| 31 | + api_key=pdfrest_api_key, |
| 32 | + base_url=pdfrest_live_base_url, |
| 33 | + ) as client: |
| 34 | + uploaded = client.files.create_from_paths([resource])[0] |
| 35 | + return str(uploaded.url) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize( |
| 39 | + ("output_name", "compression", "downsample"), |
| 40 | + [ |
| 41 | + pytest.param(None, None, None, id="defaults"), |
| 42 | + pytest.param("live-docx", "lossless", 600, id="customized"), |
| 43 | + ], |
| 44 | +) |
| 45 | +def test_live_convert_to_pdf_success( |
| 46 | + pdfrest_api_key: str, |
| 47 | + pdfrest_live_base_url: str, |
| 48 | + uploaded_docx_for_pdf: PdfRestFile, |
| 49 | + output_name: str | None, |
| 50 | + compression: str | None, |
| 51 | + downsample: int | None, |
| 52 | +) -> None: |
| 53 | + kwargs: dict[str, object] = {} |
| 54 | + if output_name is not None: |
| 55 | + kwargs["output"] = output_name |
| 56 | + if compression is not None: |
| 57 | + kwargs["compression"] = compression |
| 58 | + if downsample is not None: |
| 59 | + kwargs["downsample"] = downsample |
| 60 | + |
| 61 | + with PdfRestClient( |
| 62 | + api_key=pdfrest_api_key, |
| 63 | + base_url=pdfrest_live_base_url, |
| 64 | + ) as client: |
| 65 | + response = client.convert_to_pdf(uploaded_docx_for_pdf, **kwargs) |
| 66 | + |
| 67 | + assert response.output_files |
| 68 | + output_file = response.output_file |
| 69 | + assert output_file.type == "application/pdf" |
| 70 | + assert output_file.size > 0 |
| 71 | + assert response.warning is None |
| 72 | + assert str(response.input_id) == str(uploaded_docx_for_pdf.id) |
| 73 | + if output_name is not None: |
| 74 | + assert output_file.name.startswith(output_name) |
| 75 | + else: |
| 76 | + assert output_file.name.endswith(".pdf") |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.asyncio |
| 80 | +async def test_live_async_convert_to_pdf_success( |
| 81 | + pdfrest_api_key: str, |
| 82 | + pdfrest_live_base_url: str, |
| 83 | + uploaded_docx_for_pdf: PdfRestFile, |
| 84 | +) -> None: |
| 85 | + async with AsyncPdfRestClient( |
| 86 | + api_key=pdfrest_api_key, |
| 87 | + base_url=pdfrest_live_base_url, |
| 88 | + ) as client: |
| 89 | + response = await client.convert_to_pdf( |
| 90 | + uploaded_docx_for_pdf, |
| 91 | + output="async-docx", |
| 92 | + tagged_pdf=True, |
| 93 | + ) |
| 94 | + |
| 95 | + assert response.output_files |
| 96 | + output_file = response.output_file |
| 97 | + assert output_file.name.startswith("async-docx") |
| 98 | + assert output_file.type == "application/pdf" |
| 99 | + assert output_file.size > 0 |
| 100 | + assert response.warning is None |
| 101 | + assert str(response.input_id) == str(uploaded_docx_for_pdf.id) |
| 102 | + |
| 103 | + |
| 104 | +def test_live_convert_urls_to_pdf_success( |
| 105 | + pdfrest_api_key: str, |
| 106 | + pdfrest_live_base_url: str, |
| 107 | + uploaded_html_url_for_pdf: str, |
| 108 | +) -> None: |
| 109 | + with PdfRestClient( |
| 110 | + api_key=pdfrest_api_key, |
| 111 | + base_url=pdfrest_live_base_url, |
| 112 | + ) as client: |
| 113 | + response = client.convert_urls_to_pdf( |
| 114 | + [uploaded_html_url_for_pdf], |
| 115 | + output="live-html", |
| 116 | + page_size="letter", |
| 117 | + page_margin="8mm", |
| 118 | + page_orientation="portrait", |
| 119 | + web_layout="desktop", |
| 120 | + ) |
| 121 | + |
| 122 | + assert response.output_files |
| 123 | + output_file = response.output_file |
| 124 | + assert output_file.type == "application/pdf" |
| 125 | + assert output_file.size > 0 |
| 126 | + assert response.warning is None |
| 127 | + assert str(response.input_id) |
| 128 | + assert output_file.name.startswith("live-html") |
| 129 | + |
| 130 | + |
| 131 | +def test_live_convert_to_pdf_invalid_downsample( |
| 132 | + pdfrest_api_key: str, |
| 133 | + pdfrest_live_base_url: str, |
| 134 | + uploaded_docx_for_pdf: PdfRestFile, |
| 135 | +) -> None: |
| 136 | + with ( |
| 137 | + PdfRestClient( |
| 138 | + api_key=pdfrest_api_key, |
| 139 | + base_url=pdfrest_live_base_url, |
| 140 | + ) as client, |
| 141 | + pytest.raises(PdfRestApiError, match=r"(?i)downsample"), |
| 142 | + ): |
| 143 | + client.convert_to_pdf( |
| 144 | + uploaded_docx_for_pdf, |
| 145 | + extra_body={"downsample": 0}, |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +@pytest.mark.asyncio |
| 150 | +async def test_live_async_convert_urls_to_pdf_invalid_page_size( |
| 151 | + pdfrest_api_key: str, |
| 152 | + pdfrest_live_base_url: str, |
| 153 | + uploaded_html_url_for_pdf: str, |
| 154 | +) -> None: |
| 155 | + async with AsyncPdfRestClient( |
| 156 | + api_key=pdfrest_api_key, |
| 157 | + base_url=pdfrest_live_base_url, |
| 158 | + ) as client: |
| 159 | + with pytest.raises(PdfRestApiError, match=r"(?i)page_size|page size"): |
| 160 | + await client.convert_urls_to_pdf( |
| 161 | + [uploaded_html_url_for_pdf], |
| 162 | + extra_body={"page_size": "poster"}, |
| 163 | + ) |
0 commit comments