fix:llm测试修复给大模型提供的字段
This commit is contained in:
parent
742079d6a4
commit
ff71f2aed6
Binary file not shown.
@ -21,19 +21,30 @@ class LLMComplianceCheckTestCase(BaseAPITestCase):
|
|||||||
|
|
||||||
def validate_response(self, response_context: APIResponseContext, request_context: APIRequestContext) -> List[ValidationResult]:
|
def validate_response(self, response_context: APIResponseContext, request_context: APIRequestContext) -> List[ValidationResult]:
|
||||||
results = []
|
results = []
|
||||||
# 收集API所有关键信息
|
# 收集API所有关键信息,包括实例数据和Schema定义
|
||||||
api_info = {
|
api_info = {
|
||||||
|
# API元数据
|
||||||
|
"path_template": self.endpoint_spec.get("path"),
|
||||||
"method": request_context.method,
|
"method": request_context.method,
|
||||||
"url": request_context.url,
|
# "operationId": self.endpoint_spec.get("operationId"),
|
||||||
"path": self.endpoint_spec.get("path"),
|
"title": self.endpoint_spec.get("summary") or self.endpoint_spec.get("title"),
|
||||||
"operationId": self.endpoint_spec.get("operationId"),
|
"description": self.endpoint_spec.get("description") or self.endpoint_spec.get("desc"),
|
||||||
"headers": dict(request_context.headers) if hasattr(request_context, "headers") else {},
|
"tags": self.endpoint_spec.get("tags"),
|
||||||
"query_params": getattr(request_context, "query_params", {}),
|
|
||||||
"path_params": getattr(request_context, "path_params", {}),
|
# API Schema 定义 (从 endpoint_spec 获取)
|
||||||
"body": getattr(request_context, "body", None),
|
"schema_parameters": self.endpoint_spec.get("parameters"),
|
||||||
"response_status": response_context.status_code,
|
"schema_request_body": self.endpoint_spec.get("requestBody"),
|
||||||
"response_headers": dict(response_context.headers) if hasattr(response_context, "headers") else {},
|
"schema_responses": self.endpoint_spec.get("responses"),
|
||||||
"response_body": response_context.text_content if hasattr(response_context, "text_content") else None
|
|
||||||
|
# API 调用实例数据 (从 request_context 和 response_context 获取)
|
||||||
|
"instance_url": request_context.url,
|
||||||
|
"instance_request_headers": dict(request_context.headers) if hasattr(request_context, "headers") else {},
|
||||||
|
"instance_query_params": getattr(request_context, "query_params", {}),
|
||||||
|
"instance_path_params": getattr(request_context, "path_params", {}),
|
||||||
|
"instance_request_body": getattr(request_context, "body", None),
|
||||||
|
"instance_response_status": response_context.status_code,
|
||||||
|
"instance_response_headers": dict(response_context.headers) if hasattr(response_context, "headers") else {},
|
||||||
|
"instance_response_body": response_context.text_content if hasattr(response_context, "text_content") else None
|
||||||
}
|
}
|
||||||
# 日志打印所有API信息
|
# 日志打印所有API信息
|
||||||
self.logger.info("LLM合规性检查-API信息收集: " + json.dumps(api_info, ensure_ascii=False, indent=2))
|
self.logger.info("LLM合规性检查-API信息收集: " + json.dumps(api_info, ensure_ascii=False, indent=2))
|
||||||
|
|||||||
Binary file not shown.
@ -1,59 +1,59 @@
|
|||||||
# from ddms_compliance_suite.test_framework_core import BaseAPITestCase, ValidationResult, APIResponseContext, APIRequestContext, TestSeverity
|
from ddms_compliance_suite.test_framework_core import BaseAPITestCase, ValidationResult, APIResponseContext, APIRequestContext, TestSeverity
|
||||||
# import re
|
import re
|
||||||
# from typing import Dict, Any, List, Optional
|
from typing import Dict, Any, List, Optional
|
||||||
# # TODO 获取资源的时候复数(get方法list)
|
# TODO 获取资源的时候复数(get方法list)
|
||||||
# class ResourceCollectionPluralCheckTestCase(BaseAPITestCase):
|
class ResourceCollectionPluralCheckTestCase(BaseAPITestCase):
|
||||||
# id = "TC-RESTful-004"
|
id = "TC-RESTful-004"
|
||||||
# name = "资源集合复数命名检查"
|
name = "资源集合复数命名检查"
|
||||||
# description = "验证表示资源集合的路径是否使用复数形式。动词(如push、send等)不需要使用复数形式。"
|
description = "验证表示资源集合的路径是否使用复数形式。动词(如push、send等)不需要使用复数形式。"
|
||||||
# severity = TestSeverity.MEDIUM
|
severity = TestSeverity.MEDIUM
|
||||||
# tags = ["normative", "restful", "url-structure"]
|
tags = ["normative", "restful", "url-structure"]
|
||||||
|
|
||||||
# def __init__(self, endpoint_spec: Dict[str, Any], global_api_spec: Dict[str, Any], json_schema_validator: Optional[Any] = None, llm_service: Optional[Any] = None):
|
def __init__(self, endpoint_spec: Dict[str, Any], global_api_spec: Dict[str, Any], json_schema_validator: Optional[Any] = None, llm_service: Optional[Any] = None):
|
||||||
# super().__init__(endpoint_spec, global_api_spec, json_schema_validator, llm_service=llm_service)
|
super().__init__(endpoint_spec, global_api_spec, json_schema_validator, llm_service=llm_service)
|
||||||
# # 常见的API路径中的动词,这些不需要使用复数形式
|
# 常见的API路径中的动词,这些不需要使用复数形式
|
||||||
# self.common_verbs = {
|
self.common_verbs = {
|
||||||
# "push", "send", "publish", "subscribe", "create", "update", "delete",
|
"push", "send", "publish", "subscribe", "create", "update", "delete",
|
||||||
# "get", "set", "add", "remove", "search", "query", "find", "calculate",
|
"get", "set", "add", "remove", "search", "query", "find", "calculate",
|
||||||
# "process", "validate", "verify", "check", "analyze", "export", "import",
|
"process", "validate", "verify", "check", "analyze", "export", "import",
|
||||||
# "upload", "download", "sync", "login", "logout", "register", "activate",
|
"upload", "download", "sync", "login", "logout", "register", "activate",
|
||||||
# "deactivate", "approve", "reject", "cancel", "confirm", "notify"
|
"deactivate", "approve", "reject", "cancel", "confirm", "notify"
|
||||||
# }
|
}
|
||||||
|
|
||||||
# # 已知的单数形式名词,即使不以's'结尾也是正确的
|
# 已知的单数形式名词,即使不以's'结尾也是正确的
|
||||||
# self.known_singulars = {
|
self.known_singulars = {
|
||||||
# "status", "gas", "analysis", "data", "info", "metadata", "media",
|
"status", "gas", "analysis", "data", "info", "metadata", "media",
|
||||||
# "equipment", "staff", "fish", "sheep", "deer", "series", "species",
|
"equipment", "staff", "fish", "sheep", "deer", "series", "species",
|
||||||
# "aircraft", "offspring", "feedback", "content", "news"
|
"aircraft", "offspring", "feedback", "content", "news"
|
||||||
# }
|
}
|
||||||
|
|
||||||
# def validate_response(self, response_context: APIResponseContext, request_context: APIRequestContext) -> List[ValidationResult]:
|
def validate_response(self, response_context: APIResponseContext, request_context: APIRequestContext) -> List[ValidationResult]:
|
||||||
# path = self.endpoint_spec['path']
|
path = self.endpoint_spec['path']
|
||||||
# method = self.endpoint_spec['method']
|
method = self.endpoint_spec['method']
|
||||||
|
|
||||||
# # 这个检查通常适用于返回列表的GET请求,或者创建资源的POST请求
|
# 这个检查通常适用于返回列表的GET请求,或者创建资源的POST请求
|
||||||
# if method.lower() not in ['get', 'post']:
|
if method.lower() not in ['get', 'post']:
|
||||||
# return [self.passed(f"跳过检查:{method} 方法,不适用于资源集合复数检查。")]
|
return [self.passed(f"跳过检查:{method} 方法,不适用于资源集合复数检查。")]
|
||||||
|
|
||||||
# path_segments = [seg for seg in path.strip('/').split('/') if '{' not in seg and not re.match(r'v\d+', seg)]
|
path_segments = [seg for seg in path.strip('/').split('/') if '{' not in seg and not re.match(r'v\d+', seg)]
|
||||||
|
|
||||||
# if not path_segments:
|
if not path_segments:
|
||||||
# return [self.passed("跳过检查:路径不含有效分段。")]
|
return [self.passed("跳过检查:路径不含有效分段。")]
|
||||||
|
|
||||||
# resource_segment = path_segments[-1]
|
resource_segment = path_segments[-1]
|
||||||
|
|
||||||
# # 检查是否为动词
|
# 检查是否为动词
|
||||||
# if resource_segment.lower() in self.common_verbs:
|
if resource_segment.lower() in self.common_verbs:
|
||||||
# return [self.passed(f"路径 '{path}' 的最后一个路径分段 '{resource_segment}' 是动词,不需要使用复数形式。")]
|
return [self.passed(f"路径 '{path}' 的最后一个路径分段 '{resource_segment}' 是动词,不需要使用复数形式。")]
|
||||||
|
|
||||||
# # 检查是否为已知的单数形式名词
|
# 检查是否为已知的单数形式名词
|
||||||
# if resource_segment.lower() in self.known_singulars:
|
if resource_segment.lower() in self.known_singulars:
|
||||||
# return [self.passed(f"路径 '{path}' 的资源名 '{resource_segment}' 是已知的单数形式名词,符合规范。")]
|
return [self.passed(f"路径 '{path}' 的资源名 '{resource_segment}' 是已知的单数形式名词,符合规范。")]
|
||||||
|
|
||||||
# # 对于其他名词,检查是否使用复数形式
|
# 对于其他名词,检查是否使用复数形式
|
||||||
# if not resource_segment.endswith('s'):
|
if not resource_segment.endswith('s'):
|
||||||
# message = f"路径 '{path}' 的最后一个路径分段 '{resource_segment}' 可能不是复数形式,建议对资源集合使用复数命名。"
|
message = f"路径 '{path}' 的最后一个路径分段 '{resource_segment}' 可能不是复数形式,建议对资源集合使用复数命名。"
|
||||||
# return [self.failed(message, details={'path': path, 'segment': resource_segment})]
|
return [self.failed(message, details={'path': path, 'segment': resource_segment})]
|
||||||
|
|
||||||
# message = f"路径 '{path}' 的资源集合命名 '{resource_segment}' 符合复数命名规范。"
|
message = f"路径 '{path}' 的资源集合命名 '{resource_segment}' 符合复数命名规范。"
|
||||||
# return [self.passed(message)]
|
return [self.passed(message)]
|
||||||
Binary file not shown.
14525
log_stage.txt
14525
log_stage.txt
File diff suppressed because one or more lines are too long
@ -38,8 +38,9 @@
|
|||||||
| -------------------------------------- | -------------------------------- | ------------------------------------------------------------------- | -------- |
|
| -------------------------------------- | -------------------------------- | ------------------------------------------------------------------- | -------- |
|
||||||
| TC-RESTful-001 | 核心命名与结构规范检查 | 统一验证API的命名与结构是否遵循规范。包括:1)模块名全小写且用中划线连接;2)URL路径参数使用下划线命名法(snake_case);3)查询参数和请求体字段使用小驼峰命名法(camelCase);4)响应中的空数组为[]而非null;5)数组类型数据被包裹在list字段中。 | 高 |
|
| TC-RESTful-001 | 核心命名与结构规范检查 | 统一验证API的命名与结构是否遵循规范。包括:1)模块名全小写且用中划线连接;2)URL路径参数使用下划线命名法(snake_case);3)查询参数和请求体字段使用小驼峰命名法(camelCase);4)响应中的空数组为[]而非null;5)数组类型数据被包裹在list字段中。 | 高 |
|
||||||
| TC-RESTful-002 | 资源路径名词检查 | 验证API路径中是否使用名词而非动词来表示资源。 | 中 |
|
| TC-RESTful-002 | 资源路径名词检查 | 验证API路径中是否使用名词而非动词来表示资源。 | 中 |
|
||||||
| TC-RESTful-003 | 时间字段ISO 8601格式检查 | 验证返回的时间字段是否遵循 YYYY-MM-DDTHH:MM:SS+08:00 的ISO 8601格式。此检查为静态检查,验证规范中`string`类型且`format`为`date-time`的字段是否包含推荐的`pattern`。 | 中 |
|
| TC-RESTful-003 | 时间字段ISO 8601格式检查 | 验证返回的时间字段是否遵循 ISO 8601 格式。此检查为静态检查,会检查规范中 `format` 为 `date-time` 的字段,以及常见的时间字段名(如 createTime, update_time 等),是否包含推荐的 `pattern`。 | 中 |
|
||||||
| TC-NORMATIVE-URL-LLM-COMPREHENSIVE-001 | 合URL规范与RESTful风格检查 (LLM) | 使用LLM统一评估API路径是否符合命名、结构、版本和RESTful风格等规范。 | 中 |
|
| TC-NORMATIVE-URL-LLM-COMPREHENSIVE-001 | 合URL规范与RESTful风格检查 (LLM) | 使用LLM统一评估API路径是否符合命名、结构、版本和RESTful风格等规范。 | 中 |
|
||||||
|
| TC-LLM-COMPLIANCE-001 | LLM合规性综合检查 | 读取固定的合规性标准列表,将API所有关键信息(url、headers、params、query、body、示例响应等)发送给大模型,让其判断是否通过并给出理由。 | 中 |
|
||||||
| TC-NORMATIVE-001 | HTTP方法使用规范检查 | 验证API是否恰当使用HTTP方法(例如,GET用于检索,POST用于创建)。 | 中 |
|
| TC-NORMATIVE-001 | HTTP方法使用规范检查 | 验证API是否恰当使用HTTP方法(例如,GET用于检索,POST用于创建)。 | 中 |
|
||||||
| TC-DMS-CORE-SCHEMA-001 | DMS核心存储服务API响应格式检查 | 验证API响应的schema是否符合标准格式:{'code':int\|string\|number, 'message':string, 'data':any} | 高 |
|
| TC-DMS-CORE-SCHEMA-001 | DMS核心存储服务API响应格式检查 | 验证API响应的schema是否符合标准格式:{'code':int\|string\|number, 'message':string, 'data':any} | 高 |
|
||||||
| TC-DMS-URL-VERSION-001 | DMS API URL版本号检查 | 检查API URL是否包含标准格式的版本号,支持的格式包括:v1, api/v2, v3.0, version/1, 1.0等 | 中 |
|
| TC-DMS-URL-VERSION-001 | DMS API URL版本号检查 | 检查API URL是否包含标准格式的版本号,支持的格式包括:v1, api/v2, v3.0, version/1, 1.0等 | 中 |
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user