Coverage for tests / test_tutorial / test_path_params_numeric_validations / test_tutorial001.py: 100%
22 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("tutorial001_py310", marks=needs_py310),
14 pytest.param("tutorial001_an_py310", marks=needs_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", {"item_id": 42}),
28 ("/items/123?item-query=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") 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_openapi_schema(client: TestClient): 1abdc
53 response = client.get("/openapi.json") 1klm
54 assert response.status_code == 200, response.text 1klm
55 assert response.json() == snapshot( 1klm
56 {
57 "openapi": "3.1.0",
58 "info": {"title": "FastAPI", "version": "0.1.0"},
59 "paths": {
60 "/items/{item_id}": {
61 "get": {
62 "summary": "Read Items",
63 "operationId": "read_items_items__item_id__get",
64 "parameters": [
65 {
66 "required": True,
67 "schema": {
68 "title": "The ID of the item to get",
69 "type": "integer",
70 },
71 "name": "item_id",
72 "in": "path",
73 },
74 {
75 "required": False,
76 "schema": {
77 "anyOf": [
78 {
79 "type": "string",
80 },
81 {
82 "type": "null",
83 },
84 ],
85 "title": "Item-Query",
86 },
87 "name": "item-query",
88 "in": "query",
89 },
90 ],
91 "responses": {
92 "200": {
93 "description": "Successful Response",
94 "content": {
95 "application/json": {
96 "schema": {},
97 }
98 },
99 },
100 "422": {
101 "content": {
102 "application/json": {
103 "schema": {
104 "$ref": "#/components/schemas/HTTPValidationError",
105 },
106 },
107 },
108 "description": "Validation Error",
109 },
110 },
111 }
112 }
113 },
114 "components": {
115 "schemas": {
116 "HTTPValidationError": {
117 "properties": {
118 "detail": {
119 "items": {
120 "$ref": "#/components/schemas/ValidationError",
121 },
122 "title": "Detail",
123 "type": "array",
124 },
125 },
126 "title": "HTTPValidationError",
127 "type": "object",
128 },
129 "ValidationError": {
130 "properties": {
131 "ctx": {"title": "Context", "type": "object"},
132 "input": {"title": "Input"},
133 "loc": {
134 "items": {
135 "anyOf": [
136 {
137 "type": "string",
138 },
139 {
140 "type": "integer",
141 },
142 ],
143 },
144 "title": "Location",
145 "type": "array",
146 },
147 "msg": {
148 "title": "Message",
149 "type": "string",
150 },
151 "type": {
152 "title": "Error Type",
153 "type": "string",
154 },
155 },
156 "required": [
157 "loc",
158 "msg",
159 "type",
160 ],
161 "title": "ValidationError",
162 "type": "object",
163 },
164 },
165 },
166 }
167 )