Files
fastapi/tests/test_dependency_class.py
Sebastián Ramírez 0ac9b3ee5c Re-export utils from Starlette (#1064)
*  Re-export main features used from Starlette to simplify developer's code

* ♻️ Refactor Starlette exports

* ♻️ Refactor tutorial examples to use re-exported utils from Starlette

* 📝 Add examples for all middlewares

* 📝 Add new docs for middlewares

* 📝 Add examples for custom responses

* 📝 Extend docs for custom responses

* 📝 Update docs and add notes explaining re-exports from Starlette everywhere

* 🍱 Update screenshot for HTTP status

* 🔧 Update MkDocs config with new content

* ♻️ Refactor tests to use re-exported utils from Starlette

*  Re-export WebSocketDisconnect from Starlette for tests

*  Add extra tests for extra re-exported middleware

*  Add tests for re-exported responses from Starlette

*  Add docs about mounting WSGI apps

*  Add Flask as a dependency to test WSGIMiddleware

*  Test WSGIMiddleware example
2020-03-01 21:49:20 +01:00

71 lines
1.7 KiB
Python

import pytest
from fastapi import Depends, FastAPI
from fastapi.testclient import TestClient
app = FastAPI()
class CallableDependency:
def __call__(self, value: str) -> str:
return value
class AsyncCallableDependency:
async def __call__(self, value: str) -> str:
return value
class MethodsDependency:
def synchronous(self, value: str) -> str:
return value
async def asynchronous(self, value: str) -> str:
return value
callable_dependency = CallableDependency()
async_callable_dependency = AsyncCallableDependency()
methods_dependency = MethodsDependency()
@app.get("/callable-dependency")
async def get_callable_dependency(value: str = Depends(callable_dependency)):
return value
@app.get("/async-callable-dependency")
async def get_callable_dependency(value: str = Depends(async_callable_dependency)):
return value
@app.get("/synchronous-method-dependency")
async def get_synchronous_method_dependency(
value: str = Depends(methods_dependency.synchronous),
):
return value
@app.get("/asynchronous-method-dependency")
async def get_asynchronous_method_dependency(
value: str = Depends(methods_dependency.asynchronous),
):
return value
client = TestClient(app)
@pytest.mark.parametrize(
"route,value",
[
("/callable-dependency", "callable-dependency"),
("/async-callable-dependency", "async-callable-dependency"),
("/synchronous-method-dependency", "synchronous-method-dependency"),
("/asynchronous-method-dependency", "asynchronous-method-dependency"),
],
)
def test_class_dependency(route, value):
response = client.get(route, params={"value": value})
assert response.status_code == 200
assert response.json() == value