| 1 |
from typing import Annotated, Any |
| 2 |
from unittest.mock import Mock, patch |
| 3 |
|
| 4 |
from fastapi import Depends, FastAPI |
| 5 |
from fastapi.testclient import TestClient |
| 6 |
|
| 7 |
from docs_src.dependencies.tutorial010_py310 import get_db |
| 8 |
|
| 9 |
|
| 10 |
def test_get_db(): |
| 11 |
app = FastAPI() |
| 12 |
|
| 13 |
@app.get("/") |
| 14 |
def read_root(c: Annotated[Any, Depends(get_db)]): |
| 15 |
return {"c": str(c)} |
| 16 |
|
| 17 |
client = TestClient(app) |
| 18 |
|
| 19 |
dbsession_mock = Mock() |
| 20 |
|
| 21 |
with patch( |
| 22 |
"docs_src.dependencies.tutorial010_py310.DBSession", |
| 23 |
return_value=dbsession_mock, |
| 24 |
create=True, |
| 25 |
): |
| 26 |
response = client.get("/") |
| 27 |
|
| 28 |
assert response.status_code == 200 |
| 29 |
assert response.json() == {"c": str(dbsession_mock)} |