|
| 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_color_conversion( |
| 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.mark.parametrize( |
| 25 | + "output_name", |
| 26 | + [ |
| 27 | + pytest.param(None, id="default-output"), |
| 28 | + pytest.param("converted-colors", id="custom-output"), |
| 29 | + ], |
| 30 | +) |
| 31 | +def test_live_convert_colors_success( |
| 32 | + pdfrest_api_key: str, |
| 33 | + pdfrest_live_base_url: str, |
| 34 | + uploaded_pdf_for_color_conversion: PdfRestFile, |
| 35 | + output_name: str | None, |
| 36 | +) -> None: |
| 37 | + kwargs: dict[str, str | bool] = {"color_profile": "srgb"} |
| 38 | + if output_name is not None: |
| 39 | + kwargs["output"] = output_name |
| 40 | + |
| 41 | + with PdfRestClient( |
| 42 | + api_key=pdfrest_api_key, |
| 43 | + base_url=pdfrest_live_base_url, |
| 44 | + ) as client: |
| 45 | + response = client.convert_colors(uploaded_pdf_for_color_conversion, **kwargs) |
| 46 | + |
| 47 | + assert response.output_files |
| 48 | + output_file = response.output_file |
| 49 | + assert output_file.type == "application/pdf" |
| 50 | + assert output_file.size > 0 |
| 51 | + assert response.warning is None |
| 52 | + assert str(response.input_id) == str(uploaded_pdf_for_color_conversion.id) |
| 53 | + if output_name is not None: |
| 54 | + assert output_file.name.startswith(output_name) |
| 55 | + else: |
| 56 | + assert output_file.name.endswith(".pdf") |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.asyncio |
| 60 | +async def test_live_async_convert_colors_success( |
| 61 | + pdfrest_api_key: str, |
| 62 | + pdfrest_live_base_url: str, |
| 63 | + uploaded_pdf_for_color_conversion: PdfRestFile, |
| 64 | +) -> None: |
| 65 | + async with AsyncPdfRestClient( |
| 66 | + api_key=pdfrest_api_key, |
| 67 | + base_url=pdfrest_live_base_url, |
| 68 | + ) as client: |
| 69 | + response = await client.convert_colors( |
| 70 | + uploaded_pdf_for_color_conversion, |
| 71 | + color_profile="srgb", |
| 72 | + output="async", |
| 73 | + ) |
| 74 | + |
| 75 | + assert response.output_files |
| 76 | + output_file = response.output_file |
| 77 | + assert output_file.name.startswith("async") |
| 78 | + assert output_file.type == "application/pdf" |
| 79 | + assert output_file.size > 0 |
| 80 | + assert response.warning is None |
| 81 | + assert str(response.input_id) == str(uploaded_pdf_for_color_conversion.id) |
| 82 | + |
| 83 | + |
| 84 | +def test_live_convert_colors_invalid_file_id( |
| 85 | + pdfrest_api_key: str, |
| 86 | + pdfrest_live_base_url: str, |
| 87 | + uploaded_pdf_for_color_conversion: PdfRestFile, |
| 88 | +) -> None: |
| 89 | + with ( |
| 90 | + PdfRestClient( |
| 91 | + api_key=pdfrest_api_key, |
| 92 | + base_url=pdfrest_live_base_url, |
| 93 | + ) as client, |
| 94 | + pytest.raises(PdfRestApiError, match=r"(?i)(id|file)"), |
| 95 | + ): |
| 96 | + client.convert_colors( |
| 97 | + uploaded_pdf_for_color_conversion, |
| 98 | + color_profile="srgb", |
| 99 | + extra_body={"id": "00000000-0000-0000-0000-000000000000"}, |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.asyncio |
| 104 | +async def test_live_async_convert_colors_invalid_file_id( |
| 105 | + pdfrest_api_key: str, |
| 106 | + pdfrest_live_base_url: str, |
| 107 | + uploaded_pdf_for_color_conversion: PdfRestFile, |
| 108 | +) -> None: |
| 109 | + async with AsyncPdfRestClient( |
| 110 | + api_key=pdfrest_api_key, |
| 111 | + base_url=pdfrest_live_base_url, |
| 112 | + ) as client: |
| 113 | + with pytest.raises(PdfRestApiError, match=r"(?i)(id|file)"): |
| 114 | + await client.convert_colors( |
| 115 | + uploaded_pdf_for_color_conversion, |
| 116 | + color_profile="srgb", |
| 117 | + extra_body={"id": "ffffffff-ffff-ffff-ffff-ffffffffffff"}, |
| 118 | + ) |
0 commit comments