Coverage for tests / test_tutorial / test_query_params / test_tutorial006.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("tutorial006_py310", marks=needs_py310),
14 ],
15)
16def get_client(request: pytest.FixtureRequest): 1abdc
17 mod = importlib.import_module(f"docs_src.query_params.{request.param}") 1abc
19 c = TestClient(mod.app) 1abc
20 return c 1abc
23def test_foo_needy_very(client: TestClient): 1abdc
24 response = client.get("/items/foo?needy=very") 1efg
25 assert response.status_code == 200 1efg
26 assert response.json() == { 1efg
27 "item_id": "foo",
28 "needy": "very",
29 "skip": 0,
30 "limit": None,
31 }
34def test_foo_no_needy(client: TestClient): 1abdc
35 response = client.get("/items/foo?skip=a&limit=b") 1hij
36 assert response.status_code == 422 1hij
37 assert response.json() == { 1hij
38 "detail": [
39 {
40 "type": "missing",
41 "loc": ["query", "needy"],
42 "msg": "Field required",
43 "input": None,
44 },
45 {
46 "type": "int_parsing",
47 "loc": ["query", "skip"],
48 "msg": "Input should be a valid integer, unable to parse string as an integer",
49 "input": "a",
50 },
51 {
52 "type": "int_parsing",
53 "loc": ["query", "limit"],
54 "msg": "Input should be a valid integer, unable to parse string as an integer",
55 "input": "b",
56 },
57 ]
58 }
61def test_openapi_schema(client: TestClient): 1abdc
62 response = client.get("/openapi.json") 1klm
63 assert response.status_code == 200 1klm
64 assert response.json() == snapshot( 1klm
65 {
66 "openapi": "3.1.0",
67 "info": {"title": "FastAPI", "version": "0.1.0"},
68 "paths": {
69 "/items/{item_id}": {
70 "get": {
71 "responses": {
72 "200": {
73 "description": "Successful Response",
74 "content": {"application/json": {"schema": {}}},
75 },
76 "422": {
77 "description": "Validation Error",
78 "content": {
79 "application/json": {
80 "schema": {
81 "$ref": "#/components/schemas/HTTPValidationError"
82 }
83 }
84 },
85 },
86 },
87 "summary": "Read User Item",
88 "operationId": "read_user_item_items__item_id__get",
89 "parameters": [
90 {
91 "required": True,
92 "schema": {"title": "Item Id", "type": "string"},
93 "name": "item_id",
94 "in": "path",
95 },
96 {
97 "required": True,
98 "schema": {"title": "Needy", "type": "string"},
99 "name": "needy",
100 "in": "query",
101 },
102 {
103 "required": False,
104 "schema": {
105 "title": "Skip",
106 "type": "integer",
107 "default": 0,
108 },
109 "name": "skip",
110 "in": "query",
111 },
112 {
113 "required": False,
114 "schema": {
115 "anyOf": [{"type": "integer"}, {"type": "null"}],
116 "title": "Limit",
117 },
118 "name": "limit",
119 "in": "query",
120 },
121 ],
122 }
123 }
124 },
125 "components": {
126 "schemas": {
127 "ValidationError": {
128 "title": "ValidationError",
129 "required": ["loc", "msg", "type"],
130 "type": "object",
131 "properties": {
132 "loc": {
133 "title": "Location",
134 "type": "array",
135 "items": {
136 "anyOf": [{"type": "string"}, {"type": "integer"}]
137 },
138 },
139 "msg": {"title": "Message", "type": "string"},
140 "type": {"title": "Error Type", "type": "string"},
141 "input": {"title": "Input"},
142 "ctx": {"title": "Context", "type": "object"},
143 },
144 },
145 "HTTPValidationError": {
146 "title": "HTTPValidationError",
147 "type": "object",
148 "properties": {
149 "detail": {
150 "title": "Detail",
151 "type": "array",
152 "items": {
153 "$ref": "#/components/schemas/ValidationError"
154 },
155 }
156 },
157 },
158 }
159 },
160 }
161 )