Skip to content

Commit 5aacb81

Browse files
committed
Update test_doc.py
1 parent de8efd1 commit 5aacb81

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

tests/test_doc.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,29 @@
1818
# --- EXPECTED ERROR MESSAGES (COPIED FROM TABLE) ---
1919
# 定义预期错误常量,确保逐字对齐
2020
ERR_MSG_TOP_P_TYPE = "<400> InternalError.Algo.InvalidParameter: Input should be a valid number, unable to parse string as a number: parameters.top_p"
21-
ERR_MSG_TOP_P_RANGE = "<400> InternalError.Algo.InvalidParameter: Range of top_p should be (0.0, 1.0]"
22-
ERR_MSG_TEMP_RANGE = "<400> InternalError.Algo.InvalidParameter: Temperature should be in [0.0, 2.0]"
21+
ERR_MSG_TOP_P_RANGE = (
22+
"<400> InternalError.Algo.InvalidParameter: Range of top_p should be (0.0, 1.0]"
23+
)
24+
ERR_MSG_TEMP_RANGE = (
25+
"<400> InternalError.Algo.InvalidParameter: Temperature should be in [0.0, 2.0]"
26+
)
2327
ERR_MSG_PARTIAL_THINKING_CONFLICT = "<400> InternalError.Algo.InvalidParameter: Partial mode is not supported when enable_thinking is true"
2428
# R1 不支持 enable_thinking 的报错 (注意:表格中该报错包含 Python 字典的字符串表示,需严格匹配引号)
2529
ERR_MSG_R1_THINKING = "Error code: 400 - {'code': 20015, 'message': 'Value error, current model does not support parameter `enable_thinking`.', 'data': None}"
2630

2731
# --- HELPERS ---
2832

33+
2934
def make_request(payload: Dict[str, Any]) -> requests.Response:
3035
"""Helper to send POST request using the Dynamic Path URL structure."""
3136
model_path = payload.get("model")
3237
url = f"{BASE_URL_PREFIX}/{model_path}"
3338
return requests.post(url, headers=HEADERS, json=payload, stream=True)
3439

35-
def assert_exact_error(response: requests.Response, expected_code_str: str, expected_message: str):
40+
41+
def assert_exact_error(
42+
response: requests.Response, expected_code_str: str, expected_message: str
43+
):
3644
"""
3745
严格校验错误返回:
3846
1. HTTP 状态码通常为 4xx 或 500 (根据表格,部分 4xx 业务错误可能返回 200 或 400,此处以解析 body 为主)
@@ -46,14 +54,20 @@ def assert_exact_error(response: requests.Response, expected_code_str: str, expe
4654

4755
# 1. Check Error Code (e.g., 'InvalidParameter' or 'InternalError')
4856
actual_code = data.get("code")
49-
assert actual_code == expected_code_str, f"Error Code mismatch.\nExpected: {expected_code_str}\nActual: {actual_code}"
57+
assert (
58+
actual_code == expected_code_str
59+
), f"Error Code mismatch.\nExpected: {expected_code_str}\nActual: {actual_code}"
5060

5161
# 2. Check Error Message (Exact String Match)
5262
actual_message = data.get("message")
53-
assert actual_message == expected_message, f"Error Message mismatch.\nExpected: {expected_message}\nActual: {actual_message}"
63+
assert (
64+
actual_message == expected_message
65+
), f"Error Message mismatch.\nExpected: {expected_message}\nActual: {actual_message}"
66+
5467

5568
# --- TEST SUITE ---
5669

70+
5771
class TestStrictErrorValidation:
5872

5973
def test_invalid_parameter_type_top_p(self):
@@ -65,7 +79,7 @@ def test_invalid_parameter_type_top_p(self):
6579
payload = {
6680
"model": "pre-siliconflow/deepseek-v3",
6781
"input": {"messages": [{"role": "user", "content": "你好"}]},
68-
"parameters": {"top_p": "a"}
82+
"parameters": {"top_p": "a"},
6983
}
7084
response = make_request(payload)
7185

@@ -74,7 +88,7 @@ def test_invalid_parameter_type_top_p(self):
7488
assert_exact_error(
7589
response,
7690
expected_code_str="InvalidParameter",
77-
expected_message=ERR_MSG_TOP_P_TYPE
91+
expected_message=ERR_MSG_TOP_P_TYPE,
7892
)
7993

8094
def test_invalid_parameter_range_top_p(self):
@@ -86,14 +100,14 @@ def test_invalid_parameter_range_top_p(self):
86100
payload = {
87101
"model": "pre-siliconflow/deepseek-v3.1",
88102
"input": {"messages": [{"role": "user", "content": "你好"}]},
89-
"parameters": {"top_p": 0}
103+
"parameters": {"top_p": 0},
90104
}
91105
response = make_request(payload)
92106

93107
assert_exact_error(
94108
response,
95109
expected_code_str="InvalidParameter",
96-
expected_message=ERR_MSG_TOP_P_RANGE
110+
expected_message=ERR_MSG_TOP_P_RANGE,
97111
)
98112

99113
def test_invalid_parameter_range_temperature(self):
@@ -105,14 +119,14 @@ def test_invalid_parameter_range_temperature(self):
105119
payload = {
106120
"model": "pre-siliconflow/deepseek-v3.1",
107121
"input": {"messages": [{"role": "user", "content": "你好"}]},
108-
"parameters": {"temperature": 2.1}
122+
"parameters": {"temperature": 2.1},
109123
}
110124
response = make_request(payload)
111125

112126
assert_exact_error(
113127
response,
114128
expected_code_str="InvalidParameter",
115-
expected_message=ERR_MSG_TEMP_RANGE
129+
expected_message=ERR_MSG_TEMP_RANGE,
116130
)
117131

118132
def test_conflict_prefix_and_thinking(self):
@@ -126,17 +140,17 @@ def test_conflict_prefix_and_thinking(self):
126140
"input": {
127141
"messages": [
128142
{"role": "user", "content": "你好"},
129-
{"role": "assistant", "partial": True, "content": "你好,我是"}
143+
{"role": "assistant", "partial": True, "content": "你好,我是"},
130144
]
131145
},
132-
"parameters": {"enable_thinking": True}
146+
"parameters": {"enable_thinking": True},
133147
}
134148
response = make_request(payload)
135149

136150
assert_exact_error(
137151
response,
138152
expected_code_str="InvalidParameter",
139-
expected_message=ERR_MSG_PARTIAL_THINKING_CONFLICT
153+
expected_message=ERR_MSG_PARTIAL_THINKING_CONFLICT,
140154
)
141155

142156
def test_r1_enable_thinking_unsupported(self):
@@ -148,16 +162,17 @@ def test_r1_enable_thinking_unsupported(self):
148162
payload = {
149163
"model": "pre-siliconflow/deepseek-r1",
150164
"input": {"messages": [{"role": "user", "content": "你好"}]},
151-
"parameters": {"enable_thinking": True}
165+
"parameters": {"enable_thinking": True},
152166
}
153167
response = make_request(payload)
154168

155169
# 表格显示此处返回的是 InternalError,且 message 是上游透传回来的原始错误
156170
assert_exact_error(
157171
response,
158172
expected_code_str="InternalError",
159-
expected_message=ERR_MSG_R1_THINKING
173+
expected_message=ERR_MSG_R1_THINKING,
160174
)
161175

176+
162177
if __name__ == "__main__":
163178
pytest.main(["-v", __file__])

0 commit comments

Comments
 (0)