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

86 lines
2.9 KiB
Python

from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock
import pytest
from yuxi.channel.application.service.binding_service import BindingService
from yuxi.channel.domain.model.binding.channel_binding import ChannelBinding
class TestBindingService:
@pytest.fixture
def mock_repo(self):
return AsyncMock()
@pytest.fixture
def binding_service(self, mock_repo):
return BindingService(mock_repo)
@pytest.mark.asyncio
async def test_create(self, binding_service, mock_repo):
mock_repo.create_binding.return_value = ChannelBinding(
id=1,
channel_type="web",
account_id="user1",
group_id="",
agent_config_id=1,
)
result = await binding_service.create(
channel_type="web",
account_id="user1",
group_id="",
agent_config_id=1,
)
assert result.id == 1
mock_repo.create_binding.assert_awaited_once()
@pytest.mark.asyncio
async def test_delete(self, binding_service, mock_repo):
mock_repo.delete_binding.return_value = True
result = await binding_service.delete(1)
assert result is True
mock_repo.delete_binding.assert_awaited_once_with(1)
@pytest.mark.asyncio
async def test_get(self, binding_service, mock_repo):
mock_repo.get_binding.return_value = ChannelBinding(
id=1,
channel_type="web",
account_id="user1",
group_id="",
agent_config_id=1,
)
result = await binding_service.get(1)
assert result is not None
assert result.id == 1
@pytest.mark.asyncio
async def test_get_none(self, binding_service, mock_repo):
mock_repo.get_binding.return_value = None
result = await binding_service.get(1)
assert result is None
@pytest.mark.asyncio
async def test_list(self, binding_service, mock_repo):
mock_repo.list_bindings.return_value = (
[
ChannelBinding(id=1, channel_type="web", account_id="user1", group_id="", agent_config_id=1),
ChannelBinding(id=2, channel_type="feishu", account_id="user2", group_id="", agent_config_id=2),
],
2,
)
results, total = await binding_service.list()
assert len(results) == 2
assert total == 2
@pytest.mark.asyncio
async def test_list_with_filter(self, binding_service, mock_repo):
mock_repo.list_bindings.return_value = (
[ChannelBinding(id=1, channel_type="web", account_id="user1", group_id="", agent_config_id=1)],
1,
)
results, total = await binding_service.list(channel_type="web")
assert len(results) == 1
mock_repo.list_bindings.assert_awaited_once_with(channel_type="web", offset=0, limit=50)