Skip to content

Commit 935272e

Browse files
tests: add async validation parity for add-to-pdf endpoints
Assisted-by: Codex
1 parent 96aee41 commit 935272e

3 files changed

Lines changed: 239 additions & 0 deletions

File tree

tests/test_add_attachment_to_pdf.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,66 @@ def handler(_: httpx.Request) -> httpx.Response:
341341
make_attachment_file(str(PdfRestFileID.generate(2)), "more.txt"),
342342
],
343343
)
344+
345+
346+
@pytest.mark.asyncio
347+
async def test_async_add_attachment_to_pdf_requires_pdf_file(
348+
monkeypatch: pytest.MonkeyPatch,
349+
) -> None:
350+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
351+
not_pdf = make_attachment_file(
352+
str(PdfRestFileID.generate()),
353+
"image.png",
354+
"image/png",
355+
)
356+
attachment = make_attachment_file(str(PdfRestFileID.generate()), "note.txt")
357+
358+
def handler(_: httpx.Request) -> httpx.Response:
359+
pytest.fail("Request should not be sent when validation fails.")
360+
361+
transport = httpx.MockTransport(handler)
362+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
363+
with pytest.raises(ValidationError, match="Must be a PDF file"):
364+
await client.add_attachment_to_pdf(not_pdf, attachment=attachment)
365+
366+
367+
@pytest.mark.asyncio
368+
async def test_async_add_attachment_to_pdf_rejects_multiple_input_files(
369+
monkeypatch: pytest.MonkeyPatch,
370+
) -> None:
371+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
372+
373+
def handler(_: httpx.Request) -> httpx.Response:
374+
pytest.fail("Request should not be sent when validation fails.")
375+
376+
transport = httpx.MockTransport(handler)
377+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
378+
with pytest.raises(ValidationError, match="at most 1 item"):
379+
await client.add_attachment_to_pdf(
380+
[
381+
make_pdf_file(PdfRestFileID.generate(1)),
382+
make_pdf_file(PdfRestFileID.generate(2)),
383+
],
384+
attachment=make_attachment_file(str(PdfRestFileID.generate())),
385+
)
386+
387+
388+
@pytest.mark.asyncio
389+
async def test_async_add_attachment_to_pdf_rejects_multiple_attachments(
390+
monkeypatch: pytest.MonkeyPatch,
391+
) -> None:
392+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
393+
394+
def handler(_: httpx.Request) -> httpx.Response:
395+
pytest.fail("Request should not be sent when validation fails.")
396+
397+
transport = httpx.MockTransport(handler)
398+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
399+
with pytest.raises(ValidationError, match="at most 1 item"):
400+
await client.add_attachment_to_pdf(
401+
make_pdf_file(PdfRestFileID.generate(1)),
402+
attachment=[
403+
make_attachment_file(str(PdfRestFileID.generate(2))),
404+
make_attachment_file(str(PdfRestFileID.generate(2)), "more.txt"),
405+
],
406+
)

tests/test_add_image_to_pdf.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,93 @@ def handler(_: httpx.Request) -> httpx.Response:
401401
y=1,
402402
page=1,
403403
)
404+
405+
406+
@pytest.mark.asyncio
407+
async def test_async_add_image_to_pdf_invalid_pdf_type(
408+
monkeypatch: pytest.MonkeyPatch,
409+
) -> None:
410+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
411+
412+
def handler(_: httpx.Request) -> httpx.Response:
413+
pytest.fail("Request should not be sent when validation fails.")
414+
415+
transport = httpx.MockTransport(handler)
416+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
417+
with pytest.raises(ValidationError, match="Must be a PDF file"):
418+
await client.add_image_to_pdf(
419+
make_image_file(PdfRestFileID.generate(1)),
420+
image=make_image_file(PdfRestFileID.generate(2)),
421+
x=1,
422+
y=2,
423+
page=1,
424+
)
425+
426+
427+
@pytest.mark.asyncio
428+
async def test_async_add_image_to_pdf_page_minimum(
429+
monkeypatch: pytest.MonkeyPatch,
430+
) -> None:
431+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
432+
433+
def handler(_: httpx.Request) -> httpx.Response:
434+
pytest.fail("Request should not be sent when validation fails.")
435+
436+
transport = httpx.MockTransport(handler)
437+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
438+
with pytest.raises(ValidationError, match="greater than or equal to 1"):
439+
await client.add_image_to_pdf(
440+
make_pdf_file(PdfRestFileID.generate(1)),
441+
image=make_image_file(PdfRestFileID.generate(2)),
442+
x=0,
443+
y=0,
444+
page=0,
445+
)
446+
447+
448+
@pytest.mark.asyncio
449+
async def test_async_add_image_to_pdf_rejects_multiple_input_files(
450+
monkeypatch: pytest.MonkeyPatch,
451+
) -> None:
452+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
453+
454+
def handler(_: httpx.Request) -> httpx.Response:
455+
pytest.fail("Request should not be sent when validation fails.")
456+
457+
transport = httpx.MockTransport(handler)
458+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
459+
with pytest.raises(ValidationError, match="at most 1 item"):
460+
await client.add_image_to_pdf(
461+
[
462+
make_pdf_file(PdfRestFileID.generate(1)),
463+
make_pdf_file(PdfRestFileID.generate(2)),
464+
],
465+
image=make_image_file(PdfRestFileID.generate(2)),
466+
x=1,
467+
y=1,
468+
page=1,
469+
)
470+
471+
472+
@pytest.mark.asyncio
473+
async def test_async_add_image_to_pdf_rejects_multiple_images(
474+
monkeypatch: pytest.MonkeyPatch,
475+
) -> None:
476+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
477+
478+
def handler(_: httpx.Request) -> httpx.Response:
479+
pytest.fail("Request should not be sent when validation fails.")
480+
481+
transport = httpx.MockTransport(handler)
482+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
483+
with pytest.raises(ValidationError, match="at most 1 item"):
484+
await client.add_image_to_pdf(
485+
make_pdf_file(PdfRestFileID.generate(1)),
486+
image=[
487+
make_image_file(PdfRestFileID.generate(2)),
488+
make_image_file(PdfRestFileID.generate(2), name="secondary.png"),
489+
],
490+
x=1,
491+
y=1,
492+
page=1,
493+
)

