Some checks failed
CI / lint-and-test (push) Failing after 11s
- Upgraded `pytest-asyncio` to version 1.0 to ensure compatibility with the latest features and improvements. - Increased the coverage threshold in pytest configuration to 80%, enhancing the quality assurance process. - Added a new `conftest.py` file to manage shared fixtures and improve test organization. - Introduced multiple new test files to cover various components, ensuring comprehensive test coverage across the application. - Updated the `.coverage` file to reflect the latest coverage metrics.
40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
"""Tests for duty_teller.db.session (session_scope rollback/close, get_engine)."""
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from duty_teller.db import session as session_mod
|
|
|
|
|
|
def test_session_scope_rollback_on_exception():
|
|
"""session_scope: on exception inside block, rollback and close are called."""
|
|
mock_session = MagicMock()
|
|
with patch.object(session_mod, "get_session", return_value=mock_session):
|
|
with pytest.raises(ValueError):
|
|
with session_mod.session_scope("sqlite:///:memory:") as s:
|
|
assert s is mock_session
|
|
raise ValueError("test")
|
|
mock_session.rollback.assert_called_once()
|
|
mock_session.close.assert_called_once()
|
|
|
|
|
|
def test_session_scope_close_on_success():
|
|
"""session_scope: on normal exit, close is called (no rollback)."""
|
|
mock_session = MagicMock()
|
|
with patch.object(session_mod, "get_session", return_value=mock_session):
|
|
with session_mod.session_scope("sqlite:///:memory:") as s:
|
|
assert s is mock_session
|
|
mock_session.rollback.assert_not_called()
|
|
mock_session.close.assert_called_once()
|
|
|
|
|
|
def test_get_engine_same_url_returns_same_instance():
|
|
"""get_engine: same URL returns the same engine instance."""
|
|
session_mod._engine = None
|
|
session_mod._SessionLocal = None
|
|
url = "sqlite:///:memory:"
|
|
e1 = session_mod.get_engine(url)
|
|
e2 = session_mod.get_engine(url)
|
|
assert e1 is e2
|