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