Skip to content

Commit 014162e

Browse files
tests: Add request customization tests for PDF/X, flatten, Word
- Added parameterized tests for `test_convert_to_pdfx_success` and `test_async_convert_to_pdfx_success` to validate multiple output types. - Introduced sync and async tests to verify request customization for PDF to PDF/X, Word, and flattening PDF forms. - Ensured validation logic for accepted input/output types and custom metadata integrations in payload and headers. Assisted-by: Codex
1 parent 300e479 commit 014162e

3 files changed

Lines changed: 398 additions & 6 deletions

File tree

tests/test_convert_to_pdfx.py

Lines changed: 152 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,21 @@
1919
)
2020

2121

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:
2334
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
2435
input_file = make_pdf_file(PdfRestFileID.generate(1))
2536
output_id = str(PdfRestFileID.generate())
26-
output_type: PdfXType = "PDF/X-4"
27-
2837
payload_dump = PdfToPdfxPayload.model_validate(
2938
{"files": [input_file], "output_type": output_type, "output": "print-ready"}
3039
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
@@ -72,12 +81,21 @@ def handler(request: httpx.Request) -> httpx.Response:
7281

7382

7483
@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:
7696
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
7797
input_file = make_pdf_file(PdfRestFileID.generate(2))
7898
output_id = str(PdfRestFileID.generate())
79-
output_type: PdfXType = "PDF/X-1a"
80-
8199
payload_dump = PdfToPdfxPayload.model_validate(
82100
{"files": [input_file], "output_type": output_type}
83101
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
@@ -120,6 +138,123 @@ def handler(request: httpx.Request) -> httpx.Response:
120138
assert str(response.input_id) == str(input_file.id)
121139

122140

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+
123258
def test_convert_to_pdfx_validation(monkeypatch: pytest.MonkeyPatch) -> None:
124259
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
125260
pdf_file = make_pdf_file(PdfRestFileID.generate(1))
@@ -152,3 +287,14 @@ def test_convert_to_pdfx_validation(monkeypatch: pytest.MonkeyPatch) -> None:
152287
pytest.raises(ValidationError, match="PDF/X-1a"),
153288
):
154289
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+
)

tests/test_convert_to_word.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,68 @@ def handler(request: httpx.Request) -> httpx.Response:
7171
assert str(response.input_id) == str(input_file.id)
7272

7373

74+
def test_convert_to_word_request_customization(
75+
monkeypatch: pytest.MonkeyPatch,
76+
) -> None:
77+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
78+
input_file = make_pdf_file(PdfRestFileID.generate(1))
79+
output_id = str(PdfRestFileID.generate())
80+
captured_timeout: dict[str, float | dict[str, float] | None] = {}
81+
82+
def handler(request: httpx.Request) -> httpx.Response:
83+
if request.method == "POST" and request.url.path == "/word":
84+
assert request.url.params["trace"] == "true"
85+
assert request.headers["X-Debug"] == "sync"
86+
captured_timeout["value"] = request.extensions.get("timeout")
87+
payload = json.loads(request.content.decode("utf-8"))
88+
assert payload["debug"] is True
89+
assert payload["id"] == str(input_file.id)
90+
assert payload["output"] == "custom"
91+
return httpx.Response(
92+
200,
93+
json={
94+
"inputId": [input_file.id],
95+
"outputId": [output_id],
96+
},
97+
)
98+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
99+
assert request.url.params["format"] == "info"
100+
assert request.url.params["trace"] == "true"
101+
assert request.headers["X-Debug"] == "sync"
102+
return httpx.Response(
103+
200,
104+
json=build_file_info_payload(
105+
output_id,
106+
"custom.docx",
107+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
108+
),
109+
)
110+
msg = f"Unexpected request {request.method} {request.url}"
111+
raise AssertionError(msg)
112+
113+
transport = httpx.MockTransport(handler)
114+
with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client:
115+
response = client.convert_to_word(
116+
input_file,
117+
output="custom",
118+
extra_query={"trace": "true"},
119+
extra_headers={"X-Debug": "sync"},
120+
extra_body={"debug": True},
121+
timeout=0.4,
122+
)
123+
124+
assert isinstance(response, PdfRestFileBasedResponse)
125+
assert response.output_file.name == "custom.docx"
126+
timeout_value = captured_timeout["value"]
127+
assert timeout_value is not None
128+
if isinstance(timeout_value, dict):
129+
assert all(
130+
component == pytest.approx(0.4) for component in timeout_value.values()
131+
)
132+
else:
133+
assert timeout_value == pytest.approx(0.4)
134+
135+
74136
@pytest.mark.asyncio
75137
async def test_async_convert_to_word_success(
76138
monkeypatch: pytest.MonkeyPatch,
@@ -125,6 +187,67 @@ def handler(request: httpx.Request) -> httpx.Response:
125187
assert str(response.input_id) == str(input_file.id)
126188

127189

190+
@pytest.mark.asyncio
191+
async def test_async_convert_to_word_request_customization(
192+
monkeypatch: pytest.MonkeyPatch,
193+
) -> None:
194+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
195+
input_file = make_pdf_file(PdfRestFileID.generate(2))
196+
output_id = str(PdfRestFileID.generate())
197+
captured_timeout: dict[str, float | dict[str, float] | None] = {}
198+
199+
def handler(request: httpx.Request) -> httpx.Response:
200+
if request.method == "POST" and request.url.path == "/word":
201+
assert request.url.params["trace"] == "async"
202+
assert request.headers["X-Debug"] == "async"
203+
captured_timeout["value"] = request.extensions.get("timeout")
204+
payload = json.loads(request.content.decode("utf-8"))
205+
assert payload["debug"] == "yes"
206+
assert payload["id"] == str(input_file.id)
207+
return httpx.Response(
208+
200,
209+
json={
210+
"inputId": [input_file.id],
211+
"outputId": [output_id],
212+
},
213+
)
214+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
215+
assert request.url.params["format"] == "info"
216+
assert request.url.params["trace"] == "async"
217+
assert request.headers["X-Debug"] == "async"
218+
return httpx.Response(
219+
200,
220+
json=build_file_info_payload(
221+
output_id,
222+
"async-custom.docx",
223+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
224+
),
225+
)
226+
msg = f"Unexpected request {request.method} {request.url}"
227+
raise AssertionError(msg)
228+
229+
transport = httpx.MockTransport(handler)
230+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
231+
response = await client.convert_to_word(
232+
input_file,
233+
extra_query={"trace": "async"},
234+
extra_headers={"X-Debug": "async"},
235+
extra_body={"debug": "yes"},
236+
timeout=0.55,
237+
)
238+
239+
assert isinstance(response, PdfRestFileBasedResponse)
240+
assert response.output_file.name == "async-custom.docx"
241+
timeout_value = captured_timeout["value"]
242+
assert timeout_value is not None
243+
if isinstance(timeout_value, dict):
244+
assert all(
245+
component == pytest.approx(0.55) for component in timeout_value.values()
246+
)
247+
else:
248+
assert timeout_value == pytest.approx(0.55)
249+
250+
128251
def test_convert_to_word_validation(monkeypatch: pytest.MonkeyPatch) -> None:
129252
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
130253
pdf_file = make_pdf_file(PdfRestFileID.generate(1))

0 commit comments

Comments
 (0)