|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from collections.abc import Iterable |
| 3 | +from collections.abc import Iterable, Sequence |
4 | 4 | from typing import Any, NamedTuple, get_args |
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
8 | 8 | from pdfrest import PdfRestApiError, PdfRestClient |
| 9 | +from pdfrest.models import PdfRestFile |
9 | 10 | from pdfrest.models._internal import ( |
10 | 11 | BasePdfRestGraphicPayload, |
11 | 12 | BmpPdfRestPayload, |
@@ -107,6 +108,19 @@ def _invalid_smoothing_cases() -> list[Any]: |
107 | 108 | return cases |
108 | 109 |
|
109 | 110 |
|
| 111 | +@pytest.fixture(scope="module") |
| 112 | +def uploaded_20_page_pdf( |
| 113 | + pdfrest_api_key: str, |
| 114 | + pdfrest_live_base_url: str, |
| 115 | +) -> PdfRestFile: |
| 116 | + resource = get_test_resource_path("20-pages.pdf") |
| 117 | + with PdfRestClient( |
| 118 | + api_key=pdfrest_api_key, |
| 119 | + base_url=pdfrest_live_base_url, |
| 120 | + ) as client: |
| 121 | + return client.files.create_from_paths([resource])[0] |
| 122 | + |
| 123 | + |
110 | 124 | @pytest.mark.parametrize( |
111 | 125 | ("_endpoint_label", "spec", "color_model"), |
112 | 126 | _valid_color_cases(), |
@@ -253,3 +267,130 @@ def test_live_graphic_invalid_smoothing( |
253 | 267 | smoothing="none", |
254 | 268 | extra_body={"smoothing": invalid_smoothing}, |
255 | 269 | ) |
| 270 | + |
| 271 | + |
| 272 | +@pytest.mark.parametrize( |
| 273 | + ("page_range", "expect_success"), |
| 274 | + [ |
| 275 | + pytest.param("5", True, id="single"), |
| 276 | + pytest.param("3-7", True, id="ascending-range"), |
| 277 | + pytest.param("last", True, id="last"), |
| 278 | + pytest.param("1-last", True, id="entire-document"), |
| 279 | + pytest.param(["1", "3", "5-7"], True, id="list-mixed"), |
| 280 | + ], |
| 281 | +) |
| 282 | +def test_live_png_page_range_variants( |
| 283 | + pdfrest_api_key: str, |
| 284 | + pdfrest_live_base_url: str, |
| 285 | + uploaded_20_page_pdf: PdfRestFile, |
| 286 | + page_range: Any, |
| 287 | + expect_success: bool, |
| 288 | + request: pytest.FixtureRequest, |
| 289 | +) -> None: |
| 290 | + case_id = request.node.callspec.id |
| 291 | + with PdfRestClient( |
| 292 | + api_key=pdfrest_api_key, |
| 293 | + base_url=pdfrest_live_base_url, |
| 294 | + ) as client: |
| 295 | + info = client.query_pdf_info(uploaded_20_page_pdf) |
| 296 | + |
| 297 | + assert info.page_count == 20 |
| 298 | + assert str(info.input_id) == str(uploaded_20_page_pdf.id) |
| 299 | + assert info.filename is None or info.filename.endswith(".pdf") |
| 300 | + |
| 301 | + if expect_success: |
| 302 | + response = client.convert_to_png( |
| 303 | + uploaded_20_page_pdf, |
| 304 | + output_prefix=f"live-range-{case_id}", |
| 305 | + page_range=page_range, |
| 306 | + ) |
| 307 | + |
| 308 | + expected_pages = _expand_page_selection(page_range, total_pages=20) |
| 309 | + assert len(response.output_files) == len(expected_pages) |
| 310 | + assert any( |
| 311 | + file_info.name.endswith(".png") for file_info in response.output_files |
| 312 | + ) |
| 313 | + assert all( |
| 314 | + file_info.type == "image/png" and file_info.size > 0 |
| 315 | + for file_info in response.output_files |
| 316 | + ) |
| 317 | + assert str(response.input_id) == str(uploaded_20_page_pdf.id) |
| 318 | + else: |
| 319 | + with pytest.raises(PdfRestApiError): |
| 320 | + client.convert_to_png( |
| 321 | + uploaded_20_page_pdf, |
| 322 | + output_prefix=f"live-range-{case_id}", |
| 323 | + extra_body={"page_range": page_range}, |
| 324 | + ) |
| 325 | + |
| 326 | + |
| 327 | +@pytest.mark.parametrize( |
| 328 | + "page_override", |
| 329 | + [ |
| 330 | + pytest.param("0", id="zero"), |
| 331 | + pytest.param("last-0", id="range-with-zero"), |
| 332 | + pytest.param("7-3", id="descending-range"), |
| 333 | + pytest.param("even", id="even"), |
| 334 | + pytest.param("odd", id="odd"), |
| 335 | + pytest.param("odd,even", id="odd-even"), |
| 336 | + ], |
| 337 | +) |
| 338 | +def test_live_png_page_range_invalid_overrides( |
| 339 | + pdfrest_api_key: str, |
| 340 | + pdfrest_live_base_url: str, |
| 341 | + uploaded_20_page_pdf: PdfRestFile, |
| 342 | + page_override: str, |
| 343 | + request: pytest.FixtureRequest, |
| 344 | +) -> None: |
| 345 | + case_id = request.node.callspec.id |
| 346 | + with ( |
| 347 | + PdfRestClient( |
| 348 | + api_key=pdfrest_api_key, |
| 349 | + base_url=pdfrest_live_base_url, |
| 350 | + ) as client, |
| 351 | + pytest.raises(PdfRestApiError), |
| 352 | + ): |
| 353 | + client.convert_to_png( |
| 354 | + uploaded_20_page_pdf, |
| 355 | + output_prefix=f"live-range-invalid-{case_id}", |
| 356 | + page_range="1", |
| 357 | + extra_body={"pages": page_override}, |
| 358 | + ) |
| 359 | + |
| 360 | + |
| 361 | +def _expand_page_selection( |
| 362 | + selection: Any, |
| 363 | + *, |
| 364 | + total_pages: int, |
| 365 | +) -> list[int]: |
| 366 | + def expand_entry(entry: Any) -> list[int]: |
| 367 | + if isinstance(entry, int): |
| 368 | + return [entry] |
| 369 | + text = str(entry).strip() |
| 370 | + lowered = text.lower() |
| 371 | + if lowered == "even": |
| 372 | + return list(range(2, total_pages + 1, 2)) |
| 373 | + if lowered == "odd": |
| 374 | + return list(range(1, total_pages + 1, 2)) |
| 375 | + if lowered == "last": |
| 376 | + return [total_pages] |
| 377 | + if "-" in lowered: |
| 378 | + start_raw, end_raw = (part.strip() for part in lowered.split("-", 1)) |
| 379 | + |
| 380 | + def resolve(range_token: str) -> int: |
| 381 | + return total_pages if range_token == "last" else int(range_token) # noqa: S105 |
| 382 | + |
| 383 | + start = resolve(start_raw) |
| 384 | + end = resolve(end_raw) |
| 385 | + step = 1 if end >= start else -1 |
| 386 | + return list(range(start, end + step, step)) |
| 387 | + return [int(text)] |
| 388 | + |
| 389 | + if isinstance(selection, Sequence) and not isinstance( |
| 390 | + selection, (str, bytes, bytearray) |
| 391 | + ): |
| 392 | + expanded: list[int] = [] |
| 393 | + for segment in selection: |
| 394 | + expanded.extend(expand_entry(segment)) |
| 395 | + return expanded |
| 396 | + return expand_entry(selection) |
0 commit comments