| 1 |
from collections.abc import AsyncIterable, Iterable |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi import FastAPI |
| 5 |
from fastapi.exceptions import ResponseValidationError |
| 6 |
from fastapi.testclient import TestClient |
| 7 |
from pydantic import BaseModel |
| 8 |
|
| 9 |
|
| 10 |
class Item(BaseModel): |
| 11 |
name: str |
| 12 |
price: float |
| 13 |
|
| 14 |
|
| 15 |
app = FastAPI() |
| 16 |
|
| 17 |
|
| 18 |
@app.get("/items/stream-invalid") |
| 19 |
async def stream_items_invalid() -> AsyncIterable[Item]: |
| 20 |
yield {"name": "valid", "price": 1.0} |
| 21 |
yield {"name": "invalid", "price": "not-a-number"} |
| 22 |
|
| 23 |
|
| 24 |
@app.get("/items/stream-invalid-sync") |
| 25 |
def stream_items_invalid_sync() -> Iterable[Item]: |
| 26 |
yield {"name": "valid", "price": 1.0} |
| 27 |
yield {"name": "invalid", "price": "not-a-number"} |
| 28 |
|
| 29 |
|
| 30 |
client = TestClient(app) |
| 31 |
|
| 32 |
|
| 33 |
def test_stream_json_validation_error_async(): |
| 34 |
with pytest.raises(ResponseValidationError): |
| 35 |
client.get("/items/stream-invalid") |
| 36 |
|
| 37 |
|
| 38 |
def test_stream_json_validation_error_sync(): |
| 39 |
with pytest.raises(ResponseValidationError): |
| 40 |
client.get("/items/stream-invalid-sync") |