|
28 | 28 | CustomerAssessmentDetail, |
29 | 29 | CustomerFinding, |
30 | 30 | CustomerReviewDetail, |
| 31 | + ExportPage, |
31 | 32 | FileReviewRequest, |
32 | 33 | FileReviewResponse, |
33 | 34 | PatchReviewRequest, |
@@ -95,6 +96,13 @@ async def collect(client: Client, **kwargs: Any) -> list[Any]: |
95 | 96 | return list(iterator) |
96 | 97 |
|
97 | 98 |
|
| 99 | +async def collect_pages(client: Client, dataset: str, **kwargs: Any) -> list[ExportPage]: |
| 100 | + iterator = client.iter_export_pages(ORG, dataset, **kwargs) |
| 101 | + if hasattr(iterator, "__aiter__"): |
| 102 | + return [export async for export in iterator] |
| 103 | + return list(iterator) |
| 104 | + |
| 105 | + |
98 | 106 | class Clock: |
99 | 107 | def __init__(self, monkeypatch: pytest.MonkeyPatch) -> None: |
100 | 108 | self.now = 100.0 |
@@ -1392,6 +1400,86 @@ def respond(request: Request, index: int) -> Reply: |
1392 | 1400 | assert caught.value.status == 404 |
1393 | 1401 |
|
1394 | 1402 |
|
| 1403 | +@MODES |
| 1404 | +async def test_export_iteration_keeps_the_window_and_refuses_cycles_or_bounds(mode: str) -> None: |
| 1405 | + kwargs = { |
| 1406 | + "from_time": "2026-10-01T00:00:00Z", |
| 1407 | + "to_time": "2026-10-25T00:00:00Z", |
| 1408 | + "format": "csv", |
| 1409 | + "group_by": "severity", |
| 1410 | + } |
| 1411 | + |
| 1412 | + def csv_reply(cursor: str | None) -> Reply: |
| 1413 | + headers = { |
| 1414 | + "Content-Type": "text/csv", |
| 1415 | + "X-Proofline-Export-Version": "1", |
| 1416 | + "X-Proofline-Export-Settled-Until": "2026-10-25T00:00:00Z", |
| 1417 | + } |
| 1418 | + if cursor is not None: |
| 1419 | + headers["X-Proofline-Export-Next-Cursor"] = cursor |
| 1420 | + return Reply(body=b"dataset_version,severity\r\n1,high\r\n", headers=headers) |
| 1421 | + |
| 1422 | + def respond(request: Request, index: int) -> Reply: |
| 1423 | + query = parse_qs(urlsplit(request.path).query) |
| 1424 | + assert request.path.startswith(f"/api/v1/organizations/{ORG}/exports/finding_outcomes?") |
| 1425 | + assert query["from"] == ["2026-10-01T00:00:00Z"] |
| 1426 | + assert query["to"] == ["2026-10-25T00:00:00Z"] |
| 1427 | + assert query["format"] == ["csv"] |
| 1428 | + assert query["group_by"] == ["severity"] |
| 1429 | + assert query.get("cursor") == ([f"cursor-{index - 1}"] if index else None) |
| 1430 | + headers = { |
| 1431 | + "Content-Type": "text/csv", |
| 1432 | + "X-Proofline-Export-Version": "1", |
| 1433 | + "X-Proofline-Export-Settled-Until": "2026-10-25T00:00:00Z", |
| 1434 | + } |
| 1435 | + if index < 2: |
| 1436 | + headers["X-Proofline-Export-Next-Cursor"] = f"cursor-{index}" |
| 1437 | + return Reply( |
| 1438 | + body=f"dataset_version,severity\r\n1,page-{index}\r\n".encode(), headers=headers |
| 1439 | + ) |
| 1440 | + |
| 1441 | + with Server(respond) as server: |
| 1442 | + async with session(mode, server.url) as client: |
| 1443 | + exports = await collect_pages(client, "finding_outcomes", **kwargs) |
| 1444 | + assert [export.rows[0]["severity"] for export in exports] == [ |
| 1445 | + "page-0", |
| 1446 | + "page-1", |
| 1447 | + "page-2", |
| 1448 | + ] |
| 1449 | + assert exports[0].text.startswith("dataset_version,severity\r\n") |
| 1450 | + assert [export.next_cursor for export in exports] == ["cursor-0", "cursor-1", None] |
| 1451 | + assert exports[-1].settled_until == "2026-10-25T00:00:00Z" |
| 1452 | + assert len(server.requests) == 3 |
| 1453 | + |
| 1454 | + with Server(lambda request, index: csv_reply("repeated")) as server: |
| 1455 | + async with session(mode, server.url) as client: |
| 1456 | + with pytest.raises(ProoflineError) as raised: |
| 1457 | + await collect_pages(client, "finding_outcomes", **kwargs) |
| 1458 | + assert raised.value.type == "urn:proofline:sdk:invalid-pagination" |
| 1459 | + assert len(server.requests) == 2 |
| 1460 | + |
| 1461 | + with Server(lambda request, index: csv_reply("seeded")) as server: |
| 1462 | + async with session(mode, server.url) as client: |
| 1463 | + with pytest.raises(ProoflineError) as raised: |
| 1464 | + await collect_pages(client, "finding_outcomes", cursor="seeded", **kwargs) |
| 1465 | + assert raised.value.type == "urn:proofline:sdk:invalid-pagination" |
| 1466 | + assert len(server.requests) == 1 |
| 1467 | + |
| 1468 | + with Server(lambda request, index: csv_reply(f"cursor-{index}")) as server: |
| 1469 | + async with session(mode, server.url) as client: |
| 1470 | + with pytest.raises(ProoflineError) as raised: |
| 1471 | + await collect_pages(client, "finding_outcomes", max_pages=2, **kwargs) |
| 1472 | + assert raised.value.detail == "Export pagination reached the configured bound." |
| 1473 | + assert len(server.requests) == 2 |
| 1474 | + |
| 1475 | + with Server(lambda request, index: csv_reply(None)) as server: |
| 1476 | + async with session(mode, server.url) as client: |
| 1477 | + for bound in (0, 1_001): |
| 1478 | + with pytest.raises(ValueError): |
| 1479 | + await collect_pages(client, "finding_outcomes", max_pages=bound, **kwargs) |
| 1480 | + assert server.requests == [] |
| 1481 | + |
| 1482 | + |
1395 | 1483 | @MODES |
1396 | 1484 | async def test_knowledge_negotiation_scope_and_no_retry(mode: str) -> None: |
1397 | 1485 | refusal = False |
|
0 commit comments