Skip to content

Commit ff5a496

Browse files
Live flatten/linearize/rasterize: add async variants, missing sync tests
Assisted-by: Codex
1 parent c3a1c1f commit ff5a496

8 files changed

Lines changed: 215 additions & 23 deletions

tests/live/test_live_flatten_annotations.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,37 @@ def test_live_flatten_annotations_success(
5757

5858

5959
@pytest.mark.asyncio
60+
@pytest.mark.parametrize(
61+
"output_name",
62+
[
63+
pytest.param(None, id="default-output"),
64+
pytest.param("flatten-annotations", id="custom-output"),
65+
],
66+
)
6067
async def test_live_async_flatten_annotations_success(
6168
pdfrest_api_key: str,
6269
pdfrest_live_base_url: str,
6370
uploaded_pdf_for_annotations: PdfRestFile,
71+
output_name: str | None,
6472
) -> None:
73+
kwargs: dict[str, str] = {}
74+
if output_name is not None:
75+
kwargs["output"] = output_name
76+
6577
async with AsyncPdfRestClient(
6678
api_key=pdfrest_api_key,
6779
base_url=pdfrest_live_base_url,
6880
) as client:
6981
response = await client.flatten_annotations(
70-
uploaded_pdf_for_annotations, output="async"
82+
uploaded_pdf_for_annotations, **kwargs
7183
)
7284

7385
assert response.output_files
7486
output_file = response.output_file
75-
assert output_file.name.startswith("async")
87+
if output_name is not None:
88+
assert output_file.name.startswith(output_name)
89+
else:
90+
assert output_file.name.endswith(".pdf")
7691
assert output_file.type == "application/pdf"
7792
assert output_file.size > 0
7893
assert response.warning is None

tests/live/test_live_flatten_pdf_forms.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,38 @@ def test_live_flatten_pdf_forms(
5555

5656

5757
@pytest.mark.asyncio
58-
async def test_live_async_flatten_pdf_forms_success(
58+
@pytest.mark.parametrize(
59+
"output_name",
60+
[
61+
pytest.param(None, id="default-output"),
62+
pytest.param("flattened-live", id="custom-output"),
63+
],
64+
)
65+
async def test_live_async_flatten_pdf_forms(
5966
pdfrest_api_key: str,
6067
pdfrest_live_base_url: str,
6168
uploaded_pdf_with_forms: PdfRestFile,
69+
output_name: str | None,
6270
) -> None:
71+
kwargs: dict[str, str] = {}
72+
if output_name is not None:
73+
kwargs["output"] = output_name
74+
6375
async with AsyncPdfRestClient(
6476
api_key=pdfrest_api_key,
6577
base_url=pdfrest_live_base_url,
6678
) as client:
6779
response = await client.flatten_pdf_forms(
6880
uploaded_pdf_with_forms,
69-
output="async-flattened",
81+
**kwargs,
7082
)
7183

7284
assert response.output_files
7385
output_file = response.output_file
74-
assert output_file.name.startswith("async-flattened")
86+
if output_name is not None:
87+
assert output_file.name.startswith(output_name)
88+
else:
89+
assert output_file.name.endswith(".pdf")
7590
assert output_file.type == "application/pdf"
7691
assert str(response.input_id) == str(uploaded_pdf_with_forms.id)
7792

tests/live/test_live_flatten_transparencies.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,38 @@ def test_live_flatten_transparencies_success(
6060

6161

6262
@pytest.mark.asyncio
63+
@pytest.mark.parametrize(
64+
("output_name", "quality"),
65+
[
66+
pytest.param(None, "medium", id="default-output"),
67+
pytest.param("flatten-transparency", "high", id="custom-output-high"),
68+
],
69+
)
6370
async def test_live_async_flatten_transparencies_success(
6471
pdfrest_api_key: str,
6572
pdfrest_live_base_url: str,
6673
uploaded_pdf_for_transparencies: PdfRestFile,
74+
output_name: str | None,
75+
quality: str,
6776
) -> None:
77+
kwargs: dict[str, str] = {"quality": quality}
78+
if output_name is not None:
79+
kwargs["output"] = output_name
80+
6881
async with AsyncPdfRestClient(
6982
api_key=pdfrest_api_key,
7083
base_url=pdfrest_live_base_url,
7184
) as client:
7285
response = await client.flatten_transparencies(
73-
uploaded_pdf_for_transparencies, output="async", quality="low"
86+
uploaded_pdf_for_transparencies, **kwargs
7487
)
7588

7689
assert response.output_files
7790
output_file = response.output_file
78-
assert output_file.name.startswith("async")
91+
if output_name is not None:
92+
assert output_file.name.startswith(output_name)
93+
else:
94+
assert output_file.name.endswith(".pdf")
7995
assert output_file.type == "application/pdf"
8096
assert output_file.size > 0
8197
assert response.warning is None

tests/live/test_live_linearize_pdf.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,38 @@ def test_live_linearize_pdf(
5757

5858

5959
@pytest.mark.asyncio
60+
@pytest.mark.parametrize(
61+
"output_name",
62+
[
63+
pytest.param(None, id="default-output"),
64+
pytest.param("linearized-live", id="custom-output"),
65+
],
66+
)
6067
async def test_live_async_linearize_pdf(
6168
pdfrest_api_key: str,
6269
pdfrest_live_base_url: str,
6370
uploaded_pdf_for_linearize: PdfRestFile,
71+
output_name: str | None,
6472
) -> None:
73+
kwargs: dict[str, str] = {}
74+
if output_name is not None:
75+
kwargs["output"] = output_name
76+
6577
async with AsyncPdfRestClient(
6678
api_key=pdfrest_api_key,
6779
base_url=pdfrest_live_base_url,
6880
) as client:
6981
response = await client.linearize_pdf(
7082
uploaded_pdf_for_linearize,
71-
output="async-linearized",
83+
**kwargs,
7284
)
7385

7486
assert response.output_files
7587
output_file = response.output_file
76-
assert output_file.name.startswith("async-linearized")
88+
if output_name is not None:
89+
assert output_file.name.startswith(output_name)
90+
else:
91+
assert output_file.name.endswith(".pdf")
7792
assert output_file.type == "application/pdf"
7893
assert output_file.size > 0
7994
assert response.warning is None

tests/live/test_live_pdf_info.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,23 @@ def test_live_pdf_info_multiple_queries(
145145
_assert_expected_value(item, getattr(response, item))
146146

147147

148+
def test_live_pdf_info_all_queries(
149+
pdfrest_api_key: str,
150+
pdfrest_live_base_url: str,
151+
uploaded_pdf: PdfRestFile,
152+
) -> None:
153+
with PdfRestClient(
154+
api_key=pdfrest_api_key, base_url=pdfrest_live_base_url
155+
) as client:
156+
response = client.query_pdf_info(uploaded_pdf, queries=ALLOWED_QUERIES)
157+
158+
assert isinstance(response, PdfRestInfoResponse)
159+
assert str(response.input_id) == str(uploaded_pdf.id)
160+
assert response.all_queries_processed is True
161+
for query in ALLOWED_QUERIES:
162+
_assert_expected_value(query, getattr(response, query))
163+
164+
148165
@pytest.mark.asyncio
149166
async def test_live_pdf_info_async_all_queries(
150167
pdfrest_api_key: str,

tests/live/test_live_pdf_redactions.py

Lines changed: 110 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,32 +136,109 @@ def test_live_redaction_preview_and_apply_multiple(
136136

137137

138138
@pytest.mark.asyncio
139-
async def test_live_async_redaction_preview_and_apply(
139+
@pytest.mark.parametrize(
140+
"instructions",
141+
[
142+
pytest.param(
143+
[
144+
{
145+
"type": "literal",
146+
"value": "The quick brown fox jumped over the lazy dog.",
147+
},
148+
{"type": "regex", "value": r"\b\d{3}-\d{2}-\d{4}\b"},
149+
],
150+
id="literal-and-regex",
151+
),
152+
pytest.param(
153+
[
154+
{"type": "preset", "value": "email"},
155+
{"type": "preset", "value": "phone_number"},
156+
],
157+
id="preset-email-and-phone",
158+
),
159+
pytest.param(
160+
[
161+
{"type": "preset", "value": "credit_card"},
162+
{"type": "preset", "value": "bank_routing_number"},
163+
{"type": "preset", "value": "swift_bic_number"},
164+
],
165+
id="multiple-presets",
166+
),
167+
],
168+
)
169+
async def test_live_async_redaction_preview_and_apply_multiple(
140170
pdfrest_api_key: str,
141171
pdfrest_live_base_url: str,
142172
uploaded_pdf_for_redaction: PdfRestFile,
173+
instructions: list[PdfRedactionInstruction],
143174
) -> None:
144175
async with AsyncPdfRestClient(
145176
api_key=pdfrest_api_key,
146177
base_url=pdfrest_live_base_url,
147178
) as client:
148179
preview = await client.preview_redactions(
149180
uploaded_pdf_for_redaction,
150-
redactions=[{"type": "literal", "value": "quick brown fox"}],
151-
output="async-redaction-preview",
181+
redactions=instructions,
182+
output="redaction-preview-multi",
152183
)
153184

185+
assert preview.output_files
154186
preview_file = preview.output_files[0]
155187
applied = await client.apply_redactions(
156188
preview_file,
157-
output="async-redaction-final",
189+
output="redaction-final-multi",
190+
)
191+
192+
final_file = applied.output_files[0]
193+
assert final_file.name.endswith("redaction-final-multi.pdf")
194+
assert final_file.type == "application/pdf"
195+
196+
197+
@pytest.mark.asyncio
198+
@pytest.mark.parametrize(
199+
"instruction",
200+
[
201+
pytest.param(
202+
{
203+
"type": "literal",
204+
"value": "The quick brown fox jumped over the lazy dog.",
205+
},
206+
id="literal",
207+
),
208+
pytest.param({"type": "regex", "value": r"\b\d{3}-\d{2}-\d{4}\b"}, id="regex"),
209+
*[
210+
pytest.param({"type": "preset", "value": preset}, id=f"preset-{preset}")
211+
for preset in get_args(PdfRedactionPreset)
212+
],
213+
],
214+
)
215+
async def test_live_async_redaction_preview_and_apply_single(
216+
pdfrest_api_key: str,
217+
pdfrest_live_base_url: str,
218+
uploaded_pdf_for_redaction: PdfRestFile,
219+
instruction: PdfRedactionInstruction,
220+
) -> None:
221+
async with AsyncPdfRestClient(
222+
api_key=pdfrest_api_key,
223+
base_url=pdfrest_live_base_url,
224+
) as client:
225+
preview = await client.preview_redactions(
226+
uploaded_pdf_for_redaction,
227+
redactions=[instruction],
228+
output="redaction-preview",
229+
)
230+
231+
preview_file = preview.output_files[0]
232+
applied = await client.apply_redactions(
233+
preview_file,
234+
output="redaction-final",
158235
)
159236

160237
assert preview.output_files
161-
assert preview_file.name.endswith("async-redaction-preview.pdf")
238+
assert preview_file.name.endswith("redaction-preview.pdf")
162239
assert applied.output_files
163240
final_file = applied.output_files[0]
164-
assert final_file.name.endswith("async-redaction-final.pdf")
241+
assert final_file.name.endswith("redaction-final.pdf")
165242
assert final_file.type == "application/pdf"
166243

167244

@@ -206,18 +283,42 @@ def test_live_redactions_invalid_payloads(
206283

207284

208285
@pytest.mark.asyncio
286+
@pytest.mark.parametrize(
287+
"extra_body",
288+
[
289+
pytest.param({"redactions": "invalid"}, id="invalid-redactions"),
290+
pytest.param({"rgb_color": "-1,-1,-1"}, id="invalid-rgb"),
291+
],
292+
)
209293
async def test_live_async_redactions_invalid_payloads(
210294
pdfrest_api_key: str,
211295
pdfrest_live_base_url: str,
212296
uploaded_pdf_for_redaction: PdfRestFile,
297+
extra_body: dict[str, object],
213298
) -> None:
214299
async with AsyncPdfRestClient(
215300
api_key=pdfrest_api_key,
216301
base_url=pdfrest_live_base_url,
217302
) as client:
218-
with pytest.raises(PdfRestApiError, match=r"(?i)rgb"):
219-
await client.preview_redactions(
303+
if "redactions" in extra_body:
304+
with pytest.raises(
305+
PdfRestApiError,
306+
match=(
307+
r"The JSON data provided is not properly formatted\. Please check "
308+
r"your syntax and try again\."
309+
),
310+
):
311+
await client.preview_redactions(
312+
uploaded_pdf_for_redaction,
313+
redactions=[{"type": "literal", "value": "placeholder"}],
314+
extra_body=extra_body,
315+
)
316+
else:
317+
preview = await client.preview_redactions(
220318
uploaded_pdf_for_redaction,
221319
redactions=[{"type": "literal", "value": "placeholder"}],
222-
extra_body={"rgb_color": "-1,-1,-1"},
320+
extra_body=extra_body,
223321
)
322+
preview_file = preview.output_files[0]
323+
with pytest.raises(PdfRestApiError, match=r"(?i)rgb"):
324+
await client.apply_redactions(preview_file, extra_body=extra_body)

tests/live/test_live_pdf_split_merge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def test_live_merge_pdfs_invalid_pages(
280280

281281

282282
@pytest.mark.asyncio
283-
async def test_live_async_merge_pdfs(
283+
async def test_live_async_merge_pdfs_success(
284284
pdfrest_api_key: str,
285285
pdfrest_live_base_url: str,
286286
uploaded_live_pdfs: tuple[PdfRestFile, PdfRestFile],

tests/live/test_live_rasterize_pdf.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,35 @@ def test_live_rasterize_pdf_success(
5757

5858

5959
@pytest.mark.asyncio
60+
@pytest.mark.parametrize(
61+
"output_name",
62+
[
63+
pytest.param(None, id="default-output"),
64+
pytest.param("rasterized-live", id="custom-output"),
65+
],
66+
)
6067
async def test_live_async_rasterize_pdf_success(
6168
pdfrest_api_key: str,
6269
pdfrest_live_base_url: str,
6370
uploaded_pdf_for_rasterize: PdfRestFile,
71+
output_name: str | None,
6472
) -> None:
73+
kwargs: dict[str, str] = {}
74+
if output_name is not None:
75+
kwargs["output"] = output_name
76+
6577
async with AsyncPdfRestClient(
6678
api_key=pdfrest_api_key,
6779
base_url=pdfrest_live_base_url,
6880
) as client:
69-
response = await client.rasterize_pdf(
70-
uploaded_pdf_for_rasterize, output="async"
71-
)
81+
response = await client.rasterize_pdf(uploaded_pdf_for_rasterize, **kwargs)
7282

7383
assert response.output_files
7484
output_file = response.output_file
75-
assert output_file.name.startswith("async")
85+
if output_name is not None:
86+
assert output_file.name.startswith(output_name)
87+
else:
88+
assert output_file.name.endswith(".pdf")
7689
assert output_file.type == "application/pdf"
7790
assert output_file.size > 0
7891
assert response.warning is None

0 commit comments

Comments
 (0)