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-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1import importlib 1abcd
3import pytest 1abcd
4from fastapi.testclient import TestClient 1abcd
5from inline_snapshot import snapshot 1abcd
7from ...utils import needs_py310 1abcd
10@pytest.fixture( 1abcd
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): 1abcd
18 mod = importlib.import_module( 1efghijklmnopqrstuvwxyzABCDEFGHIJ
19 f"docs_src.query_params_str_validations.{request.param}"
20 )
21 client = TestClient(mod.app) 1efghijklmnopqrstuvwxyzABCDEFGHIJ
22 return client 1efghijklmnopqrstuvwxyzABCDEFGHIJ
25@pytest.mark.xfail( 1abcd
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): 1abcd
29 response = client.get("/items/") 189!#$%'(
30 assert response.status_code == 200 189!#$%'(
31 assert response.json() == { # pragma: no cover
32 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
33 }
36@pytest.mark.xfail( 1abcd
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): 1abcd
40 response = client.get("/items/?q=") 1)*+,-./:
41 assert response.status_code == 200 1)*+,-./:
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): 1abcd
48 response = client.get("/items/", params={"q": "query"}) 1KLMNOPQR
49 assert response.status_code == 200 1KLMNOPQR
50 assert response.json() == { 1KLMNOPQR
51 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
52 "q": "query",
53 }
56def test_query_params_str_validations_q_short(client: TestClient): 1abcd
57 response = client.get("/items/", params={"q": "fa"}) 1STUVWXYZ
58 assert response.status_code == 422 1STUVWXYZ
59 assert response.json() == { 1STUVWXYZ
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): 1abcd
73 response = client.get("/openapi.json") 101234567
74 assert response.status_code == 200, response.text 101234567
75 assert response.json() == snapshot( 101234567
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 )