fix(api): use async site query for mapping (#6416)

This commit is contained in:
InfinityPacer
2026-08-23 17:54:26 +08:00
committed by GitHub
parent e1d7918297
commit f7f5a7e107
2 changed files with 23 additions and 2 deletions
+2 -2
View File
@@ -580,14 +580,14 @@ def auth_site(
response_model=_SchemaResponse[_SchemaSiteMappingData],
)
async def site_mapping(
query: SiteQueryService = Depends(get_site_sync_query_service),
query: SiteQueryService = Depends(get_site_query_service),
_: ApiPrincipal = Depends(get_current_active_superuser_async),
):
"""
获取站点域名到名称的映射关系
"""
try:
sites = query.list_sync()
sites = await query.list_ordered()
mapping = {}
for site in sites:
mapping[site.domain] = site.name
+21
View File
@@ -82,3 +82,24 @@ def test_read_sites_by_media_type_rejects_unknown_type():
assert error.value.status_code == 400
assert error.value.detail == "不支持的媒体类型"
def test_site_mapping_uses_async_query_port():
"""异步站点映射不得在事件循环内调用同步数据库查询。"""
sites = [
SimpleNamespace(domain="one.example", name="One"),
SimpleNamespace(domain="two.example", name="Two"),
]
query = SimpleNamespace(
list_ordered=AsyncMock(return_value=sites),
list_sync=pytest.fail,
)
result = asyncio.run(site_endpoint.site_mapping(query=query))
assert result.success is True
assert result.data == {
"one.example": "One",
"two.example": "Two",
}
query.list_ordered.assert_awaited_once()