Coverage for tests / test_tutorial / test_query_params_str_validations / test_tutorial003.py: 100%
30 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("tutorial003_py310", marks=needs_py310),
14 pytest.param("tutorial003_an_py310", marks=needs_py310),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abdc
18 mod = importlib.import_module( 1abc
19 f"docs_src.query_params_str_validations.{request.param}"
20 )
22 client = TestClient(mod.app) 1abc
23 return client 1abc
26def test_query_params_str_validations_no_query(client: TestClient): 1abdc
27 response = client.get("/items/") 1efg
28 assert response.status_code == 200 1efg
29 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} 1efg
32def test_query_params_str_validations_q_query(client: TestClient): 1abdc
33 response = client.get("/items/", params={"q": "query"}) 1hij
34 assert response.status_code == 200 1hij
35 assert response.json() == { 1hij
36 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
37 "q": "query",
38 }
41def test_query_params_str_validations_q_too_short(client: TestClient): 1abdc
42 response = client.get("/items/", params={"q": "qu"}) 1klm
43 assert response.status_code == 422 1klm
44 assert response.json() == { 1klm
45 "detail": [
46 {
47 "type": "string_too_short",
48 "loc": ["query", "q"],
49 "msg": "String should have at least 3 characters",
50 "input": "qu",
51 "ctx": {"min_length": 3},
52 }
53 ]
54 }
57def test_query_params_str_validations_q_too_long(client: TestClient): 1abdc
58 response = client.get("/items/", params={"q": "q" * 51}) 1nop
59 assert response.status_code == 422 1nop
60 assert response.json() == { 1nop
61 "detail": [
62 {
63 "type": "string_too_long",
64 "loc": ["query", "q"],
65 "msg": "String should have at most 50 characters",
66 "input": "q" * 51,
67 "ctx": {"max_length": 50},
68 }
69 ]
70 }
73def test_openapi_schema(client: TestClient): 1abdc
74 response = client.get("/openapi.json") 1qrs
75 assert response.status_code == 200, response.text 1qrs
76 assert response.json() == snapshot( 1qrs
77 {
78 "openapi": "3.1.0",
79 "info": {"title": "FastAPI", "version": "0.1.0"},
80 "paths": {
81 "/items/": {
82 "get": {
83 "responses": {
84 "200": {
85 "description": "Successful Response",
86 "content": {"application/json": {"schema": {}}},
87 },
88 "422": {
89 "description": "Validation Error",
90 "content": {
91 "application/json": {
92 "schema": {
93 "$ref": "#/components/schemas/HTTPValidationError"
94 }
95 }
96 },
97 },
98 },
99 "summary": "Read Items",
100 "operationId": "read_items_items__get",
101 "parameters": [
102 {
103 "required": False,
104 "schema": {
105 "anyOf": [
106 {
107 "type": "string",
108 "minLength": 3,
109 "maxLength": 50,
110 },
111 {"type": "null"},
112 ],
113 "title": "Q",
114 },
115 "name": "q",
116 "in": "query",
117 }
118 ],
119 }
120 }
121 },
122 "components": {
123 "schemas": {
124 "ValidationError": {
125 "title": "ValidationError",
126 "required": ["loc", "msg", "type"],
127 "type": "object",
128 "properties": {
129 "loc": {
130 "title": "Location",
131 "type": "array",
132 "items": {
133 "anyOf": [{"type": "string"}, {"type": "integer"}]
134 },
135 },
136 "msg": {"title": "Message", "type": "string"},
137 "type": {"title": "Error Type", "type": "string"},
138 "input": {"title": "Input"},
139 "ctx": {"title": "Context", "type": "object"},
140 },
141 },
142 "HTTPValidationError": {
143 "title": "HTTPValidationError",
144 "type": "object",
145 "properties": {
146 "detail": {
147 "title": "Detail",
148 "type": "array",
149 "items": {
150 "$ref": "#/components/schemas/ValidationError"
151 },
152 }
153 },
154 },
155 }
156 },
157 }
158 )