Files
fastapi/tests/test_tutorial/test_dependencies/test_tutorial007.py
T
3063ada72f ✅ Add missing tests for code examples (#14569)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nils-Hero Lindemann <nilsherolindemann@proton.me>
2025-12-26 11:43:02 +01:00

25 lines
642 B
Python

import asyncio
from contextlib import asynccontextmanager
from unittest.mock import Mock, patch
from docs_src.dependencies.tutorial007_py39 import get_db
def test_get_db(): # Just for coverage
async def test_async_gen():
cm = asynccontextmanager(get_db)
async with cm() as db_session:
return db_session
dbsession_moock = Mock()
with patch(
"docs_src.dependencies.tutorial007_py39.DBSession",
return_value=dbsession_moock,
create=True,
):
value = asyncio.run(test_async_gen())
assert value is dbsession_moock
dbsession_moock.close.assert_called_once()