Skip to content

Commit ca21c8f

Browse files
datalogics-kamdatalogics-cgreen
authored andcommitted
tests: Add validation and request customization tests for PDF conversions
- Added tests for invalid page size handling across various conversion methods: `convert_html_to_pdf`, `convert_url_to_pdf`, `convert_email_to_pdf`, `convert_image_to_pdf`, `convert_office_to_pdf`, and `convert_postscript_to_pdf`. - Introduced request customization tests, including validations for custom query parameters, headers, body, and timeout behavior. - Ensured comprehensive validation of async and sync client operations. Assisted-by: Codex
1 parent b7b25f0 commit ca21c8f

8 files changed

Lines changed: 478 additions & 1 deletion

tests/live/test_live_convert_html_to_pdf.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from pdfrest import AsyncPdfRestClient, PdfRestClient
5+
from pdfrest import AsyncPdfRestClient, PdfRestApiError, PdfRestClient
66
from pdfrest.models import PdfRestFile
77

88
from ..resources import get_test_resource_path
@@ -71,3 +71,38 @@ async def test_live_async_convert_html_to_pdf_success(
7171
assert response.warning is None
7272
assert str(response.input_id) == str(uploaded_html_for_pdf.id)
7373
assert output_file.name.startswith("live-html-file-async")
74+
75+
76+
def test_live_convert_html_to_pdf_invalid_page_size(
77+
pdfrest_api_key: str,
78+
pdfrest_live_base_url: str,
79+
uploaded_html_for_pdf: PdfRestFile,
80+
) -> None:
81+
with (
82+
PdfRestClient(
83+
api_key=pdfrest_api_key,
84+
base_url=pdfrest_live_base_url,
85+
) as client,
86+
pytest.raises(PdfRestApiError, match=r"(?i)page_size|page size"),
87+
):
88+
client.convert_html_to_pdf(
89+
uploaded_html_for_pdf,
90+
extra_body={"page_size": "poster"},
91+
)
92+
93+
94+
@pytest.mark.asyncio
95+
async def test_live_async_convert_html_to_pdf_invalid_page_size(
96+
pdfrest_api_key: str,
97+
pdfrest_live_base_url: str,
98+
uploaded_html_for_pdf: PdfRestFile,
99+
) -> None:
100+
async with AsyncPdfRestClient(
101+
api_key=pdfrest_api_key,
102+
base_url=pdfrest_live_base_url,
103+
) as client:
104+
with pytest.raises(PdfRestApiError, match=r"(?i)page_size|page size"):
105+
await client.convert_html_to_pdf(
106+
uploaded_html_for_pdf,
107+
extra_body={"page_size": "poster"},
108+
)

tests/live/test_live_convert_url_to_pdf.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,47 @@ def test_live_convert_url_to_pdf_success(
3333
assert output_file.name.startswith("live-html-url")
3434

3535

36+
@pytest.mark.asyncio
37+
async def test_live_async_convert_url_to_pdf_success(
38+
pdfrest_api_key: str,
39+
pdfrest_live_base_url: str,
40+
) -> None:
41+
async with AsyncPdfRestClient(
42+
api_key=pdfrest_api_key,
43+
base_url=pdfrest_live_base_url,
44+
) as client:
45+
response = await client.convert_url_to_pdf(
46+
LIVE_HTML_URL,
47+
output="live-html-url-async",
48+
page_orientation="portrait",
49+
)
50+
51+
assert response.output_files
52+
output_file = response.output_file
53+
assert output_file.type == "application/pdf"
54+
assert output_file.size > 0
55+
assert response.warning is None
56+
assert str(response.input_id)
57+
assert output_file.name.startswith("live-html-url-async")
58+
59+
60+
def test_live_convert_url_to_pdf_invalid_page_size(
61+
pdfrest_api_key: str,
62+
pdfrest_live_base_url: str,
63+
) -> None:
64+
with (
65+
PdfRestClient(
66+
api_key=pdfrest_api_key,
67+
base_url=pdfrest_live_base_url,
68+
) as client,
69+
pytest.raises(PdfRestApiError, match=r"(?i)page_size|page size"),
70+
):
71+
client.convert_url_to_pdf(
72+
LIVE_HTML_URL,
73+
extra_body={"page_size": "poster"},
74+
)
75+
76+
3677
@pytest.mark.asyncio
3778
async def test_live_async_convert_url_to_pdf_invalid_page_size(
3879
pdfrest_api_key: str,

tests/test_convert_email_to_pdf.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,67 @@ def handler(request: httpx.Request) -> httpx.Response:
210210
assert response.output_file.name == "async-converted.pdf"
211211
assert response.output_file.type == "application/pdf"
212212
assert str(response.input_id) == str(input_file.id)
213+
214+
215+
@pytest.mark.asyncio
216+
async def test_async_convert_email_to_pdf_request_customization(
217+
monkeypatch: pytest.MonkeyPatch,
218+
) -> None:
219+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
220+
input_file = make_source_file(
221+
str(PdfRestFileID.generate(2)),
222+
"message/rfc822",
223+
"async-message.eml",
224+
)
225+
output_id = str(PdfRestFileID.generate())
226+
captured_timeout: dict[str, float | dict[str, float] | None] = {}
227+
228+
def handler(request: httpx.Request) -> httpx.Response:
229+
if request.method == "POST" and request.url.path == "/pdf":
230+
assert request.url.params["trace"] == "async"
231+
assert request.headers["X-Debug"] == "async"
232+
captured_timeout["value"] = request.extensions.get("timeout")
233+
payload = json.loads(request.content.decode("utf-8"))
234+
assert payload["debug"] is True
235+
assert payload["id"] == str(input_file.id)
236+
assert payload["output"] == "async-custom"
237+
return httpx.Response(
238+
200,
239+
json={
240+
"inputId": [input_file.id],
241+
"outputId": [output_id],
242+
},
243+
)
244+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
245+
assert request.url.params["trace"] == "async"
246+
assert request.headers["X-Debug"] == "async"
247+
return httpx.Response(
248+
200,
249+
json=build_file_info_payload(
250+
output_id, "async-custom.pdf", "application/pdf"
251+
),
252+
)
253+
msg = f"Unexpected request {request.method} {request.url}"
254+
raise AssertionError(msg)
255+
256+
transport = httpx.MockTransport(handler)
257+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
258+
response = await client.convert_email_to_pdf(
259+
input_file,
260+
output="async-custom",
261+
extra_query={"trace": "async"},
262+
extra_headers={"X-Debug": "async"},
263+
extra_body={"debug": True},
264+
timeout=0.6,
265+
)
266+
267+
assert isinstance(response, PdfRestFileBasedResponse)
268+
assert response.output_file.name == "async-custom.pdf"
269+
timeout_value = captured_timeout["value"]
270+
assert timeout_value is not None
271+
if isinstance(timeout_value, dict):
272+
assert all(
273+
component == pytest.approx(0.6) for component in timeout_value.values()
274+
)
275+
else:
276+
assert timeout_value == pytest.approx(0.6)

tests/test_convert_html_to_pdf.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,69 @@ def handler(request: httpx.Request) -> httpx.Response:
231231
assert response.output_file.name == "async-converted.pdf"
232232
assert response.output_file.type == "application/pdf"
233233
assert str(response.input_id) == str(input_file.id)
234+
235+
236+
@pytest.mark.asyncio
237+
async def test_async_convert_html_to_pdf_request_customization(
238+
monkeypatch: pytest.MonkeyPatch,
239+
) -> None:
240+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
241+
input_file = make_source_file(
242+
str(PdfRestFileID.generate(2)),
243+
"text/html",
244+
"example.html",
245+
)
246+
output_id = str(PdfRestFileID.generate())
247+
captured_timeout: dict[str, float | dict[str, float] | None] = {}
248+
249+
def handler(request: httpx.Request) -> httpx.Response:
250+
if request.method == "POST" and request.url.path == "/pdf":
251+
assert request.url.params["trace"] == "async"
252+
assert request.headers["X-Debug"] == "async"
253+
captured_timeout["value"] = request.extensions.get("timeout")
254+
payload = json.loads(request.content.decode("utf-8"))
255+
assert payload["debug"] is True
256+
assert payload["id"] == str(input_file.id)
257+
assert payload["output"] == "async-custom"
258+
assert payload["downsample"] == 300
259+
return httpx.Response(
260+
200,
261+
json={
262+
"inputId": [input_file.id],
263+
"outputId": [output_id],
264+
},
265+
)
266+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
267+
assert request.url.params["trace"] == "async"
268+
assert request.headers["X-Debug"] == "async"
269+
return httpx.Response(
270+
200,
271+
json=build_file_info_payload(
272+
output_id, "async-custom.pdf", "application/pdf"
273+
),
274+
)
275+
msg = f"Unexpected request {request.method} {request.url}"
276+
raise AssertionError(msg)
277+
278+
transport = httpx.MockTransport(handler)
279+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
280+
response = await client.convert_html_to_pdf(
281+
input_file,
282+
output="async-custom",
283+
downsample=300,
284+
extra_query={"trace": "async"},
285+
extra_headers={"X-Debug": "async"},
286+
extra_body={"debug": True},
287+
timeout=0.6,
288+
)
289+
290+
assert isinstance(response, PdfRestFileBasedResponse)
291+
assert response.output_file.name == "async-custom.pdf"
292+
timeout_value = captured_timeout["value"]
293+
assert timeout_value is not None
294+
if isinstance(timeout_value, dict):
295+
assert all(
296+
component == pytest.approx(0.6) for component in timeout_value.values()
297+
)
298+
else:
299+
assert timeout_value == pytest.approx(0.6)

tests/test_convert_image_to_pdf.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,67 @@ def handler(request: httpx.Request) -> httpx.Response:
203203
assert response.output_file.name == "async-converted.pdf"
204204
assert response.output_file.type == "application/pdf"
205205
assert str(response.input_id) == str(input_file.id)
206+
207+
208+
@pytest.mark.asyncio
209+
async def test_async_convert_image_to_pdf_request_customization(
210+
monkeypatch: pytest.MonkeyPatch,
211+
) -> None:
212+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
213+
input_file = make_source_file(
214+
str(PdfRestFileID.generate(2)),
215+
"image/png",
216+
"async.png",
217+
)
218+
output_id = str(PdfRestFileID.generate())
219+
captured_timeout: dict[str, float | dict[str, float] | None] = {}
220+
221+
def handler(request: httpx.Request) -> httpx.Response:
222+
if request.method == "POST" and request.url.path == "/pdf":
223+
assert request.url.params["trace"] == "async"
224+
assert request.headers["X-Debug"] == "async"
225+
captured_timeout["value"] = request.extensions.get("timeout")
226+
payload = json.loads(request.content.decode("utf-8"))
227+
assert payload["debug"] is True
228+
assert payload["id"] == str(input_file.id)
229+
assert payload["output"] == "async-custom"
230+
return httpx.Response(
231+
200,
232+
json={
233+
"inputId": [input_file.id],
234+
"outputId": [output_id],
235+
},
236+
)
237+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
238+
assert request.url.params["trace"] == "async"
239+
assert request.headers["X-Debug"] == "async"
240+
return httpx.Response(
241+
200,
242+
json=build_file_info_payload(
243+
output_id, "async-custom.pdf", "application/pdf"
244+
),
245+
)
246+
msg = f"Unexpected request {request.method} {request.url}"
247+
raise AssertionError(msg)
248+
249+
transport = httpx.MockTransport(handler)
250+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
251+
response = await client.convert_image_to_pdf(
252+
input_file,
253+
output="async-custom",
254+
extra_query={"trace": "async"},
255+
extra_headers={"X-Debug": "async"},
256+
extra_body={"debug": True},
257+
timeout=0.6,
258+
)
259+
260+
assert isinstance(response, PdfRestFileBasedResponse)
261+
assert response.output_file.name == "async-custom.pdf"
262+
timeout_value = captured_timeout["value"]
263+
assert timeout_value is not None
264+
if isinstance(timeout_value, dict):
265+
assert all(
266+
component == pytest.approx(0.6) for component in timeout_value.values()
267+
)
268+
else:
269+
assert timeout_value == pytest.approx(0.6)

tests/test_convert_office_to_pdf.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,73 @@ def handler(request: httpx.Request) -> httpx.Response:
234234
assert response.output_file.name == "async-converted.pdf"
235235
assert response.output_file.type == "application/pdf"
236236
assert str(response.input_id) == str(input_file.id)
237+
238+
239+
@pytest.mark.asyncio
240+
async def test_async_convert_office_to_pdf_request_customization(
241+
monkeypatch: pytest.MonkeyPatch,
242+
) -> None:
243+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
244+
input_file = make_source_file(
245+
str(PdfRestFileID.generate(2)),
246+
"application/vnd.ms-excel",
247+
"sheet.xls",
248+
)
249+
output_id = str(PdfRestFileID.generate())
250+
captured_timeout: dict[str, float | dict[str, float] | None] = {}
251+
252+
def handler(request: httpx.Request) -> httpx.Response:
253+
if request.method == "POST" and request.url.path == "/pdf":
254+
assert request.url.params["trace"] == "async"
255+
assert request.headers["X-Debug"] == "async"
256+
captured_timeout["value"] = request.extensions.get("timeout")
257+
payload = json.loads(request.content.decode("utf-8"))
258+
assert payload["debug"] is True
259+
assert payload["compression"] == "lossy"
260+
assert payload["downsample"] == 600
261+
assert payload["locale"] == "US"
262+
assert payload["id"] == str(input_file.id)
263+
assert payload["output"] == "async-custom"
264+
return httpx.Response(
265+
200,
266+
json={
267+
"inputId": [input_file.id],
268+
"outputId": [output_id],
269+
},
270+
)
271+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
272+
assert request.url.params["trace"] == "async"
273+
assert request.headers["X-Debug"] == "async"
274+
return httpx.Response(
275+
200,
276+
json=build_file_info_payload(
277+
output_id, "async-custom.pdf", "application/pdf"
278+
),
279+
)
280+
msg = f"Unexpected request {request.method} {request.url}"
281+
raise AssertionError(msg)
282+
283+
transport = httpx.MockTransport(handler)
284+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
285+
response = await client.convert_office_to_pdf(
286+
input_file,
287+
output="async-custom",
288+
compression="lossy",
289+
downsample=600,
290+
locale="US",
291+
extra_query={"trace": "async"},
292+
extra_headers={"X-Debug": "async"},
293+
extra_body={"debug": True},
294+
timeout=0.6,
295+
)
296+
297+
assert isinstance(response, PdfRestFileBasedResponse)
298+
assert response.output_file.name == "async-custom.pdf"
299+
timeout_value = captured_timeout["value"]
300+
assert timeout_value is not None
301+
if isinstance(timeout_value, dict):
302+
assert all(
303+
component == pytest.approx(0.6) for component in timeout_value.values()
304+
)
305+
else:
306+
assert timeout_value == pytest.approx(0.6)

0 commit comments

Comments
 (0)