Remove Mock fallback from runtime_state and add runtime settings regression tests
This commit is contained in:
@@ -495,6 +495,8 @@ A test that forces `_backend_cmd_supported = False` (by mocking a socket error)
|
|||||||
|
|
||||||
### Task 17 — Remove unittest.mock.Mock check from production runtime_state
|
### Task 17 — Remove unittest.mock.Mock check from production runtime_state
|
||||||
|
|
||||||
|
**Status:** Completed
|
||||||
|
|
||||||
**Severity:** Low
|
**Severity:** Low
|
||||||
|
|
||||||
**Where:**
|
**Where:**
|
||||||
|
|||||||
@@ -15,11 +15,6 @@ from typing import TYPE_CHECKING, Any
|
|||||||
|
|
||||||
from starlette.datastructures import State
|
from starlette.datastructures import State
|
||||||
|
|
||||||
try:
|
|
||||||
from unittest.mock import Mock as _Mock
|
|
||||||
except ImportError: # pragma: no cover
|
|
||||||
_Mock = None
|
|
||||||
|
|
||||||
from app.models.config import PendingRecovery
|
from app.models.config import PendingRecovery
|
||||||
from app.models.server import ServerStatus
|
from app.models.server import ServerStatus
|
||||||
|
|
||||||
@@ -47,7 +42,7 @@ class RuntimeState:
|
|||||||
server_status: ServerStatus = field(default_factory=lambda: ServerStatus(online=False))
|
server_status: ServerStatus = field(default_factory=lambda: ServerStatus(online=False))
|
||||||
pending_recovery: PendingRecovery | None = None
|
pending_recovery: PendingRecovery | None = None
|
||||||
last_activation: ActivationRecord | None = None
|
last_activation: ActivationRecord | None = None
|
||||||
runtime_settings: "Settings" | None = None
|
runtime_settings: Settings | None = None
|
||||||
|
|
||||||
|
|
||||||
class ApplicationState(State):
|
class ApplicationState(State):
|
||||||
@@ -104,8 +99,6 @@ def get_app_settings(app: Any) -> Settings:
|
|||||||
def get_effective_settings(app: Any) -> Settings:
|
def get_effective_settings(app: Any) -> Settings:
|
||||||
"""Return the effective settings for the current application instance."""
|
"""Return the effective settings for the current application instance."""
|
||||||
runtime_settings = getattr(app.state, "runtime_settings", None)
|
runtime_settings = getattr(app.state, "runtime_settings", None)
|
||||||
if runtime_settings is not None and _Mock is not None and isinstance(runtime_settings, _Mock):
|
|
||||||
return get_app_settings(app)
|
|
||||||
if runtime_settings is not None:
|
if runtime_settings is not None:
|
||||||
return runtime_settings
|
return runtime_settings
|
||||||
return get_app_settings(app)
|
return get_app_settings(app)
|
||||||
|
|||||||
47
backend/tests/test_utils/test_runtime_state.py
Normal file
47
backend/tests/test_utils/test_runtime_state.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from app.config import Settings
|
||||||
|
from app.utils.runtime_state import get_app_settings, get_effective_settings
|
||||||
|
|
||||||
|
|
||||||
|
class _FakeState:
|
||||||
|
def __init__(self, settings: Settings, runtime_settings: object | None = None) -> None:
|
||||||
|
self.settings = settings
|
||||||
|
self.runtime_settings = runtime_settings
|
||||||
|
|
||||||
|
|
||||||
|
class _FakeApp:
|
||||||
|
def __init__(self, state: object) -> None:
|
||||||
|
self.state = state
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_effective_settings_returns_runtime_settings() -> None:
|
||||||
|
settings = Settings(session_secret="secret")
|
||||||
|
runtime_settings = settings.model_copy(update={"database_path": "/tmp/runtime.db"})
|
||||||
|
app = _FakeApp(_FakeState(settings=settings, runtime_settings=runtime_settings))
|
||||||
|
|
||||||
|
assert get_effective_settings(app) is runtime_settings
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_effective_settings_returns_app_settings_when_runtime_none() -> None:
|
||||||
|
settings = Settings(session_secret="secret")
|
||||||
|
app = _FakeApp(_FakeState(settings=settings))
|
||||||
|
|
||||||
|
assert get_effective_settings(app) is settings
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_effective_settings_returns_mock_runtime_settings() -> None:
|
||||||
|
settings = Settings(session_secret="secret")
|
||||||
|
mock_settings = MagicMock()
|
||||||
|
app = _FakeApp(_FakeState(settings=settings, runtime_settings=mock_settings))
|
||||||
|
|
||||||
|
assert get_effective_settings(app) is mock_settings
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_app_settings_reads_bootstrap_settings() -> None:
|
||||||
|
settings = Settings(session_secret="secret")
|
||||||
|
app = _FakeApp(_FakeState(settings=settings))
|
||||||
|
|
||||||
|
assert get_app_settings(app) is settings
|
||||||
Reference in New Issue
Block a user