Coverage for tests / test_tutorial / test_fastapi / test_response_model / test_tutorial001.py: 100%
30 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-11-09 00:02 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-11-09 00:02 +0000
1import importlib 1jklmnopqr
2from types import ModuleType 1jklmnopqr
4import pytest 1jklmnopqr
5from dirty_equals import IsOneOf 1jklmnopqr
6from fastapi.testclient import TestClient 1jklmnopqr
7from sqlalchemy import Engine 1jklmnopqr
10@pytest.fixture( 1jklmnopqr
11 name="module",
12 params=[
13 pytest.param("tutorial001_py310"),
14 ],
15)
16def get_module(request: pytest.FixtureRequest, database_engine: Engine) -> ModuleType: 1jklmnopqr
17 mod = importlib.import_module( 1jklmnopqr
18 f"docs_src.tutorial.fastapi.response_model.{request.param}"
19 )
20 mod.engine = database_engine 1jklmnopqr
21 return mod 1jklmnopqr
24def test_tutorial(module: ModuleType): 1jklmnopqr
25 with TestClient(module.app) as client: 1abcdefghi
26 hero_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1abcdefghi
27 response = client.post("/heroes/", json=hero_data) 1abcdefghi
28 data = response.json() 1abcdefghi
30 assert response.status_code == 200, response.text 1abcdefghi
31 assert data["name"] == hero_data["name"] 1abcdefghi
32 assert data["secret_name"] == hero_data["secret_name"] 1abcdefghi
33 assert data["id"] is not None 1abcdefghi
34 assert data["age"] is None 1abcdefghi
36 response = client.get("/heroes/") 1abcdefghi
37 data = response.json() 1abcdefghi
39 assert response.status_code == 200, response.text 1abcdefghi
40 assert len(data) == 1 1abcdefghi
41 assert data[0]["name"] == hero_data["name"] 1abcdefghi
42 assert data[0]["secret_name"] == hero_data["secret_name"] 1abcdefghi
44 response = client.get("/openapi.json") 1abcdefghi
46 assert response.status_code == 200, response.text 1abcdefghi
48 assert response.json() == { 1abcdefghi
49 "openapi": "3.1.0",
50 "info": {"title": "FastAPI", "version": "0.1.0"},
51 "paths": {
52 "/heroes/": {
53 "get": {
54 "summary": "Read Heroes",
55 "operationId": "read_heroes_heroes__get",
56 "responses": {
57 "200": {
58 "description": "Successful Response",
59 "content": {
60 "application/json": {
61 "schema": {
62 "title": "Response Read Heroes Heroes Get",
63 "type": "array",
64 "items": {
65 "$ref": "#/components/schemas/Hero"
66 },
67 }
68 }
69 },
70 }
71 },
72 },
73 "post": {
74 "summary": "Create Hero",
75 "operationId": "create_hero_heroes__post",
76 "requestBody": {
77 "content": {
78 "application/json": {
79 "schema": {"$ref": "#/components/schemas/Hero"}
80 }
81 },
82 "required": True,
83 },
84 "responses": {
85 "200": {
86 "description": "Successful Response",
87 "content": {
88 "application/json": {
89 "schema": {"$ref": "#/components/schemas/Hero"}
90 }
91 },
92 },
93 "422": {
94 "description": "Validation Error",
95 "content": {
96 "application/json": {
97 "schema": {
98 "$ref": "#/components/schemas/HTTPValidationError"
99 }
100 }
101 },
102 },
103 },
104 },
105 }
106 },
107 "components": {
108 "schemas": {
109 "HTTPValidationError": {
110 "title": "HTTPValidationError",
111 "type": "object",
112 "properties": {
113 "detail": {
114 "title": "Detail",
115 "type": "array",
116 "items": {
117 "$ref": "#/components/schemas/ValidationError"
118 },
119 }
120 },
121 },
122 "Hero": {
123 "title": "Hero",
124 "required": ["name", "secret_name"],
125 "type": "object",
126 "properties": {
127 "id": {
128 "title": "Id",
129 "anyOf": [{"type": "integer"}, {"type": "null"}],
130 },
131 "name": {"title": "Name", "type": "string"},
132 "secret_name": {"title": "Secret Name", "type": "string"},
133 "age": {
134 "title": "Age",
135 "anyOf": [{"type": "integer"}, {"type": "null"}],
136 },
137 },
138 },
139 "ValidationError": {
140 "title": "ValidationError",
141 "required": ["loc", "msg", "type"],
142 "type": "object",
143 "properties": IsOneOf(
144 {
145 "loc": {
146 "title": "Location",
147 "type": "array",
148 "items": {
149 "anyOf": [
150 {"type": "string"},
151 {"type": "integer"},
152 ]
153 },
154 },
155 "msg": {"title": "Message", "type": "string"},
156 "type": {"title": "Error Type", "type": "string"},
157 },
158 {
159 "loc": {
160 "title": "Location",
161 "type": "array",
162 "items": {
163 "anyOf": [
164 {"type": "string"},
165 {"type": "integer"},
166 ]
167 },
168 },
169 "msg": {"title": "Message", "type": "string"},
170 "type": {"title": "Error Type", "type": "string"},
171 "ctx": {"title": "Context", "type": "object"},
172 "input": {"title": "Input"},
173 },
174 ),
175 },
176 }
177 },
178 }