diff --git a/app/api/endpoints/site.py b/app/api/endpoints/site.py index e1dcae9dc..08c932a12 100644 --- a/app/api/endpoints/site.py +++ b/app/api/endpoints/site.py @@ -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 diff --git a/tests/test_site_media_filter.py b/tests/test_site_media_filter.py index b386bb5b3..0ab2be6cc 100644 --- a/tests/test_site_media_filter.py +++ b/tests/test_site_media_filter.py @@ -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()