85 lines
2.7 KiB
Python
85 lines
2.7 KiB
Python
"""Tests for Chinese validation error translation."""
|
|
|
|
import pytest
|
|
|
|
from server.api.schemas import SettingUpdatePayload
|
|
from server.utils.validation_errors_zh import (
|
|
translate_loc_zh,
|
|
translate_validation_msg,
|
|
translate_validation_errors,
|
|
)
|
|
|
|
|
|
def test_translate_exact_string_type():
|
|
assert translate_validation_msg("Input should be a valid string") == "必须是字符串"
|
|
|
|
|
|
def test_translate_missing_type():
|
|
assert translate_validation_msg("", "missing") == "必填"
|
|
|
|
|
|
def test_translate_min_length_pattern():
|
|
msg = "String should have at least 1 character"
|
|
assert translate_validation_msg(msg) == "至少需要 1 个字符"
|
|
|
|
|
|
def test_translate_validation_errors_list():
|
|
errors = [{"type": "string_type", "loc": ["body", "value"], "msg": "Input should be a valid string", "input": 1}]
|
|
out = translate_validation_errors(errors)
|
|
assert out[0]["msg"] == "必须是字符串"
|
|
assert out[0]["loc"] == ["body", "value"]
|
|
assert out[0]["loc_zh"] == "值"
|
|
|
|
|
|
def test_translate_loc_zh_query_per_page():
|
|
assert translate_loc_zh(["query", "per_page"]) == "每页条数"
|
|
|
|
|
|
def test_translate_per_page_le_via_ctx():
|
|
out = translate_validation_errors([
|
|
{
|
|
"type": "less_than_equal",
|
|
"loc": ["query", "per_page"],
|
|
"msg": "Input should be less than or equal to 200",
|
|
"ctx": {"le": 200},
|
|
},
|
|
])
|
|
assert out[0]["msg"] == "不能大于 200"
|
|
assert out[0]["loc_zh"] == "每页条数"
|
|
|
|
|
|
def test_setting_update_payload_coerces_int():
|
|
payload = SettingUpdatePayload.model_validate({"value": 160})
|
|
assert payload.value == "160"
|
|
|
|
|
|
def test_translate_list_too_long_ctx():
|
|
msg = translate_validation_msg(
|
|
"List should have at most 50 items after validation, not 101",
|
|
"too_long",
|
|
{"field_type": "List", "max_length": 50, "actual_length": 101},
|
|
)
|
|
assert msg == "最多 50 项,当前 101 项"
|
|
|
|
|
|
def test_translate_strips_non_json_ctx():
|
|
"""Pydantic value_error ctx may contain Exception objects — must not leak into JSON."""
|
|
out = translate_validation_errors([
|
|
{
|
|
"type": "value_error",
|
|
"loc": ("body",),
|
|
"msg": "Value error, query 或 history 至少提供一个",
|
|
"input": {"query": None},
|
|
"ctx": {"error": ValueError("query 或 history 至少提供一个")},
|
|
},
|
|
])
|
|
assert "ctx" not in out[0]
|
|
assert out[0]["msg"] == "query 或 history 至少提供一个"
|
|
|
|
|
|
def test_batch_agent_accepts_101_ids():
|
|
from server.api.schemas import BatchAgentAction
|
|
|
|
payload = BatchAgentAction.model_validate({"server_ids": list(range(1, 102))})
|
|
assert len(payload.server_ids) == 101
|