Skip to content

Commit d64fb28

Browse files
tests: Add async tests for extract_pdf_text method error handling
- Introduced tests for async `extract_pdf_text` to validate various error scenarios: - `test_async_extract_pdf_text_multi_file_guard`: Ensures validation for single file restriction. - `test_async_extract_pdf_text_invalid_pages`: Verifies validation for improper page ranges. - `test_async_extract_pdf_text_server_error`: Tests handling of server error responses. - `test_async_extract_pdf_text_invalid_option_values`: Checks validation for invalid option values. - Mocked API transport to simulate responses and validate exception handling. Assisted-by: Codex
1 parent aac13f4 commit d64fb28

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

tests/test_extract_pdf_text.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,21 @@ def test_extract_pdf_text_multi_file_guard(monkeypatch: pytest.MonkeyPatch) -> N
292292
client.extract_pdf_text(files)
293293

294294

295+
@pytest.mark.asyncio
296+
async def test_async_extract_pdf_text_multi_file_guard(
297+
monkeypatch: pytest.MonkeyPatch,
298+
) -> None:
299+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
300+
files = [
301+
make_pdf_file(PdfRestFileID.generate(1)),
302+
make_pdf_file(PdfRestFileID.generate(2)),
303+
]
304+
transport = httpx.MockTransport(lambda request: httpx.Response(500))
305+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
306+
with pytest.raises(ValidationError, match="at most 1 item"):
307+
await client.extract_pdf_text(files)
308+
309+
295310
def test_extract_pdf_text_invalid_pages(monkeypatch: pytest.MonkeyPatch) -> None:
296311
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
297312
input_file = make_pdf_file(PdfRestFileID.generate(1))
@@ -306,6 +321,21 @@ def test_extract_pdf_text_invalid_pages(monkeypatch: pytest.MonkeyPatch) -> None
306321
client.extract_pdf_text(input_file, pages=["5-1"])
307322

308323

324+
@pytest.mark.asyncio
325+
async def test_async_extract_pdf_text_invalid_pages(
326+
monkeypatch: pytest.MonkeyPatch,
327+
) -> None:
328+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
329+
input_file = make_pdf_file(PdfRestFileID.generate(1))
330+
transport = httpx.MockTransport(lambda request: httpx.Response(500))
331+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
332+
with pytest.raises(
333+
ValidationError,
334+
match="The start page must be less than or equal to the end",
335+
):
336+
await client.extract_pdf_text(input_file, pages=["5-1"])
337+
338+
309339
def test_extract_pdf_text_server_error(monkeypatch: pytest.MonkeyPatch) -> None:
310340
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
311341
input_file = make_pdf_file(PdfRestFileID.generate(1))
@@ -324,6 +354,25 @@ def handler(request: httpx.Request) -> httpx.Response:
324354
client.extract_pdf_text(input_file, full_text="off")
325355

326356

357+
@pytest.mark.asyncio
358+
async def test_async_extract_pdf_text_server_error(
359+
monkeypatch: pytest.MonkeyPatch,
360+
) -> None:
361+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
362+
input_file = make_pdf_file(PdfRestFileID.generate(1))
363+
364+
def handler(request: httpx.Request) -> httpx.Response:
365+
if request.method == "POST" and request.url.path == "/extracted-text":
366+
return httpx.Response(400, json={"message": "Invalid option"})
367+
msg = f"Unexpected request {request.method} {request.url}"
368+
raise AssertionError(msg)
369+
370+
transport = httpx.MockTransport(handler)
371+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
372+
with pytest.raises(PdfRestApiError, match="Invalid option"):
373+
await client.extract_pdf_text(input_file, full_text="off")
374+
375+
327376
@pytest.mark.parametrize(
328377
("invalid_kwargs", "match"),
329378
[
@@ -352,3 +401,32 @@ def test_extract_pdf_text_invalid_option_values(
352401
pytest.raises(ValidationError, match=match),
353402
):
354403
client.extract_pdf_text(input_file, **invalid_kwargs)
404+
405+
406+
@pytest.mark.asyncio
407+
@pytest.mark.parametrize(
408+
("invalid_kwargs", "match"),
409+
[
410+
pytest.param({"full_text": "pages"}, "full_text", id="bad-full-text"),
411+
pytest.param(
412+
{"preserve_line_breaks": "maybe"},
413+
"preserve_line_breaks",
414+
id="bad-preserve-line-breaks",
415+
),
416+
pytest.param({"word_style": "maybe"}, "word_style", id="bad-word-style"),
417+
pytest.param(
418+
{"word_coordinates": "maybe"}, "word_coordinates", id="bad-word-coordinates"
419+
),
420+
],
421+
)
422+
async def test_async_extract_pdf_text_invalid_option_values(
423+
invalid_kwargs: Mapping[str, object],
424+
match: str,
425+
monkeypatch: pytest.MonkeyPatch,
426+
) -> None:
427+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
428+
input_file = make_pdf_file(PdfRestFileID.generate(1))
429+
transport = httpx.MockTransport(lambda request: httpx.Response(500))
430+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
431+
with pytest.raises(ValidationError, match=match):
432+
await client.extract_pdf_text(input_file, **invalid_kwargs)

0 commit comments

Comments
 (0)