|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | +import httpx |
| 6 | +import pytest |
| 7 | +from pydantic import ValidationError |
| 8 | + |
| 9 | +from pdfrest import AsyncPdfRestClient, PdfRestClient |
| 10 | +from pdfrest.models import PdfRestFile, PdfRestFileBasedResponse, PdfRestFileID |
| 11 | +from pdfrest.models._internal import PdfToPdfxPayload |
| 12 | +from pdfrest.types import PdfXType |
| 13 | + |
| 14 | +from .graphics_test_helpers import ( |
| 15 | + ASYNC_API_KEY, |
| 16 | + VALID_API_KEY, |
| 17 | + build_file_info_payload, |
| 18 | + make_pdf_file, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def test_convert_to_pdfx_success(monkeypatch: pytest.MonkeyPatch) -> None: |
| 23 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 24 | + input_file = make_pdf_file(PdfRestFileID.generate(1)) |
| 25 | + output_id = str(PdfRestFileID.generate()) |
| 26 | + output_type: PdfXType = "PDF/X-4" |
| 27 | + |
| 28 | + payload_dump = PdfToPdfxPayload.model_validate( |
| 29 | + {"files": [input_file], "output_type": output_type, "output": "print-ready"} |
| 30 | + ).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True) |
| 31 | + |
| 32 | + seen: dict[str, int] = {"post": 0, "get": 0} |
| 33 | + |
| 34 | + def handler(request: httpx.Request) -> httpx.Response: |
| 35 | + if request.method == "POST" and request.url.path == "/pdfx": |
| 36 | + seen["post"] += 1 |
| 37 | + payload = json.loads(request.content.decode("utf-8")) |
| 38 | + assert payload == payload_dump |
| 39 | + return httpx.Response( |
| 40 | + 200, |
| 41 | + json={ |
| 42 | + "inputId": [input_file.id], |
| 43 | + "outputId": [output_id], |
| 44 | + }, |
| 45 | + ) |
| 46 | + if request.method == "GET" and request.url.path == f"/resource/{output_id}": |
| 47 | + seen["get"] += 1 |
| 48 | + assert request.url.params["format"] == "info" |
| 49 | + return httpx.Response( |
| 50 | + 200, |
| 51 | + json=build_file_info_payload( |
| 52 | + output_id, "print-ready.pdf", "application/pdf" |
| 53 | + ), |
| 54 | + ) |
| 55 | + msg = f"Unexpected request {request.method} {request.url}" |
| 56 | + raise AssertionError(msg) |
| 57 | + |
| 58 | + transport = httpx.MockTransport(handler) |
| 59 | + with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client: |
| 60 | + response = client.convert_to_pdfx( |
| 61 | + input_file, |
| 62 | + output_type=output_type, |
| 63 | + output="print-ready", |
| 64 | + ) |
| 65 | + |
| 66 | + assert seen == {"post": 1, "get": 1} |
| 67 | + assert isinstance(response, PdfRestFileBasedResponse) |
| 68 | + assert response.output_file.name == "print-ready.pdf" |
| 69 | + assert response.output_file.type == "application/pdf" |
| 70 | + assert str(response.input_id) == str(input_file.id) |
| 71 | + assert response.warning is None |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.asyncio |
| 75 | +async def test_async_convert_to_pdfx_success(monkeypatch: pytest.MonkeyPatch) -> None: |
| 76 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 77 | + input_file = make_pdf_file(PdfRestFileID.generate(2)) |
| 78 | + output_id = str(PdfRestFileID.generate()) |
| 79 | + output_type: PdfXType = "PDF/X-1a" |
| 80 | + |
| 81 | + payload_dump = PdfToPdfxPayload.model_validate( |
| 82 | + {"files": [input_file], "output_type": output_type} |
| 83 | + ).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True) |
| 84 | + |
| 85 | + seen: dict[str, int] = {"post": 0, "get": 0} |
| 86 | + |
| 87 | + def handler(request: httpx.Request) -> httpx.Response: |
| 88 | + if request.method == "POST" and request.url.path == "/pdfx": |
| 89 | + seen["post"] += 1 |
| 90 | + payload = json.loads(request.content.decode("utf-8")) |
| 91 | + assert payload == payload_dump |
| 92 | + return httpx.Response( |
| 93 | + 200, |
| 94 | + json={ |
| 95 | + "inputId": [input_file.id], |
| 96 | + "outputId": [output_id], |
| 97 | + }, |
| 98 | + ) |
| 99 | + if request.method == "GET" and request.url.path == f"/resource/{output_id}": |
| 100 | + seen["get"] += 1 |
| 101 | + assert request.url.params["format"] == "info" |
| 102 | + return httpx.Response( |
| 103 | + 200, |
| 104 | + json=build_file_info_payload(output_id, "async.pdf", "application/pdf"), |
| 105 | + ) |
| 106 | + msg = f"Unexpected request {request.method} {request.url}" |
| 107 | + raise AssertionError(msg) |
| 108 | + |
| 109 | + transport = httpx.MockTransport(handler) |
| 110 | + async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client: |
| 111 | + response = await client.convert_to_pdfx( |
| 112 | + input_file, |
| 113 | + output_type=output_type, |
| 114 | + ) |
| 115 | + |
| 116 | + assert seen == {"post": 1, "get": 1} |
| 117 | + assert isinstance(response, PdfRestFileBasedResponse) |
| 118 | + assert response.output_file.name == "async.pdf" |
| 119 | + assert response.output_file.type == "application/pdf" |
| 120 | + assert str(response.input_id) == str(input_file.id) |
| 121 | + |
| 122 | + |
| 123 | +def test_convert_to_pdfx_validation(monkeypatch: pytest.MonkeyPatch) -> None: |
| 124 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 125 | + pdf_file = make_pdf_file(PdfRestFileID.generate(1)) |
| 126 | + png_file = PdfRestFile.model_validate( |
| 127 | + build_file_info_payload( |
| 128 | + PdfRestFileID.generate(), |
| 129 | + "example.png", |
| 130 | + "image/png", |
| 131 | + ) |
| 132 | + ) |
| 133 | + transport = httpx.MockTransport(lambda request: (_ for _ in ()).throw(RuntimeError)) |
| 134 | + |
| 135 | + with ( |
| 136 | + PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client, |
| 137 | + pytest.raises(ValidationError, match="Field required"), |
| 138 | + ): |
| 139 | + client.convert_to_pdfx(pdf_file, output_type=None) # type: ignore[arg-type] |
| 140 | + |
| 141 | + with ( |
| 142 | + PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client, |
| 143 | + pytest.raises(ValidationError, match="Must be a PDF file"), |
| 144 | + ): |
| 145 | + client.convert_to_pdfx(png_file, output_type="PDF/X-3") |
| 146 | + |
| 147 | + with ( |
| 148 | + PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client, |
| 149 | + pytest.raises(ValidationError, match="PDF/X-1a"), |
| 150 | + ): |
| 151 | + client.convert_to_pdfx(pdf_file, output_type="PDF/X-5") # type: ignore[arg-type] |
0 commit comments