test: 更新 AgentScope 相关单元测试与集成测试
- 重命名 test_react_runner.py 为 test_runner.py - 新增 test_utils.py 测试工具函数 - 更新现有测试用例适配新架构
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import cast
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
@@ -10,6 +11,7 @@ from fastapi import HTTPException
|
||||
from core.auth.models import CurrentUser
|
||||
from models.friendships import Friendship, FriendshipStatus
|
||||
from models.inbox_messages import InboxMessage, InboxMessageStatus, InboxMessageType
|
||||
from models.profile import Profile
|
||||
from v1.friendships.repository import FriendshipRepository
|
||||
from v1.friendships.schemas import (
|
||||
FriendRequestCreate,
|
||||
@@ -22,14 +24,14 @@ def _create_mock_profile(
|
||||
user_id: UUID = UUID("00000000-0000-0000-0000-000000000001"),
|
||||
username: str = "testuser",
|
||||
avatar_url: str | None = None,
|
||||
) -> MagicMock:
|
||||
) -> Profile:
|
||||
"""Create a mock Profile ORM object."""
|
||||
profile = MagicMock()
|
||||
profile.id = user_id
|
||||
profile.username = username
|
||||
profile.avatar_url = avatar_url
|
||||
profile.bio = None
|
||||
return profile
|
||||
return cast(Profile, profile)
|
||||
|
||||
|
||||
class FakeFriendshipRepo:
|
||||
@@ -65,7 +67,7 @@ class FakeFriendshipRepo:
|
||||
inbox.status = InboxMessageStatus.PENDING
|
||||
inbox.message_type = InboxMessageType.FRIEND_REQUEST
|
||||
inbox.friendship_id = friendship.id
|
||||
inbox.content = content
|
||||
inbox.content = {"type": "request", "message": content}
|
||||
self._inbox_messages.append(inbox)
|
||||
|
||||
return friendship, inbox
|
||||
@@ -92,7 +94,7 @@ class FakeFriendshipRepo:
|
||||
inbox.status = InboxMessageStatus.PENDING
|
||||
inbox.message_type = InboxMessageType.FRIEND_REQUEST
|
||||
inbox.friendship_id = friendship.id
|
||||
inbox.content = content
|
||||
inbox.content = {"type": "request", "message": content}
|
||||
self._inbox_messages.append(inbox)
|
||||
|
||||
return friendship, inbox
|
||||
@@ -121,6 +123,16 @@ class FakeFriendshipRepo:
|
||||
return f
|
||||
return None
|
||||
|
||||
async def get_friendships_by_ids(
|
||||
self, friendship_ids: list[UUID]
|
||||
) -> dict[UUID, Friendship]:
|
||||
friendship_set = set(friendship_ids)
|
||||
return {
|
||||
f.id: f
|
||||
for f in self._friendships
|
||||
if getattr(f, "id", None) in friendship_set
|
||||
}
|
||||
|
||||
async def get_inbox_messages_for_user(
|
||||
self, user_id: UUID, status: InboxMessageStatus | None = None
|
||||
) -> list[InboxMessage]:
|
||||
@@ -148,12 +160,41 @@ class FakeFriendshipRepo:
|
||||
class FakeUserRepo:
|
||||
"""Fake user repository for testing."""
|
||||
|
||||
def __init__(self, profiles: dict[UUID, MagicMock] | None = None) -> None:
|
||||
def __init__(self, profiles: dict[UUID, Profile] | None = None) -> None:
|
||||
self._profiles = profiles or {}
|
||||
|
||||
async def get_by_user_id(self, user_id: UUID) -> MagicMock | None:
|
||||
async def get_by_user_id(self, user_id: UUID) -> Profile | None:
|
||||
return self._profiles.get(user_id)
|
||||
|
||||
async def get_by_user_ids(self, user_ids: list[UUID]) -> dict[UUID, Profile]:
|
||||
user_id_set = set(user_ids)
|
||||
return {
|
||||
uid: profile
|
||||
for uid, profile in self._profiles.items()
|
||||
if uid in user_id_set
|
||||
}
|
||||
|
||||
async def get_by_username(self, username: str) -> Profile | None:
|
||||
for profile in self._profiles.values():
|
||||
if profile.username == username:
|
||||
return profile
|
||||
return None
|
||||
|
||||
async def update_by_user_id(
|
||||
self, user_id: UUID, update_data: dict[str, str | None]
|
||||
) -> Profile | None:
|
||||
del update_data
|
||||
return self._profiles.get(user_id)
|
||||
|
||||
async def search_users(self, query: str, limit: int = 20) -> list[Profile]:
|
||||
del limit
|
||||
query_lower = query.lower()
|
||||
return [
|
||||
profile
|
||||
for profile in self._profiles.values()
|
||||
if query_lower in profile.username.lower()
|
||||
]
|
||||
|
||||
|
||||
_repo_check: FriendshipRepository = FakeFriendshipRepo()
|
||||
_user_repo_check: UserRepository = FakeUserRepo()
|
||||
@@ -208,7 +249,9 @@ class TestSendRequest:
|
||||
current_user=current_user,
|
||||
)
|
||||
|
||||
result = await service.send_request(FriendRequestCreate(target_user_id=USER_B))
|
||||
result = await service.send_request(
|
||||
FriendRequestCreate(target_user_id=USER_B, content=None)
|
||||
)
|
||||
|
||||
assert result is not None
|
||||
mock_session.commit.assert_awaited_once()
|
||||
@@ -233,7 +276,7 @@ class TestSendRequest:
|
||||
FriendRequestCreate(target_user_id=USER_B, content=content)
|
||||
)
|
||||
|
||||
assert result.content == content
|
||||
assert result.content == {"type": "request", "message": content}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_request_to_self_raises_400(
|
||||
@@ -252,7 +295,7 @@ class TestSendRequest:
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await service.send_request(
|
||||
FriendRequestCreate(target_user_id=current_user.id)
|
||||
FriendRequestCreate(target_user_id=current_user.id, content=None)
|
||||
)
|
||||
|
||||
assert exc_info.value.status_code == 400
|
||||
@@ -280,7 +323,9 @@ class TestSendRequest:
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await service.send_request(FriendRequestCreate(target_user_id=USER_B))
|
||||
await service.send_request(
|
||||
FriendRequestCreate(target_user_id=USER_B, content=None)
|
||||
)
|
||||
|
||||
assert exc_info.value.status_code == 400
|
||||
|
||||
@@ -307,7 +352,9 @@ class TestSendRequest:
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await service.send_request(FriendRequestCreate(target_user_id=USER_B))
|
||||
await service.send_request(
|
||||
FriendRequestCreate(target_user_id=USER_B, content=None)
|
||||
)
|
||||
|
||||
assert exc_info.value.status_code == 400
|
||||
|
||||
|
||||
Reference in New Issue
Block a user