test_test_pre_start.py 939 B

1234567891011121314151617181920212223242526272829
  1. from unittest.mock import MagicMock, patch
  2. from app.tests_pre_start import init, logger
  3. from sqlmodel import select
  4. def test_init_successful_connection() -> None:
  5. engine_mock = MagicMock()
  6. session_mock = MagicMock()
  7. exec_mock = MagicMock(return_value=True)
  8. session_mock.configure_mock(**{"exec.return_value": exec_mock})
  9. with patch("sqlmodel.Session", return_value=session_mock), patch.object(
  10. logger, "info"
  11. ), patch.object(logger, "error"), patch.object(logger, "warn"):
  12. try:
  13. init(engine_mock)
  14. connection_successful = True
  15. except Exception:
  16. connection_successful = False
  17. assert (
  18. connection_successful
  19. ), "The database connection should be successful and not raise an exception."
  20. assert session_mock.exec.called_once_with(
  21. select(1)
  22. ), "The session should execute a select statement once."