Skip to content

Commit 24dd1d4

Browse files
tests: cover add-text local validation branches
Add missing sync and async tests for add_text_to_pdf local validation: - reject non-PDF input files - reject page values below 1 - reject text objects that provide both RGB and CMYK colors Assisted-by: Codex
1 parent a68e908 commit 24dd1d4

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

tests/test_add_text_to_pdf.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
ASYNC_API_KEY,
1616
VALID_API_KEY,
1717
build_file_info_payload,
18+
make_image_file,
1819
make_pdf_file,
1920
)
2021

@@ -216,6 +217,62 @@ def handler(_: httpx.Request) -> httpx.Response:
216217
)
217218

218219

220+
def test_add_text_to_pdf_pdf_mime_validation(monkeypatch: pytest.MonkeyPatch) -> None:
221+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
222+
223+
def handler(_: httpx.Request) -> httpx.Response:
224+
pytest.fail("Request should not be sent when validation fails.")
225+
226+
transport = httpx.MockTransport(handler)
227+
with (
228+
PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client,
229+
pytest.raises(ValidationError, match="Must be a PDF file"),
230+
):
231+
client.add_text_to_pdf(
232+
make_image_file(PdfRestFileID.generate(1)),
233+
text_objects=[make_text_object()],
234+
)
235+
236+
237+
def test_add_text_to_pdf_page_minimum(monkeypatch: pytest.MonkeyPatch) -> None:
238+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
239+
240+
def handler(_: httpx.Request) -> httpx.Response:
241+
pytest.fail("Request should not be sent when validation fails.")
242+
243+
transport = httpx.MockTransport(handler)
244+
with (
245+
PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client,
246+
pytest.raises(ValidationError, match="greater than or equal to 1"),
247+
):
248+
client.add_text_to_pdf(
249+
make_pdf_file(PdfRestFileID.generate(1)),
250+
text_objects=[make_text_object(page=0)],
251+
)
252+
253+
254+
def test_add_text_to_pdf_rejects_both_color_modes(
255+
monkeypatch: pytest.MonkeyPatch,
256+
) -> None:
257+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
258+
259+
def handler(_: httpx.Request) -> httpx.Response:
260+
pytest.fail("Request should not be sent when validation fails.")
261+
262+
transport = httpx.MockTransport(handler)
263+
with (
264+
PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client,
265+
pytest.raises(
266+
ValidationError,
267+
match=re.escape("Provide only one of text_color_rgb or text_color_cmyk."),
268+
),
269+
):
270+
client.add_text_to_pdf(
271+
make_pdf_file(PdfRestFileID.generate(1)),
272+
text_objects=[make_text_object(text_color_cmyk=(0, 0, 0, 0))],
273+
)
274+
275+
219276
def test_add_text_to_pdf_rgb_bounds(monkeypatch: pytest.MonkeyPatch) -> None:
220277
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
221278

@@ -429,6 +486,63 @@ def handler(_: httpx.Request) -> httpx.Response:
429486
)
430487

431488

489+
@pytest.mark.asyncio
490+
async def test_async_add_text_to_pdf_pdf_mime_validation(
491+
monkeypatch: pytest.MonkeyPatch,
492+
) -> None:
493+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
494+
495+
def handler(_: httpx.Request) -> httpx.Response:
496+
pytest.fail("Request should not be sent when validation fails.")
497+
498+
transport = httpx.MockTransport(handler)
499+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
500+
with pytest.raises(ValidationError, match="Must be a PDF file"):
501+
await client.add_text_to_pdf(
502+
make_image_file(PdfRestFileID.generate(1)),
503+
text_objects=[make_text_object()],
504+
)
505+
506+
507+
@pytest.mark.asyncio
508+
async def test_async_add_text_to_pdf_page_minimum(
509+
monkeypatch: pytest.MonkeyPatch,
510+
) -> None:
511+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
512+
513+
def handler(_: httpx.Request) -> httpx.Response:
514+
pytest.fail("Request should not be sent when validation fails.")
515+
516+
transport = httpx.MockTransport(handler)
517+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
518+
with pytest.raises(ValidationError, match="greater than or equal to 1"):
519+
await client.add_text_to_pdf(
520+
make_pdf_file(PdfRestFileID.generate(1)),
521+
text_objects=[make_text_object(page=0)],
522+
)
523+
524+
525+
@pytest.mark.asyncio
526+
async def test_async_add_text_to_pdf_rejects_both_color_modes(
527+
monkeypatch: pytest.MonkeyPatch,
528+
) -> None:
529+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
530+
531+
def handler(_: httpx.Request) -> httpx.Response:
532+
pytest.fail("Request should not be sent when validation fails.")
533+
534+
transport = httpx.MockTransport(handler)
535+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
536+
with pytest.raises(
537+
ValidationError,
538+
match=re.escape("Provide only one of text_color_rgb or text_color_cmyk."),
539+
):
540+
await client.add_text_to_pdf(
541+
make_pdf_file(PdfRestFileID.generate(1)),
542+
text_objects=[make_text_object(text_color_cmyk=(0, 0, 0, 0))],
543+
)
544+
545+
432546
@pytest.mark.asyncio
433547
async def test_async_add_text_to_pdf_requires_color(
434548
monkeypatch: pytest.MonkeyPatch,

0 commit comments

Comments
 (0)