mirror of
https://github.com/fastapi/full-stack-fastapi-template.git
synced 2026-11-03 12:29:42 +00:00
♻️ Simplify database readiness checks (#2460)
This commit is contained in:
committed by
GitHub
parent
2ccfa25845
commit
f8dd304eeb
@@ -1,33 +0,0 @@
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from sqlmodel import select
|
||||
|
||||
from app.backend_pre_start import init, logger
|
||||
|
||||
|
||||
def test_init_successful_connection() -> None:
|
||||
engine_mock = MagicMock()
|
||||
|
||||
session_mock = MagicMock()
|
||||
session_mock.__enter__.return_value = session_mock
|
||||
|
||||
select1 = select(1)
|
||||
|
||||
with (
|
||||
patch("app.backend_pre_start.Session", return_value=session_mock),
|
||||
patch("app.backend_pre_start.select", return_value=select1),
|
||||
patch.object(logger, "info"),
|
||||
patch.object(logger, "error"),
|
||||
patch.object(logger, "warn"),
|
||||
):
|
||||
try:
|
||||
init(engine_mock)
|
||||
connection_successful = True
|
||||
except Exception:
|
||||
connection_successful = False
|
||||
|
||||
assert connection_successful, (
|
||||
"The database connection should be successful and not raise an exception."
|
||||
)
|
||||
|
||||
session_mock.exec.assert_called_once_with(select1)
|
||||
@@ -1,33 +0,0 @@
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from sqlmodel import select
|
||||
|
||||
from app.tests_pre_start import init, logger
|
||||
|
||||
|
||||
def test_init_successful_connection() -> None:
|
||||
engine_mock = MagicMock()
|
||||
|
||||
session_mock = MagicMock()
|
||||
session_mock.__enter__.return_value = session_mock
|
||||
|
||||
select1 = select(1)
|
||||
|
||||
with (
|
||||
patch("app.tests_pre_start.Session", return_value=session_mock),
|
||||
patch("app.tests_pre_start.select", return_value=select1),
|
||||
patch.object(logger, "info"),
|
||||
patch.object(logger, "error"),
|
||||
patch.object(logger, "warn"),
|
||||
):
|
||||
try:
|
||||
init(engine_mock)
|
||||
connection_successful = True
|
||||
except Exception:
|
||||
connection_successful = False
|
||||
|
||||
assert connection_successful, (
|
||||
"The database connection should be successful and not raise an exception."
|
||||
)
|
||||
|
||||
session_mock.exec.assert_called_once_with(select1)
|
||||
Reference in New Issue
Block a user