ForcePilot/backend/test/unit/channel/startup/test_startup_module.py
Kris 6c95dc006a test: 新增渠道模块全链路单元测试用例与目录结构
完成渠道模块的单元测试目录搭建,新增多个领域模型、端口、中间件、事件、服务以及基础设施层的单元测试文件,同时补充了conftest.py的环境变量配置,完善测试基础环境。
2026-05-30 21:55:35 +08:00

77 lines
2.5 KiB
Python

from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from yuxi.channel.startup import get_container, init_channel, register_channel_middleware, shutdown_channel
class TestStartupModule:
@pytest.fixture(autouse=True)
def reset_container(self):
import yuxi.channel.startup as startup
startup._container = None
yield
startup._container = None
@pytest.mark.asyncio
async def test_init_channel(self):
mock_container = MagicMock()
with patch("yuxi.channel.startup.ChannelContainerFactory") as mock_factory:
mock_factory.create = AsyncMock(return_value=mock_container)
result = await init_channel(redis_url="redis://localhost")
assert result is mock_container
mock_factory.create.assert_awaited_once()
@pytest.mark.asyncio
async def test_shutdown_channel_with_container(self):
mock_container = AsyncMock()
await shutdown_channel(mock_container)
mock_container.shutdown.assert_awaited_once()
@pytest.mark.asyncio
async def test_shutdown_channel_with_global(self):
import yuxi.channel.startup as startup
mock_container = AsyncMock()
startup._container = mock_container
await shutdown_channel()
mock_container.shutdown.assert_awaited_once()
assert startup._container is None
@pytest.mark.asyncio
async def test_shutdown_channel_none(self):
result = await shutdown_channel(None)
assert result is None
def test_get_container_initial(self):
result = get_container()
assert result is None
def test_get_container_after_init(self):
import yuxi.channel.startup as startup
mock_container = MagicMock()
startup._container = mock_container
result = get_container()
assert result is mock_container
def test_register_channel_middleware(self):
import yuxi.channel.startup as startup
mock_container = MagicMock()
startup._container = mock_container
app = MagicMock()
app.state = MagicMock()
with patch("yuxi.channel.startup.setup_channel") as mock_setup:
register_channel_middleware(app)
mock_setup.assert_called_once_with(app, mock_container)
def test_register_channel_middleware_no_container(self):
app = MagicMock()
with patch("yuxi.channel.startup.setup_channel") as mock_setup:
register_channel_middleware(app)
mock_setup.assert_not_called()