|
| 1 | +"""End-to-end test of generated SSE streaming methods. |
| 2 | +
|
| 3 | +Uses ``httpx.MockTransport`` so the test does not need a running server, |
| 4 | +but exercises the *generated* client classes (sync + async) to make sure |
| 5 | +the codegen wiring is correct end-to-end. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import json |
| 11 | + |
| 12 | +import httpx |
| 13 | +import pytest |
| 14 | + |
| 15 | +from generated import AsyncClient, Client, MyapiModelOutputPet as Pet |
| 16 | +from reflectapi_runtime import ApplicationError |
| 17 | + |
| 18 | + |
| 19 | +def _sse(items: list[dict]) -> bytes: |
| 20 | + return ("".join(f"data: {json.dumps(it)}\n\n" for it in items)).encode() |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def pet_payloads() -> list[dict]: |
| 25 | + # Pet on the wire from the demo schema: {"name": str, "kind": ..., "behaviors": [...]}. |
| 26 | + # Use minimal values that pass Pydantic validation. |
| 27 | + return [ |
| 28 | + { |
| 29 | + "name": "fido", |
| 30 | + "kind": {"type": "dog", "breed": "lab"}, |
| 31 | + "behaviors": [], |
| 32 | + "updated_at": "2026-01-01T00:00:00Z", |
| 33 | + }, |
| 34 | + { |
| 35 | + "name": "whiskers", |
| 36 | + "kind": {"type": "cat", "lives": 9}, |
| 37 | + "behaviors": [], |
| 38 | + "updated_at": "2026-01-02T00:00:00Z", |
| 39 | + }, |
| 40 | + ] |
| 41 | + |
| 42 | + |
| 43 | +def _async_client(handler) -> AsyncClient: |
| 44 | + return AsyncClient( |
| 45 | + "http://test", |
| 46 | + client=httpx.AsyncClient(transport=httpx.MockTransport(handler)), |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def _sync_client(handler) -> Client: |
| 51 | + return Client( |
| 52 | + "http://test", |
| 53 | + client=httpx.Client(transport=httpx.MockTransport(handler)), |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.asyncio |
| 58 | +async def test_async_cdc_events_stream(pet_payloads): |
| 59 | + captured: dict[str, str] = {} |
| 60 | + |
| 61 | + def handler(request: httpx.Request) -> httpx.Response: |
| 62 | + captured["accept"] = request.headers.get("accept", "") |
| 63 | + captured["path"] = request.url.path |
| 64 | + captured["method"] = request.method |
| 65 | + return httpx.Response(200, content=_sse(pet_payloads)) |
| 66 | + |
| 67 | + client = _async_client(handler) |
| 68 | + received = [pet async for pet in client.pets.cdc_events()] |
| 69 | + |
| 70 | + assert captured["accept"] == "text/event-stream" |
| 71 | + assert captured["path"] == "/pets.cdc-events" |
| 72 | + assert captured["method"] == "POST" |
| 73 | + assert all(isinstance(p, Pet) for p in received) |
| 74 | + assert [p.name for p in received] == ["fido", "whiskers"] |
| 75 | + await client.aclose() |
| 76 | + |
| 77 | + |
| 78 | +def test_sync_cdc_events_stream(pet_payloads): |
| 79 | + def handler(request: httpx.Request) -> httpx.Response: |
| 80 | + return httpx.Response(200, content=_sse(pet_payloads)) |
| 81 | + |
| 82 | + client = _sync_client(handler) |
| 83 | + received = list(client.pets.cdc_events()) |
| 84 | + assert [p.name for p in received] == ["fido", "whiskers"] |
| 85 | + client.close() |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.asyncio |
| 89 | +async def test_async_cdc_events_4xx_raises(): |
| 90 | + def handler(request: httpx.Request) -> httpx.Response: |
| 91 | + return httpx.Response(401, json={"reason": "denied"}) |
| 92 | + |
| 93 | + client = _async_client(handler) |
| 94 | + with pytest.raises(ApplicationError) as ei: |
| 95 | + async for _ in client.pets.cdc_events(): |
| 96 | + break |
| 97 | + assert ei.value.status_code == 401 |
| 98 | + await client.aclose() |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.asyncio |
| 102 | +async def test_async_cdc_events_partial_consumption_closes_response(): |
| 103 | + pets_lots = [ |
| 104 | + { |
| 105 | + "name": f"p{i}", |
| 106 | + "kind": {"type": "dog", "breed": "x"}, |
| 107 | + "behaviors": [], |
| 108 | + "updated_at": "2026-01-01T00:00:00Z", |
| 109 | + } |
| 110 | + for i in range(50) |
| 111 | + ] |
| 112 | + |
| 113 | + def handler(request: httpx.Request) -> httpx.Response: |
| 114 | + return httpx.Response(200, content=_sse(pets_lots)) |
| 115 | + |
| 116 | + client = _async_client(handler) |
| 117 | + stream = client.pets.cdc_events() |
| 118 | + first = await stream.__anext__() |
| 119 | + assert first.name == "p0" |
| 120 | + # closing the generator triggers the runtime's `finally: response.aclose()`. |
| 121 | + await stream.aclose() |
| 122 | + await client.aclose() |
0 commit comments