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