同步操作将从 Gitee 极速下载/FastAPI 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import pytestfrom dirty_equals import IsDictfrom fastapi import APIRouter, FastAPI, Queryfrom fastapi.testclient import TestClientfrom fastapi.utils import match_pydantic_error_urlfrom typing_extensions import Annotatedapp = FastAPI()@app.get("/default")async def default(foo: Annotated[str, Query()] = "foo"):return {"foo": foo}@app.get("/required")async def required(foo: Annotated[str, Query(min_length=1)]):return {"foo": foo}@app.get("/multiple")async def multiple(foo: Annotated[str, object(), Query(min_length=1)]):return {"foo": foo}@app.get("/unrelated")async def unrelated(foo: Annotated[str, object()]):return {"foo": foo}client = TestClient(app)foo_is_missing = {"detail": [IsDict({"loc": ["query", "foo"],"msg": "Field required","type": "missing","input": None,"url": match_pydantic_error_url("missing"),})# TODO: remove when deprecating Pydantic v1| IsDict({"loc": ["query", "foo"],"msg": "field required","type": "value_error.missing",})]}foo_is_short = {"detail": [IsDict({"ctx": {"min_length": 1},"loc": ["query", "foo"],"msg": "String should have at least 1 character","type": "string_too_short","input": "","url": match_pydantic_error_url("string_too_short"),})# TODO: remove when deprecating Pydantic v1| IsDict({"ctx": {"limit_value": 1},"loc": ["query", "foo"],"msg": "ensure this value has at least 1 characters","type": "value_error.any_str.min_length",})]}@pytest.mark.parametrize("path,expected_status,expected_response",[("/default", 200, {"foo": "foo"}),("/default?foo=bar", 200, {"foo": "bar"}),("/required?foo=bar", 200, {"foo": "bar"}),("/required", 422, foo_is_missing),("/required?foo=", 422, foo_is_short),("/multiple?foo=bar", 200, {"foo": "bar"}),("/multiple", 422, foo_is_missing),("/multiple?foo=", 422, foo_is_short),("/unrelated?foo=bar", 200, {"foo": "bar"}),("/unrelated", 422, foo_is_missing),],)def test_get(path, expected_status, expected_response):response = client.get(path)assert response.status_code == expected_statusassert response.json() == expected_responsedef test_multiple_path():app = FastAPI()@app.get("/test1")@app.get("/test2")async def test(var: Annotated[str, Query()] = "bar"):return {"foo": var}client = TestClient(app)response = client.get("/test1")assert response.status_code == 200assert response.json() == {"foo": "bar"}response = client.get("/test1", params={"var": "baz"})assert response.status_code == 200assert response.json() == {"foo": "baz"}response = client.get("/test2")assert response.status_code == 200assert response.json() == {"foo": "bar"}response = client.get("/test2", params={"var": "baz"})assert response.status_code == 200assert response.json() == {"foo": "baz"}def test_nested_router():app = FastAPI()router = APIRouter(prefix="/nested")@router.get("/test")async def test(var: Annotated[str, Query()] = "bar"):return {"foo": var}app.include_router(router)client = TestClient(app)response = client.get("/nested/test")assert response.status_code == 200assert response.json() == {"foo": "bar"}def test_openapi_schema():response = client.get("/openapi.json")assert response.status_code == 200assert response.json() == {"openapi": "3.1.0","info": {"title": "FastAPI", "version": "0.1.0"},"paths": {"/default": {"get": {"summary": "Default","operationId": "default_default_get","parameters": [{"required": False,"schema": {"title": "Foo","type": "string","default": "foo",},"name": "foo","in": "query",}],"responses": {"200": {"description": "Successful Response","content": {"application/json": {"schema": {}}},},"422": {"description": "Validation Error","content": {"application/json": {"schema": {"$ref": "#/components/schemas/HTTPValidationError"}}},},},}},"/required": {"get": {"summary": "Required","operationId": "required_required_get","parameters": [{"required": True,"schema": {"title": "Foo","minLength": 1,"type": "string",},"name": "foo","in": "query",}],"responses": {"200": {"description": "Successful Response","content": {"application/json": {"schema": {}}},},"422": {"description": "Validation Error","content": {"application/json": {"schema": {"$ref": "#/components/schemas/HTTPValidationError"}}},},},}},"/multiple": {"get": {"summary": "Multiple","operationId": "multiple_multiple_get","parameters": [{"required": True,"schema": {"title": "Foo","minLength": 1,"type": "string",},"name": "foo","in": "query",}],"responses": {"200": {"description": "Successful Response","content": {"application/json": {"schema": {}}},},"422": {"description": "Validation Error","content": {"application/json": {"schema": {"$ref": "#/components/schemas/HTTPValidationError"}}},},},}},"/unrelated": {"get": {"summary": "Unrelated","operationId": "unrelated_unrelated_get","parameters": [{"required": True,"schema": {"title": "Foo", "type": "string"},"name": "foo","in": "query",}],"responses": {"200": {"description": "Successful Response","content": {"application/json": {"schema": {}}},},"422": {"description": "Validation Error","content": {"application/json": {"schema": {"$ref": "#/components/schemas/HTTPValidationError"}}},},},}},},"components": {"schemas": {"HTTPValidationError": {"title": "HTTPValidationError","type": "object","properties": {"detail": {"title": "Detail","type": "array","items": {"$ref": "#/components/schemas/ValidationError"},}},},"ValidationError": {"title": "ValidationError","required": ["loc", "msg", "type"],"type": "object","properties": {"loc": {"title": "Location","type": "array","items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},},"msg": {"title": "Message", "type": "string"},"type": {"title": "Error Type", "type": "string"},},},}},}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。