Coverage for tests/test_tutorial/test_response_model/test_tutorial003.py: 100%
18 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-09-22 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-09-22 00:03 +0000
1import importlib 1abcdef
3import pytest 1abcdef
4from dirty_equals import IsDict, IsOneOf 1abcdef
5from fastapi.testclient import TestClient 1abcdef
7from ...utils import needs_py310 1abcdef
10@pytest.fixture( 1abcdef
11 name="client",
12 params=[
13 "tutorial003",
14 pytest.param("tutorial003_py310", marks=needs_py310),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abcdef
18 mod = importlib.import_module(f"docs_src.response_model.{request.param}") 1abcdef
20 client = TestClient(mod.app) 1abcdef
21 return client 1abcdef
24def test_post_user(client: TestClient): 1abcdef
25 response = client.post( 1ghijkl
26 "/user/",
27 json={
28 "username": "foo",
29 "password": "fighter",
30 "email": "foo@example.com",
31 "full_name": "Grave Dohl",
32 },
33 )
34 assert response.status_code == 200, response.text 1ghijkl
35 assert response.json() == { 1ghijkl
36 "username": "foo",
37 "email": "foo@example.com",
38 "full_name": "Grave Dohl",
39 }
42def test_openapi_schema(client: TestClient): 1abcdef
43 response = client.get("/openapi.json") 1mnopqr
44 assert response.status_code == 200, response.text 1mnopqr
45 assert response.json() == { 1mnopqr
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": {"$ref": "#/components/schemas/UserOut"}
57 }
58 },
59 },
60 "422": {
61 "description": "Validation Error",
62 "content": {
63 "application/json": {
64 "schema": {
65 "$ref": "#/components/schemas/HTTPValidationError"
66 }
67 }
68 },
69 },
70 },
71 "summary": "Create User",
72 "operationId": "create_user_user__post",
73 "requestBody": {
74 "content": {
75 "application/json": {
76 "schema": {"$ref": "#/components/schemas/UserIn"}
77 }
78 },
79 "required": True,
80 },
81 }
82 }
83 },
84 "components": {
85 "schemas": {
86 "UserOut": {
87 "title": "UserOut",
88 "required": IsOneOf(
89 ["username", "email", "full_name"],
90 # TODO: remove when deprecating Pydantic v1
91 ["username", "email"],
92 ),
93 "type": "object",
94 "properties": {
95 "username": {"title": "Username", "type": "string"},
96 "email": {
97 "title": "Email",
98 "type": "string",
99 "format": "email",
100 },
101 "full_name": IsDict(
102 {
103 "title": "Full Name",
104 "anyOf": [{"type": "string"}, {"type": "null"}],
105 }
106 )
107 | IsDict(
108 # TODO: remove when deprecating Pydantic v1
109 {"title": "Full Name", "type": "string"}
110 ),
111 },
112 },
113 "UserIn": {
114 "title": "UserIn",
115 "required": ["username", "password", "email"],
116 "type": "object",
117 "properties": {
118 "username": {"title": "Username", "type": "string"},
119 "password": {"title": "Password", "type": "string"},
120 "email": {
121 "title": "Email",
122 "type": "string",
123 "format": "email",
124 },
125 "full_name": IsDict(
126 {
127 "title": "Full Name",
128 "anyOf": [{"type": "string"}, {"type": "null"}],
129 }
130 )
131 | IsDict(
132 # TODO: remove when deprecating Pydantic v1
133 {"title": "Full Name", "type": "string"}
134 ),
135 },
136 },
137 "ValidationError": {
138 "title": "ValidationError",
139 "required": ["loc", "msg", "type"],
140 "type": "object",
141 "properties": {
142 "loc": {
143 "title": "Location",
144 "type": "array",
145 "items": {
146 "anyOf": [{"type": "string"}, {"type": "integer"}]
147 },
148 },
149 "msg": {"title": "Message", "type": "string"},
150 "type": {"title": "Error Type", "type": "string"},
151 },
152 },
153 "HTTPValidationError": {
154 "title": "HTTPValidationError",
155 "type": "object",
156 "properties": {
157 "detail": {
158 "title": "Detail",
159 "type": "array",
160 "items": {"$ref": "#/components/schemas/ValidationError"},
161 }
162 },
163 },
164 }
165 },
166 }