|
19 | 19 | ) |
20 | 20 |
|
21 | 21 |
|
22 | | -def test_convert_to_pdfx_success(monkeypatch: pytest.MonkeyPatch) -> None: |
| 22 | +@pytest.mark.parametrize( |
| 23 | + "output_type", |
| 24 | + [ |
| 25 | + pytest.param("PDF/X-1a", id="pdfx-1a"), |
| 26 | + pytest.param("PDF/X-3", id="pdfx-3"), |
| 27 | + pytest.param("PDF/X-4", id="pdfx-4"), |
| 28 | + pytest.param("PDF/X-6", id="pdfx-6"), |
| 29 | + ], |
| 30 | +) |
| 31 | +def test_convert_to_pdfx_success( |
| 32 | + monkeypatch: pytest.MonkeyPatch, output_type: PdfXType |
| 33 | +) -> None: |
23 | 34 | monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
24 | 35 | input_file = make_pdf_file(PdfRestFileID.generate(1)) |
25 | 36 | output_id = str(PdfRestFileID.generate()) |
26 | | - output_type: PdfXType = "PDF/X-4" |
27 | | - |
28 | 37 | payload_dump = PdfToPdfxPayload.model_validate( |
29 | 38 | {"files": [input_file], "output_type": output_type, "output": "print-ready"} |
30 | 39 | ).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True) |
@@ -72,12 +81,21 @@ def handler(request: httpx.Request) -> httpx.Response: |
72 | 81 |
|
73 | 82 |
|
74 | 83 | @pytest.mark.asyncio |
75 | | -async def test_async_convert_to_pdfx_success(monkeypatch: pytest.MonkeyPatch) -> None: |
| 84 | +@pytest.mark.parametrize( |
| 85 | + "output_type", |
| 86 | + [ |
| 87 | + pytest.param("PDF/X-1a", id="async-pdfx-1a"), |
| 88 | + pytest.param("PDF/X-3", id="async-pdfx-3"), |
| 89 | + pytest.param("PDF/X-4", id="async-pdfx-4"), |
| 90 | + pytest.param("PDF/X-6", id="async-pdfx-6"), |
| 91 | + ], |
| 92 | +) |
| 93 | +async def test_async_convert_to_pdfx_success( |
| 94 | + monkeypatch: pytest.MonkeyPatch, output_type: PdfXType |
| 95 | +) -> None: |
76 | 96 | monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
77 | 97 | input_file = make_pdf_file(PdfRestFileID.generate(2)) |
78 | 98 | output_id = str(PdfRestFileID.generate()) |
79 | | - output_type: PdfXType = "PDF/X-1a" |
80 | | - |
81 | 99 | payload_dump = PdfToPdfxPayload.model_validate( |
82 | 100 | {"files": [input_file], "output_type": output_type} |
83 | 101 | ).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True) |
@@ -120,6 +138,123 @@ def handler(request: httpx.Request) -> httpx.Response: |
120 | 138 | assert str(response.input_id) == str(input_file.id) |
121 | 139 |
|
122 | 140 |
|
| 141 | +def test_convert_to_pdfx_request_customization( |
| 142 | + monkeypatch: pytest.MonkeyPatch, |
| 143 | +) -> None: |
| 144 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 145 | + input_file = make_pdf_file(PdfRestFileID.generate(1)) |
| 146 | + output_id = str(PdfRestFileID.generate()) |
| 147 | + captured_timeout: dict[str, float | dict[str, float] | None] = {} |
| 148 | + |
| 149 | + def handler(request: httpx.Request) -> httpx.Response: |
| 150 | + if request.method == "POST" and request.url.path == "/pdfx": |
| 151 | + assert request.url.params["trace"] == "true" |
| 152 | + assert request.headers["X-Debug"] == "sync" |
| 153 | + captured_timeout["value"] = request.extensions.get("timeout") |
| 154 | + payload = json.loads(request.content.decode("utf-8")) |
| 155 | + assert payload["output_type"] == "PDF/X-3" |
| 156 | + assert payload["debug"] == "yes" |
| 157 | + assert payload["id"] == str(input_file.id) |
| 158 | + assert payload["output"] == "custom" |
| 159 | + return httpx.Response( |
| 160 | + 200, |
| 161 | + json={"inputId": [input_file.id], "outputId": [output_id]}, |
| 162 | + ) |
| 163 | + if request.method == "GET" and request.url.path == f"/resource/{output_id}": |
| 164 | + assert request.url.params["format"] == "info" |
| 165 | + assert request.url.params["trace"] == "true" |
| 166 | + assert request.headers["X-Debug"] == "sync" |
| 167 | + return httpx.Response( |
| 168 | + 200, |
| 169 | + json=build_file_info_payload( |
| 170 | + output_id, "custom.pdf", "application/pdf" |
| 171 | + ), |
| 172 | + ) |
| 173 | + msg = f"Unexpected request {request.method} {request.url}" |
| 174 | + raise AssertionError(msg) |
| 175 | + |
| 176 | + transport = httpx.MockTransport(handler) |
| 177 | + with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client: |
| 178 | + response = client.convert_to_pdfx( |
| 179 | + input_file, |
| 180 | + output_type="PDF/X-3", |
| 181 | + output="custom", |
| 182 | + extra_query={"trace": "true"}, |
| 183 | + extra_headers={"X-Debug": "sync"}, |
| 184 | + extra_body={"debug": "yes"}, |
| 185 | + timeout=0.33, |
| 186 | + ) |
| 187 | + |
| 188 | + assert isinstance(response, PdfRestFileBasedResponse) |
| 189 | + assert response.output_file.name == "custom.pdf" |
| 190 | + timeout_value = captured_timeout["value"] |
| 191 | + assert timeout_value is not None |
| 192 | + if isinstance(timeout_value, dict): |
| 193 | + assert all( |
| 194 | + component == pytest.approx(0.33) for component in timeout_value.values() |
| 195 | + ) |
| 196 | + else: |
| 197 | + assert timeout_value == pytest.approx(0.33) |
| 198 | + |
| 199 | + |
| 200 | +@pytest.mark.asyncio |
| 201 | +async def test_async_convert_to_pdfx_request_customization( |
| 202 | + monkeypatch: pytest.MonkeyPatch, |
| 203 | +) -> None: |
| 204 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 205 | + input_file = make_pdf_file(PdfRestFileID.generate(2)) |
| 206 | + output_id = str(PdfRestFileID.generate()) |
| 207 | + captured_timeout: dict[str, float | dict[str, float] | None] = {} |
| 208 | + |
| 209 | + def handler(request: httpx.Request) -> httpx.Response: |
| 210 | + if request.method == "POST" and request.url.path == "/pdfx": |
| 211 | + assert request.url.params["trace"] == "async" |
| 212 | + assert request.headers["X-Debug"] == "async" |
| 213 | + captured_timeout["value"] = request.extensions.get("timeout") |
| 214 | + payload = json.loads(request.content.decode("utf-8")) |
| 215 | + assert payload["output_type"] == "PDF/X-6" |
| 216 | + assert payload["id"] == str(input_file.id) |
| 217 | + assert payload["extra"] == {"note": "async"} |
| 218 | + return httpx.Response( |
| 219 | + 200, |
| 220 | + json={"inputId": [input_file.id], "outputId": [output_id]}, |
| 221 | + ) |
| 222 | + if request.method == "GET" and request.url.path == f"/resource/{output_id}": |
| 223 | + assert request.url.params["format"] == "info" |
| 224 | + assert request.url.params["trace"] == "async" |
| 225 | + assert request.headers["X-Debug"] == "async" |
| 226 | + return httpx.Response( |
| 227 | + 200, |
| 228 | + json=build_file_info_payload( |
| 229 | + output_id, "async-custom.pdf", "application/pdf" |
| 230 | + ), |
| 231 | + ) |
| 232 | + msg = f"Unexpected request {request.method} {request.url}" |
| 233 | + raise AssertionError(msg) |
| 234 | + |
| 235 | + transport = httpx.MockTransport(handler) |
| 236 | + async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client: |
| 237 | + response = await client.convert_to_pdfx( |
| 238 | + input_file, |
| 239 | + output_type="PDF/X-6", |
| 240 | + extra_query={"trace": "async"}, |
| 241 | + extra_headers={"X-Debug": "async"}, |
| 242 | + extra_body={"extra": {"note": "async"}}, |
| 243 | + timeout=0.72, |
| 244 | + ) |
| 245 | + |
| 246 | + assert isinstance(response, PdfRestFileBasedResponse) |
| 247 | + assert response.output_file.name == "async-custom.pdf" |
| 248 | + timeout_value = captured_timeout["value"] |
| 249 | + assert timeout_value is not None |
| 250 | + if isinstance(timeout_value, dict): |
| 251 | + assert all( |
| 252 | + component == pytest.approx(0.72) for component in timeout_value.values() |
| 253 | + ) |
| 254 | + else: |
| 255 | + assert timeout_value == pytest.approx(0.72) |
| 256 | + |
| 257 | + |
123 | 258 | def test_convert_to_pdfx_validation(monkeypatch: pytest.MonkeyPatch) -> None: |
124 | 259 | monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
125 | 260 | pdf_file = make_pdf_file(PdfRestFileID.generate(1)) |
@@ -152,3 +287,14 @@ def test_convert_to_pdfx_validation(monkeypatch: pytest.MonkeyPatch) -> None: |
152 | 287 | pytest.raises(ValidationError, match="PDF/X-1a"), |
153 | 288 | ): |
154 | 289 | client.convert_to_pdfx(pdf_file, output_type="PDF/X-5") # type: ignore[arg-type] |
| 290 | + |
| 291 | + with ( |
| 292 | + PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client, |
| 293 | + pytest.raises( |
| 294 | + ValidationError, match="List should have at most 1 item after validation" |
| 295 | + ), |
| 296 | + ): |
| 297 | + client.convert_to_pdfx( |
| 298 | + [pdf_file, make_pdf_file(PdfRestFileID.generate())], |
| 299 | + output_type="PDF/X-3", |
| 300 | + ) |
0 commit comments