Skip to content

Commit c27a9b3

Browse files
Ensure text_objects is stringified in testing
Assisted-by: Codex
1 parent 97f1b4b commit c27a9b3

2 files changed

Lines changed: 65 additions & 14 deletions

File tree

tests/live/test_live_add_text_to_pdf.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import json
4+
35
import pytest
46

57
from pdfrest import AsyncPdfRestClient, PdfRestApiError, PdfRestClient
@@ -36,6 +38,19 @@ def _default_text_object() -> dict[str, object]:
3638
}
3739

3840

41+
def _serialize_text_object_for_extra_body(
42+
text_object: dict[str, object],
43+
) -> dict[str, object]:
44+
serialized = dict(text_object)
45+
rgb = serialized.get("text_color_rgb")
46+
if isinstance(rgb, (list, tuple)):
47+
serialized["text_color_rgb"] = ",".join(str(channel) for channel in rgb)
48+
cmyk = serialized.get("text_color_cmyk")
49+
if isinstance(cmyk, (list, tuple)):
50+
serialized["text_color_cmyk"] = ",".join(str(channel) for channel in cmyk)
51+
return serialized
52+
53+
3954
def test_live_add_text_to_pdf(
4055
pdfrest_api_key: str,
4156
pdfrest_live_base_url: str,
@@ -95,12 +110,17 @@ def test_live_add_text_to_pdf_invalid_page(
95110
uploaded_pdf_for_text,
96111
text_objects=[_default_text_object()],
97112
extra_body={
98-
"text_objects": [
99-
{
100-
**_default_text_object(),
101-
"page": 0,
102-
}
103-
]
113+
"text_objects": json.dumps(
114+
[
115+
_serialize_text_object_for_extra_body(
116+
{
117+
**_default_text_object(),
118+
"page": 0,
119+
}
120+
)
121+
],
122+
separators=(",", ":"),
123+
)
104124
},
105125
)
106126

@@ -120,11 +140,16 @@ async def test_live_async_add_text_to_pdf_invalid_page(
120140
uploaded_pdf_for_text,
121141
text_objects=[_default_text_object()],
122142
extra_body={
123-
"text_objects": [
124-
{
125-
**_default_text_object(),
126-
"page": 0,
127-
}
128-
]
143+
"text_objects": json.dumps(
144+
[
145+
_serialize_text_object_for_extra_body(
146+
{
147+
**_default_text_object(),
148+
"page": 0,
149+
}
150+
)
151+
],
152+
separators=(",", ":"),
153+
)
129154
},
130155
)

tests/test_add_text_to_pdf.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def _serialize_text_object_for_request(
3939
text_object: dict[str, object],
4040
) -> dict[str, object]:
4141
serialized = dict(text_object)
42+
for key in ("max_width", "rotation", "text_size", "x", "y"):
43+
value = serialized.get(key)
44+
if isinstance(value, int):
45+
serialized[key] = float(value)
4246
rgb = serialized.get("text_color_rgb")
4347
if isinstance(rgb, (list, tuple)):
4448
serialized["text_color_rgb"] = ",".join(str(channel) for channel in rgb)
@@ -108,13 +112,18 @@ def test_add_text_to_pdf_request_customization(
108112
pdf_file = make_pdf_file(PdfRestFileID.generate(1))
109113
output_id = str(PdfRestFileID.generate())
110114
captured_timeout: dict[str, float | dict[str, float] | None] = {}
115+
overridden_text_object = make_text_object(page=2)
111116

112117
def handler(request: httpx.Request) -> httpx.Response:
113118
if request.method == "POST" and request.url.path == "/pdf-with-added-text":
114119
assert request.url.params["trace"] == "true"
115120
assert request.headers["X-Debug"] == "1"
116121
payload = json.loads(request.content.decode("utf-8"))
117122
assert payload["rotation"] == 15
123+
assert payload["text_objects"] == json.dumps(
124+
[_serialize_text_object_for_request(overridden_text_object)],
125+
separators=(",", ":"),
126+
)
118127
captured_timeout["value"] = request.extensions.get("timeout")
119128
return httpx.Response(
120129
200,
@@ -144,7 +153,13 @@ def handler(request: httpx.Request) -> httpx.Response:
144153
text_objects=[make_text_object(rotation=15)],
145154
extra_query={"trace": "true"},
146155
extra_headers={"X-Debug": "1"},
147-
extra_body={"rotation": 15},
156+
extra_body={
157+
"rotation": 15,
158+
"text_objects": json.dumps(
159+
[_serialize_text_object_for_request(overridden_text_object)],
160+
separators=(",", ":"),
161+
),
162+
},
148163
timeout=0.25,
149164
)
150165

@@ -304,13 +319,18 @@ async def test_async_add_text_to_pdf_request_customization(
304319
pdf_file = make_pdf_file(PdfRestFileID.generate(1))
305320
output_id = str(PdfRestFileID.generate())
306321
captured_timeout: dict[str, float | dict[str, float] | None] = {}
322+
overridden_text_object = make_text_object(page=3)
307323

308324
def handler(request: httpx.Request) -> httpx.Response:
309325
if request.method == "POST" and request.url.path == "/pdf-with-added-text":
310326
assert request.url.params["trace"] == "true"
311327
assert request.headers["X-Test"] == "async"
312328
payload = json.loads(request.content.decode("utf-8"))
313329
assert payload["text_size"] == 18
330+
assert payload["text_objects"] == json.dumps(
331+
[_serialize_text_object_for_request(overridden_text_object)],
332+
separators=(",", ":"),
333+
)
314334
captured_timeout["value"] = request.extensions.get("timeout")
315335
return httpx.Response(
316336
200,
@@ -340,7 +360,13 @@ def handler(request: httpx.Request) -> httpx.Response:
340360
text_objects=[make_text_object(text_size=18)],
341361
extra_query={"trace": "true"},
342362
extra_headers={"X-Test": "async"},
343-
extra_body={"text_size": 18},
363+
extra_body={
364+
"text_size": 18,
365+
"text_objects": json.dumps(
366+
[_serialize_text_object_for_request(overridden_text_object)],
367+
separators=(",", ":"),
368+
),
369+
},
344370
timeout=1.0,
345371
)
346372

0 commit comments

Comments
 (0)