Coverage for tests/test_tutorial/test_response_model/test_tutorial003_01.py: 100%
18 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 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_01",
14 pytest.param("tutorial003_01_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 "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": {"$ref": "#/components/schemas/BaseUser"}
67 }
68 },
69 },
70 "422": {
71 "description": "Validation Error",
72 "content": {
73 "application/json": {
74 "schema": {
75 "$ref": "#/components/schemas/HTTPValidationError"
76 }
77 }
78 },
79 },
80 },
81 }
82 }
83 },
84 "components": {
85 "schemas": {
86 "BaseUser": {
87 "title": "BaseUser",
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 "HTTPValidationError": {
114 "title": "HTTPValidationError",
115 "type": "object",
116 "properties": {
117 "detail": {
118 "title": "Detail",
119 "type": "array",
120 "items": {"$ref": "#/components/schemas/ValidationError"},
121 }
122 },
123 },
124 "UserIn": {
125 "title": "UserIn",
126 "required": ["username", "email", "password"],
127 "type": "object",
128 "properties": {
129 "username": {"title": "Username", "type": "string"},
130 "email": {
131 "title": "Email",
132 "type": "string",
133 "format": "email",
134 },
135 "full_name": IsDict(
136 {
137 "title": "Full Name",
138 "anyOf": [{"type": "string"}, {"type": "null"}],
139 }
140 )
141 | IsDict(
142 # TODO: remove when deprecating Pydantic v1
143 {"title": "Full Name", "type": "string"}
144 ),
145 "password": {"title": "Password", "type": "string"},
146 },
147 },
148 "ValidationError": {
149 "title": "ValidationError",
150 "required": ["loc", "msg", "type"],
151 "type": "object",
152 "properties": {
153 "loc": {
154 "title": "Location",
155 "type": "array",
156 "items": {
157 "anyOf": [{"type": "string"}, {"type": "integer"}]
158 },
159 },
160 "msg": {"title": "Message", "type": "string"},
161 "type": {"title": "Error Type", "type": "string"},
162 },
163 },
164 }
165 },
166 }