ForcePilot/backend/test/unit/scheduler/infrastructure/test_container.py
Kris 41bd0a618c refactor: 统一将日志方法从 warn 改为 warning
将代码库中所有使用 logger.warn 的地方替换为标准的 logger.warning,对齐日志方法命名规范,修复多处测试和业务代码中的方法调用不匹配问题。
2026-07-13 17:32:29 +08:00

165 lines
6.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""infrastructure/container.py 装配容器单元测试。
覆盖 ``yuxi.scheduler.infrastructure.container`` 模块的两个 factory 函数:
- ``create_scheduler_service``api 进程请求级 factoryhandler_registry=None
- ``create_worker_scheduler_service``worker 进程请求级 factory完整 runtime
验证装配结果 ``SchedulerService`` 实例的依赖注入正确性。
"""
from __future__ import annotations
from unittest.mock import MagicMock, patch
import pytest
from yuxi.scheduler.framework.runtime import HandlerRegistry, RuntimeServices
from yuxi.scheduler.infrastructure.container import (
SchedulerService,
create_scheduler_service,
create_worker_scheduler_service,
)
pytestmark = pytest.mark.unit
@pytest.fixture
def db_session() -> MagicMock:
"""提供 MagicMock 模拟 AsyncSession。"""
return MagicMock()
# ─── create_scheduler_service ─────────────────────────────────────────────
@pytest.mark.unit
class TestCreateSchedulerService:
def test_returns_scheduler_service_instance(self, db_session: MagicMock):
# Arrange & Act
with patch("yuxi.scheduler.infrastructure.container.app_config"):
service = create_scheduler_service(db_session)
# Assert
assert isinstance(service, SchedulerService)
def test_handler_registry_is_none(self, db_session: MagicMock):
# Arrange & Act - api 进程 handler_registry=None
with patch("yuxi.scheduler.infrastructure.container.app_config"):
service = create_scheduler_service(db_session)
# Assert
assert service._runtime.handler_registry is None
def test_arq_pool_defaults_to_none(self, db_session: MagicMock):
with patch("yuxi.scheduler.infrastructure.container.app_config"):
service = create_scheduler_service(db_session)
assert service._runtime.arq_pool is None
def test_arq_pool_passed_through(self, db_session: MagicMock):
# Arrange
pool = MagicMock()
# Act
with patch("yuxi.scheduler.infrastructure.container.app_config"):
service = create_scheduler_service(db_session, arq_pool=pool)
# Assert
assert service._runtime.arq_pool is pool
def test_repos_injected(self, db_session: MagicMock):
with patch("yuxi.scheduler.infrastructure.container.app_config"):
service = create_scheduler_service(db_session)
# Assert - 4 个仓储实例被注入
assert service._repos.task is not None
assert service._repos.run_log is not None
assert service._repos.run_log_daily is not None
assert service._repos.idempotency is not None
def test_tx_port_injected(self, db_session: MagicMock):
with patch("yuxi.scheduler.infrastructure.container.app_config"):
service = create_scheduler_service(db_session)
assert service._tx_port is not None
def test_config_injected(self, db_session: MagicMock):
with patch("yuxi.scheduler.infrastructure.container.app_config") as mock_config:
service = create_scheduler_service(db_session)
assert service._runtime.config is mock_config
# ─── create_worker_scheduler_service ──────────────────────────────────────
@pytest.mark.unit
class TestCreateWorkerSchedulerService:
def test_returns_scheduler_service_instance(self, db_session: MagicMock):
# Arrange
registry = HandlerRegistry()
pool = MagicMock()
# Act
with patch("yuxi.scheduler.infrastructure.container.app_config"):
service = create_worker_scheduler_service(
db_session, handler_registry=registry, arq_pool=pool
)
# Assert
assert isinstance(service, SchedulerService)
def test_handler_registry_injected(self, db_session: MagicMock):
registry = HandlerRegistry()
pool = MagicMock()
with patch("yuxi.scheduler.infrastructure.container.app_config"):
service = create_worker_scheduler_service(
db_session, handler_registry=registry, arq_pool=pool
)
assert service._runtime.handler_registry is registry
def test_arq_pool_injected(self, db_session: MagicMock):
registry = HandlerRegistry()
pool = MagicMock()
with patch("yuxi.scheduler.infrastructure.container.app_config"):
service = create_worker_scheduler_service(
db_session, handler_registry=registry, arq_pool=pool
)
assert service._runtime.arq_pool is pool
def test_repos_injected(self, db_session: MagicMock):
registry = HandlerRegistry()
pool = MagicMock()
with patch("yuxi.scheduler.infrastructure.container.app_config"):
service = create_worker_scheduler_service(
db_session, handler_registry=registry, arq_pool=pool
)
assert service._repos.task is not None
assert service._repos.run_log is not None
assert service._repos.run_log_daily is not None
assert service._repos.idempotency is not None
def test_tx_port_injected(self, db_session: MagicMock):
registry = HandlerRegistry()
pool = MagicMock()
with patch("yuxi.scheduler.infrastructure.container.app_config"):
service = create_worker_scheduler_service(
db_session, handler_registry=registry, arq_pool=pool
)
assert service._tx_port is not None
# ─── RuntimeServices 装配 ─────────────────────────────────────────────────
@pytest.mark.unit
class TestRuntimeServicesAssembly:
def test_api_runtime_has_none_registry(self, db_session: MagicMock):
with patch("yuxi.scheduler.infrastructure.container.app_config"):
service = create_scheduler_service(db_session)
# Assert - RuntimeServices dataclass 字段正确
assert isinstance(service._runtime, RuntimeServices)
assert service._runtime.handler_registry is None
def test_worker_runtime_has_registry_and_pool(self, db_session: MagicMock):
registry = HandlerRegistry()
pool = MagicMock()
with patch("yuxi.scheduler.infrastructure.container.app_config"):
service = create_worker_scheduler_service(
db_session, handler_registry=registry, arq_pool=pool
)
assert isinstance(service._runtime, RuntimeServices)
assert service._runtime.handler_registry is registry
assert service._runtime.arq_pool is pool