fix:增强时间格式检查
This commit is contained in:
Binary file not shown.
@@ -6,7 +6,7 @@ from ddms_compliance_suite.utils import schema_utils
|
|||||||
class TimeFormatCheckTestCase(BaseAPITestCase):
|
class TimeFormatCheckTestCase(BaseAPITestCase):
|
||||||
id = "TC-RESTful-003"
|
id = "TC-RESTful-003"
|
||||||
name = "时间字段ISO 8601格式检查"
|
name = "时间字段ISO 8601格式检查"
|
||||||
description = "验证返回的时间字段是否遵循 YYYY-MM-DDTHH:MM:SS+08:00 的ISO 8601格式。此检查为静态检查,验证规范中`string`类型且`format`为`date-time`的字段是否包含推荐的`pattern`。"
|
description = "验证返回的时间字段是否遵循 ISO 8601 格式。此检查为静态检查,会检查规范中 `format` 为 `date-time` 的字段,以及常见的时间字段名(如 createTime, update_time 等),是否包含推荐的 `pattern`。"
|
||||||
severity = TestSeverity.MEDIUM
|
severity = TestSeverity.MEDIUM
|
||||||
tags = ["normative", "schema", "time-format"]
|
tags = ["normative", "schema", "time-format"]
|
||||||
|
|
||||||
@@ -14,6 +14,12 @@ class TimeFormatCheckTestCase(BaseAPITestCase):
|
|||||||
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)
|
||||||
# 推荐的 pattern
|
# 推荐的 pattern
|
||||||
self.recommended_iso_8601_pattern = r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}([+-]\d{2}:\d{2}|Z)$'
|
self.recommended_iso_8601_pattern = r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}([+-]\d{2}:\d{2}|Z)$'
|
||||||
|
# 常见时间字段名称(小写,用于不区分大小写匹配)
|
||||||
|
self.time_field_names = {
|
||||||
|
"createtime", "updatetime", "starttime", "endtime", "publishtime", "timestamp",
|
||||||
|
"created_at", "updated_at", "create_time", "update_time", "start_time", "end_time",
|
||||||
|
"gmtcreate", "gmtmodified", "datetime", "date_time", "time"
|
||||||
|
}
|
||||||
|
|
||||||
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 = []
|
||||||
@@ -31,7 +37,7 @@ class TimeFormatCheckTestCase(BaseAPITestCase):
|
|||||||
self._check_schema_properties(media_spec['schema'], results)
|
self._check_schema_properties(media_spec['schema'], results)
|
||||||
|
|
||||||
if not results:
|
if not results:
|
||||||
return [self.passed("在API规范中未找到可供静态检查的时间相关字段(类型为string且格式为date-time)。")]
|
return [self.passed("在API规范中未找到可供静态检查的时间相关字段(如 format: date-time 或 常见时间字段名)。")]
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
@@ -50,10 +56,24 @@ class TimeFormatCheckTestCase(BaseAPITestCase):
|
|||||||
prop_spec = self._get_resolved_schema(prop_spec)
|
prop_spec = self._get_resolved_schema(prop_spec)
|
||||||
current_path = f"{path}.{prop_name}" if path else prop_name
|
current_path = f"{path}.{prop_name}" if path else prop_name
|
||||||
|
|
||||||
# 检查类型为string且格式为date-time的字段
|
# 检查是否为时间相关字段
|
||||||
if prop_spec.get('type') == 'string' and prop_spec.get('format') == 'date-time':
|
is_datetime_format = prop_spec.get('format') == 'date-time'
|
||||||
|
is_common_time_name = prop_name.lower() in self.time_field_names
|
||||||
|
|
||||||
|
# 必须是string类型,且满足 (format是date-time) 或 (字段名在常见列表里)
|
||||||
|
if prop_spec.get('type') == 'string' and (is_datetime_format or is_common_time_name):
|
||||||
pattern = prop_spec.get('pattern')
|
pattern = prop_spec.get('pattern')
|
||||||
message = f"时间字段 '{current_path}' (format: date-time) "
|
|
||||||
|
# 确定字段来源以提供更清晰的消息
|
||||||
|
source_reason = ""
|
||||||
|
if is_datetime_format and is_common_time_name:
|
||||||
|
source_reason = f"(format: date-time, name: '{prop_name}')"
|
||||||
|
elif is_datetime_format:
|
||||||
|
source_reason = f"(format: date-time)"
|
||||||
|
else: # is_common_time_name
|
||||||
|
source_reason = f"(name: '{prop_name}')"
|
||||||
|
|
||||||
|
message = f"时间字段 '{current_path}' {source_reason} "
|
||||||
if not pattern:
|
if not pattern:
|
||||||
results.append(self.failed(
|
results.append(self.failed(
|
||||||
message + f"缺少建议的 `pattern` ({self.recommended_iso_8601_pattern}) 来强制执行ISO 8601格式。",
|
message + f"缺少建议的 `pattern` ({self.recommended_iso_8601_pattern}) 来强制执行ISO 8601格式。",
|
||||||
|
|||||||
+5882
-5884
File diff suppressed because one or more lines are too long
+3526
-3519
File diff suppressed because it is too large
Load Diff
+370
-405
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user