Coverage for tests / test_tutorial / test_path_params_numeric_validations / test_tutorial004.py: 100%
29 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
8@pytest.fixture( 1abdc
9 name="client",
10 params=[
11 pytest.param("tutorial004_py310"),
12 pytest.param("tutorial004_an_py310"),
13 ],
14)
15def get_client(request: pytest.FixtureRequest) -> TestClient: 1abdc
16 mod = importlib.import_module( 1abc
17 f"docs_src.path_params_numeric_validations.{request.param}"
18 )
19 return TestClient(mod.app) 1abc
22@pytest.mark.parametrize( 1abdc
23 "path,expected_response",
24 [
25 ("/items/42?q=", {"item_id": 42}),
26 ("/items/1?q=somequery", {"item_id": 1, "q": "somequery"}),
27 ],
28)
29def test_read_items(client: TestClient, path, expected_response): 1abdc
30 response = client.get(path) 1efg
31 assert response.status_code == 200, response.text 1efg
32 assert response.json() == expected_response 1efg
35def test_read_items_non_int_item_id(client: TestClient): 1abdc
36 response = client.get("/items/invalid_id?q=somequery") 1hij
37 assert response.status_code == 422, response.text 1hij
38 assert response.json() == { 1hij
39 "detail": [
40 {
41 "loc": ["path", "item_id"],
42 "input": "invalid_id",
43 "msg": "Input should be a valid integer, unable to parse string as an integer",
44 "type": "int_parsing",
45 }
46 ]
47 }
50def test_read_items_item_id_less_than_one(client: TestClient): 1abdc
51 response = client.get("/items/0?q=somequery") 1klm
52 assert response.status_code == 422, response.text 1klm
53 assert response.json() == { 1klm
54 "detail": [
55 {
56 "loc": ["path", "item_id"],
57 "input": "0",
58 "msg": "Input should be greater than or equal to 1",
59 "type": "greater_than_equal",
60 "ctx": {"ge": 1},
61 }
62 ]
63 }
66def test_read_items_missing_q(client: TestClient): 1abdc
67 response = client.get("/items/42") 1nop
68 assert response.status_code == 422, response.text 1nop
69 assert response.json() == { 1nop
70 "detail": [
71 {
72 "loc": ["query", "q"],
73 "input": None,
74 "msg": "Field required",
75 "type": "missing",
76 }
77 ]
78 }
81def test_openapi_schema(client: TestClient): 1abdc
82 response = client.get("/openapi.json") 1qrs
83 assert response.status_code == 200, response.text 1qrs
84 assert response.json() == snapshot( 1qrs
85 {
86 "openapi": "3.1.0",
87 "info": {"title": "FastAPI", "version": "0.1.0"},
88 "paths": {
89 "/items/{item_id}": {
90 "get": {
91 "summary": "Read Items",
92 "operationId": "read_items_items__item_id__get",
93 "parameters": [
94 {
95 "required": True,
96 "schema": {
97 "title": "The ID of the item to get",
98 "type": "integer",
99 "minimum": 1,
100 },
101 "name": "item_id",
102 "in": "path",
103 },
104 {
105 "required": True,
106 "schema": {
107 "type": "string",
108 "title": "Q",
109 },
110 "name": "q",
111 "in": "query",
112 },
113 ],
114 "responses": {
115 "200": {
116 "description": "Successful Response",
117 "content": {
118 "application/json": {
119 "schema": {},
120 }
121 },
122 },
123 "422": {
124 "content": {
125 "application/json": {
126 "schema": {
127 "$ref": "#/components/schemas/HTTPValidationError",
128 },
129 },
130 },
131 "description": "Validation Error",
132 },
133 },
134 }
135 }
136 },
137 "components": {
138 "schemas": {
139 "HTTPValidationError": {
140 "properties": {
141 "detail": {
142 "items": {
143 "$ref": "#/components/schemas/ValidationError",
144 },
145 "title": "Detail",
146 "type": "array",
147 },
148 },
149 "title": "HTTPValidationError",
150 "type": "object",
151 },
152 "ValidationError": {
153 "properties": {
154 "ctx": {"title": "Context", "type": "object"},
155 "input": {"title": "Input"},
156 "loc": {
157 "items": {
158 "anyOf": [
159 {
160 "type": "string",
161 },
162 {
163 "type": "integer",
164 },
165 ],
166 },
167 "title": "Location",
168 "type": "array",
169 },
170 "msg": {
171 "title": "Message",
172 "type": "string",
173 },
174 "type": {
175 "title": "Error Type",
176 "type": "string",
177 },
178 },
179 "required": [
180 "loc",
181 "msg",
182 "type",
183 ],
184 "title": "ValidationError",
185 "type": "object",
186 },
187 },
188 },
189 }
190 )