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