From 212f6cd522f5f8ffaaa44d28819c8d063f51eddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Fri, 27 Feb 2026 15:22:36 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20tests=20to=20increase=20cover?= =?UTF-8?q?age?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_stream_bare_type.py | 44 ++++++++++++++++++++++ tests/test_stream_json_validation_error.py | 40 ++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 tests/test_stream_bare_type.py create mode 100644 tests/test_stream_json_validation_error.py diff --git a/tests/test_stream_bare_type.py b/tests/test_stream_bare_type.py new file mode 100644 index 0000000000..85b4655345 --- /dev/null +++ b/tests/test_stream_bare_type.py @@ -0,0 +1,44 @@ +import json +from collections.abc import AsyncIterable, Iterable + +from fastapi import FastAPI +from fastapi.testclient import TestClient +from pydantic import BaseModel + + +class Item(BaseModel): + name: str + + +app = FastAPI() + + +@app.get("/items/stream-bare-async") +async def stream_bare_async() -> AsyncIterable: + yield {"name": "foo"} + + +@app.get("/items/stream-bare-sync") +def stream_bare_sync() -> Iterable: + yield {"name": "bar"} + + +client = TestClient(app) + + +def test_stream_bare_async_iterable(): + """Test that bare AsyncIterable (no type args) works and streams JSONL.""" + response = client.get("/items/stream-bare-async") + assert response.status_code == 200 + assert response.headers["content-type"] == "application/jsonl" + lines = [json.loads(line) for line in response.text.strip().splitlines()] + assert lines == [{"name": "foo"}] + + +def test_stream_bare_sync_iterable(): + """Test that bare Iterable (no type args) works and streams JSONL.""" + response = client.get("/items/stream-bare-sync") + assert response.status_code == 200 + assert response.headers["content-type"] == "application/jsonl" + lines = [json.loads(line) for line in response.text.strip().splitlines()] + assert lines == [{"name": "bar"}] diff --git a/tests/test_stream_json_validation_error.py b/tests/test_stream_json_validation_error.py new file mode 100644 index 0000000000..f60b4a6abe --- /dev/null +++ b/tests/test_stream_json_validation_error.py @@ -0,0 +1,40 @@ +from collections.abc import AsyncIterable, Iterable + +import pytest +from fastapi import FastAPI +from fastapi.exceptions import ResponseValidationError +from fastapi.testclient import TestClient +from pydantic import BaseModel + + +class Item(BaseModel): + name: str + price: float + + +app = FastAPI() + + +@app.get("/items/stream-invalid") +async def stream_items_invalid() -> AsyncIterable[Item]: + yield {"name": "valid", "price": 1.0} + yield {"name": "invalid", "price": "not-a-number"} + + +@app.get("/items/stream-invalid-sync") +def stream_items_invalid_sync() -> Iterable[Item]: + yield {"name": "valid", "price": 1.0} + yield {"name": "invalid", "price": "not-a-number"} + + +client = TestClient(app) + + +def test_stream_json_validation_error_async(): + with pytest.raises(ResponseValidationError): + client.get("/items/stream-invalid") + + +def test_stream_json_validation_error_sync(): + with pytest.raises(ResponseValidationError): + client.get("/items/stream-invalid-sync")