Skip to content

Commit 1d9ecbb

Browse files
Markdown/powerpoint: Add missing async tests
Assisted-by: Codex
1 parent d5f602e commit 1d9ecbb

2 files changed

Lines changed: 133 additions & 0 deletions

File tree

tests/test_convert_to_markdown.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,44 @@ def test_convert_to_markdown_payload_invalid_page_break_comments() -> None:
6666
)
6767

6868

69+
@pytest.mark.asyncio
70+
async def test_async_convert_to_markdown_payload_rejects_non_pdf() -> None:
71+
file_id = str(PdfRestFileID.generate())
72+
text_file = PdfRestFile.model_validate(
73+
{
74+
"id": file_id,
75+
"name": "notes.txt",
76+
"url": f"https://api.pdfrest.com/resource/{file_id}",
77+
"type": "text/plain",
78+
"size": 64,
79+
"modified": "2024-01-01T00:00:00Z",
80+
"scheduledDeletionTimeUtc": None,
81+
}
82+
)
83+
with pytest.raises(ValidationError, match="Must be a PDF file"):
84+
ConvertToMarkdownPayload.model_validate({"files": [text_file]})
85+
86+
87+
@pytest.mark.asyncio
88+
async def test_async_convert_to_markdown_payload_invalid_page_range() -> None:
89+
file_repr = make_pdf_file(PdfRestFileID.generate(1))
90+
with pytest.raises(
91+
ValidationError, match="The start page must be less than or equal to the end"
92+
):
93+
ConvertToMarkdownPayload.model_validate(
94+
{"files": [file_repr], "pages": ["5-2"]}
95+
)
96+
97+
98+
@pytest.mark.asyncio
99+
async def test_async_convert_to_markdown_payload_invalid_page_break_comments() -> None:
100+
file_repr = make_pdf_file(PdfRestFileID.generate(1))
101+
with pytest.raises(ValidationError, match="Input should be 'on' or 'off'"):
102+
ConvertToMarkdownPayload.model_validate(
103+
{"files": [file_repr], "page_break_comments": "maybe"}
104+
)
105+
106+
69107
def test_convert_to_markdown_success(monkeypatch: pytest.MonkeyPatch) -> None:
70108
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
71109
input_file = make_pdf_file(PdfRestFileID.generate(1))
@@ -198,6 +236,74 @@ def handler(request: httpx.Request) -> httpx.Response:
198236
assert get_timeout == pytest.approx(0.4)
199237

200238

239+
@pytest.mark.asyncio
240+
async def test_async_convert_to_markdown_request_customization(
241+
monkeypatch: pytest.MonkeyPatch,
242+
) -> None:
243+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
244+
input_file = make_pdf_file(PdfRestFileID.generate(2))
245+
payload_dump = ConvertToMarkdownPayload.model_validate(
246+
{
247+
"files": [input_file],
248+
"output_type": "file",
249+
"page_break_comments": "off",
250+
}
251+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
252+
output_id = str(PdfRestFileID.generate())
253+
captured_timeout: dict[str, float | dict[str, float] | None] = {}
254+
255+
def handler(request: httpx.Request) -> httpx.Response:
256+
if request.method == "POST" and request.url.path == "/markdown":
257+
assert request.url.params["trace"] == "true"
258+
assert request.headers["X-Debug"] == "async"
259+
captured_timeout["value"] = request.extensions.get("timeout")
260+
payload = json.loads(request.content.decode("utf-8"))
261+
for key, value in payload_dump.items():
262+
assert payload[key] == value
263+
assert payload["debug"] is True
264+
return httpx.Response(
265+
200,
266+
json={
267+
"inputId": [str(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["format"] == "info"
273+
assert request.url.params["trace"] == "true"
274+
assert request.headers["X-Debug"] == "async"
275+
return httpx.Response(
276+
200,
277+
json=_make_markdown_file(output_id, "debug-async.md").model_dump(
278+
mode="json", by_alias=True
279+
),
280+
)
281+
msg = f"Unexpected request {request.method} {request.url}"
282+
raise AssertionError(msg)
283+
284+
transport = httpx.MockTransport(handler)
285+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
286+
response = await client.convert_to_markdown(
287+
input_file,
288+
extra_query={"trace": "true"},
289+
extra_headers={"X-Debug": "async"},
290+
extra_body={"debug": True},
291+
timeout=0.4,
292+
page_break_comments="off",
293+
)
294+
295+
assert isinstance(response, PdfRestFileBasedResponse)
296+
assert len(response.output_files) == 1
297+
timeout_value = captured_timeout["value"]
298+
assert timeout_value is not None
299+
if isinstance(timeout_value, dict):
300+
assert all(
301+
component == pytest.approx(0.4) for component in timeout_value.values()
302+
)
303+
else:
304+
assert timeout_value == pytest.approx(0.4)
305+
306+
201307
@pytest.mark.asyncio
202308
async def test_async_convert_to_markdown_success(
203309
monkeypatch: pytest.MonkeyPatch,

tests/test_convert_to_powerpoint.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,30 @@ def test_convert_to_powerpoint_validation(monkeypatch: pytest.MonkeyPatch) -> No
275275
client.convert_to_powerpoint(
276276
[pdf_file, make_pdf_file(PdfRestFileID.generate())]
277277
)
278+
279+
280+
@pytest.mark.asyncio
281+
async def test_async_convert_to_powerpoint_validation(
282+
monkeypatch: pytest.MonkeyPatch,
283+
) -> None:
284+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
285+
pdf_file = make_pdf_file(PdfRestFileID.generate(1))
286+
png_file = PdfRestFile.model_validate(
287+
build_file_info_payload(
288+
PdfRestFileID.generate(),
289+
"example.png",
290+
"image/png",
291+
)
292+
)
293+
transport = httpx.MockTransport(lambda request: (_ for _ in ()).throw(RuntimeError))
294+
295+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
296+
with pytest.raises(ValidationError, match="Must be a PDF file"):
297+
await client.convert_to_powerpoint(png_file)
298+
299+
with pytest.raises(
300+
ValidationError, match="List should have at most 1 item after validation"
301+
):
302+
await client.convert_to_powerpoint(
303+
[pdf_file, make_pdf_file(PdfRestFileID.generate())]
304+
)

0 commit comments

Comments
 (0)