Coverage for tests/test_tutorial/test_dependencies/test_tutorial001.py: 100%
19 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 1abcdef
5from fastapi.testclient import TestClient 1abcdef
7from ...utils import needs_py39, needs_py310 1abcdef
10@pytest.fixture( 1abcdef
11 name="client",
12 params=[
13 "tutorial001",
14 pytest.param("tutorial001_py310", marks=needs_py310),
15 "tutorial001_an",
16 pytest.param("tutorial001_an_py39", marks=needs_py39),
17 pytest.param("tutorial001_an_py310", marks=needs_py310),
18 ],
19)
20def get_client(request: pytest.FixtureRequest): 1abcdef
21 mod = importlib.import_module(f"docs_src.dependencies.{request.param}") 1abcdef
23 client = TestClient(mod.app) 1abcdef
24 return client 1abcdef
27@pytest.mark.parametrize( 1abcdef
28 "path,expected_status,expected_response",
29 [
30 ("/items", 200, {"q": None, "skip": 0, "limit": 100}),
31 ("/items?q=foo", 200, {"q": "foo", "skip": 0, "limit": 100}),
32 ("/items?q=foo&skip=5", 200, {"q": "foo", "skip": 5, "limit": 100}),
33 ("/items?q=foo&skip=5&limit=30", 200, {"q": "foo", "skip": 5, "limit": 30}),
34 ("/users", 200, {"q": None, "skip": 0, "limit": 100}),
35 ],
36)
37def test_get(path, expected_status, expected_response, client: TestClient): 1abcdef
38 response = client.get(path) 1ghijkl
39 assert response.status_code == expected_status 1ghijkl
40 assert response.json() == expected_response 1ghijkl
43def test_openapi_schema(client: TestClient): 1abcdef
44 response = client.get("/openapi.json") 1mnopqr
45 assert response.status_code == 200, response.text 1mnopqr
46 assert response.json() == { 1mnopqr
47 "openapi": "3.1.0",
48 "info": {"title": "FastAPI", "version": "0.1.0"},
49 "paths": {
50 "/items/": {
51 "get": {
52 "responses": {
53 "200": {
54 "description": "Successful Response",
55 "content": {"application/json": {"schema": {}}},
56 },
57 "422": {
58 "description": "Validation Error",
59 "content": {
60 "application/json": {
61 "schema": {
62 "$ref": "#/components/schemas/HTTPValidationError"
63 }
64 }
65 },
66 },
67 },
68 "summary": "Read Items",
69 "operationId": "read_items_items__get",
70 "parameters": [
71 {
72 "required": False,
73 "schema": IsDict(
74 {
75 "anyOf": [{"type": "string"}, {"type": "null"}],
76 "title": "Q",
77 }
78 )
79 | IsDict(
80 # TODO: remove when deprecating Pydantic v1
81 {"title": "Q", "type": "string"}
82 ),
83 "name": "q",
84 "in": "query",
85 },
86 {
87 "required": False,
88 "schema": {
89 "title": "Skip",
90 "type": "integer",
91 "default": 0,
92 },
93 "name": "skip",
94 "in": "query",
95 },
96 {
97 "required": False,
98 "schema": {
99 "title": "Limit",
100 "type": "integer",
101 "default": 100,
102 },
103 "name": "limit",
104 "in": "query",
105 },
106 ],
107 }
108 },
109 "/users/": {
110 "get": {
111 "responses": {
112 "200": {
113 "description": "Successful Response",
114 "content": {"application/json": {"schema": {}}},
115 },
116 "422": {
117 "description": "Validation Error",
118 "content": {
119 "application/json": {
120 "schema": {
121 "$ref": "#/components/schemas/HTTPValidationError"
122 }
123 }
124 },
125 },
126 },
127 "summary": "Read Users",
128 "operationId": "read_users_users__get",
129 "parameters": [
130 {
131 "required": False,
132 "schema": IsDict(
133 {
134 "anyOf": [{"type": "string"}, {"type": "null"}],
135 "title": "Q",
136 }
137 )
138 | IsDict(
139 # TODO: remove when deprecating Pydantic v1
140 {"title": "Q", "type": "string"}
141 ),
142 "name": "q",
143 "in": "query",
144 },
145 {
146 "required": False,
147 "schema": {
148 "title": "Skip",
149 "type": "integer",
150 "default": 0,
151 },
152 "name": "skip",
153 "in": "query",
154 },
155 {
156 "required": False,
157 "schema": {
158 "title": "Limit",
159 "type": "integer",
160 "default": 100,
161 },
162 "name": "limit",
163 "in": "query",
164 },
165 ],
166 }
167 },
168 },
169 "components": {
170 "schemas": {
171 "ValidationError": {
172 "title": "ValidationError",
173 "required": ["loc", "msg", "type"],
174 "type": "object",
175 "properties": {
176 "loc": {
177 "title": "Location",
178 "type": "array",
179 "items": {
180 "anyOf": [{"type": "string"}, {"type": "integer"}]
181 },
182 },
183 "msg": {"title": "Message", "type": "string"},
184 "type": {"title": "Error Type", "type": "string"},
185 },
186 },
187 "HTTPValidationError": {
188 "title": "HTTPValidationError",
189 "type": "object",
190 "properties": {
191 "detail": {
192 "title": "Detail",
193 "type": "array",
194 "items": {"$ref": "#/components/schemas/ValidationError"},
195 }
196 },
197 },
198 }
199 },
200 }