44断言API
55"""
66from typing import Any , Dict , List
7-
87from fastapi import APIRouter , Body
9- from fastapi .responses import JSONResponse
10-
11- from backend .common .response .response_schema import response_base
12- from backend .plugin .api_testing .utils .assertion import Assertion , AssertionEngine , AssertionResult
8+ from backend .common .response .response_schema import response_base , ResponseModel , ResponseSchemaModel
9+ from backend .plugin .api_testing .utils .assertion import Assertion , AssertionEngine
1310
1411router = APIRouter ()
1512
1613
17- @router .post ("/validate" , response_model = Dict [ str , Any ] , summary = "执行断言验证" )
14+ @router .post ("/validate" , response_model = ResponseModel , summary = "执行断言验证" )
1815async def execute_assertion (
19- assertion : Assertion = Body (...),
20- response_data : Dict [str , Any ] = Body (...)
21- ) -> JSONResponse :
16+ assertion : Assertion = Body (...),
17+ response_data : Dict [str , Any ] = Body (...)
18+ ) -> ResponseModel | ResponseSchemaModel :
2219 """
2320 执行断言验证接口
2421
@@ -27,17 +24,17 @@ async def execute_assertion(
2724 try :
2825 # 执行断言
2926 result = AssertionEngine .execute_assertion (assertion , response_data )
30-
27+
3128 return response_base .success (data = result .model_dump ())
3229 except Exception as e :
33- return response_base .fail (msg = f"断言执行失败: { str (e )} " )
30+ return response_base .fail (data = f"断言执行失败: { str (e )} " )
3431
3532
36- @router .post ("/batch-validate" , response_model = Dict [ str , Any ] , summary = "批量执行断言验证" )
33+ @router .post ("/batch-validate" , response_model = ResponseModel , summary = "批量执行断言验证" )
3734async def execute_batch_assertions (
38- assertions : List [Assertion ] = Body (...),
39- response_data : Dict [str , Any ] = Body (...)
40- ) -> JSONResponse :
35+ assertions : List [Assertion ] = Body (...),
36+ response_data : Dict [str , Any ] = Body (...)
37+ ) -> ResponseModel | ResponseSchemaModel :
4138 """
4239 批量执行断言验证接口
4340
@@ -46,7 +43,7 @@ async def execute_batch_assertions(
4643 try :
4744 # 执行批量断言
4845 results = AssertionEngine .execute_assertions (assertions , response_data )
49-
46+
5047 # 构建响应
5148 data = {
5249 "results" : [result .model_dump () for result in results ],
@@ -56,7 +53,7 @@ async def execute_batch_assertions(
5653 "failed" : sum (1 for result in results if not result .success )
5754 }
5855 }
59-
56+
6057 return response_base .success (data = data )
6158 except Exception as e :
62- return response_base .fail (msg = f"批量断言执行失败: { str (e )} " )
59+ return response_base .fail (data = f"批量断言执行失败: { str (e )} " )
0 commit comments