tests/test_add_text_to_pdf.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,89 @@ def handler(_: httpx.Request) -> httpx.Response:
410410
)
411411
],
412412
)
413+
414+
415+
@pytest.mark.asyncio
416+
async def test_async_add_text_to_pdf_requires_color(
417+
monkeypatch: pytest.MonkeyPatch,
418+
) -> None:
419+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
420+
421+
def handler(_: httpx.Request) -> httpx.Response:
422+
pytest.fail("Request should not be sent when validation fails.")
423+
424+
transport = httpx.MockTransport(handler)
425+
text_object = make_text_object()
426+
text_object.pop("text_color_rgb")
427+
428+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
429+
with pytest.raises(
430+
ValidationError,
431+
match=re.escape(
432+
"Either text_color_rgb or text_color_cmyk must be provided."
433+
),
434+
):
435+
await client.add_text_to_pdf(
436+
make_pdf_file(PdfRestFileID.generate(1)),
437+
text_objects=[text_object],
438+
)
439+
440+
441+
@pytest.mark.asyncio
442+
async def test_async_add_text_to_pdf_rgb_bounds(
443+
monkeypatch: pytest.MonkeyPatch,
444+
) -> None:
445+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
446+
447+
def handler(_: httpx.Request) -> httpx.Response:
448+
pytest.fail("Request should not be sent when validation fails.")
449+
450+
transport = httpx.MockTransport(handler)
451+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
452+
with pytest.raises(
453+
ValidationError,
454+
match=re.escape("text_color_rgb values must be between 0 and 255."),
455+
):
456+
await client.add_text_to_pdf(
457+
make_pdf_file(PdfRestFileID.generate(1)),
458+
text_objects=[make_text_object(text_color_rgb=(0, 0, 256))],
459+
)
460+
461+
462+
@pytest.mark.asyncio
463+
async def test_async_add_text_to_pdf_text_size_bounds(
464+
monkeypatch: pytest.MonkeyPatch,
465+
) -> None:
466+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
467+
468+
def handler(_: httpx.Request) -> httpx.Response:
469+
pytest.fail("Request should not be sent when validation fails.")
470+
471+
transport = httpx.MockTransport(handler)
472+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
473+
with pytest.raises(ValidationError, match="greater than or equal to 5"):
474+
await client.add_text_to_pdf(
475+
make_pdf_file(PdfRestFileID.generate(1)),
476+
text_objects=[make_text_object(text_size=4)],
477+
)
478+
479+
480+
@pytest.mark.asyncio
481+
async def test_async_add_text_to_pdf_rejects_multiple_input_files(
482+
monkeypatch: pytest.MonkeyPatch,
483+
) -> None:
484+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
485+
486+
def handler(_: httpx.Request) -> httpx.Response:
487+
pytest.fail("Request should not be sent when validation fails.")
488+
489+
transport = httpx.MockTransport(handler)
490+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
491+
with pytest.raises(ValidationError, match="at most 1 item"):
492+
await client.add_text_to_pdf(
493+
[
494+
make_pdf_file(PdfRestFileID.generate(1)),
495+
make_pdf_file(PdfRestFileID.generate(2)),
496+
],
497+
text_objects=[make_text_object()],
498+
)

0 commit comments

Comments
 (0)