86 lines
2.9 KiB
Python
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)
|