Skip to content

Commit 0a3602e

Browse files
Add to PDF tests: Fix testing guideline infractions
Assisted-by: Codex
1 parent 4f29f07 commit 0a3602e

5 files changed

Lines changed: 217 additions & 77 deletions

File tree

tests/live/test_live_add_image_to_pdf.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,25 @@ def test_live_add_image_to_pdf_invalid_page(
108108
page=1,
109109
extra_body={"page": 0},
110110
)
111+
112+
113+
@pytest.mark.asyncio
114+
async def test_live_async_add_image_to_pdf_invalid_page(
115+
pdfrest_api_key: str,
116+
pdfrest_live_base_url: str,
117+
uploaded_pdf_for_image_addition: PdfRestFile,
118+
uploaded_image: PdfRestFile,
119+
) -> None:
120+
async with AsyncPdfRestClient(
121+
api_key=pdfrest_api_key,
122+
base_url=pdfrest_live_base_url,
123+
) as client:
124+
with pytest.raises(PdfRestApiError, match=r"(?i)page"):
125+
await client.add_image_to_pdf(
126+
uploaded_pdf_for_image_addition,
127+
image=uploaded_image,
128+
x=0,
129+
y=0,
130+
page=1,
131+
extra_body={"page": 0},
132+
)

tests/live/test_live_add_text_to_pdf.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,28 @@ def test_live_add_text_to_pdf_invalid_page(
103103
]
104104
},
105105
)
106+
107+
108+
@pytest.mark.asyncio
109+
async def test_live_async_add_text_to_pdf_invalid_page(
110+
pdfrest_api_key: str,
111+
pdfrest_live_base_url: str,
112+
uploaded_pdf_for_text: PdfRestFile,
113+
) -> None:
114+
async with AsyncPdfRestClient(
115+
api_key=pdfrest_api_key,
116+
base_url=pdfrest_live_base_url,
117+
) as client:
118+
with pytest.raises(PdfRestApiError, match=r"(?i)page"):
119+
await client.add_text_to_pdf(
120+
uploaded_pdf_for_text,
121+
text_objects=[_default_text_object()],
122+
extra_body={
123+
"text_objects": [
124+
{
125+
**_default_text_object(),
126+
"page": 0,
127+
}
128+
]
129+
},
130+
)

tests/test_add_attachment_to_pdf.py

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
from __future__ import annotations
22

33
import json
4-
from collections.abc import Callable
54

65
import httpx
76
import pytest
87
from pydantic import ValidationError
98

109
from pdfrest import AsyncPdfRestClient, PdfRestClient
1110
from pdfrest.models import PdfRestFile, PdfRestFileBasedResponse, PdfRestFileID
12-
from pdfrest.models._internal import PdfAddAttachmentPayload
1311

