Skip to content

Commit

Permalink
add tests for web.api (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
bokobring authored Jan 1, 2025
1 parent 5199f83 commit 8db5200
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
22 changes: 11 additions & 11 deletions physicsLab/web/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ class _login_res(TypedDict):
AuthCode: str
Data: dict

def get_avatar(id: str, index: int, category: str, size_category: str) -> bytes:
def get_avatar(target_id: str, index: int, category: str, size_category: str) -> bytes:
''' 获取头像/实验封面
@param id: 用户id或实验id
@param target_id: 用户id或实验id
@param index: 历史图片的索引
@param category: 只能为 "experiments" 或 "users"
@param size_category: 只能为 "small.round" 或 "thumbnail" 或 "full"
'''
if not isinstance(id, str) or \
if not isinstance(target_id, str) or \
not isinstance(index, int) or \
not isinstance(category, str) or \
not isinstance(size_category, str):
Expand All @@ -96,15 +96,15 @@ def get_avatar(id: str, index: int, category: str, size_category: str) -> bytes:

response = requests.get(
f"http://physics-static-cn.turtlesim.com:80/{category}"
f"/{id[0:4]}/{id[4:6]}/{id[6:8]}/{id[8:]}/{index}.jpg!{size_category}",
f"/{target_id[0:4]}/{target_id[4:6]}/{target_id[6:8]}/{target_id[8:]}/{index}.jpg!{size_category}",
)

if b'<Error>' in response.content:
raise IndexError("avatar not found")
return response.content

async def async_get_avatar(id: str, index: int, category: str, size_category: str):
return await _async_wrapper(get_avatar, id, index, category, size_category)
async def async_get_avatar(target_id: str, index: int, category: str, size_category: str):
return await _async_wrapper(get_avatar, target_id, index, category, size_category)

class User:
def __init__(
Expand Down Expand Up @@ -491,8 +491,8 @@ def get_comments(
skip: int = 0,
comment_id: Optional[str] = None,
) -> dict:
''' 获取留言板信息
@param id: 物实用户的ID/实验的id
''' 获取评论板信息
@param target_id: 物实用户的ID/实验的id
@param target_type: User, Discussion, Experiment
@param take: 获取留言的数量
@param skip: 跳过的留言数量, 为(unix时间戳 * 1000)
Expand Down Expand Up @@ -529,12 +529,12 @@ def get_comments(

async def async_get_comments(
self,
id: str,
target_id: str,
target_type: str,
take: int = 16,
skip: int = 0,
):
return await _async_wrapper(self.get_comments, id, target_type, take, skip)
return await _async_wrapper(self.get_comments, target_id, target_type, take, skip)

def get_summary(self, content_id: str, category: Category) -> dict:
''' 获取实验介绍
Expand Down Expand Up @@ -707,7 +707,7 @@ async def async_star_content(self, content_id: str, category: Category, status:

def upload_image(self, policy: str, authorization: str, image_path: str) -> dict:
''' 上传实验图片
@policy @authorization 可通过/Contents/SubmitExperiment获取
@param authorization 可通过/Contents/SubmitExperiment获取
@param image_path: 待上传的图片在本地的路径
'''
if policy is None or authorization is None:
Expand Down
67 changes: 62 additions & 5 deletions test_pl/test_pl_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,90 @@
from .base import *

class _WebTest:
# NOTE: 暴露token与auth_code是危险的行为
# NOTE: 暴露token与auth_code是危险的行为,可能导致被盗号
# 但 @AMDYES 主动暴露了自己的token与auth_code
# 详见 <discussion=674ab7f4ce449cb493ced3a7>转让此号</discussion>
user = web.User(
token="yYReEg0oCtGlVmJqQwFr1zZXhL9NAvBH",
auth_code="nENz1xlrueQUmkqjYZKtCG9SI53vF8Xc"
)

# 验证用户ID是否为测试用户
if user.user_id != "5ce629e157035932b52f9315":
raise TestError

raise TestError("User ID does not match")
@staticmethod
async def test_get_start_page():
''' 测试获取主页数据 '''
await web.async_get_start_page()

@staticmethod
async def test_get_avatar():
''' 测试获取头像 '''
await web.async_get_avatar(
target_id="5ce629e157035932b52f9315", # 用户ID
index=1, # 头像索引
category="users", # 用户头像
size_category="small.round" # 头像尺寸
)

@classmethod
async def test_get_library(cls):
''' 测试获取社区作品列表 '''
await cls.user.async_get_library()

@classmethod
async def test_query_experiments(cls):
await cls.user.async_query_experiments()
''' 测试查询实验 '''
await cls.user.async_query_experiments(
tags=[Tag.Featured], # 精选标签
category=Category.Experiment # 实验区
)

@classmethod
async def test_get_experiment(cls):
await cls.user.async_get_experiment("642cf37a494746375aae306a", Category.Discussion)
''' 测试获取实验数据 '''
await cls.user.async_get_experiment(
content_id="6317fabebfd18200013c710c", # 作品序列号
category=Category.Experiment
)

@classmethod
async def test_get_comments(cls):
''' 测试获取评论 '''
await cls.user.async_get_comments(
target_id="6317fabebfd18200013c710c",
target_type="Experiment" # 目标类型为实验
)

@classmethod
async def test_get_summary(cls):
''' 测试获取实验介绍 '''
await cls.user.async_get_summary(
content_id="6317fabebfd18200013c710c",
category=Category.Experiment
)

@classmethod
async def test_get_derivatives(cls):
''' 测试获取作品详细信息 '''
await cls.user.async_get_derivatives(
content_id="6317fabebfd18200013c710c",
category=Category.Experiment
)

@classmethod
async def test_get_user(cls):
''' 测试获取用户信息 '''
await cls.user.async_get_user(user_id="5ce629e157035932b52f9315")

@classmethod
async def test_get_profile(cls):
''' 测试获取用户个人资料 '''
await cls.user.async_get_profile()

async def test_web_main():
''' 收集并运行所有测试任务 '''
test_tasks = []
for _, a_test_task in inspect.getmembers(_WebTest, inspect.iscoroutinefunction):
test_tasks.append(a_test_task)
Expand All @@ -41,6 +97,7 @@ async def test_web_main():
class WebTest(TestCase, ViztracerTool):
@staticmethod
def test_web_impl():
''' 运行所有测试并记录耗时 '''
start = time.time()
asyncio.run(test_web_main())
end = time.time()
Expand Down

0 comments on commit 8db5200

Please sign in to comment.