| 1 |
from typing import Annotated |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi import FastAPI, Form |
| 5 |
from fastapi.exceptions import FastAPIDeprecationWarning |
| 6 |
from fastapi.testclient import TestClient |
| 7 |
from inline_snapshot import snapshot |
| 8 |
|
| 9 |
from .utils import needs_py310 |
| 10 |
|
| 11 |
|
| 12 |
def get_client(): |
| 13 |
app = FastAPI() |
| 14 |
with pytest.warns(FastAPIDeprecationWarning): |
| 15 |
|
| 16 |
@app.post("/items/") |
| 17 |
async def read_items( |
| 18 |
q: Annotated[str | None, Form(regex="^fixedquery$")] = None, |
| 19 |
): |
| 20 |
if q: |
| 21 |
return f"Hello {q}" |
| 22 |
else: |
| 23 |
return "Hello World" |
| 24 |
|
| 25 |
client = TestClient(app) |
| 26 |
return client |
| 27 |
|
| 28 |
|
| 29 |
@needs_py310 |
| 30 |
def test_no_query(): |
| 31 |
client = get_client() |
| 32 |
response = client.post("/items/") |
| 33 |
assert response.status_code == 200 |
| 34 |
assert response.json() == "Hello World" |
| 35 |
|
| 36 |
|
| 37 |
@needs_py310 |
| 38 |
def test_q_fixedquery(): |
| 39 |
client = get_client() |
| 40 |
response = client.post("/items/", data={"q": "fixedquery"}) |
| 41 |
assert response.status_code == 200 |
| 42 |
assert response.json() == "Hello fixedquery" |
| 43 |
|
| 44 |
|
| 45 |
@needs_py310 |
| 46 |
def test_query_nonregexquery(): |
| 47 |
client = get_client() |
| 48 |
response = client.post("/items/", data={"q": "nonregexquery"}) |
| 49 |
assert response.status_code == 422 |
| 50 |
assert response.json() == { |
| 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 |
} |
| 61 |
|
| 62 |
|
| 63 |
@needs_py310 |
| 64 |
def test_openapi_schema(): |
| 65 |
client = get_client() |
| 66 |
response = client.get("/openapi.json") |
| 67 |
assert response.status_code == 200, response.text |
| 68 |
assert response.json() == snapshot( |
| 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 |
) |