Coverage for tests/test_tutorial/test_fastapi/test_read_one/test_tutorial001.py: 100%
30 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
1from dirty_equals import IsDict 1ghijkl
2from fastapi.testclient import TestClient 1ghijkl
3from sqlmodel import create_engine 1ghijkl
4from sqlmodel.pool import StaticPool 1ghijkl
7def test_tutorial(clear_sqlmodel): 1ghijkl
8 from docs_src.tutorial.fastapi.read_one import tutorial001 as mod 1fabcde
10 mod.sqlite_url = "sqlite://" 1fabcde
11 mod.engine = create_engine( 1fabcde
12 mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
13 )
15 with TestClient(mod.app) as client: 1fabcde
16 hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1fabcde
17 hero2_data = { 1abcde
18 "name": "Spider-Boy",
19 "secret_name": "Pedro Parqueador",
20 "id": 9000,
21 }
22 response = client.post("/heroes/", json=hero1_data) 1fabcde
23 assert response.status_code == 200, response.text 1fabcde
24 response = client.post("/heroes/", json=hero2_data) 1fabcde
25 assert response.status_code == 200, response.text 1fabcde
26 hero2 = response.json() 1fabcde
27 response = client.get("/heroes/") 1fabcde
28 assert response.status_code == 200, response.text 1fabcde
29 data = response.json() 1fabcde
30 assert len(data) == 2 1fabcde
32 hero_id = hero2["id"] 1fabcde
33 response = client.get(f"/heroes/{hero_id}") 1fabcde
34 assert response.status_code == 200, response.text 1fabcde
35 data = response.json() 1fabcde
36 assert data == hero2 1fabcde
38 response = client.get("/heroes/9000") 1fabcde
39 assert response.status_code == 404, response.text 1fabcde
41 response = client.get("/openapi.json") 1fabcde
43 assert response.status_code == 200, response.text 1fabcde
45 assert response.json() == { 1fabcde
46 "openapi": "3.1.0",
47 "info": {"title": "FastAPI", "version": "0.1.0"},
48 "paths": {
49 "/heroes/": {
50 "get": {
51 "summary": "Read Heroes",
52 "operationId": "read_heroes_heroes__get",
53 "responses": {
54 "200": {
55 "description": "Successful Response",
56 "content": {
57 "application/json": {
58 "schema": {
59 "title": "Response Read Heroes Heroes Get",
60 "type": "array",
61 "items": {
62 "$ref": "#/components/schemas/HeroPublic"
63 },
64 }
65 }
66 },
67 }
68 },
69 },
70 "post": {
71 "summary": "Create Hero",
72 "operationId": "create_hero_heroes__post",
73 "requestBody": {
74 "content": {
75 "application/json": {
76 "schema": {
77 "$ref": "#/components/schemas/HeroCreate"
78 }
79 }
80 },
81 "required": True,
82 },
83 "responses": {
84 "200": {
85 "description": "Successful Response",
86 "content": {
87 "application/json": {
88 "schema": {
89 "$ref": "#/components/schemas/HeroPublic"
90 }
91 }
92 },
93 },
94 "422": {
95 "description": "Validation Error",
96 "content": {
97 "application/json": {
98 "schema": {
99 "$ref": "#/components/schemas/HTTPValidationError"
100 }
101 }
102 },
103 },
104 },
105 },
106 },
107 "/heroes/{hero_id}": {
108 "get": {
109 "summary": "Read Hero",
110 "operationId": "read_hero_heroes__hero_id__get",
111 "parameters": [
112 {
113 "required": True,
114 "schema": {"title": "Hero Id", "type": "integer"},
115 "name": "hero_id",
116 "in": "path",
117 }
118 ],
119 "responses": {
120 "200": {
121 "description": "Successful Response",
122 "content": {
123 "application/json": {
124 "schema": {
125 "$ref": "#/components/schemas/HeroPublic"
126 }
127 }
128 },
129 },
130 "422": {
131 "description": "Validation Error",
132 "content": {
133 "application/json": {
134 "schema": {
135 "$ref": "#/components/schemas/HTTPValidationError"
136 }
137 }
138 },
139 },
140 },
141 }
142 },
143 },
144 "components": {
145 "schemas": {
146 "HTTPValidationError": {
147 "title": "HTTPValidationError",
148 "type": "object",
149 "properties": {
150 "detail": {
151 "title": "Detail",
152 "type": "array",
153 "items": {
154 "$ref": "#/components/schemas/ValidationError"
155 },
156 }
157 },
158 },
159 "HeroCreate": {
160 "title": "HeroCreate",
161 "required": ["name", "secret_name"],
162 "type": "object",
163 "properties": {
164 "name": {"title": "Name", "type": "string"},
165 "secret_name": {"title": "Secret Name", "type": "string"},
166 "age": IsDict(
167 {
168 "title": "Age",
169 "anyOf": [{"type": "integer"}, {"type": "null"}],
170 }
171 )
172 | IsDict(
173 # TODO: remove when deprecating Pydantic v1
174 {"title": "Age", "type": "integer"}
175 ),
176 },
177 },
178 "HeroPublic": {
179 "title": "HeroPublic",
180 "required": ["name", "secret_name", "id"],
181 "type": "object",
182 "properties": {
183 "name": {"title": "Name", "type": "string"},
184 "secret_name": {"title": "Secret Name", "type": "string"},
185 "age": IsDict(
186 {
187 "title": "Age",
188 "anyOf": [{"type": "integer"}, {"type": "null"}],
189 }
190 )
191 | IsDict(
192 # TODO: remove when deprecating Pydantic v1
193 {"title": "Age", "type": "integer"}
194 ),
195 "id": {"title": "Id", "type": "integer"},
196 },
197 },
198 "ValidationError": {
199 "title": "ValidationError",
200 "required": ["loc", "msg", "type"],
201 "type": "object",
202 "properties": {
203 "loc": {
204 "title": "Location",
205 "type": "array",
206 "items": {
207 "anyOf": [{"type": "string"}, {"type": "integer"}]
208 },
209 },
210 "msg": {"title": "Message", "type": "string"},
211 "type": {"title": "Error Type", "type": "string"},
212 },
213 },
214 }
215 },
216 }