Coverage for tests / test_get_request_body.py: 100%
21 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1from fastapi import FastAPI 1abcd
2from fastapi.testclient import TestClient 1abcd
3from inline_snapshot import snapshot 1abcd
4from pydantic import BaseModel 1abcd
6app = FastAPI() 1abcd
9class Product(BaseModel): 1abcd
10 name: str 1abcd
11 description: str = None # type: ignore 1abcd
12 price: float 1abcd
15@app.get("/product") 1abcd
16async def create_item(product: Product): 1abcd
17 return product 1efg
20client = TestClient(app) 1abcd
23def test_get_with_body(): 1abcd
24 body = {"name": "Foo", "description": "Some description", "price": 5.5} 1efg
25 response = client.request("GET", "/product", json=body) 1efg
26 assert response.json() == body 1efg
29def test_openapi_schema(): 1abcd
30 response = client.get("/openapi.json") 1hij
31 assert response.status_code == 200, response.text 1hij
32 assert response.json() == snapshot( 1hij
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 )