Skip to content

Commit aac13f4

Browse files
tests: Add async customization test for extract_pdf_text method
- Introduced `test_async_extract_pdf_text_request_customization` to validate async behavior with customized query parameters, headers, body data, and timeout configurations. - Ensured payload consistency and timeout accuracy within the test. - Mocked API transport to emulate request handling and validate expected responses. Assisted-by: Codex
1 parent 9fd96c3 commit aac13f4

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

tests/test_extract_pdf_text.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,61 @@ def handler(request: httpx.Request) -> httpx.Response:
166166
assert response.model_dump(by_alias=True, exclude_none=True) == expected_response
167167

168168

169+
@pytest.mark.asyncio
170+
async def test_async_extract_pdf_text_request_customization(
171+
monkeypatch: pytest.MonkeyPatch,
172+
) -> None:
173+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
174+
input_file = make_pdf_file(PdfRestFileID.generate(2))
175+
payload_dump = ExtractTextPayload.model_validate(
176+
{
177+
"files": [input_file],
178+
"full_text": "document",
179+
"preserve_line_breaks": False,
180+
"word_style": False,
181+
"word_coordinates": False,
182+
"output_type": "json",
183+
}
184+
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
185+
expected_response = _make_extracted_text_document_payload(str(input_file.id))
186+
captured_timeout: dict[str, float | dict[str, float] | None] = {}
187+
188+
def handler(request: httpx.Request) -> httpx.Response:
189+
if request.method == "POST" and request.url.path == "/extracted-text":
190+
assert request.url.params["trace"] == "true"
191+
assert request.headers["X-Debug"] == "async-json"
192+
captured_timeout["post"] = request.extensions.get("timeout")
193+
payload = json.loads(request.content.decode("utf-8"))
194+
assert payload == payload_dump | {"debug": True}
195+
return httpx.Response(200, json=expected_response)
196+
msg = f"Unexpected request {request.method} {request.url}"
197+
raise AssertionError(msg)
198+
199+
transport = httpx.MockTransport(handler)
200+
async with AsyncPdfRestClient(
201+
api_key=ASYNC_API_KEY,
202+
transport=transport,
203+
) as client:
204+
response = await client.extract_pdf_text(
205+
input_file,
206+
extra_query={"trace": "true"},
207+
extra_headers={"X-Debug": "async-json"},
208+
extra_body={"debug": True},
209+
timeout=0.25,
210+
)
211+
212+
assert isinstance(response, ExtractedTextDocument)
213+
post_timeout = captured_timeout["post"]
214+
assert post_timeout is not None
215+
if isinstance(post_timeout, dict):
216+
assert all(
217+
component == pytest.approx(0.25) for component in post_timeout.values()
218+
)
219+
else:
220+
assert post_timeout == pytest.approx(0.25)
221+
assert response.model_dump(by_alias=True, exclude_none=True) == expected_response
222+
223+
169224
@pytest.mark.asyncio
170225
@pytest.mark.parametrize("options", EXTRACT_TEXT_OPTION_SETS)
171226
@pytest.mark.parametrize("pages", PAGES_OPTION_SETS)

0 commit comments

Comments
 (0)