|
13 | 13 | from .graphics_test_helpers import ASYNC_API_KEY, VALID_API_KEY, build_file_info_payload |
14 | 14 |
|
15 | 15 |
|
| 16 | +@pytest.mark.parametrize( |
| 17 | + "page_size", |
| 18 | + [ |
| 19 | + pytest.param("letter", id="letter"), |
| 20 | + pytest.param("legal", id="legal"), |
| 21 | + pytest.param("ledger", id="ledger"), |
| 22 | + pytest.param("A3", id="a3"), |
| 23 | + pytest.param("A4", id="a4"), |
| 24 | + pytest.param("A5", id="a5"), |
| 25 | + ], |
| 26 | +) |
| 27 | +@pytest.mark.parametrize( |
| 28 | + "page_orientation", |
| 29 | + [ |
| 30 | + pytest.param("portrait", id="portrait"), |
| 31 | + pytest.param("landscape", id="landscape"), |
| 32 | + ], |
| 33 | +) |
| 34 | +def test_blank_pdf_standard_page_literals( |
| 35 | + monkeypatch: pytest.MonkeyPatch, |
| 36 | + page_size: str, |
| 37 | + page_orientation: str, |
| 38 | +) -> None: |
| 39 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 40 | + output_id = str(PdfRestFileID.generate()) |
| 41 | + |
| 42 | + payload_dump = PdfBlankPayload.model_validate( |
| 43 | + { |
| 44 | + "page_size": page_size, |
| 45 | + "page_count": 1, |
| 46 | + "page_orientation": page_orientation, |
| 47 | + } |
| 48 | + ).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True) |
| 49 | + |
| 50 | + seen: dict[str, int] = {"post": 0, "get": 0} |
| 51 | + |
| 52 | + def handler(request: httpx.Request) -> httpx.Response: |
| 53 | + if request.method == "POST" and request.url.path == "/blank-pdf": |
| 54 | + seen["post"] += 1 |
| 55 | + payload = json.loads(request.content.decode("utf-8")) |
| 56 | + assert payload == payload_dump |
| 57 | + return httpx.Response(200, json={"outputId": [output_id]}) |
| 58 | + if request.method == "GET" and request.url.path == f"/resource/{output_id}": |
| 59 | + seen["get"] += 1 |
| 60 | + return httpx.Response( |
| 61 | + 200, |
| 62 | + json=build_file_info_payload( |
| 63 | + output_id, |
| 64 | + "blank.pdf", |
| 65 | + "application/pdf", |
| 66 | + ), |
| 67 | + ) |
| 68 | + msg = f"Unexpected request {request.method} {request.url}" |
| 69 | + raise AssertionError(msg) |
| 70 | + |
| 71 | + transport = httpx.MockTransport(handler) |
| 72 | + with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client: |
| 73 | + response = client.blank_pdf( |
| 74 | + page_size=page_size, |
| 75 | + page_count=1, |
| 76 | + page_orientation=page_orientation, |
| 77 | + ) |
| 78 | + |
| 79 | + assert seen == {"post": 1, "get": 1} |
| 80 | + assert isinstance(response, PdfRestFileBasedResponse) |
| 81 | + assert response.output_file.type == "application/pdf" |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.asyncio |
| 85 | +@pytest.mark.parametrize( |
| 86 | + "page_size", |
| 87 | + [ |
| 88 | + pytest.param("letter", id="letter"), |
| 89 | + pytest.param("legal", id="legal"), |
| 90 | + pytest.param("ledger", id="ledger"), |
| 91 | + pytest.param("A3", id="a3"), |
| 92 | + pytest.param("A4", id="a4"), |
| 93 | + pytest.param("A5", id="a5"), |
| 94 | + ], |
| 95 | +) |
| 96 | +@pytest.mark.parametrize( |
| 97 | + "page_orientation", |
| 98 | + [ |
| 99 | + pytest.param("portrait", id="portrait"), |
| 100 | + pytest.param("landscape", id="landscape"), |
| 101 | + ], |
| 102 | +) |
| 103 | +async def test_async_blank_pdf_standard_page_literals( |
| 104 | + monkeypatch: pytest.MonkeyPatch, |
| 105 | + page_size: str, |
| 106 | + page_orientation: str, |
| 107 | +) -> None: |
| 108 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 109 | + output_id = str(PdfRestFileID.generate()) |
| 110 | + |
| 111 | + payload_dump = PdfBlankPayload.model_validate( |
| 112 | + { |
| 113 | + "page_size": page_size, |
| 114 | + "page_count": 1, |
| 115 | + "page_orientation": page_orientation, |
| 116 | + } |
| 117 | + ).model_dump(mode="json", by_alias=True, exclude_none=True, exclude_unset=True) |
| 118 | + |
| 119 | + seen: dict[str, int] = {"post": 0, "get": 0} |
| 120 | + |
| 121 | + def handler(request: httpx.Request) -> httpx.Response: |
| 122 | + if request.method == "POST" and request.url.path == "/blank-pdf": |
| 123 | + seen["post"] += 1 |
| 124 | + payload = json.loads(request.content.decode("utf-8")) |
| 125 | + assert payload == payload_dump |
| 126 | + return httpx.Response(200, json={"outputId": [output_id]}) |
| 127 | + if request.method == "GET" and request.url.path == f"/resource/{output_id}": |
| 128 | + seen["get"] += 1 |
| 129 | + return httpx.Response( |
| 130 | + 200, |
| 131 | + json=build_file_info_payload( |
| 132 | + output_id, |
| 133 | + "blank.pdf", |
| 134 | + "application/pdf", |
| 135 | + ), |
| 136 | + ) |
| 137 | + msg = f"Unexpected request {request.method} {request.url}" |
| 138 | + raise AssertionError(msg) |
| 139 | + |
| 140 | + transport = httpx.MockTransport(handler) |
| 141 | + async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client: |
| 142 | + response = await client.blank_pdf( |
| 143 | + page_size=page_size, |
| 144 | + page_count=1, |
| 145 | + page_orientation=page_orientation, |
| 146 | + ) |
| 147 | + |
| 148 | + assert seen == {"post": 1, "get": 1} |
| 149 | + assert isinstance(response, PdfRestFileBasedResponse) |
| 150 | + assert response.output_file.type == "application/pdf" |
| 151 | + |
| 152 | + |
| 153 | +@pytest.mark.parametrize( |
| 154 | + "page_count", |
| 155 | + [ |
| 156 | + pytest.param(1, id="min-page-count"), |
| 157 | + pytest.param(1000, id="max-page-count"), |
| 158 | + ], |
| 159 | +) |
| 160 | +def test_blank_pdf_page_count_boundaries_success( |
| 161 | + monkeypatch: pytest.MonkeyPatch, |
| 162 | + page_count: int, |
| 163 | +) -> None: |
| 164 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 165 | + output_id = str(PdfRestFileID.generate()) |
| 166 | + |
| 167 | + seen: dict[str, int] = {"post": 0, "get": 0} |
| 168 | + |
| 169 | + def handler(request: httpx.Request) -> httpx.Response: |
| 170 | + if request.method == "POST" and request.url.path == "/blank-pdf": |
| 171 | + seen["post"] += 1 |
| 172 | + payload = json.loads(request.content.decode("utf-8")) |
| 173 | + assert payload["page_count"] == page_count |
| 174 | + return httpx.Response(200, json={"outputId": [output_id]}) |
| 175 | + if request.method == "GET" and request.url.path == f"/resource/{output_id}": |
| 176 | + seen["get"] += 1 |
| 177 | + return httpx.Response( |
| 178 | + 200, |
| 179 | + json=build_file_info_payload( |
| 180 | + output_id, |
| 181 | + "blank.pdf", |
| 182 | + "application/pdf", |
| 183 | + ), |
| 184 | + ) |
| 185 | + msg = f"Unexpected request {request.method} {request.url}" |
| 186 | + raise AssertionError(msg) |
| 187 | + |
| 188 | + transport = httpx.MockTransport(handler) |
| 189 | + with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client: |
| 190 | + response = client.blank_pdf( |
| 191 | + page_size="letter", |
| 192 | + page_count=page_count, |
| 193 | + page_orientation="portrait", |
| 194 | + ) |
| 195 | + |
| 196 | + assert isinstance(response, PdfRestFileBasedResponse) |
| 197 | + assert seen == {"post": 1, "get": 1} |
| 198 | + |
| 199 | + |
| 200 | +@pytest.mark.asyncio |
| 201 | +@pytest.mark.parametrize( |
| 202 | + "page_count", |
| 203 | + [ |
| 204 | + pytest.param(1, id="min-page-count"), |
| 205 | + pytest.param(1000, id="max-page-count"), |
| 206 | + ], |
| 207 | +) |
| 208 | +async def test_async_blank_pdf_page_count_boundaries_success( |
| 209 | + monkeypatch: pytest.MonkeyPatch, |
| 210 | + page_count: int, |
| 211 | +) -> None: |
| 212 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 213 | + output_id = str(PdfRestFileID.generate()) |
| 214 | + |
| 215 | + seen: dict[str, int] = {"post": 0, "get": 0} |
| 216 | + |
| 217 | + def handler(request: httpx.Request) -> httpx.Response: |
| 218 | + if request.method == "POST" and request.url.path == "/blank-pdf": |
| 219 | + seen["post"] += 1 |
| 220 | + payload = json.loads(request.content.decode("utf-8")) |
| 221 | + assert payload["page_count"] == page_count |
| 222 | + return httpx.Response(200, json={"outputId": [output_id]}) |
| 223 | + if request.method == "GET" and request.url.path == f"/resource/{output_id}": |
| 224 | + seen["get"] += 1 |
| 225 | + return httpx.Response( |
| 226 | + 200, |
| 227 | + json=build_file_info_payload( |
| 228 | + output_id, |
| 229 | + "blank.pdf", |
| 230 | + "application/pdf", |
| 231 | + ), |
| 232 | + ) |
| 233 | + msg = f"Unexpected request {request.method} {request.url}" |
| 234 | + raise AssertionError(msg) |
| 235 | + |
| 236 | + transport = httpx.MockTransport(handler) |
| 237 | + async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client: |
| 238 | + response = await client.blank_pdf( |
| 239 | + page_size="letter", |
| 240 | + page_count=page_count, |
| 241 | + page_orientation="portrait", |
| 242 | + ) |
| 243 | + |
| 244 | + assert isinstance(response, PdfRestFileBasedResponse) |
| 245 | + assert seen == {"post": 1, "get": 1} |
| 246 | + |
| 247 | + |
| 248 | +@pytest.mark.parametrize( |
| 249 | + ("page_count", "match"), |
| 250 | + [ |
| 251 | + pytest.param(0, "greater than or equal to 1", id="below-min"), |
| 252 | + pytest.param(1001, "less than or equal to 1000", id="above-max"), |
| 253 | + ], |
| 254 | +) |
| 255 | +def test_blank_pdf_page_count_boundaries_validation( |
| 256 | + monkeypatch: pytest.MonkeyPatch, |
| 257 | + page_count: int, |
| 258 | + match: str, |
| 259 | +) -> None: |
| 260 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 261 | + transport = httpx.MockTransport(lambda request: (_ for _ in ()).throw(RuntimeError)) |
| 262 | + |
| 263 | + with ( |
| 264 | + PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client, |
| 265 | + pytest.raises(ValidationError, match=match), |
| 266 | + ): |
| 267 | + client.blank_pdf( |
| 268 | + page_size="A4", |
| 269 | + page_count=page_count, |
| 270 | + page_orientation="portrait", |
| 271 | + ) |
| 272 | + |
| 273 | + |
| 274 | +@pytest.mark.asyncio |
| 275 | +@pytest.mark.parametrize( |
| 276 | + ("page_count", "match"), |
| 277 | + [ |
| 278 | + pytest.param(0, "greater than or equal to 1", id="below-min"), |
| 279 | + pytest.param(1001, "less than or equal to 1000", id="above-max"), |
| 280 | + ], |
| 281 | +) |
| 282 | +async def test_async_blank_pdf_page_count_boundaries_validation( |
| 283 | + monkeypatch: pytest.MonkeyPatch, |
| 284 | + page_count: int, |
| 285 | + match: str, |
| 286 | +) -> None: |
| 287 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 288 | + transport = httpx.MockTransport(lambda request: (_ for _ in ()).throw(RuntimeError)) |
| 289 | + |
| 290 | + async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client: |
| 291 | + with pytest.raises(ValidationError, match=match): |
| 292 | + await client.blank_pdf( |
| 293 | + page_size="A4", |
| 294 | + page_count=page_count, |
| 295 | + page_orientation="portrait", |
| 296 | + ) |
| 297 | + |
| 298 | + |
| 299 | +@pytest.mark.parametrize( |
| 300 | + ("custom_height", "custom_width", "match"), |
| 301 | + [ |
| 302 | + pytest.param(0.0, 10.0, "greater than 0", id="height-zero"), |
| 303 | + pytest.param(-1.0, 10.0, "greater than 0", id="height-negative"), |
| 304 | + pytest.param(10.0, 0.0, "greater than 0", id="width-zero"), |
| 305 | + pytest.param(10.0, -1.0, "greater than 0", id="width-negative"), |
| 306 | + ], |
| 307 | +) |
| 308 | +def test_blank_pdf_custom_dimensions_validation( |
| 309 | + monkeypatch: pytest.MonkeyPatch, |
| 310 | + custom_height: float, |
| 311 | + custom_width: float, |
| 312 | + match: str, |
| 313 | +) -> None: |
| 314 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 315 | + transport = httpx.MockTransport(lambda request: (_ for _ in ()).throw(RuntimeError)) |
| 316 | + |
| 317 | + with ( |
| 318 | + PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client, |
| 319 | + pytest.raises(ValidationError, match=match), |
| 320 | + ): |
| 321 | + client.blank_pdf( |
| 322 | + page_size="custom", |
| 323 | + page_count=1, |
| 324 | + custom_height=custom_height, |
| 325 | + custom_width=custom_width, |
| 326 | + ) |
| 327 | + |
| 328 | + |
| 329 | +@pytest.mark.asyncio |
| 330 | +@pytest.mark.parametrize( |
| 331 | + ("custom_height", "custom_width", "match"), |
| 332 | + [ |
| 333 | + pytest.param(0.0, 10.0, "greater than 0", id="height-zero"), |
| 334 | + pytest.param(-1.0, 10.0, "greater than 0", id="height-negative"), |
| 335 | + pytest.param(10.0, 0.0, "greater than 0", id="width-zero"), |
| 336 | + pytest.param(10.0, -1.0, "greater than 0", id="width-negative"), |
| 337 | + ], |
| 338 | +) |
| 339 | +async def test_async_blank_pdf_custom_dimensions_validation( |
| 340 | + monkeypatch: pytest.MonkeyPatch, |
| 341 | + custom_height: float, |
| 342 | + custom_width: float, |
| 343 | + match: str, |
| 344 | +) -> None: |
| 345 | + monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
| 346 | + transport = httpx.MockTransport(lambda request: (_ for _ in ()).throw(RuntimeError)) |
| 347 | + |
| 348 | + async with AsyncPdfRestClient(api_key=ASYNC_API_KEY, transport=transport) as client: |
| 349 | + with pytest.raises(ValidationError, match=match): |
| 350 | + await client.blank_pdf( |
| 351 | + page_size="custom", |
| 352 | + page_count=1, |
| 353 | + custom_height=custom_height, |
| 354 | + custom_width=custom_width, |
| 355 | + ) |
| 356 | + |
| 357 | + |
16 | 358 | def test_blank_pdf_success(monkeypatch: pytest.MonkeyPatch) -> None: |
17 | 359 | monkeypatch.delenv("PDFREST_API_KEY", raising=False) |
18 | 360 | output_id = str(PdfRestFileID.generate()) |
|
0 commit comments