from __future__ import annotations import pytest from fastapi import HTTPException from sqlalchemy.exc import ProgrammingError from backend.api.operations._deps import OperationsContext, require_operations_permission from backend.services.operations.deploy_model_queries import ( _load_deploy_rows, _load_monitor_snapshots, ) @pytest.mark.asyncio async def test_missing_deploy_table_degrades_to_empty_models() -> None: class MissingTableSession: async def execute(self, *args, **kwargs): raise ProgrammingError( "SELECT ...", {}, Exception(1146, "table does not exist"), ) rows = await _load_deploy_rows( MissingTableSession(), workspace_code="001", workspace_name="001", ) assert rows == [] @pytest.mark.asyncio async def test_missing_monitoring_table_degrades_to_empty_snapshots() -> None: class MissingTableSession: async def execute(self, *args, **kwargs): raise ProgrammingError( "SELECT ...", {}, Exception(1146, "table does not exist"), ) snapshots = await _load_monitor_snapshots(MissingTableSession(), "W" * 26) assert snapshots == {} @pytest.mark.asyncio async def test_operations_permission_dependency_checks_effective_permissions() -> None: allowed = OperationsContext( request_id="R" * 26, user_id="U" * 26, workspace_id="W" * 26, workspace_code="001", workspace_name="001", role="business_team", permissions=frozenset({"operations:model-overview:view"}), ) denied = OperationsContext( request_id="R" * 26, user_id="U" * 26, workspace_id="W" * 26, workspace_code="001", workspace_name="001", role="business_team", permissions=frozenset(), ) dependency = require_operations_permission("operations:model-overview:view") assert await dependency(allowed) is allowed with pytest.raises(HTTPException) as error: await dependency(denied) assert error.value.status_code == 403