Coverage for tests / test_tutorial / test_query_params_str_validations / test_tutorial006c.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("tutorial006c_py310", marks=needs_py310),
14 pytest.param("tutorial006c_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 )
21 client = TestClient(mod.app) 1abc
22 return client 1abc
25@pytest.mark.xfail( 1abdc
26 reason="Code example is not valid. See https://github.com/fastapi/fastapi/issues/12419"
27)
28def test_query_params_str_validations_no_query(client: TestClient): 1abdc
29 response = client.get("/items/") 1nop
30 assert response.status_code == 200 1nop
31 assert response.json() == { # pragma: no cover
32 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
33 }
36@pytest.mark.xfail( 1abdc
37 reason="Code example is not valid. See https://github.com/fastapi/fastapi/issues/12419"
38)
39def test_query_params_str_validations_empty_str(client: TestClient): 1abdc
40 response = client.get("/items/?q=") 1qrs
41 assert response.status_code == 200 1qrs
42 assert response.json() == { # pragma: no cover
43 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
44 }
47def test_query_params_str_validations_q_query(client: TestClient): 1abdc
48 response = client.get("/items/", params={"q": "query"}) 1efg
49 assert response.status_code == 200 1efg
50 assert response.json() == { 1efg
51 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
52 "q": "query",
53 }
56def test_query_params_str_validations_q_short(client: TestClient): 1abdc
57 response = client.get("/items/", params={"q": "fa"}) 1hij
58 assert response.status_code == 422 1hij
59 assert response.json() == { 1hij
60 "detail": [
61 {
62 "type": "string_too_short",
63 "loc": ["query", "q"],
64 "msg": "String should have at least 3 characters",
65 "input": "fa",
66 "ctx": {"min_length": 3},
67 }
68 ]
69 }
72def test_openapi_schema(client: TestClient): 1abdc
73 response = client.get("/openapi.json") 1klm
74 assert response.status_code == 200, response.text 1klm
75 assert response.json() == snapshot( 1klm
76 {
77 "openapi": "3.1.0",
78 "info": {"title": "FastAPI", "version": "0.1.0"},
79 "paths": {
80 "/items/": {
81 "get": {
82 "responses": {
83 "200": {
84 "description": "Successful Response",
85 "content": {"application/json": {"schema": {}}},
86 },
87 "422": {
88 "description": "Validation Error",
89 "content": {
90 "application/json": {
91 "schema": {
92 "$ref": "#/components/schemas/HTTPValidationError"
93 }
94 }
95 },
96 },
97 },
98 "summary": "Read Items",
99 "operationId": "read_items_items__get",
100 "parameters": [
101 {
102 "required": True,
103 "schema": {
104 "anyOf": [
105 {"type": "string", "minLength": 3},
106 {"type": "null"},
107 ],
108 "title": "Q",
109 },
110 "name": "q",
111 "in": "query",
112 }
113 ],
114 }
115 }
116 },
117 "components": {
118 "schemas": {
119 "ValidationError": {
120 "title": "ValidationError",
121 "required": ["loc", "msg", "type"],
122 "type": "object",
123 "properties": {
124 "loc": {
125 "title": "Location",
126 "type": "array",
127 "items": {
128 "anyOf": [{"type": "string"}, {"type": "integer"}]
129 },
130 },
131 "msg": {"title": "Message", "type": "string"},
132 "type": {"title": "Error Type", "type": "string"},
133 "input": {"title": "Input"},
134 "ctx": {"title": "Context", "type": "object"},
135 },
136 },
137 "HTTPValidationError": {
138 "title": "HTTPValidationError",
139 "type": "object",
140 "properties": {
141 "detail": {
142 "title": "Detail",
143 "type": "array",
144 "items": {
145 "$ref": "#/components/schemas/ValidationError"
146 },
147 }
148 },
149 },
150 }
151 },
152 }
153 )