Coverage for tests / test_tutorial / test_dependencies / test_tutorial002_tutorial003_tutorial004.py: 100%
19 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
3import pytest 1abdc
4from fastapi.testclient import TestClient 1abdc
5from inline_snapshot import snapshot 1abdc
7from ...utils import needs_py310 1abdc
10@pytest.fixture( 1abdc
11 name="client",
12 params=[
13 pytest.param("tutorial002_py310", marks=needs_py310),
14 pytest.param("tutorial002_an_py310", marks=needs_py310),
15 pytest.param("tutorial003_py310", marks=needs_py310),
16 pytest.param("tutorial003_an_py310", marks=needs_py310),
17 pytest.param("tutorial004_py310", marks=needs_py310),
18 pytest.param("tutorial004_an_py310", marks=needs_py310),
19 ],
20)
21def get_client(request: pytest.FixtureRequest): 1abdc
22 mod = importlib.import_module(f"docs_src.dependencies.{request.param}") 1abc
24 client = TestClient(mod.app) 1abc
25 return client 1abc
28@pytest.mark.parametrize( 1abdc
29 "path,expected_status,expected_response",
30 [
31 (
32 "/items",
33 200,
34 {
35 "items": [
36 {"item_name": "Foo"},
37 {"item_name": "Bar"},
38 {"item_name": "Baz"},
39 ]
40 },
41 ),
42 (
43 "/items?q=foo",
44 200,
45 {
46 "items": [
47 {"item_name": "Foo"},
48 {"item_name": "Bar"},
49 {"item_name": "Baz"},
50 ],
51 "q": "foo",
52 },
53 ),
54 (
55 "/items?q=foo&skip=1",
56 200,
57 {"items": [{"item_name": "Bar"}, {"item_name": "Baz"}], "q": "foo"},
58 ),
59 (
60 "/items?q=bar&limit=2",
61 200,
62 {"items": [{"item_name": "Foo"}, {"item_name": "Bar"}], "q": "bar"},
63 ),
64 (
65 "/items?q=bar&skip=1&limit=1",
66 200,
67 {"items": [{"item_name": "Bar"}], "q": "bar"},
68 ),
69 (
70 "/items?limit=1&q=bar&skip=1",
71 200,
72 {"items": [{"item_name": "Bar"}], "q": "bar"},
73 ),
74 ],
75)
76def test_get(path, expected_status, expected_response, client: TestClient): 1abdc
77 response = client.get(path) 1efg
78 assert response.status_code == expected_status 1efg
79 assert response.json() == expected_response 1efg
82def test_openapi_schema(client: TestClient): 1abdc
83 response = client.get("/openapi.json") 1hij
84 assert response.status_code == 200, response.text 1hij
85 assert response.json() == snapshot( 1hij
86 {
87 "openapi": "3.1.0",
88 "info": {"title": "FastAPI", "version": "0.1.0"},
89 "paths": {
90 "/items/": {
91 "get": {
92 "responses": {
93 "200": {
94 "description": "Successful Response",
95 "content": {"application/json": {"schema": {}}},
96 },
97 "422": {
98 "description": "Validation Error",
99 "content": {
100 "application/json": {
101 "schema": {
102 "$ref": "#/components/schemas/HTTPValidationError"
103 }
104 }
105 },
106 },
107 },
108 "summary": "Read Items",
109 "operationId": "read_items_items__get",
110 "parameters": [
111 {
112 "required": False,
113 "schema": {
114 "anyOf": [{"type": "string"}, {"type": "null"}],
115 "title": "Q",
116 },
117 "name": "q",
118 "in": "query",
119 },
120 {
121 "required": False,
122 "schema": {
123 "title": "Skip",
124 "type": "integer",
125 "default": 0,
126 },
127 "name": "skip",
128 "in": "query",
129 },
130 {
131 "required": False,
132 "schema": {
133 "title": "Limit",
134 "type": "integer",
135 "default": 100,
136 },
137 "name": "limit",
138 "in": "query",
139 },
140 ],
141 }
142 }
143 },
144 "components": {
145 "schemas": {
146 "ValidationError": {
147 "title": "ValidationError",
148 "required": ["loc", "msg", "type"],
149 "type": "object",
150 "properties": {
151 "loc": {
152 "title": "Location",
153 "type": "array",
154 "items": {
155 "anyOf": [{"type": "string"}, {"type": "integer"}]
156 },
157 },
158 "msg": {"title": "Message", "type": "string"},
159 "type": {"title": "Error Type", "type": "string"},
160 "input": {"title": "Input"},
161 "ctx": {"title": "Context", "type": "object"},
162 },
163 },
164 "HTTPValidationError": {
165 "title": "HTTPValidationError",
166 "type": "object",
167 "properties": {
168 "detail": {
169 "title": "Detail",
170 "type": "array",
171 "items": {
172 "$ref": "#/components/schemas/ValidationError"
173 },
174 }
175 },
176 },
177 }
178 },
179 }
180 )