Skip to content

Commit 4f29f07

Browse files
Add tests of Add Text
Assisted-by: Codex
1 parent 94bcd33 commit 4f29f07

2 files changed

Lines changed: 447 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
5+
from pdfrest import AsyncPdfRestClient, PdfRestApiError, PdfRestClient
6+
from pdfrest.models import PdfRestFile
7+
8+
from ..resources import get_test_resource_path
9+
10+
11+
@pytest.fixture(scope="module")
12+
def uploaded_pdf_for_text(
13+
pdfrest_api_key: str,
14+
pdfrest_live_base_url: str,
15+
) -> PdfRestFile:
16+
resource = get_test_resource_path("report.pdf")
17+
with PdfRestClient(
18+
api_key=pdfrest_api_key,
19+
base_url=pdfrest_live_base_url,
20+
) as client:
21+
return client.files.create_from_paths([resource])[0]
22+
23+
24+
def _default_text_object() -> dict[str, object]:
25+
return {
26+
"font": "Arial",
27+
"max_width": 200,
28+
"opacity": 1,
29+
"page": 1,
30+
"rotation": 0,
31+
"text": "Live add text",
32+
"text_color_rgb": (0, 0, 0),
33+
"text_size": 14,
34+
"x": 72,
35+
"y": 144,
36+
}
37+
38+
39+
def test_live_add_text_to_pdf(
40+
pdfrest_api_key: str,
41+
pdfrest_live_base_url: str,
42+
uploaded_pdf_for_text: PdfRestFile,
43+
) -> None:
44+
with PdfRestClient(
45+
api_key=pdfrest_api_key,
46+
base_url=pdfrest_live_base_url,
47+
) as client:
48+
response = client.add_text_to_pdf(
49+
uploaded_pdf_for_text,
50+
text_objects=[_default_text_object()],
51+
output="live-added-text",
52+
)
53+
54+
assert response.output_files
55+
output_file = response.output_file
56+
assert output_file.type == "application/pdf"
57+
assert output_file.name.startswith("live-added-text")
58+
assert uploaded_pdf_for_text.id in response.input_ids
59+
60+
61+
@pytest.mark.asyncio
62+
async def test_live_async_add_text_to_pdf(
63+
pdfrest_api_key: str,
64+
pdfrest_live_base_url: str,
65+
uploaded_pdf_for_text: PdfRestFile,
66+
) -> None:
67+
async with AsyncPdfRestClient(
68+
api_key=pdfrest_api_key,
69+
base_url=pdfrest_live_base_url,
70+
) as client:
71+
response = await client.add_text_to_pdf(
72+
uploaded_pdf_for_text,
73+
text_objects=[_default_text_object()],
74+
)
75+
76+
assert response.output_files
77+
output_file = response.output_file
78+
assert output_file.type == "application/pdf"
79+
assert uploaded_pdf_for_text.id in response.input_ids
80+
81+
82+
def test_live_add_text_to_pdf_invalid_page(
83+
pdfrest_api_key: str,
84+
pdfrest_live_base_url: str,
85+
uploaded_pdf_for_text: PdfRestFile,
86+
) -> None:
87+
with (
88+
PdfRestClient(
89+
api_key=pdfrest_api_key,
90+
base_url=pdfrest_live_base_url,
91+
) as client,
92+
pytest.raises(PdfRestApiError, match=r"(?i)page"),
93+
):
94+
client.add_text_to_pdf(
95+
uploaded_pdf_for_text,
96+
text_objects=[_default_text_object()],
97+
extra_body={
98+
"text_objects": [
99+
{
100+
**_default_text_object(),
101+
"page": 0,
102+
}
103+
]
104+
},
105+
)

0 commit comments

Comments
 (0)