Skip to content

Commit a8ce096

Browse files
Flatten annots/transparencies/linearize/rasterize: Fix async test parity
Assisted-by: Codex
1 parent adc6aec commit a8ce096

4 files changed

Lines changed: 136 additions & 0 deletions

File tree

tests/test_flatten_annotations.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,30 @@ def test_flatten_annotations_validation(monkeypatch: pytest.MonkeyPatch) -> None
279279
),
280280
):
281281
client.flatten_annotations([pdf_file, make_pdf_file(PdfRestFileID.generate())])
282+
283+
284+
@pytest.mark.asyncio
285+
async def test_async_flatten_annotations_validation(
286+
monkeypatch: pytest.MonkeyPatch,
287+
) -> None:
288+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
289+
pdf_file = make_pdf_file(PdfRestFileID.generate(1))
290+
png_file = PdfRestFile.model_validate(
291+
build_file_info_payload(
292+
PdfRestFileID.generate(),
293+
"example.png",
294+
"image/png",
295+
)
296+
)
297+
transport = httpx.MockTransport(lambda request: (_ for _ in ()).throw(RuntimeError))
298+
299+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
300+
with pytest.raises(ValidationError, match="Must be a PDF file"):
301+
await client.flatten_annotations(png_file)
302+
303+
with pytest.raises(
304+
ValidationError, match="List should have at most 1 item after validation"
305+
):
306+
await client.flatten_annotations(
307+
[pdf_file, make_pdf_file(PdfRestFileID.generate())]
308+
)

tests/test_flatten_transparencies.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,38 @@ def test_flatten_transparencies_validation(monkeypatch: pytest.MonkeyPatch) -> N
295295
),
296296
):
297297
client.flatten_transparencies(pdf_file, quality="ultra") # type: ignore[arg-type]
298+
299+
300+
@pytest.mark.asyncio
301+
async def test_async_flatten_transparencies_validation(
302+
monkeypatch: pytest.MonkeyPatch,
303+
) -> None:
304+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
305+
pdf_file = make_pdf_file(PdfRestFileID.generate(1))
306+
png_file = PdfRestFile.model_validate(
307+
build_file_info_payload(
308+
PdfRestFileID.generate(),
309+
"example.png",
310+
"image/png",
311+
)
312+
)
313+
transport = httpx.MockTransport(lambda request: (_ for _ in ()).throw(RuntimeError))
314+
315+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
316+
with pytest.raises(ValidationError, match="Must be a PDF file"):
317+
await client.flatten_transparencies(png_file)
318+
319+
with pytest.raises(
320+
ValidationError, match="List should have at most 1 item after validation"
321+
):
322+
await client.flatten_transparencies(
323+
[pdf_file, make_pdf_file(PdfRestFileID.generate())]
324+
)
325+
326+
with pytest.raises(
327+
ValidationError, match="Input should be 'low', 'medium' or 'high'"
328+
):
329+
await client.flatten_transparencies(
330+
pdf_file,
331+
quality="ultra", # type: ignore[arg-type]
332+
)

tests/test_linearize_pdf.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,45 @@ def test_linearize_pdf_validation(
280280
pytest.raises(ValidationError, match=match),
281281
):
282282
client.linearize_pdf(files_argument)
283+
284+
285+
@pytest.mark.asyncio
286+
@pytest.mark.parametrize(
287+
("files", "match"),
288+
[
289+
pytest.param(
290+
"png",
291+
"Must be a PDF file",
292+
id="non-pdf-file",
293+
),
294+
pytest.param(
295+
"multiple",
296+
"List should have at most 1 item after validation",
297+
id="multiple-files",
298+
),
299+
],
300+
)
301+
async def test_async_linearize_pdf_validation(
302+
monkeypatch: pytest.MonkeyPatch,
303+
files: str,
304+
match: str,
305+
) -> None:
306+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
307+
pdf_file = make_pdf_file(PdfRestFileID.generate(1))
308+
png_file = PdfRestFile.model_validate(
309+
build_file_info_payload(
310+
PdfRestFileID.generate(),
311+
"example.png",
312+
"image/png",
313+
)
314+
)
315+
transport = httpx.MockTransport(lambda request: (_ for _ in ()).throw(RuntimeError))
316+
files_argument = (
317+
png_file
318+
if files == "png"
319+
else [pdf_file, make_pdf_file(PdfRestFileID.generate())]
320+
)
321+
322+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
323+
with pytest.raises(ValidationError, match=match):
324+
await client.linearize_pdf(files_argument)

tests/test_rasterize_pdf.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,35 @@ def test_rasterize_pdf_validation(monkeypatch: pytest.MonkeyPatch) -> None:
263263
),
264264
):
265265
client.rasterize_pdf([pdf_file, make_pdf_file(PdfRestFileID.generate())])
266+
267+
268+
@pytest.mark.asyncio
269+
async def test_async_rasterize_pdf_validation(monkeypatch: pytest.MonkeyPatch) -> None:
270+
monkeypatch.delenv("PDFREST_API_KEY", raising=False)
271+
pdf_file = make_pdf_file(PdfRestFileID.generate(1))
272+
png_file = PdfRestFile.model_validate(
273+
build_file_info_payload(
274+
PdfRestFileID.generate(),
275+
"example.png",
276+
"image/png",
277+
)
278+
)
279+
transport = httpx.MockTransport(lambda request: (_ for _ in ()).throw(RuntimeError))
280+
281+
async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client:
282+
with pytest.raises(ValidationError, match="Must be a PDF file"):
283+
await client.rasterize_pdf(png_file)
284+
285+
with pytest.raises(
286+
ValidationError, match="List should have at most 1 item after validation"
287+
):
288+
await client.rasterize_pdf(
289+
[pdf_file, make_pdf_file(PdfRestFileID.generate())]
290+
)
291+
292+
with pytest.raises(ValidationError, match="Timeout must be greater than 0"):
293+
await client.rasterize_pdf(
294+
make_pdf_file(PdfRestFileID.generate(1)),
295+
output="output",
296+
timeout=0,
297+
)

0 commit comments

Comments
 (0)