|
| 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 PdfToWordPayload |
| 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_convert_to_word_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 = PdfToWordPayload.model_validate( |
| 27 | + {"files": [input_file], "output": "report"} |
| 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 == "/word": |
| 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 | + "report.docx", |
| 52 | + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| 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_word(input_file, output="report") |
| 61 | + |
| 62 | + assert seen == {"post": 1, "get": 1} |
| 63 | + assert isinstance(response, PdfRestFileBasedResponse) |
| 64 | + output_file = response.output_file |
| 65 | + assert output_file.name == "report.docx" |
| 66 | + assert ( |
| 67 | + output_file.type |
| 68 | + == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" |
| 69 | + ) |
| 70 | + assert response.warning is None |
| 71 | + assert str(response.input_id) == str(input_file.id) |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.asyncio |
| 75 | +async def test_async_convert_to_word_success( |
| 76 | + monkeypatch: pytest.MonkeyPatch, |
| 77 | +) -> None: |
| 78 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 79 | + input_file = make_pdf_file(PdfRestFileID.generate(2)) |
| 80 | + output_id = str(PdfRestFileID.generate()) |
| 81 | + |
| 82 | + payload_dump = PdfToWordPayload.model_validate({"files": [input_file]}).model_dump( |
| 83 | + mode="json", by_alias=True, exclude_none=True, exclude_unset=True |
| 84 | + ) |
| 85 | + |
| 86 | + seen: dict[str, int] = {"post": 0, "get": 0} |
| 87 | + |
| 88 | + def handler(request: httpx.Request) -> httpx.Response: |
| 89 | + if request.method == "POST" and request.url.path == "/word": |
| 90 | + seen["post"] += 1 |
| 91 | + payload = json.loads(request.content.decode("utf-8")) |
| 92 | + assert payload == payload_dump |
| 93 | + return httpx.Response( |
| 94 | + 200, |
| 95 | + json={ |
| 96 | + "inputId": [input_file.id], |
| 97 | + "outputId": [output_id], |
| 98 | + }, |
| 99 | + ) |
| 100 | + if request.method == "GET" and request.url.path == f"/resource/{output_id}": |
| 101 | + seen["get"] += 1 |
| 102 | + assert request.url.params["format"] == "info" |
| 103 | + return httpx.Response( |
| 104 | + 200, |
| 105 | + json=build_file_info_payload( |
| 106 | + output_id, |
| 107 | + "async.docx", |
| 108 | + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| 109 | + ), |
| 110 | + ) |
| 111 | + msg = f"Unexpected request {request.method} {request.url}" |
| 112 | + raise AssertionError(msg) |
| 113 | + |
| 114 | + transport = httpx.MockTransport(handler) |
| 115 | + async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client: |
| 116 | + response = await client.convert_to_word(input_file) |
| 117 | + |
| 118 | + assert seen == {"post": 1, "get": 1} |
| 119 | + assert isinstance(response, PdfRestFileBasedResponse) |
| 120 | + assert response.output_file.name == "async.docx" |
| 121 | + assert ( |
| 122 | + response.output_file.type |
| 123 | + == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" |
| 124 | + ) |
| 125 | + assert str(response.input_id) == str(input_file.id) |
| 126 | + |
| 127 | + |
| 128 | +def test_convert_to_word_validation(monkeypatch: pytest.MonkeyPatch) -> None: |
| 129 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 130 | + pdf_file = make_pdf_file(PdfRestFileID.generate(1)) |
| 131 | + png_file = PdfRestFile.model_validate( |
| 132 | + build_file_info_payload( |
| 133 | + PdfRestFileID.generate(), |
| 134 | + "example.png", |
| 135 | + "image/png", |
| 136 | + ) |
| 137 | + ) |
| 138 | + transport = httpx.MockTransport(lambda request: (_ for _ in ()).throw(RuntimeError)) |
| 139 | + |
| 140 | + with ( |
| 141 | + PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client, |
| 142 | + pytest.raises(ValidationError, match="Must be a PDF file"), |
| 143 | + ): |
| 144 | + client.convert_to_word(png_file) |
| 145 | + |
| 146 | + with ( |
| 147 | + PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client, |
| 148 | + pytest.raises( |
| 149 | + ValidationError, match="List should have at most 1 item after validation" |
| 150 | + ), |
| 151 | + ): |
| 152 | + client.convert_to_word([pdf_file, make_pdf_file(PdfRestFileID.generate())]) |
0 commit comments