Coverage for tests / test_regex_deprecated_params.py: 100%
41 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
1from typing import Annotated 1abcd
3import pytest 1abcd
4from fastapi import FastAPI, Query 1abcd
5from fastapi.exceptions import FastAPIDeprecationWarning 1abcd
6from fastapi.testclient import TestClient 1abcd
7from inline_snapshot import snapshot 1abcd
9from .utils import needs_py310 1abcd
12def get_client(): 1abcd
13 app = FastAPI() 1klefmnghopij
14 with pytest.warns(FastAPIDeprecationWarning): 1klefmnghopij
16 @app.get("/items/") 1klefmnghopij
17 async def read_items( 1klefmnghopij
18 q: Annotated[str | None, Query(regex="^fixedquery$")] = None,
19 ):
20 if q: 1efghij
21 return f"Hello {q}" 1fhj
22 else:
23 return "Hello World" 1egi
25 client = TestClient(app) 1klefmnghopij
26 return client 1klefmnghopij
29@needs_py310 1abcd
30def test_query_params_str_validations_no_query(): 1abcd
31 client = get_client() 1egi
32 response = client.get("/items/") 1egi
33 assert response.status_code == 200 1egi
34 assert response.json() == "Hello World" 1egi
37@needs_py310 1abcd
38def test_query_params_str_validations_q_fixedquery(): 1abcd
39 client = get_client() 1fhj
40 response = client.get("/items/", params={"q": "fixedquery"}) 1fhj
41 assert response.status_code == 200 1fhj
42 assert response.json() == "Hello fixedquery" 1fhj
45@needs_py310 1abcd
46def test_query_params_str_validations_item_query_nonregexquery(): 1abcd
47 client = get_client() 1lnp
48 response = client.get("/items/", params={"q": "nonregexquery"}) 1lnp
49 assert response.status_code == 422 1lnp
50 assert response.json() == { 1lnp
51 "detail": [
52 {
53 "type": "string_pattern_mismatch",
54 "loc": ["query", "q"],
55 "msg": "String should match pattern '^fixedquery$'",
56 "input": "nonregexquery",
57 "ctx": {"pattern": "^fixedquery$"},
58 }
59 ]
60 }
63@needs_py310 1abcd
64def test_openapi_schema(): 1abcd
65 client = get_client() 1kmo
66 response = client.get("/openapi.json") 1kmo
67 assert response.status_code == 200, response.text 1kmo
68 assert response.json() == snapshot( 1kmo
69 {
70 "openapi": "3.1.0",
71 "info": {"title": "FastAPI", "version": "0.1.0"},
72 "paths": {
73 "/items/": {
74 "get": {
75 "summary": "Read Items",
76 "operationId": "read_items_items__get",
77 "parameters": [
78 {
79 "name": "q",
80 "in": "query",
81 "required": False,
82 "schema": {
83 "anyOf": [
84 {"type": "string", "pattern": "^fixedquery$"},
85 {"type": "null"},
86 ],
87 "title": "Q",
88 },
89 }
90 ],
91 "responses": {
92 "200": {
93 "description": "Successful Response",
94 "content": {"application/json": {"schema": {}}},
95 },
96 "422": {
97 "description": "Validation Error",
98 "content": {
99 "application/json": {
100 "schema": {
101 "$ref": "#/components/schemas/HTTPValidationError"
102 }
103 }
104 },
105 },
106 },
107 }
108 }
109 },
110 "components": {
111 "schemas": {
112 "HTTPValidationError": {
113 "properties": {
114 "detail": {
115 "items": {
116 "$ref": "#/components/schemas/ValidationError"
117 },
118 "type": "array",
119 "title": "Detail",
120 }
121 },
122 "type": "object",
123 "title": "HTTPValidationError",
124 },
125 "ValidationError": {
126 "properties": {
127 "ctx": {"title": "Context", "type": "object"},
128 "input": {"title": "Input"},
129 "loc": {
130 "items": {
131 "anyOf": [{"type": "string"}, {"type": "integer"}]
132 },
133 "type": "array",
134 "title": "Location",
135 },
136 "msg": {"type": "string", "title": "Message"},
137 "type": {"type": "string", "title": "Error Type"},
138 },
139 "type": "object",
140 "required": ["loc", "msg", "type"],
141 "title": "ValidationError",
142 },
143 }
144 },
145 }
146 )