Skip to content

Commit c743f65

Browse files
tests: Add optional-branch coverage for client payloads
- Add translate-to-file tests with pages/output defaults verified - Add async summarize/markdown/ocr/extract payload branch coverage - Add async redaction apply coverage for rgb_color serialization Assisted-by: Codex
1 parent 958752f commit c743f65

6 files changed

Lines changed: 415 additions & 2 deletions

tests/test_convert_to_markdown.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,63 @@ def handler(request: httpx.Request) -> httpx.Response:
304304
assert timeout_value == pytest.approx(0.4)
305305

306306

307+
@pytest.mark.asyncio
308+
async def test_async_convert_to_markdown_includes_pages_and_output(
309+
monkeypatch: pytest.MonkeyPatch,
310+
) -> None:
311+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
312+
input_file = make_pdf_file(PdfRestFileID.generate(2))
313+
output_id = str(PdfRestFileID.generate())
314+
payload_dump = ConvertToMarkdownPayload.model_validate(
315+
{
316+
"files": [input_file],
317+
"output_type": "file",
318+
"page_break_comments": "on",
319+
"pages": ["2-4"],
320+
"output": "async-md",
321+
}
322+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
323+
324+
seen: dict[str, int] = {"post": 0, "get": 0}
325+
326+
def handler(request: httpx.Request) -> httpx.Response:
327+
if request.method == "POST" and request.url.path == "/markdown":
328+
seen["post"] += 1
329+
payload = json.loads(request.content.decode("utf-8"))
330+
assert payload == payload_dump
331+
return httpx.Response(
332+
200,
333+
json={
334+
"inputId": [str(input_file.id)],
335+
"outputId": [output_id],
336+
},
337+
)
338+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
339+
seen["get"] += 1
340+
assert request.url.params["format"] == "info"
341+
return httpx.Response(
342+
200,
343+
json=_make_markdown_file(output_id, "async-pages.md").model_dump(
344+
mode="json", by_alias=True
345+
),
346+
)
347+
msg = f"Unexpected request {request.method} {request.url}"
348+
raise AssertionError(msg)
349+
350+
transport = httpx.MockTransport(handler)
351+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
352+
response = await client.convert_to_markdown(
353+
input_file,
354+
pages=["2-4"],
355+
output="async-md",
356+
page_break_comments="on",
357+
)
358+
359+
assert seen == {"post": 1, "get": 1}
360+
assert isinstance(response, PdfRestFileBasedResponse)
361+
assert len(response.output_files) == 1
362+
363+
307364
@pytest.mark.asyncio
308365
async def test_async_convert_to_markdown_success(
309366
monkeypatch: pytest.MonkeyPatch,

tests/test_extract_pdf_text_to_file.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,61 @@ def handler(request: httpx.Request) -> httpx.Response:
344344
assert isinstance(response, PdfRestFileBasedResponse)
345345
assert len(response.output_files) == 1
346346
assert response.input_id == input_file.id
347+
348+
349+
@pytest.mark.asyncio
350+
async def test_async_extract_pdf_text_to_file_includes_pages(
351+
monkeypatch: pytest.MonkeyPatch,
352+
) -> None:
353+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
354+
input_file = make_pdf_file(PdfRestFileID.generate(2))
355+
output_id = str(PdfRestFileID.generate())
356+
payload_dump = ExtractTextPayload.model_validate(
357+
{
358+
"files": [input_file],
359+
"full_text": "document",
360+
"preserve_line_breaks": "off",
361+
"word_style": "off",
362+
"word_coordinates": "off",
363+
"output_type": "file",
364+
"pages": ["2-3"],
365+
}
366+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
367+
368+
seen: dict[str, int] = {"post": 0, "get": 0}
369+
370+
def handler(request: httpx.Request) -> httpx.Response:
371+
if request.method == "POST" and request.url.path == "/extracted-text":
372+
seen["post"] += 1
373+
payload = json.loads(request.content.decode("utf-8"))
374+
assert payload == payload_dump
375+
return httpx.Response(
376+
200,
377+
json={
378+
"inputId": [str(input_file.id)],
379+
"outputId": [output_id],
380+
},
381+
)
382+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
383+
seen["get"] += 1
384+
assert request.url.params["format"] == "info"
385+
return httpx.Response(
386+
200,
387+
json=_make_text_file(output_id, "async-pages.txt").model_dump(
388+
mode="json", by_alias=True
389+
),
390+
)
391+
msg = f"Unexpected request {request.method} {request.url}"
392+
raise AssertionError(msg)
393+
394+
transport = httpx.MockTransport(handler)
395+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
396+
response = await client.extract_pdf_text_to_file(
397+
input_file,
398+
pages=["2-3"],
399+
)
400+
401+
assert seen == {"post": 1, "get": 1}
402+
assert isinstance(response, PdfRestFileBasedResponse)
403+
assert len(response.output_files) == 1
404+
assert response.input_id == input_file.id

tests/test_ocr_pdf.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,57 @@ def handler(request: httpx.Request) -> httpx.Response:
281281
assert timeout_value == pytest.approx(0.4)
282282

283283

284+
@pytest.mark.asyncio
285+
async def test_async_ocr_pdf_includes_pages(monkeypatch: pytest.MonkeyPatch) -> None:
286+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
287+
input_file = make_pdf_file(PdfRestFileID.generate(2))
288+
payload_dump = OcrPdfPayload.model_validate(
289+
{
290+
"files": [input_file],
291+
"pages": ["1-2"],
292+
"languages": ["English"],
293+
}
294+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
295+
output_id = str(PdfRestFileID.generate())
296+
297+
seen: dict[str, int] = {"post": 0, "get": 0}
298+
299+
def handler(request: httpx.Request) -> httpx.Response:
300+
if request.method == "POST" and request.url.path == "/pdf-with-ocr-text":
301+
seen["post"] += 1
302+
payload = json.loads(request.content.decode("utf-8"))
303+
assert payload == payload_dump
304+
return httpx.Response(
305+
200,
306+
json={
307+
"inputId": str(input_file.id),
308+
"outputId": output_id,
309+
},
310+
)
311+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
312+
seen["get"] += 1
313+
return httpx.Response(
314+
200,
315+
json=make_pdf_file(output_id, "async-ocr.pdf").model_dump(
316+
mode="json", by_alias=True
317+
),
318+
)
319+
msg = f"Unexpected request {request.method} {request.url}"
320+
raise AssertionError(msg)
321+
322+
transport = httpx.MockTransport(handler)
323+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
324+
response = await client.ocr_pdf(
325+
input_file,
326+
pages=["1-2"],
327+
languages=["English"],
328+
)
329+
330+
assert seen == {"post": 1, "get": 1}
331+
assert isinstance(response, PdfRestFileBasedResponse)
332+
assert response.output_file.id == output_id
333+
334+
284335
@pytest.mark.asyncio
285336
async def test_async_ocr_pdf_success(
286337
monkeypatch: pytest.MonkeyPatch,

tests/test_pdf_redaction_apply.py

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
import pytest
77
from pydantic import ValidationError
88

9-
from pdfrest import PdfRestClient
9+
from pdfrest import AsyncPdfRestClient, PdfRestClient
1010
from pdfrest.models import PdfRestFileBasedResponse, PdfRestFileID
1111
from pdfrest.models._internal import PdfRedactionApplyPayload
1212
from pdfrest.types import PdfRGBColor
1313

14-
from .graphics_test_helpers import VALID_API_KEY, build_file_info_payload, make_pdf_file
14+
from .graphics_test_helpers import (
15+
ASYNC_API_KEY,
16+
VALID_API_KEY,
17+
build_file_info_payload,
18+
make_pdf_file,
19+
)
1520

1621

1722
@pytest.mark.parametrize(
@@ -87,3 +92,63 @@ def test_apply_redactions_invalid_color(monkeypatch: pytest.MonkeyPatch) -> None
8792

8893
with pytest.raises(ValidationError, match="greater than or equal to 0"):
8994
client.apply_redactions(input_file, rgb_color=[-1, 0, 0])
95+
96+
97+
@pytest.mark.asyncio
98+
async def test_async_apply_redactions_includes_rgb_color(
99+
monkeypatch: pytest.MonkeyPatch,
100+
) -> None:
101+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
102+
input_file = make_pdf_file(PdfRestFileID.generate(2))
103+
output_id = str(PdfRestFileID.generate())
104+
105+
payload_data: dict[str, object] = {
106+
"files": [input_file],
107+
"rgb_color": (10, 20, 30),
108+
"output": "async-output",
109+
}
110+
111+
payload_model_dump = PdfRedactionApplyPayload.model_validate(
112+
payload_data
113+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
114+
115+
seen: dict[str, int] = {"post": 0, "get": 0}
116+
117+
def handler(request: httpx.Request) -> httpx.Response:
118+
if (
119+
request.method == "POST"
120+
and request.url.path == "/pdf-with-redacted-text-applied"
121+
):
122+
seen["post"] += 1
123+
body = json.loads(request.content.decode("utf-8"))
124+
assert body == payload_model_dump
125+
return httpx.Response(
126+
200,
127+
json={
128+
"inputId": [input_file.id],
129+
"outputId": [output_id],
130+
},
131+
)
132+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
133+
seen["get"] += 1
134+
return httpx.Response(
135+
200,
136+
json=build_file_info_payload(
137+
output_id, "async-output.pdf", "application/pdf"
138+
),
139+
)
140+
msg = f"Unexpected request {request.method} {request.url}"
141+
raise AssertionError(msg)
142+
143+
transport = httpx.MockTransport(handler)
144+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
145+
response = await client.apply_redactions(
146+
input_file,
147+
rgb_color=(10, 20, 30),
148+
output="async-output",
149+
)
150+
151+
assert seen == {"post": 1, "get": 1}
152+
assert isinstance(response, PdfRestFileBasedResponse)
153+
assert response.output_files[0].name == "async-output.pdf"
154+
assert response.output_files[0].type == "application/pdf"

tests/test_summarize_pdf_text.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,65 @@ def handler(request: httpx.Request) -> httpx.Response:
402402
assert timeout_value == pytest.approx(0.25)
403403

404404

405+
@pytest.mark.asyncio
406+
async def test_async_summarize_text_to_file_includes_pages_and_output(
407+
monkeypatch: pytest.MonkeyPatch,
408+
) -> None:
409+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
410+
input_file = make_pdf_file(PdfRestFileID.generate(2))
411+
payload_dump = SummarizePdfTextPayload.model_validate(
412+
{
413+
"files": [input_file],
414+
"output_type": "file",
415+
"output_format": "markdown",
416+
"summary_format": "overview",
417+
"target_word_count": 400,
418+
"pages": ["1-3"],
419+
"output": "async-summary",
420+
}
421+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
422+
output_id = str(PdfRestFileID.generate())
423+
424+
seen: dict[str, int] = {"post": 0, "get": 0}
425+
426+
def handler(request: httpx.Request) -> httpx.Response:
427+
if request.method == "POST" and request.url.path == "/summarized-pdf-text":
428+
seen["post"] += 1
429+
payload = json.loads(request.content.decode("utf-8"))
430+
assert payload == payload_dump
431+
return httpx.Response(
432+
200,
433+
json={
434+
"outputId": output_id,
435+
"inputId": str(input_file.id),
436+
},
437+
)
438+
if request.method == "GET" and request.url.path == f"/resource/{output_id}":
439+
seen["get"] += 1
440+
assert request.url.params["format"] == "info"
441+
return httpx.Response(
442+
200,
443+
json=build_file_info_payload(
444+
output_id, "async-summary.md", "text/markdown"
445+
),
446+
)
447+
msg = f"Unexpected request {request.method} {request.url}"
448+
raise AssertionError(msg)
449+
450+
transport = httpx.MockTransport(handler)
451+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
452+
response = await client.summarize_text_to_file(
453+
input_file,
454+
pages=["1-3"],
455+
output="async-summary",
456+
)
457+
458+
assert seen == {"post": 1, "get": 1}
459+
assert isinstance(response, PdfRestFileBasedResponse)
460+
assert response.output_file.id == output_id
461+
assert response.output_file.name == "async-summary.md"
462+
463+
405464
def test_summarize_text_success(monkeypatch: pytest.MonkeyPatch) -> None:
406465
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
407466
input_file = make_pdf_file(PdfRestFileID.generate(2))

0 commit comments

Comments
 (0)