Browse Source
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sebastián Ramírez <[email protected]>pull/9269/head
committed by
GitHub
24 changed files with 1293 additions and 156 deletions
@ -0,0 +1,18 @@ |
|||||
|
from typing import Optional |
||||
|
|
||||
|
from fastapi import Depends, FastAPI |
||||
|
from typing_extensions import Annotated |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
async def common_parameters(q: Optional[str] = None, skip: int = 0, limit: int = 100): |
||||
|
return {"q": q, "skip": skip, "limit": limit} |
||||
|
|
||||
|
|
||||
|
CommonParamsDepends = Annotated[dict, Depends(common_parameters)] |
||||
|
|
||||
|
|
||||
|
@app.get("/items/") |
||||
|
async def read_items(commons: CommonParamsDepends): |
||||
|
return commons |
@ -0,0 +1,17 @@ |
|||||
|
from typing import Annotated, Optional |
||||
|
|
||||
|
from fastapi import Depends, FastAPI |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
async def common_parameters(q: Optional[str] = None, skip: int = 0, limit: int = 100): |
||||
|
return {"q": q, "skip": skip, "limit": limit} |
||||
|
|
||||
|
|
||||
|
CommonParamsDepends = Annotated[dict, Depends(common_parameters)] |
||||
|
|
||||
|
|
||||
|
@app.get("/items/") |
||||
|
async def read_items(commons: CommonParamsDepends): |
||||
|
return commons |
@ -0,0 +1,21 @@ |
|||||
|
from typing import Optional |
||||
|
|
||||
|
from fastapi import Depends, FastAPI |
||||
|
from typing_extensions import Annotated |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
class CommonQueryParams: |
||||
|
def __init__(self, q: Optional[str] = None, skip: int = 0, limit: int = 100): |
||||
|
self.q = q |
||||
|
self.skip = skip |
||||
|
self.limit = limit |
||||
|
|
||||
|
|
||||
|
CommonQueryParamsDepends = Annotated[CommonQueryParams, Depends()] |
||||
|
|
||||
|
|
||||
|
@app.get("/items/") |
||||
|
async def read_items(commons: CommonQueryParamsDepends): |
||||
|
return commons |
@ -0,0 +1,20 @@ |
|||||
|
from typing import Annotated, Optional |
||||
|
|
||||
|
from fastapi import Depends, FastAPI |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
class CommonQueryParams: |
||||
|
def __init__(self, q: Optional[str] = None, skip: int = 0, limit: int = 100): |
||||
|
self.q = q |
||||
|
self.skip = skip |
||||
|
self.limit = limit |
||||
|
|
||||
|
|
||||
|
CommonQueryParamsDepends = Annotated[CommonQueryParams, Depends()] |
||||
|
|
||||
|
|
||||
|
@app.get("/items/") |
||||
|
async def read_items(commons: CommonQueryParamsDepends): |
||||
|
return commons |
@ -0,0 +1,15 @@ |
|||||
|
from fastapi import FastAPI, Path |
||||
|
from fastapi.param_functions import Query |
||||
|
from typing_extensions import Annotated |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
@app.get("/items/{item_id}") |
||||
|
async def read_items(item_id: Annotated[int, Path(gt=0)]): |
||||
|
return {"item_id": item_id} |
||||
|
|
||||
|
|
||||
|
@app.get("/users") |
||||
|
async def read_users(user_id: Annotated[str, Query(min_length=1)] = "me"): |
||||
|
return {"user_id": user_id} |
@ -0,0 +1,16 @@ |
|||||
|
from typing import Annotated |
||||
|
|
||||
|
from fastapi import FastAPI, Path |
||||
|
from fastapi.param_functions import Query |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
@app.get("/items/{item_id}") |
||||
|
async def read_items(item_id: Annotated[int, Path(gt=0)]): |
||||
|
return {"item_id": item_id} |
||||
|
|
||||
|
|
||||
|
@app.get("/users") |
||||
|
async def read_users(user_id: Annotated[str, Query(min_length=1)] = "me"): |
||||
|
return {"user_id": user_id} |
@ -0,0 +1,66 @@ |
|||||
|
import pytest |
||||
|
from fastapi import Depends, FastAPI, Path |
||||
|
from fastapi.param_functions import Query |
||||
|
from typing_extensions import Annotated |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
def test_no_annotated_defaults(): |
||||
|
with pytest.raises( |
||||
|
AssertionError, match="Path parameters cannot have a default value" |
||||
|
): |
||||
|
|
||||
|
@app.get("/items/{item_id}/") |
||||
|
async def get_item(item_id: Annotated[int, Path(default=1)]): |
||||
|
pass # pragma: nocover |
||||
|
|
||||
|
with pytest.raises( |
||||
|
AssertionError, |
||||
|
match=( |
||||
|
"`Query` default value cannot be set in `Annotated` for 'item_id'. Set the" |
||||
|
" default value with `=` instead." |
||||
|
), |
||||
|
): |
||||
|
|
||||
|
@app.get("/") |
||||
|
async def get(item_id: Annotated[int, Query(default=1)]): |
||||
|
pass # pragma: nocover |
||||
|
|
||||
|
|
||||
|
def test_no_multiple_annotations(): |
||||
|
async def dep(): |
||||
|
pass # pragma: nocover |
||||
|
|
||||
|
with pytest.raises( |
||||
|
AssertionError, |
||||
|
match="Cannot specify multiple `Annotated` FastAPI arguments for 'foo'", |
||||
|
): |
||||
|
|
||||
|
@app.get("/") |
||||
|
async def get(foo: Annotated[int, Query(min_length=1), Query()]): |
||||
|
pass # pragma: nocover |
||||
|
|
||||
|
with pytest.raises( |
||||
|
AssertionError, |
||||
|
match=( |
||||
|
"Cannot specify `Depends` in `Annotated` and default value" |
||||
|
" together for 'foo'" |
||||
|
), |
||||
|
): |
||||
|
|
||||
|
@app.get("/") |
||||
|
async def get2(foo: Annotated[int, Depends(dep)] = Depends(dep)): |
||||
|
pass # pragma: nocover |
||||
|
|
||||
|
with pytest.raises( |
||||
|
AssertionError, |
||||
|
match=( |
||||
|
"Cannot specify a FastAPI annotation in `Annotated` and `Depends` as a" |
||||
|
" default value together for 'foo'" |
||||
|
), |
||||
|
): |
||||
|
|
||||
|
@app.get("/") |
||||
|
async def get3(foo: Annotated[int, Query(min_length=1)] = Depends(dep)): |
||||
|
pass # pragma: nocover |
@ -0,0 +1,226 @@ |
|||||
|
import pytest |
||||
|
from fastapi import FastAPI, Query |
||||
|
from fastapi.testclient import TestClient |
||||
|
from typing_extensions import Annotated |
||||
|
|
||||
|
app = 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) |
||||
|
|
||||
|
openapi_schema = { |
||||
|
"openapi": "3.0.2", |
||||
|
"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"}, |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
} |
||||
|
foo_is_missing = { |
||||
|
"detail": [ |
||||
|
{ |
||||
|
"loc": ["query", "foo"], |
||||
|
"msg": "field required", |
||||
|
"type": "value_error.missing", |
||||
|
} |
||||
|
] |
||||
|
} |
||||
|
foo_is_short = { |
||||
|
"detail": [ |
||||
|
{ |
||||
|
"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), |
||||
|
("/openapi.json", 200, openapi_schema), |
||||
|
], |
||||
|
) |
||||
|
def test_get(path, expected_status, expected_response): |
||||
|
response = client.get(path) |
||||
|
assert response.status_code == expected_status |
||||
|
assert response.json() == expected_response |
@ -0,0 +1,100 @@ |
|||||
|
import pytest |
||||
|
from fastapi.testclient import TestClient |
||||
|
|
||||
|
from docs_src.annotated.tutorial001 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
openapi_schema = { |
||||
|
"openapi": "3.0.2", |
||||
|
"info": {"title": "FastAPI", "version": "0.1.0"}, |
||||
|
"paths": { |
||||
|
"/items/": { |
||||
|
"get": { |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "Successful Response", |
||||
|
"content": {"application/json": {"schema": {}}}, |
||||
|
}, |
||||
|
"422": { |
||||
|
"description": "Validation Error", |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": { |
||||
|
"$ref": "#/components/schemas/HTTPValidationError" |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
"summary": "Read Items", |
||||
|
"operationId": "read_items_items__get", |
||||
|
"parameters": [ |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Q", "type": "string"}, |
||||
|
"name": "q", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Skip", "type": "integer", "default": 0}, |
||||
|
"name": "skip", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Limit", "type": "integer", "default": 100}, |
||||
|
"name": "limit", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
], |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
"components": { |
||||
|
"schemas": { |
||||
|
"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"}, |
||||
|
}, |
||||
|
}, |
||||
|
"HTTPValidationError": { |
||||
|
"title": "HTTPValidationError", |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"detail": { |
||||
|
"title": "Detail", |
||||
|
"type": "array", |
||||
|
"items": {"$ref": "#/components/schemas/ValidationError"}, |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@pytest.mark.parametrize( |
||||
|
"path,expected_status,expected_response", |
||||
|
[ |
||||
|
("/items", 200, {"q": None, "skip": 0, "limit": 100}), |
||||
|
("/items?q=foo", 200, {"q": "foo", "skip": 0, "limit": 100}), |
||||
|
("/items?q=foo&skip=5", 200, {"q": "foo", "skip": 5, "limit": 100}), |
||||
|
("/items?q=foo&skip=5&limit=30", 200, {"q": "foo", "skip": 5, "limit": 30}), |
||||
|
("/openapi.json", 200, openapi_schema), |
||||
|
], |
||||
|
) |
||||
|
def test_get(path, expected_status, expected_response): |
||||
|
response = client.get(path) |
||||
|
assert response.status_code == expected_status |
||||
|
assert response.json() == expected_response |
@ -0,0 +1,107 @@ |
|||||
|
import pytest |
||||
|
from fastapi.testclient import TestClient |
||||
|
|
||||
|
from ...utils import needs_py39 |
||||
|
|
||||
|
openapi_schema = { |
||||
|
"openapi": "3.0.2", |
||||
|
"info": {"title": "FastAPI", "version": "0.1.0"}, |
||||
|
"paths": { |
||||
|
"/items/": { |
||||
|
"get": { |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "Successful Response", |
||||
|
"content": {"application/json": {"schema": {}}}, |
||||
|
}, |
||||
|
"422": { |
||||
|
"description": "Validation Error", |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": { |
||||
|
"$ref": "#/components/schemas/HTTPValidationError" |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
"summary": "Read Items", |
||||
|
"operationId": "read_items_items__get", |
||||
|
"parameters": [ |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Q", "type": "string"}, |
||||
|
"name": "q", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Skip", "type": "integer", "default": 0}, |
||||
|
"name": "skip", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Limit", "type": "integer", "default": 100}, |
||||
|
"name": "limit", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
], |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
"components": { |
||||
|
"schemas": { |
||||
|
"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"}, |
||||
|
}, |
||||
|
}, |
||||
|
"HTTPValidationError": { |
||||
|
"title": "HTTPValidationError", |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"detail": { |
||||
|
"title": "Detail", |
||||
|
"type": "array", |
||||
|
"items": {"$ref": "#/components/schemas/ValidationError"}, |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@pytest.fixture(name="client") |
||||
|
def get_client(): |
||||
|
from docs_src.annotated.tutorial001_py39 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
return client |
||||
|
|
||||
|
|
||||
|
@needs_py39 |
||||
|
@pytest.mark.parametrize( |
||||
|
"path,expected_status,expected_response", |
||||
|
[ |
||||
|
("/items", 200, {"q": None, "skip": 0, "limit": 100}), |
||||
|
("/items?q=foo", 200, {"q": "foo", "skip": 0, "limit": 100}), |
||||
|
("/items?q=foo&skip=5", 200, {"q": "foo", "skip": 5, "limit": 100}), |
||||
|
("/items?q=foo&skip=5&limit=30", 200, {"q": "foo", "skip": 5, "limit": 30}), |
||||
|
("/openapi.json", 200, openapi_schema), |
||||
|
], |
||||
|
) |
||||
|
def test_get(path, expected_status, expected_response, client): |
||||
|
response = client.get(path) |
||||
|
assert response.status_code == expected_status |
||||
|
assert response.json() == expected_response |
@ -0,0 +1,100 @@ |
|||||
|
import pytest |
||||
|
from fastapi.testclient import TestClient |
||||
|
|
||||
|
from docs_src.annotated.tutorial002 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
openapi_schema = { |
||||
|
"openapi": "3.0.2", |
||||
|
"info": {"title": "FastAPI", "version": "0.1.0"}, |
||||
|
"paths": { |
||||
|
"/items/": { |
||||
|
"get": { |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "Successful Response", |
||||
|
"content": {"application/json": {"schema": {}}}, |
||||
|
}, |
||||
|
"422": { |
||||
|
"description": "Validation Error", |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": { |
||||
|
"$ref": "#/components/schemas/HTTPValidationError" |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
"summary": "Read Items", |
||||
|
"operationId": "read_items_items__get", |
||||
|
"parameters": [ |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Q", "type": "string"}, |
||||
|
"name": "q", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Skip", "type": "integer", "default": 0}, |
||||
|
"name": "skip", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Limit", "type": "integer", "default": 100}, |
||||
|
"name": "limit", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
], |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
"components": { |
||||
|
"schemas": { |
||||
|
"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"}, |
||||
|
}, |
||||
|
}, |
||||
|
"HTTPValidationError": { |
||||
|
"title": "HTTPValidationError", |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"detail": { |
||||
|
"title": "Detail", |
||||
|
"type": "array", |
||||
|
"items": {"$ref": "#/components/schemas/ValidationError"}, |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@pytest.mark.parametrize( |
||||
|
"path,expected_status,expected_response", |
||||
|
[ |
||||
|
("/items", 200, {"q": None, "skip": 0, "limit": 100}), |
||||
|
("/items?q=foo", 200, {"q": "foo", "skip": 0, "limit": 100}), |
||||
|
("/items?q=foo&skip=5", 200, {"q": "foo", "skip": 5, "limit": 100}), |
||||
|
("/items?q=foo&skip=5&limit=30", 200, {"q": "foo", "skip": 5, "limit": 30}), |
||||
|
("/openapi.json", 200, openapi_schema), |
||||
|
], |
||||
|
) |
||||
|
def test_get(path, expected_status, expected_response): |
||||
|
response = client.get(path) |
||||
|
assert response.status_code == expected_status |
||||
|
assert response.json() == expected_response |
@ -0,0 +1,107 @@ |
|||||
|
import pytest |
||||
|
from fastapi.testclient import TestClient |
||||
|
|
||||
|
from ...utils import needs_py39 |
||||
|
|
||||
|
openapi_schema = { |
||||
|
"openapi": "3.0.2", |
||||
|
"info": {"title": "FastAPI", "version": "0.1.0"}, |
||||
|
"paths": { |
||||
|
"/items/": { |
||||
|
"get": { |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "Successful Response", |
||||
|
"content": {"application/json": {"schema": {}}}, |
||||
|
}, |
||||
|
"422": { |
||||
|
"description": "Validation Error", |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": { |
||||
|
"$ref": "#/components/schemas/HTTPValidationError" |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
"summary": "Read Items", |
||||
|
"operationId": "read_items_items__get", |
||||
|
"parameters": [ |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Q", "type": "string"}, |
||||
|
"name": "q", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Skip", "type": "integer", "default": 0}, |
||||
|
"name": "skip", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Limit", "type": "integer", "default": 100}, |
||||
|
"name": "limit", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
], |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
"components": { |
||||
|
"schemas": { |
||||
|
"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"}, |
||||
|
}, |
||||
|
}, |
||||
|
"HTTPValidationError": { |
||||
|
"title": "HTTPValidationError", |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"detail": { |
||||
|
"title": "Detail", |
||||
|
"type": "array", |
||||
|
"items": {"$ref": "#/components/schemas/ValidationError"}, |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@pytest.fixture(name="client") |
||||
|
def get_client(): |
||||
|
from docs_src.annotated.tutorial002_py39 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
return client |
||||
|
|
||||
|
|
||||
|
@needs_py39 |
||||
|
@pytest.mark.parametrize( |
||||
|
"path,expected_status,expected_response", |
||||
|
[ |
||||
|
("/items", 200, {"q": None, "skip": 0, "limit": 100}), |
||||
|
("/items?q=foo", 200, {"q": "foo", "skip": 0, "limit": 100}), |
||||
|
("/items?q=foo&skip=5", 200, {"q": "foo", "skip": 5, "limit": 100}), |
||||
|
("/items?q=foo&skip=5&limit=30", 200, {"q": "foo", "skip": 5, "limit": 30}), |
||||
|
("/openapi.json", 200, openapi_schema), |
||||
|
], |
||||
|
) |
||||
|
def test_get(path, expected_status, expected_response, client): |
||||
|
response = client.get(path) |
||||
|
assert response.status_code == expected_status |
||||
|
assert response.json() == expected_response |
@ -0,0 +1,138 @@ |
|||||
|
import pytest |
||||
|
from fastapi.testclient import TestClient |
||||
|
|
||||
|
from docs_src.annotated.tutorial003 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
openapi_schema = { |
||||
|
"openapi": "3.0.2", |
||||
|
"info": {"title": "FastAPI", "version": "0.1.0"}, |
||||
|
"paths": { |
||||
|
"/items/{item_id}": { |
||||
|
"get": { |
||||
|
"summary": "Read Items", |
||||
|
"operationId": "read_items_items__item_id__get", |
||||
|
"parameters": [ |
||||
|
{ |
||||
|
"required": True, |
||||
|
"schema": { |
||||
|
"title": "Item Id", |
||||
|
"exclusiveMinimum": 0.0, |
||||
|
"type": "integer", |
||||
|
}, |
||||
|
"name": "item_id", |
||||
|
"in": "path", |
||||
|
} |
||||
|
], |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "Successful Response", |
||||
|
"content": {"application/json": {"schema": {}}}, |
||||
|
}, |
||||
|
"422": { |
||||
|
"description": "Validation Error", |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": { |
||||
|
"$ref": "#/components/schemas/HTTPValidationError" |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
"/users": { |
||||
|
"get": { |
||||
|
"summary": "Read Users", |
||||
|
"operationId": "read_users_users_get", |
||||
|
"parameters": [ |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": { |
||||
|
"title": "User Id", |
||||
|
"minLength": 1, |
||||
|
"type": "string", |
||||
|
"default": "me", |
||||
|
}, |
||||
|
"name": "user_id", |
||||
|
"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"}, |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
item_id_negative = { |
||||
|
"detail": [ |
||||
|
{ |
||||
|
"ctx": {"limit_value": 0}, |
||||
|
"loc": ["path", "item_id"], |
||||
|
"msg": "ensure this value is greater than 0", |
||||
|
"type": "value_error.number.not_gt", |
||||
|
} |
||||
|
] |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@pytest.mark.parametrize( |
||||
|
"path,expected_status,expected_response", |
||||
|
[ |
||||
|
("/items/1", 200, {"item_id": 1}), |
||||
|
("/items/-1", 422, item_id_negative), |
||||
|
("/users", 200, {"user_id": "me"}), |
||||
|
("/users?user_id=foo", 200, {"user_id": "foo"}), |
||||
|
("/openapi.json", 200, openapi_schema), |
||||
|
], |
||||
|
) |
||||
|
def test_get(path, expected_status, expected_response): |
||||
|
response = client.get(path) |
||||
|
assert response.status_code == expected_status, response.text |
||||
|
assert response.json() == expected_response |
@ -0,0 +1,145 @@ |
|||||
|
import pytest |
||||
|
from fastapi.testclient import TestClient |
||||
|
|
||||
|
from ...utils import needs_py39 |
||||
|
|
||||
|
openapi_schema = { |
||||
|
"openapi": "3.0.2", |
||||
|
"info": {"title": "FastAPI", "version": "0.1.0"}, |
||||
|
"paths": { |
||||
|
"/items/{item_id}": { |
||||
|
"get": { |
||||
|
"summary": "Read Items", |
||||
|
"operationId": "read_items_items__item_id__get", |
||||
|
"parameters": [ |
||||
|
{ |
||||
|
"required": True, |
||||
|
"schema": { |
||||
|
"title": "Item Id", |
||||
|
"exclusiveMinimum": 0.0, |
||||
|
"type": "integer", |
||||
|
}, |
||||
|
"name": "item_id", |
||||
|
"in": "path", |
||||
|
} |
||||
|
], |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "Successful Response", |
||||
|
"content": {"application/json": {"schema": {}}}, |
||||
|
}, |
||||
|
"422": { |
||||
|
"description": "Validation Error", |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": { |
||||
|
"$ref": "#/components/schemas/HTTPValidationError" |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
"/users": { |
||||
|
"get": { |
||||
|
"summary": "Read Users", |
||||
|
"operationId": "read_users_users_get", |
||||
|
"parameters": [ |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": { |
||||
|
"title": "User Id", |
||||
|
"minLength": 1, |
||||
|
"type": "string", |
||||
|
"default": "me", |
||||
|
}, |
||||
|
"name": "user_id", |
||||
|
"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"}, |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
item_id_negative = { |
||||
|
"detail": [ |
||||
|
{ |
||||
|
"ctx": {"limit_value": 0}, |
||||
|
"loc": ["path", "item_id"], |
||||
|
"msg": "ensure this value is greater than 0", |
||||
|
"type": "value_error.number.not_gt", |
||||
|
} |
||||
|
] |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@pytest.fixture(name="client") |
||||
|
def get_client(): |
||||
|
from docs_src.annotated.tutorial003_py39 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
return client |
||||
|
|
||||
|
|
||||
|
@needs_py39 |
||||
|
@pytest.mark.parametrize( |
||||
|
"path,expected_status,expected_response", |
||||
|
[ |
||||
|
("/items/1", 200, {"item_id": 1}), |
||||
|
("/items/-1", 422, item_id_negative), |
||||
|
("/users", 200, {"user_id": "me"}), |
||||
|
("/users?user_id=foo", 200, {"user_id": "foo"}), |
||||
|
("/openapi.json", 200, openapi_schema), |
||||
|
], |
||||
|
) |
||||
|
def test_get(path, expected_status, expected_response, client): |
||||
|
response = client.get(path) |
||||
|
assert response.status_code == expected_status, response.text |
||||
|
assert response.json() == expected_response |
Loading…
Reference in new issue