Skip to content

Commit 300e479

Browse files
tests: Add live tests for PDF/X, Word, and form flattening
- Added `test_live_convert_to_pdfx.py` to test PDF to PDF/X conversion with valid and invalid output types. - Added `test_live_convert_to_word.py` to test PDF to Word conversion with support for custom and default output names. - Added `test_live_flatten_pdf_forms.py` to test flattening of PDF forms with validation for custom and default output names. - Introduced new test resource file: `form_with_data.pdf`. Assisted-by: Codex
1 parent 7f175dc commit 300e479

4 files changed

Lines changed: 225 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from __future__ import annotations
2+
3+
from typing import cast, get_args
4+
5+
import pytest
6+
7+
from pdfrest import PdfRestApiError, PdfRestClient
8+
from pdfrest.models import PdfRestFile
9+
from pdfrest.types import PdfXType
10+
11+
from ..resources import get_test_resource_path
12+
13+
PDFX_TYPES: tuple[PdfXType, ...] = cast(tuple[PdfXType, ...], get_args(PdfXType))
14+
15+
16+
@pytest.fixture(scope="module")
17+
def uploaded_pdf_for_pdfx(
18+
pdfrest_api_key: str,
19+
pdfrest_live_base_url: str,
20+
) -> PdfRestFile:
21+
resource = get_test_resource_path("report.pdf")
22+
with PdfRestClient(
23+
api_key=pdfrest_api_key,
24+
base_url=pdfrest_live_base_url,
25+
) as client:
26+
return client.files.create_from_paths([resource])[0]
27+
28+
29+
@pytest.mark.parametrize("output_type", PDFX_TYPES, ids=list(PDFX_TYPES))
30+
def test_live_convert_to_pdfx_success(
31+
pdfrest_api_key: str,
32+
pdfrest_live_base_url: str,
33+
uploaded_pdf_for_pdfx: PdfRestFile,
34+
output_type: PdfXType,
35+
) -> None:
36+
with PdfRestClient(
37+
api_key=pdfrest_api_key,
38+
base_url=pdfrest_live_base_url,
39+
) as client:
40+
response = client.convert_to_pdfx(
41+
uploaded_pdf_for_pdfx,
42+
output_type=output_type,
43+
output="pdfx-live",
44+
)
45+
46+
assert response.output_files
47+
output_file = response.output_file
48+
assert output_file.type == "application/pdf"
49+
assert str(response.input_id) == str(uploaded_pdf_for_pdfx.id)
50+
assert output_file.name.startswith("pdfx-live")
51+
52+
53+
@pytest.mark.parametrize(
54+
"invalid_output_type",
55+
[
56+
pytest.param("PDF/X-0", id="pdfx-0"),
57+
pytest.param("PDF/X-99", id="pdfx-99"),
58+
pytest.param("pdf/x-4", id="lowercase"),
59+
],
60+
)
61+
def test_live_convert_to_pdfx_invalid_output_type(
62+
pdfrest_api_key: str,
63+
pdfrest_live_base_url: str,
64+
uploaded_pdf_for_pdfx: PdfRestFile,
65+
invalid_output_type: str,
66+
) -> None:
67+
with (
68+
PdfRestClient(
69+
api_key=pdfrest_api_key,
70+
base_url=pdfrest_live_base_url,
71+
) as client,
72+
pytest.raises(PdfRestApiError),
73+
):
74+
client.convert_to_pdfx(
75+
uploaded_pdf_for_pdfx,
76+
output_type="PDF/X-1a",
77+
extra_body={"output_type": invalid_output_type},
78+
)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
5+
from pdfrest import 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_word(
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.mark.parametrize(
25+
"output_name",
26+
[
27+
pytest.param(None, id="default-output"),
28+
pytest.param("live-word", id="custom-output"),
29+
],
30+
)
31+
def test_live_convert_to_word_success(
32+
pdfrest_api_key: str,
33+
pdfrest_live_base_url: str,
34+
uploaded_pdf_for_word: PdfRestFile,
35+
output_name: str | None,
36+
) -> None:
37+
kwargs: dict[str, str] = {}
38+
if output_name is not None:
39+
kwargs["output"] = output_name
40+
41+
with PdfRestClient(
42+
api_key=pdfrest_api_key,
43+
base_url=pdfrest_live_base_url,
44+
) as client:
45+
response = client.convert_to_word(uploaded_pdf_for_word, **kwargs)
46+
47+
assert response.output_files
48+
output_file = response.output_file
49+
assert (
50+
output_file.type
51+
== "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
52+
)
53+
assert str(response.input_id) == str(uploaded_pdf_for_word.id)
54+
if output_name is not None:
55+
assert output_file.name.startswith(output_name)
56+
else:
57+
assert output_file.name.endswith(".docx")
58+
59+
60+
def test_live_convert_to_word_invalid_file_id(
61+
pdfrest_api_key: str,
62+
pdfrest_live_base_url: str,
63+
uploaded_pdf_for_word: PdfRestFile,
64+
) -> None:
65+
with (
66+
PdfRestClient(
67+
api_key=pdfrest_api_key,
68+
base_url=pdfrest_live_base_url,
69+
) as client,
70+
pytest.raises(PdfRestApiError),
71+
):
72+
client.convert_to_word(
73+
uploaded_pdf_for_word,
74+
extra_body={"id": "00000000-0000-0000-0000-000000000000"},
75+
)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
5+
from pdfrest import 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_with_forms(
13+
pdfrest_api_key: str,
14+
pdfrest_live_base_url: str,
15+
) -> PdfRestFile:
16+
resource = get_test_resource_path("form_with_data.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.mark.parametrize(
25+
"output_name",
26+
[
27+
pytest.param(None, id="default-output"),
28+
pytest.param("flattened-live", id="custom-output"),
29+
],
30+
)
31+
def test_live_flatten_pdf_forms(
32+
pdfrest_api_key: str,
33+
pdfrest_live_base_url: str,
34+
uploaded_pdf_with_forms: PdfRestFile,
35+
output_name: str | None,
36+
) -> None:
37+
kwargs: dict[str, str] = {}
38+
if output_name is not None:
39+
kwargs["output"] = output_name
40+
41+
with PdfRestClient(
42+
api_key=pdfrest_api_key,
43+
base_url=pdfrest_live_base_url,
44+
) as client:
45+
response = client.flatten_pdf_forms(uploaded_pdf_with_forms, **kwargs)
46+
47+
assert response.output_files
48+
output_file = response.output_file
49+
assert output_file.type == "application/pdf"
50+
assert str(response.input_id) == str(uploaded_pdf_with_forms.id)
51+
if output_name is not None:
52+
assert output_file.name.startswith(output_name)
53+
else:
54+
assert output_file.name.endswith(".pdf")
55+
56+
57+
def test_live_flatten_pdf_forms_invalid_file_id(
58+
pdfrest_api_key: str,
59+
pdfrest_live_base_url: str,
60+
uploaded_pdf_with_forms: PdfRestFile,
61+
) -> None:
62+
with (
63+
PdfRestClient(
64+
api_key=pdfrest_api_key,
65+
base_url=pdfrest_live_base_url,
66+
) as client,
67+
pytest.raises(PdfRestApiError),
68+
):
69+
client.flatten_pdf_forms(
70+
uploaded_pdf_with_forms,
71+
extra_body={"id": "ffffffff-ffff-ffff-ffff-ffffffffffff"},
72+
)

tests/resources/form_with_data.pdf

22.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)