Coverage for tests / test_regex_deprecated_body.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, Form 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() 1ekflgmhniojp
14 with pytest.warns(FastAPIDeprecationWarning): 1ekflgmhniojp
16 @app.post("/items/") 1ekflgmhniojp
17 async def read_items( 1ekflgmhniojp
18 q: Annotated[str | None, Form(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) 1ekflgmhniojp
26 return client 1ekflgmhniojp
29@needs_py310 1abcd
30def test_no_query(): 1abcd
31 client = get_client() 1egi
32 response = client.post("/items/") 1egi
33 assert response.status_code == 200 1egi
34 assert response.json() == "Hello World" 1egi
37@needs_py310 1abcd
38def test_q_fixedquery(): 1abcd
39 client = get_client() 1fhj
40 response = client.post("/items/", data={"q": "fixedquery"}) 1fhj
41 assert response.status_code == 200 1fhj
42 assert response.json() == "Hello fixedquery" 1fhj
45@needs_py310 1abcd
46def test_query_nonregexquery(): 1abcd
47 client = get_client() 1lnp
48 response = client.post("/items/", data={"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": ["body", "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 "post": {
75 "summary": "Read Items",
76 "operationId": "read_items_items__post",
77 "requestBody": {
78 "content": {
79 "application/x-www-form-urlencoded": {
80 "schema": {
81 "$ref": "#/components/schemas/Body_read_items_items__post"
82 }
83 }
84 }
85 },
86 "responses": {
87 "200": {
88 "description": "Successful Response",
89 "content": {"application/json": {"schema": {}}},
90 },
91 "422": {
92 "description": "Validation Error",
93 "content": {
94 "application/json": {
95 "schema": {
96 "$ref": "#/components/schemas/HTTPValidationError"
97 }
98 }
99 },
100 },
101 },
102 }
103 }
104 },
105 "components": {
106 "schemas": {
107 "Body_read_items_items__post": {
108 "properties": {
109 "q": {
110 "anyOf": [
111 {"type": "string", "pattern": "^fixedquery$"},
112 {"type": "null"},
113 ],
114 "title": "Q",
115 }
116 },
117 "type": "object",
118 "title": "Body_read_items_items__post",
119 },
120 "HTTPValidationError": {
121 "properties": {
122 "detail": {
123 "items": {
124 "$ref": "#/components/schemas/ValidationError"
125 },
126 "type": "array",
127 "title": "Detail",
128 }
129 },
130 "type": "object",
131 "title": "HTTPValidationError",
132 },
133 "ValidationError": {
134 "properties": {
135 "ctx": {"title": "Context", "type": "object"},
136 "input": {"title": "Input"},
137 "loc": {
138 "items": {
139 "anyOf": [{"type": "string"}, {"type": "integer"}]
140 },
141 "type": "array",
142 "title": "Location",
143 },
144 "msg": {"type": "string", "title": "Message"},
145 "type": {"type": "string", "title": "Error Type"},
146 },
147 "type": "object",
148 "required": ["loc", "msg", "type"],
149 "title": "ValidationError",
150 },
151 }
152 },
153 }
154 )