Coverage for tests/test_tutorial/test_query_params_str_validations/test_tutorial010.py: 100%
31 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-06 08:24 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-06 08:24 +0000
1import importlib 1abcdef
3import pytest 1abcdef
4from dirty_equals import IsDict 1abcdef
5from fastapi._compat import PYDANTIC_VERSION_MINOR_TUPLE 1abcdef
6from fastapi.testclient import TestClient 1abcdef
8from ...utils import needs_py39, needs_py310 1abcdef
11@pytest.fixture( 1abcdef
12 name="client",
13 params=[
14 "tutorial010",
15 pytest.param("tutorial010_py310", marks=needs_py310),
16 "tutorial010_an",
17 pytest.param("tutorial010_an_py39", marks=needs_py39),
18 pytest.param("tutorial010_an_py310", marks=needs_py310),
19 ],
20)
21def get_client(request: pytest.FixtureRequest): 1abcdef
22 mod = importlib.import_module( 1abcdef
23 f"docs_src.query_params_str_validations.{request.param}"
24 )
26 client = TestClient(mod.app) 1abcdef
27 return client 1abcdef
30def test_query_params_str_validations_no_query(client: TestClient): 1abcdef
31 response = client.get("/items/") 1ghijkl
32 assert response.status_code == 200 1ghijkl
33 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} 1ghijkl
36def test_query_params_str_validations_item_query_fixedquery(client: TestClient): 1abcdef
37 response = client.get("/items/", params={"item-query": "fixedquery"}) 1mnopqr
38 assert response.status_code == 200 1mnopqr
39 assert response.json() == { 1mnopqr
40 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
41 "q": "fixedquery",
42 }
45def test_query_params_str_validations_q_fixedquery(client: TestClient): 1abcdef
46 response = client.get("/items/", params={"q": "fixedquery"}) 1stuvwx
47 assert response.status_code == 200 1stuvwx
48 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} 1stuvwx
51def test_query_params_str_validations_item_query_nonregexquery(client: TestClient): 1abcdef
52 response = client.get("/items/", params={"item-query": "nonregexquery"}) 1yzABCD
53 assert response.status_code == 422 1yzABCD
54 assert response.json() == IsDict( 1yzABCD
55 {
56 "detail": [
57 {
58 "type": "string_pattern_mismatch",
59 "loc": ["query", "item-query"],
60 "msg": "String should match pattern '^fixedquery$'",
61 "input": "nonregexquery",
62 "ctx": {"pattern": "^fixedquery$"},
63 }
64 ]
65 }
66 ) | IsDict(
67 # TODO: remove when deprecating Pydantic v1
68 {
69 "detail": [
70 {
71 "ctx": {"pattern": "^fixedquery$"},
72 "loc": ["query", "item-query"],
73 "msg": 'string does not match regex "^fixedquery$"',
74 "type": "value_error.str.regex",
75 }
76 ]
77 }
78 )
81def test_openapi_schema(client: TestClient): 1abcdef
82 response = client.get("/openapi.json") 1EFGHIJ
83 assert response.status_code == 200, response.text 1EFGHIJ
84 assert response.json() == { 1EFGHIJ
85 "openapi": "3.1.0",
86 "info": {"title": "FastAPI", "version": "0.1.0"},
87 "paths": {
88 "/items/": {
89 "get": {
90 "responses": {
91 "200": {
92 "description": "Successful Response",
93 "content": {"application/json": {"schema": {}}},
94 },
95 "422": {
96 "description": "Validation Error",
97 "content": {
98 "application/json": {
99 "schema": {
100 "$ref": "#/components/schemas/HTTPValidationError"
101 }
102 }
103 },
104 },
105 },
106 "summary": "Read Items",
107 "operationId": "read_items_items__get",
108 "parameters": [
109 {
110 "description": "Query string for the items to search in the database that have a good match",
111 "required": False,
112 "deprecated": True,
113 "schema": IsDict(
114 {
115 "anyOf": [
116 {
117 "type": "string",
118 "minLength": 3,
119 "maxLength": 50,
120 "pattern": "^fixedquery$",
121 },
122 {"type": "null"},
123 ],
124 "title": "Query string",
125 "description": "Query string for the items to search in the database that have a good match",
126 # See https://github.com/pydantic/pydantic/blob/80353c29a824c55dea4667b328ba8f329879ac9f/tests/test_fastapi.sh#L25-L34.
127 **(
128 {"deprecated": True}
129 if PYDANTIC_VERSION_MINOR_TUPLE >= (2, 10)
130 else {}
131 ),
132 }
133 )
134 | IsDict(
135 # TODO: remove when deprecating Pydantic v1
136 {
137 "title": "Query string",
138 "maxLength": 50,
139 "minLength": 3,
140 "pattern": "^fixedquery$",
141 "type": "string",
142 "description": "Query string for the items to search in the database that have a good match",
143 }
144 ),
145 "name": "item-query",
146 "in": "query",
147 }
148 ],
149 }
150 }
151 },
152 "components": {
153 "schemas": {
154 "ValidationError": {
155 "title": "ValidationError",
156 "required": ["loc", "msg", "type"],
157 "type": "object",
158 "properties": {
159 "loc": {
160 "title": "Location",
161 "type": "array",
162 "items": {
163 "anyOf": [{"type": "string"}, {"type": "integer"}]
164 },
165 },
166 "msg": {"title": "Message", "type": "string"},
167 "type": {"title": "Error Type", "type": "string"},
168 },
169 },
170 "HTTPValidationError": {
171 "title": "HTTPValidationError",
172 "type": "object",
173 "properties": {
174 "detail": {
175 "title": "Detail",
176 "type": "array",
177 "items": {"$ref": "#/components/schemas/ValidationError"},
178 }
179 },
180 },
181 }
182 },
183 }