Coverage for tests/test_tutorial/test_response_model/test_tutorial003_01_py310.py: 100%
19 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1import pytest 1deabc
2from dirty_equals import IsDict, IsOneOf 1deabc
3from fastapi.testclient import TestClient 1deabc
5from ...utils import needs_py310 1deabc
8@pytest.fixture(name="client") 1deabc
9def get_client(): 1deabc
10 from docs_src.response_model.tutorial003_01_py310 import app 1abc
12 client = TestClient(app) 1abc
13 return client 1abc
16@needs_py310 1deabc
17def test_post_user(client: TestClient): 1deabc
18 response = client.post( 1abc
19 "/user/",
20 json={
21 "username": "foo",
22 "password": "fighter",
23 "email": "foo@example.com",
24 "full_name": "Grave Dohl",
25 },
26 )
27 assert response.status_code == 200, response.text 1abc
28 assert response.json() == { 1abc
29 "username": "foo",
30 "email": "foo@example.com",
31 "full_name": "Grave Dohl",
32 }
35@needs_py310 1deabc
36def test_openapi_schema(client: TestClient): 1deabc
37 response = client.get("/openapi.json") 1abc
38 assert response.status_code == 200, response.text 1abc
39 assert response.json() == { 1abc
40 "openapi": "3.1.0",
41 "info": {"title": "FastAPI", "version": "0.1.0"},
42 "paths": {
43 "/user/": {
44 "post": {
45 "summary": "Create User",
46 "operationId": "create_user_user__post",
47 "requestBody": {
48 "content": {
49 "application/json": {
50 "schema": {"$ref": "#/components/schemas/UserIn"}
51 }
52 },
53 "required": True,
54 },
55 "responses": {
56 "200": {
57 "description": "Successful Response",
58 "content": {
59 "application/json": {
60 "schema": {"$ref": "#/components/schemas/BaseUser"}
61 }
62 },
63 },
64 "422": {
65 "description": "Validation Error",
66 "content": {
67 "application/json": {
68 "schema": {
69 "$ref": "#/components/schemas/HTTPValidationError"
70 }
71 }
72 },
73 },
74 },
75 }
76 }
77 },
78 "components": {
79 "schemas": {
80 "BaseUser": {
81 "title": "BaseUser",
82 "required": IsOneOf(
83 ["username", "email", "full_name"],
84 # TODO: remove when deprecating Pydantic v1
85 ["username", "email"],
86 ),
87 "type": "object",
88 "properties": {
89 "username": {"title": "Username", "type": "string"},
90 "email": {
91 "title": "Email",
92 "type": "string",
93 "format": "email",
94 },
95 "full_name": IsDict(
96 {
97 "title": "Full Name",
98 "anyOf": [{"type": "string"}, {"type": "null"}],
99 }
100 )
101 | IsDict(
102 # TODO: remove when deprecating Pydantic v1
103 {"title": "Full Name", "type": "string"}
104 ),
105 },
106 },
107 "HTTPValidationError": {
108 "title": "HTTPValidationError",
109 "type": "object",
110 "properties": {
111 "detail": {
112 "title": "Detail",
113 "type": "array",
114 "items": {"$ref": "#/components/schemas/ValidationError"},
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": IsDict(
130 {
131 "title": "Full Name",
132 "anyOf": [{"type": "string"}, {"type": "null"}],
133 }
134 )
135 | IsDict(
136 # TODO: remove when deprecating Pydantic v1
137 {"title": "Full Name", "type": "string"}
138 ),
139 "password": {"title": "Password", "type": "string"},
140 },
141 },
142 "ValidationError": {
143 "title": "ValidationError",
144 "required": ["loc", "msg", "type"],
145 "type": "object",
146 "properties": {
147 "loc": {
148 "title": "Location",
149 "type": "array",
150 "items": {
151 "anyOf": [{"type": "string"}, {"type": "integer"}]
152 },
153 },
154 "msg": {"title": "Message", "type": "string"},
155 "type": {"title": "Error Type", "type": "string"},
156 },
157 },
158 }
159 },
160 }