Coverage for tests / test_tutorial / test_response_model / test_tutorial003.py: 100%
18 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
1import importlib 1abdc
3import pytest 1abdc
4from fastapi.testclient import TestClient 1abdc
5from inline_snapshot import snapshot 1abdc
7from ...utils import needs_py310 1abdc
10@pytest.fixture( 1abdc
11 name="client",
12 params=[
13 pytest.param("tutorial003_py310", marks=needs_py310),
14 ],
15)
16def get_client(request: pytest.FixtureRequest): 1abdc
17 mod = importlib.import_module(f"docs_src.response_model.{request.param}") 1abc
19 client = TestClient(mod.app) 1abc
20 return client 1abc
23def test_post_user(client: TestClient): 1abdc
24 response = client.post( 1efg
25 "/user/",
26 json={
27 "username": "foo",
28 "password": "fighter",
29 "email": "foo@example.com",
30 "full_name": "Grave Dohl",
31 },
32 )
33 assert response.status_code == 200, response.text 1efg
34 assert response.json() == { 1efg
35 "username": "foo",
36 "email": "foo@example.com",
37 "full_name": "Grave Dohl",
38 }
41def test_openapi_schema(client: TestClient): 1abdc
42 response = client.get("/openapi.json") 1hij
43 assert response.status_code == 200, response.text 1hij
44 assert response.json() == snapshot( 1hij
45 {
46 "openapi": "3.1.0",
47 "info": {"title": "FastAPI", "version": "0.1.0"},
48 "paths": {
49 "/user/": {
50 "post": {
51 "responses": {
52 "200": {
53 "description": "Successful Response",
54 "content": {
55 "application/json": {
56 "schema": {
57 "$ref": "#/components/schemas/UserOut"
58 }
59 }
60 },
61 },
62 "422": {
63 "description": "Validation Error",
64 "content": {
65 "application/json": {
66 "schema": {
67 "$ref": "#/components/schemas/HTTPValidationError"
68 }
69 }
70 },
71 },
72 },
73 "summary": "Create User",
74 "operationId": "create_user_user__post",
75 "requestBody": {
76 "content": {
77 "application/json": {
78 "schema": {"$ref": "#/components/schemas/UserIn"}
79 }
80 },
81 "required": True,
82 },
83 }
84 }
85 },
86 "components": {
87 "schemas": {
88 "UserOut": {
89 "title": "UserOut",
90 "required": ["username", "email"],
91 "type": "object",
92 "properties": {
93 "username": {"title": "Username", "type": "string"},
94 "email": {
95 "title": "Email",
96 "type": "string",
97 "format": "email",
98 },
99 "full_name": {
100 "title": "Full Name",
101 "anyOf": [{"type": "string"}, {"type": "null"}],
102 },
103 },
104 },
105 "UserIn": {
106 "title": "UserIn",
107 "required": ["username", "password", "email"],
108 "type": "object",
109 "properties": {
110 "username": {"title": "Username", "type": "string"},
111 "password": {"title": "Password", "type": "string"},
112 "email": {
113 "title": "Email",
114 "type": "string",
115 "format": "email",
116 },
117 "full_name": {
118 "title": "Full Name",
119 "anyOf": [{"type": "string"}, {"type": "null"}],
120 },
121 },
122 },
123 "ValidationError": {
124 "title": "ValidationError",
125 "required": ["loc", "msg", "type"],
126 "type": "object",
127 "properties": {
128 "ctx": {"title": "Context", "type": "object"},
129 "input": {"title": "Input"},
130 "loc": {
131 "title": "Location",
132 "type": "array",
133 "items": {
134 "anyOf": [{"type": "string"}, {"type": "integer"}]
135 },
136 },
137 "msg": {"title": "Message", "type": "string"},
138 "type": {"title": "Error Type", "type": "string"},
139 },
140 },
141 "HTTPValidationError": {
142 "title": "HTTPValidationError",
143 "type": "object",
144 "properties": {
145 "detail": {
146 "title": "Detail",
147 "type": "array",
148 "items": {
149 "$ref": "#/components/schemas/ValidationError"
150 },
151 }
152 },
153 },
154 }
155 },
156 }
157 )