Skip to content

Commit 64ee63e

Browse files
Add tests of Watermark PDF
Assisted-by: Codex
1 parent 399b49a commit 64ee63e

2 files changed

Lines changed: 594 additions & 0 deletions

File tree

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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_watermark(
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+
@pytest.fixture(scope="module")
25+
def uploaded_watermark_pdf(
26+
pdfrest_api_key: str,
27+
pdfrest_live_base_url: str,
28+
) -> PdfRestFile:
29+
resource = get_test_resource_path("duckhat.pdf")
30+
with PdfRestClient(
31+
api_key=pdfrest_api_key,
32+
base_url=pdfrest_live_base_url,
33+
) as client:
34+
return client.files.create_from_paths([resource])[0]
35+
36+
37+
@pytest.mark.parametrize(
38+
"output_name",
39+
[
40+
pytest.param(None, id="default-output"),
41+
pytest.param("watermark-text", id="custom-output"),
42+
],
43+
)
44+
def test_live_watermark_pdf_text_success(
45+
pdfrest_api_key: str,
46+
pdfrest_live_base_url: str,
47+
uploaded_pdf_for_watermark: PdfRestFile,
48+
output_name: str | None,
49+
) -> None:
50+
kwargs: dict[str, str] = {}
51+
if output_name is not None:
52+
kwargs["output"] = output_name
53+
54+
with PdfRestClient(
55+
api_key=pdfrest_api_key,
56+
base_url=pdfrest_live_base_url,
57+
) as client:
58+
response = client.watermark_pdf(
59+
uploaded_pdf_for_watermark,
60+
watermark_text="CONFIDENTIAL",
61+
opacity=0.6,
62+
pages=["1", "last"],
63+
**kwargs,
64+
)
65+
66+
output_file = response.output_file
67+
assert output_file.type == "application/pdf"
68+
assert output_file.size > 0
69+
assert str(response.input_id) == str(uploaded_pdf_for_watermark.id)
70+
if output_name is not None:
71+
assert output_file.name.startswith(output_name)
72+
else:
73+
assert output_file.name.endswith(".pdf")
74+
75+
76+
def test_live_watermark_pdf_file_success(
77+
pdfrest_api_key: str,
78+
pdfrest_live_base_url: str,
79+
uploaded_pdf_for_watermark: PdfRestFile,
80+
uploaded_watermark_pdf: PdfRestFile,
81+
) -> None:
82+
with PdfRestClient(
83+
api_key=pdfrest_api_key,
84+
base_url=pdfrest_live_base_url,
85+
) as client:
86+
response = client.watermark_pdf(
87+
uploaded_pdf_for_watermark,
88+
watermark_file=uploaded_watermark_pdf,
89+
watermark_file_scale=0.75,
90+
behind_page=True,
91+
output="watermark-file",
92+
)
93+
94+
output_file = response.output_file
95+
assert output_file.type == "application/pdf"
96+
assert output_file.size > 0
97+
assert output_file.name.startswith("watermark-file")
98+
assert [str(value) for value in response.input_id] == [
99+
str(uploaded_pdf_for_watermark.id),
100+
str(uploaded_watermark_pdf.id),
101+
]
102+
103+
104+
@pytest.mark.asyncio
105+
async def test_live_async_watermark_pdf_text_success(
106+
pdfrest_api_key: str,
107+
pdfrest_live_base_url: str,
108+
uploaded_pdf_for_watermark: PdfRestFile,
109+
) -> None:
110+
async with AsyncPdfRestClient(
111+
api_key=pdfrest_api_key,
112+
base_url=pdfrest_live_base_url,
113+
) as client:
114+
response = await client.watermark_pdf(
115+
uploaded_pdf_for_watermark,
116+
watermark_text="ASYNC",
117+
horizontal_alignment="right",
118+
vertical_alignment="top",
119+
x=-36,
120+
y=36,
121+
output="async-watermark",
122+
)
123+
124+
output_file = response.output_file
125+
assert output_file.name.startswith("async-watermark")
126+
assert output_file.type == "application/pdf"
127+
assert output_file.size > 0
128+
assert str(response.input_id) == str(uploaded_pdf_for_watermark.id)
129+
130+
131+
def test_live_watermark_pdf_invalid_alignment(
132+
pdfrest_api_key: str,
133+
pdfrest_live_base_url: str,
134+
uploaded_pdf_for_watermark: PdfRestFile,
135+
) -> None:
136+
with (
137+
PdfRestClient(
138+
api_key=pdfrest_api_key,
139+
base_url=pdfrest_live_base_url,
140+
) as client,
141+
pytest.raises(PdfRestApiError, match=r"(?i)alignment"),
142+
):
143+
client.watermark_pdf(
144+
uploaded_pdf_for_watermark,
145+
watermark_text="BadAlignment",
146+
extra_body={"horizontal_alignment": "diagonal"},
147+
)
148+
149+
150+
@pytest.mark.asyncio
151+
async def test_live_async_watermark_pdf_invalid_file_id(
152+
pdfrest_api_key: str,
153+
pdfrest_live_base_url: str,
154+
uploaded_pdf_for_watermark: PdfRestFile,
155+
) -> None:
156+
async with AsyncPdfRestClient(
157+
api_key=pdfrest_api_key,
158+
base_url=pdfrest_live_base_url,
159+
) as client:
160+
with pytest.raises(PdfRestApiError, match=r"(?i)(id|file)"):
161+
await client.watermark_pdf(
162+
uploaded_pdf_for_watermark,
163+
watermark_text="AsyncInvalid",
164+
extra_body={"id": "00000000-0000-0000-0000-000000000000"},
165+
)

0 commit comments

Comments
 (0)