Coverage for tests/test_regex_deprecated_body.py: 100%
40 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
1import pytest 1abcdefg
2from dirty_equals import IsDict 1abcdefg
3from fastapi import FastAPI, Form 1abcdefg
4from fastapi.testclient import TestClient 1abcdefg
5from typing_extensions import Annotated 1abcdefg
7from .utils import needs_py310 1abcdefg
10def get_client(): 1abcdefg
11 app = FastAPI() 1hrisjtkulvmwnxoypzqA
12 with pytest.warns(DeprecationWarning): 1hrisjtkulvmwnxoypzqA
14 @app.post("/items/") 1hrisjtkulvmwnxoypzqA
15 async def read_items( 1hrisjtkulvmwnxoypzqA
16 q: Annotated[str | None, Form(regex="^fixedquery$")] = None,
17 ):
18 if q: 1hijklmnopq
19 return f"Hello {q}" 1ikmoq
20 else:
21 return "Hello World" 1hjlnp
23 client = TestClient(app) 1hrisjtkulvmwnxoypzqA
24 return client 1hrisjtkulvmwnxoypzqA
27@needs_py310 1abcdefg
28def test_no_query(): 1abcdefg
29 client = get_client() 1hjlnp
30 response = client.post("/items/") 1hjlnp
31 assert response.status_code == 200 1hjlnp
32 assert response.json() == "Hello World" 1hjlnp
35@needs_py310 1abcdefg
36def test_q_fixedquery(): 1abcdefg
37 client = get_client() 1ikmoq
38 response = client.post("/items/", data={"q": "fixedquery"}) 1ikmoq
39 assert response.status_code == 200 1ikmoq
40 assert response.json() == "Hello fixedquery" 1ikmoq
43@needs_py310 1abcdefg
44def test_query_nonregexquery(): 1abcdefg
45 client = get_client() 1suwyA
46 response = client.post("/items/", data={"q": "nonregexquery"}) 1suwyA
47 assert response.status_code == 422 1suwyA
48 assert response.json() == IsDict( 1suwyA
49 {
50 "detail": [
51 {
52 "type": "string_pattern_mismatch",
53 "loc": ["body", "q"],
54 "msg": "String should match pattern '^fixedquery$'",
55 "input": "nonregexquery",
56 "ctx": {"pattern": "^fixedquery$"},
57 }
58 ]
59 }
60 ) | IsDict(
61 # TODO: remove when deprecating Pydantic v1
62 {
63 "detail": [
64 {
65 "ctx": {"pattern": "^fixedquery$"},
66 "loc": ["body", "q"],
67 "msg": 'string does not match regex "^fixedquery$"',
68 "type": "value_error.str.regex",
69 }
70 ]
71 }
72 )
75@needs_py310 1abcdefg
76def test_openapi_schema(): 1abcdefg
77 client = get_client() 1rtvxz
78 response = client.get("/openapi.json") 1rtvxz
79 assert response.status_code == 200, response.text 1rtvxz
80 # insert_assert(response.json())
81 assert response.json() == { 1rtvxz
82 "openapi": "3.1.0",
83 "info": {"title": "FastAPI", "version": "0.1.0"},
84 "paths": {
85 "/items/": {
86 "post": {
87 "summary": "Read Items",
88 "operationId": "read_items_items__post",
89 "requestBody": {
90 "content": {
91 "application/x-www-form-urlencoded": {
92 "schema": IsDict(
93 {
94 "allOf": [
95 {
96 "$ref": "#/components/schemas/Body_read_items_items__post"
97 }
98 ],
99 "title": "Body",
100 }
101 )
102 | IsDict(
103 # TODO: remove when deprecating Pydantic v1
104 {
105 "$ref": "#/components/schemas/Body_read_items_items__post"
106 }
107 )
108 }
109 }
110 },
111 "responses": {
112 "200": {
113 "description": "Successful Response",
114 "content": {"application/json": {"schema": {}}},
115 },
116 "422": {
117 "description": "Validation Error",
118 "content": {
119 "application/json": {
120 "schema": {
121 "$ref": "#/components/schemas/HTTPValidationError"
122 }
123 }
124 },
125 },
126 },
127 }
128 }
129 },
130 "components": {
131 "schemas": {
132 "Body_read_items_items__post": {
133 "properties": {
134 "q": IsDict(
135 {
136 "anyOf": [
137 {"type": "string", "pattern": "^fixedquery$"},
138 {"type": "null"},
139 ],
140 "title": "Q",
141 }
142 )
143 | IsDict(
144 # TODO: remove when deprecating Pydantic v1
145 {"type": "string", "pattern": "^fixedquery$", "title": "Q"}
146 )
147 },
148 "type": "object",
149 "title": "Body_read_items_items__post",
150 },
151 "HTTPValidationError": {
152 "properties": {
153 "detail": {
154 "items": {"$ref": "#/components/schemas/ValidationError"},
155 "type": "array",
156 "title": "Detail",
157 }
158 },
159 "type": "object",
160 "title": "HTTPValidationError",
161 },
162 "ValidationError": {
163 "properties": {
164 "loc": {
165 "items": {
166 "anyOf": [{"type": "string"}, {"type": "integer"}]
167 },
168 "type": "array",
169 "title": "Location",
170 },
171 "msg": {"type": "string", "title": "Message"},
172 "type": {"type": "string", "title": "Error Type"},
173 },
174 "type": "object",
175 "required": ["loc", "msg", "type"],
176 "title": "ValidationError",
177 },
178 }
179 },
180 }