1412
from .graphics_test_helpers import (
1513
ASYNC_API_KEY,
@@ -33,10 +31,6 @@ def test_add_attachment_to_pdf_success(monkeypatch: pytest.MonkeyPatch) -> None:
3331
attachment = make_attachment_file(str(PdfRestFileID.generate()), "notes.txt")
3432
output_id = str(PdfRestFileID.generate())
3533

36-
payload_dump = PdfAddAttachmentPayload.model_validate(
37-
{"files": [input_file], "attachments": [attachment], "output": "attached"}
38-
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
39-
4034
seen: dict[str, int] = {"post": 0, "get": 0}
4135

4236
def handler(request: httpx.Request) -> httpx.Response:
@@ -46,7 +40,9 @@ def handler(request: httpx.Request) -> httpx.Response:
4640
):
4741
seen["post"] += 1
4842
payload = json.loads(request.content.decode("utf-8"))
49-
assert payload == payload_dump
43+
assert payload["id"] == str(input_file.id)
44+
assert payload["id_to_attach"] == str(attachment.id)
45+
assert payload["output"] == "attached"
5046
return httpx.Response(
5147
200,
5248
json={
@@ -303,33 +299,45 @@ def test_add_attachment_to_pdf_requires_pdf_file(
303299
client.add_attachment_to_pdf(not_pdf, attachment=attachment)
304300

305301

306-
@pytest.mark.parametrize(
307-
"payload_data",
308-
[
309-
pytest.param(
310-
lambda pdf, attachment: {
311-
"files": [pdf, make_pdf_file(PdfRestFileID.generate())],
312-
"attachments": [attachment],
313-
},
314-
id="multiple-input-files",
315-
),
316-
pytest.param(
317-
lambda pdf, attachment: {
318-
"files": [pdf],
319-
"attachments": [
320-
attachment,
321-
make_attachment_file(str(PdfRestFileID.generate())),
322-
],
323-
},
324-
id="multiple-attachments",
325-
),
326-
],
327-
)
328-
def test_add_attachment_to_pdf_rejects_multiple_files(
329-
payload_data: Callable[[PdfRestFile, PdfRestFile], dict[str, object]],
302+
def test_add_attachment_to_pdf_rejects_multiple_input_files(
303+
monkeypatch: pytest.MonkeyPatch,
330304
) -> None:
331-
input_file = make_pdf_file(PdfRestFileID.generate())
332-
attachment = make_attachment_file(str(PdfRestFileID.generate()))
305+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
333306

334-
with pytest.raises(ValidationError):
335-
PdfAddAttachmentPayload.model_validate(payload_data(input_file, attachment))
307+
def handler(_: httpx.Request) -> httpx.Response:
308+
pytest.fail("Request should not be sent when validation fails.")
309+
310+
transport = httpx.MockTransport(handler)
311+
with (
312+
PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client,
313+
pytest.raises(ValidationError, match="at most 1 item"),
314+
):
315+
client.add_attachment_to_pdf(
316+
[
317+
make_pdf_file(PdfRestFileID.generate(1)),
318+
make_pdf_file(PdfRestFileID.generate(2)),
319+
],
320+
attachment=make_attachment_file(str(PdfRestFileID.generate())),
321+
)
322+
323+
324+
def test_add_attachment_to_pdf_rejects_multiple_attachments(
325+
monkeypatch: pytest.MonkeyPatch,
326+
) -> None:
327+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
328+
329+
def handler(_: httpx.Request) -> httpx.Response:
330+
pytest.fail("Request should not be sent when validation fails.")
331+
332+
transport = httpx.MockTransport(handler)
333+
with (
334+
PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client,
335+
pytest.raises(ValidationError, match="at most 1 item"),
336+
):
337+
client.add_attachment_to_pdf(
338+
make_pdf_file(PdfRestFileID.generate(1)),
339+
attachment=[
340+
make_attachment_file(str(PdfRestFileID.generate(2))),
341+
make_attachment_file(str(PdfRestFileID.generate(3)), "more.txt"),
342+
],
343+
)

tests/test_add_image_to_pdf.py

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
from pdfrest import AsyncPdfRestClient, PdfRestClient
1111
from pdfrest.models import PdfRestFileBasedResponse, PdfRestFileID
12-
from pdfrest.models._internal import PdfAddImagePayload
1312

1413
from .graphics_test_helpers import (
1514
ASYNC_API_KEY,
@@ -28,25 +27,19 @@ def test_add_image_to_pdf_success(monkeypatch: pytest.MonkeyPatch) -> None:
2827
)
2928
output_id = str(PdfRestFileID.generate())
3029

31-
request_payload = PdfAddImagePayload.model_validate(
32-
{
33-
"files": [pdf_file],
34-
"image": [image_file],
35-
"x": 12,
36-
"y": 34,
37-
"page": 3,
38-
"output": "with-image",
39-
}
40-
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
41-
4230
seen: dict[str, int] = {"post": 0, "get": 0}
4331

4432
def handler(request: httpx.Request) -> httpx.Response:
4533
if request.method == "POST" and request.url.path == "/pdf-with-added-image":
4634
seen["post"] += 1
4735
assert request.headers["wsn"] == "pdfrest-python"
4836
payload = json.loads(request.content.decode("utf-8"))
49-
assert payload == request_payload
37+
assert payload["id"] == str(pdf_file.id)
38+
assert payload["image_id"] == str(image_file.id)
39+
assert payload["x"] == 12
40+
assert payload["y"] == 34
41+
assert payload["page"] == 3
42+
assert payload["output"] == "with-image"
5043
return httpx.Response(
5144
200,
5245
json={
@@ -222,6 +215,56 @@ def handler(_: httpx.Request) -> httpx.Response:
222215
)
223216

224217

218+
def test_add_image_to_pdf_rejects_multiple_input_files(
219+
monkeypatch: pytest.MonkeyPatch,
220+
) -> 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="at most 1 item"),
230+
):
231+
client.add_image_to_pdf(
232+
[
233+
make_pdf_file(PdfRestFileID.generate(1)),
234+
make_pdf_file(PdfRestFileID.generate(2)),
235+
],
236+
image=make_image_file(PdfRestFileID.generate(3)),
237+
x=1,
238+
y=1,
239+
page=1,
240+
)
241+
242+
243+
def test_add_image_to_pdf_rejects_multiple_images(
244+
monkeypatch: pytest.MonkeyPatch,
245+
) -> None:
246+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
247+
248+
def handler(_: httpx.Request) -> httpx.Response:
249+
pytest.fail("Request should not be sent when validation fails.")
250+
251+
transport = httpx.MockTransport(handler)
252+
with (
253+
PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client,
254+
pytest.raises(ValidationError, match="at most 1 item"),
255+
):
256+
client.add_image_to_pdf(
257+
make_pdf_file(PdfRestFileID.generate(1)),
258+
image=[
259+
make_image_file(PdfRestFileID.generate(2)),
260+
make_image_file(PdfRestFileID.generate(3), name="secondary.png"),
261+
],
262+
x=1,
263+
y=1,
264+
page=1,
265+
)
266+
267+
225268
@pytest.mark.asyncio
226269
async def test_async_add_image_to_pdf_success(
227270
monkeypatch: pytest.MonkeyPatch,
@@ -231,23 +274,17 @@ async def test_async_add_image_to_pdf_success(
231274
image_file = make_image_file(PdfRestFileID.generate(2), mime_type="image/gif")
232275
output_id = str(PdfRestFileID.generate())
233276

234-
request_payload = PdfAddImagePayload.model_validate(
235-
{
236-
"files": [pdf_file],
237-
"image": [image_file],
238-
"x": 5,
239-
"y": 6,
240-
"page": 7,
241-
}
242-
).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True)
243-
244277
seen: dict[str, int] = {"post": 0, "get": 0}
245278

246279
def handler(request: httpx.Request) -> httpx.Response:
247280
if request.method == "POST" and request.url.path == "/pdf-with-added-image":
248281
seen["post"] += 1
249282
payload = json.loads(request.content.decode("utf-8"))
250-
assert payload == request_payload
283+
assert payload["id"] == str(pdf_file.id)
284+
assert payload["image_id"] == str(image_file.id)
285+
assert payload["x"] == 5
286+
assert payload["y"] == 6
287+
assert payload["page"] == 7
251288
return httpx.Response(
252289
200,
253290
json={
@@ -289,6 +326,7 @@ async def test_async_add_image_to_pdf_request_customization(
289326
pdf_file = make_pdf_file(PdfRestFileID.generate(1))
290327
image_file = make_image_file(PdfRestFileID.generate(2))
291328
output_id = str(PdfRestFileID.generate())
329+
captured_timeout: dict[str, float | dict[str, float] | None] = {}
292330

293331
def handler(request: httpx.Request) -> httpx.Response:
294332
if request.method == "POST" and request.url.path == "/pdf-with-added-image":
@@ -297,6 +335,7 @@ def handler(request: httpx.Request) -> httpx.Response:
297335
payload = json.loads(request.content.decode("utf-8"))
298336
assert payload["x"] == 15
299337
assert payload["y"] == 25
338+
captured_timeout["value"] = request.extensions.get("timeout")
300339
return httpx.Response(
301340
200,
302341
json={
@@ -331,6 +370,14 @@ def handler(request: httpx.Request) -> httpx.Response:
331370
)
332371

333372
assert response.output_files[0].name == "async-custom-with-image.pdf"
373+
timeout_value = captured_timeout["value"]
374+
assert timeout_value is not None
375+
if isinstance(timeout_value, dict):
376+
assert all(
377+
component == pytest.approx(1.0) for component in timeout_value.values()
378+
)
379+
else:
380+
assert timeout_value == pytest.approx(1.0)
334381

335382

336383
@pytest.mark.asyncio

0 commit comments

Comments
 (0)