Coverage for tests / test_tutorial / test_response_model / test_tutorial003_01.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_01_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 "summary": "Create User",
52 "operationId": "create_user_user__post",
53 "requestBody": {
54 "content": {
55 "application/json": {
56 "schema": {"$ref": "#/components/schemas/UserIn"}
57 }
58 },
59 "required": True,
60 },
61 "responses": {
62 "200": {
63 "description": "Successful Response",
64 "content": {
65 "application/json": {
66 "schema": {
67 "$ref": "#/components/schemas/BaseUser"
68 }
69 }
70 },
71 },
72 "422": {
73 "description": "Validation Error",
74 "content": {
75 "application/json": {
76 "schema": {
77 "$ref": "#/components/schemas/HTTPValidationError"
78 }
79 }
80 },
81 },
82 },
83 }
84 }
85 },
86 "components": {
87 "schemas": {
88 "BaseUser": {
89 "title": "BaseUser",
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 "HTTPValidationError": {
106 "title": "HTTPValidationError",
107 "type": "object",
108 "properties": {
109 "detail": {
110 "title": "Detail",
111 "type": "array",
112 "items": {
113 "$ref": "#/components/schemas/ValidationError"
114 },
115 }
116 },
117 },
118 "UserIn": {
119 "title": "UserIn",
120 "required": ["username", "email", "password"],
121 "type": "object",
122 "properties": {
123 "username": {"title": "Username", "type": "string"},
124 "email": {
125 "title": "Email",
126 "type": "string",
127 "format": "email",
128 },
129 "full_name": {
130 "title": "Full Name",
131 "anyOf": [{"type": "string"}, {"type": "null"}],
132 },
133 "password": {"title": "Password", "type": "string"},
134 },
135 },
136 "ValidationError": {
137 "title": "ValidationError",
138 "required": ["loc", "msg", "type"],
139 "type": "object",
140 "properties": {
141 "ctx": {"title": "Context", "type": "object"},
142 "input": {"title": "Input"},
143 "loc": {
144 "title": "Location",
145 "type": "array",
146 "items": {
147 "anyOf": [{"type": "string"}, {"type": "integer"}]
148 },
149 },
150 "msg": {"title": "Message", "type": "string"},
151 "type": {"title": "Error Type", "type": "string"},
152 },
153 },
154 }
155 },
156 }
157 )