| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import uuid |
| 4 |
from dataclasses import dataclass, field |
| 5 |
|
| 6 |
from dirty_equals import IsUUID |
| 7 |
from fastapi import FastAPI |
| 8 |
from fastapi.testclient import TestClient |
| 9 |
from inline_snapshot import snapshot |
| 10 |
|
| 11 |
|
| 12 |
@dataclass |
| 13 |
class Item: |
| 14 |
id: uuid.UUID |
| 15 |
name: str |
| 16 |
price: float |
| 17 |
tags: list[str] = field(default_factory=list) |
| 18 |
description: str | None = None |
| 19 |
tax: float | None = None |
| 20 |
|
| 21 |
|
| 22 |
app = FastAPI() |
| 23 |
|
| 24 |
|
| 25 |
@app.get("/item", response_model=Item) |
| 26 |
async def read_item(): |
| 27 |
return { |
| 28 |
"id": uuid.uuid4(), |
| 29 |
"name": "Island In The Moon", |
| 30 |
"price": 12.99, |
| 31 |
"description": "A place to be playin' and havin' fun", |
| 32 |
"tags": ["breater"], |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
client = TestClient(app) |
| 37 |
|
| 38 |
|
| 39 |
def test_annotations(): |
| 40 |
response = client.get("/item") |
| 41 |
assert response.status_code == 200, response.text |
| 42 |
assert response.json() == snapshot( |
| 43 |
{ |
| 44 |
"id": IsUUID(), |
| 45 |
"name": "Island In The Moon", |
| 46 |
"price": 12.99, |
| 47 |
"tags": ["breater"], |
| 48 |
"description": "A place to be playin' and havin' fun", |
| 49 |
"tax": None, |
| 50 |
} |
| 51 |
) |