2
from dirty_equals import IsDict
3
from fastapi import FastAPI, Form
4
from fastapi.testclient import TestClient
5
from fastapi.utils import match_pydantic_error_url
6
from typing_extensions import Annotated
8
from .utils import needs_py310
13
with pytest.warns(DeprecationWarning):
17
q: Annotated[str | None, Form(regex="^fixedquery$")] = None
24
client = TestClient(app)
31
response = client.post("/items/")
32
assert response.status_code == 200
33
assert response.json() == "Hello World"
37
def test_q_fixedquery():
39
response = client.post("/items/", data={"q": "fixedquery"})
40
assert response.status_code == 200
41
assert response.json() == "Hello fixedquery"
45
def test_query_nonregexquery():
47
response = client.post("/items/", data={"q": "nonregexquery"})
48
assert response.status_code == 422
49
assert response.json() == IsDict(
53
"type": "string_pattern_mismatch",
55
"msg": "String should match pattern '^fixedquery$'",
56
"input": "nonregexquery",
57
"ctx": {"pattern": "^fixedquery$"},
58
"url": match_pydantic_error_url("string_pattern_mismatch"),
63
# TODO: remove when deprecating Pydantic v1
67
"ctx": {"pattern": "^fixedquery$"},
69
"msg": 'string does not match regex "^fixedquery$"',
70
"type": "value_error.str.regex",
78
def test_openapi_schema():
80
response = client.get("/openapi.json")
81
assert response.status_code == 200, response.text
82
# insert_assert(response.json())
83
assert response.json() == {
85
"info": {"title": "FastAPI", "version": "0.1.0"},
89
"summary": "Read Items",
90
"operationId": "read_items_items__post",
93
"application/x-www-form-urlencoded": {
98
"$ref": "#/components/schemas/Body_read_items_items__post"
105
# TODO: remove when deprecating Pydantic v1
107
"$ref": "#/components/schemas/Body_read_items_items__post"
115
"description": "Successful Response",
116
"content": {"application/json": {"schema": {}}},
119
"description": "Validation Error",
121
"application/json": {
123
"$ref": "#/components/schemas/HTTPValidationError"
134
"Body_read_items_items__post": {
139
{"type": "string", "pattern": "^fixedquery$"},
146
# TODO: remove when deprecating Pydantic v1
147
{"type": "string", "pattern": "^fixedquery$", "title": "Q"}
151
"title": "Body_read_items_items__post",
153
"HTTPValidationError": {
156
"items": {"$ref": "#/components/schemas/ValidationError"},
162
"title": "HTTPValidationError",
168
"anyOf": [{"type": "string"}, {"type": "integer"}]
173
"msg": {"type": "string", "title": "Message"},
174
"type": {"type": "string", "title": "Error Type"},
177
"required": ["loc", "msg", "type"],
178
"title": "ValidationError",