Coverage for tests / test_get_request_body.py: 100%
21 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1from fastapi import FastAPI 1adbc
2from fastapi.testclient import TestClient 1adbc
3from inline_snapshot import snapshot 1adbc
4from pydantic import BaseModel 1adbc
6app = FastAPI() 1adbc
9class Product(BaseModel): 1adbc
10 name: str 1abc
11 description: str = None # type: ignore 1adbc
12 price: float 1abc
15@app.get("/product") 1adbc
16async def create_item(product: Product): 1adbc
17 return product 1efgh
20client = TestClient(app) 1adbc
23def test_get_with_body(): 1adbc
24 body = {"name": "Foo", "description": "Some description", "price": 5.5} 1efgh
25 response = client.request("GET", "/product", json=body) 1efgh
26 assert response.json() == body 1efgh
29def test_openapi_schema(): 1adbc
30 response = client.get("/openapi.json") 1ijkl
31 assert response.status_code == 200, response.text 1ijkl
32 assert response.json() == snapshot( 1ijkl
33 {
34 "openapi": "3.1.0",
35 "info": {"title": "FastAPI", "version": "0.1.0"},
36 "paths": {
37 "/product": {
38 "get": {
39 "summary": "Create Item",
40 "operationId": "create_item_product_get",
41 "requestBody": {
42 "content": {
43 "application/json": {
44 "schema": {"$ref": "#/components/schemas/Product"}
45 }
46 },
47 "required": True,
48 },
49 "responses": {
50 "200": {
51 "description": "Successful Response",
52 "content": {"application/json": {"schema": {}}},
53 },
54 "422": {
55 "description": "Validation Error",
56 "content": {
57 "application/json": {
58 "schema": {
59 "$ref": "#/components/schemas/HTTPValidationError"
60 }
61 }
62 },
63 },
64 },
65 }
66 }
67 },
68 "components": {
69 "schemas": {
70 "HTTPValidationError": {
71 "title": "HTTPValidationError",
72 "type": "object",
73 "properties": {
74 "detail": {
75 "title": "Detail",
76 "type": "array",
77 "items": {
78 "$ref": "#/components/schemas/ValidationError"
79 },
80 }
81 },
82 },
83 "Product": {
84 "title": "Product",
85 "required": ["name", "price"],
86 "type": "object",
87 "properties": {
88 "name": {"title": "Name", "type": "string"},
89 "description": {"title": "Description", "type": "string"},
90 "price": {"title": "Price", "type": "number"},
91 },
92 },
93 "ValidationError": {
94 "title": "ValidationError",
95 "required": ["loc", "msg", "type"],
96 "type": "object",
97 "properties": {
98 "loc": {
99 "title": "Location",
100 "type": "array",
101 "items": {
102 "anyOf": [{"type": "string"}, {"type": "integer"}]
103 },
104 },
105 "msg": {"title": "Message", "type": "string"},
106 "type": {"title": "Error Type", "type": "string"},
107 "input": {"title": "Input"},
108 "ctx": {"title": "Context", "type": "object"},
109 },
110 },
111 }
112 },
113 }
114 )