|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from collections.abc import AsyncIterable |
| 4 | + |
| 5 | + |
| 6 | +from hypercorn.asyncio import serve |
| 7 | +from hypercorn.config import Config |
| 8 | +import pydantic |
| 9 | +from fastapi import FastAPI |
| 10 | +from fastapi.sse import EventSourceResponse, ServerSentEvent |
| 11 | + |
| 12 | +import pytest |
| 13 | +import pytest_asyncio |
| 14 | + |
| 15 | + |
| 16 | +import aiopenapi3 |
| 17 | + |
| 18 | +app = FastAPI( |
| 19 | + version="1.0.0", |
| 20 | + title="Sequential Streaming tests", |
| 21 | + servers=[{"url": "/", "description": "Default, relative server"}], |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def openapi32(): |
| 26 | + """ |
| 27 | + https://github.com/fastapi/fastapi/discussions/15328#discussioncomment-16543627 |
| 28 | + """ |
| 29 | + if app.openapi_schema: |
| 30 | + return app.openapi_schema |
| 31 | + from fastapi.openapi.utils import get_openapi |
| 32 | + |
| 33 | + openapi_schema = get_openapi( |
| 34 | + openapi_version="3.2.0", title=app.title, version=app.version, routes=app.routes, servers=app.servers |
| 35 | + ) |
| 36 | + app.openapi_schema = openapi_schema |
| 37 | + return app.openapi_schema |
| 38 | + |
| 39 | + |
| 40 | +app.openapi = openapi32 |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture(scope="session") |
| 44 | +def config(unused_tcp_port_factory): |
| 45 | + c = Config() |
| 46 | + c.bind = [f"localhost:{unused_tcp_port_factory()}"] |
| 47 | + return c |
| 48 | + |
| 49 | + |
| 50 | +@pytest_asyncio.fixture(loop_scope="session") |
| 51 | +async def server(config): |
| 52 | + event_loop = asyncio.get_event_loop() |
| 53 | + try: |
| 54 | + sd = asyncio.Event() |
| 55 | + task = event_loop.create_task(serve(app, config, shutdown_trigger=sd.wait)) |
| 56 | + yield config |
| 57 | + finally: |
| 58 | + sd.set() |
| 59 | + await task |
| 60 | + |
| 61 | + |
| 62 | +@pytest_asyncio.fixture(loop_scope="session") |
| 63 | +async def client(server): |
| 64 | + api = await aiopenapi3.OpenAPI.load_async(f"http://{server.bind[0]}/openapi.json") |
| 65 | + return api |
| 66 | + |
| 67 | + |
| 68 | +class Item(pydantic.BaseModel): |
| 69 | + name: str |
| 70 | + description: str | None |
| 71 | + |
| 72 | + |
| 73 | +items = [ |
| 74 | + Item(name="Plumbus", description="A multi-purpose household device."), |
| 75 | + Item(name="Portal Gun", description="A portal opening device."), |
| 76 | + Item(name="Meeseeks Box", description="A box that summons a Meeseeks."), |
| 77 | +] |
| 78 | + |
| 79 | + |
| 80 | +@app.get("/jsonl", operation_id="jsonl") |
| 81 | +async def jsonl() -> AsyncIterable[Item]: |
| 82 | + """ |
| 83 | + https://fastapi.tiangolo.com/tutorial/stream-json-lines/#use-cases |
| 84 | + """ |
| 85 | + for item in items: |
| 86 | + yield item |
| 87 | + |
| 88 | + |
| 89 | +@app.get("/sse", operation_id="sse", response_class=EventSourceResponse) |
| 90 | +async def sse() -> AsyncIterable[ServerSentEvent]: |
| 91 | + for idx, item in enumerate(items): |
| 92 | + yield ServerSentEvent(comment=str(idx), data=item) |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.asyncio(loop_scope="session") |
| 96 | +async def test_jsonl(server, client): |
| 97 | + req = client.createRequest("jsonl") |
| 98 | + async with req.sequence() as sequence: |
| 99 | + async for obj in sequence: |
| 100 | + print(obj) |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.asyncio(loop_scope="session") |
| 104 | +async def test_sse(server, client): |
| 105 | + from aiopenapi3.request import AsyncRequestBase |
| 106 | + |
| 107 | + req: AsyncRequestBase |
| 108 | + req = client.createRequest("sse") |
| 109 | + async with req.sequence() as sequence: |
| 110 | + async for obj in sequence: |
| 111 | + print(obj) |
| 112 | + await asyncio.sleep(1.1) |
0 commit comments