Files
group_xinghuo_jinrong/tests/test_wave6_guardrail.py
T
zhanghongyu_0626 21ced4d38f feat(report): Add battery report generation and update course documentation
- Introduced `battery_report.json` for local data analysis, excluding it from the database.
- Enhanced `AGENTS.md` to reflect updated test baseline with 795 passed tests.
- Added new metrics for trade flow in `dict_service.py`, improving transaction data analysis.
- Updated regex patterns in `guardrail.py` to better handle numeric extraction and prevent misinterpretation of tokens.
- Expanded course modules with new content on FR-8/9/10 capabilities and L3 role management.
- Improved concurrency handling in transaction processing to ensure accurate alert generation.

This update enhances data analysis capabilities and improves the overall structure and clarity of course materials.
2026-09-10 14:32:25 +08:00

78 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""guardrail 数字护栏单元测试。"""
import unittest
from decimal import Decimal
from app.model.analyst_schemas import TableData
from app.service.guardrail import check_numbers, extract_numbers, result_numbers, verify
class TestGuardrail(unittest.TestCase):
def _table(self):
return TableData(
columns=["risk_code", "cnt"],
rows=[["C1", 4], ["C2", 6]],
)
def test_extract_numbers(self):
self.assertEqual(extract_numbers("共 2 个,高风险 6 人,占比 5%"), [2.0, 6.0, 5.0])
def test_extract_comma_numbers(self):
self.assertEqual(extract_numbers("2,625,000.00 元 和 1,229,150 元"), [2625000.0, 1229150.0])
def test_correct_answer_no_issues(self):
self.assertEqual(check_numbers("共 2 个风险等级,高风险 6 人", self._table()), [])
def test_wrong_number_flagged(self):
# 表格只有 4/6/合计10/行数2,答案说 123 应被拦截
issues = check_numbers("金额加起来是 123 万元", self._table())
self.assertIn(123.0, issues)
def test_result_numbers(self):
nums = result_numbers(self._table())
self.assertIn(2.0, nums) # 行数
self.assertIn(4.0, nums)
self.assertIn(6.0, nums)
self.assertIn(10.0, nums) # 数值列求和
def test_verify_wrong_fails(self):
r = verify("共 999 个", self._table(), data_as_of="2026-09-04")
self.assertFalse(r.passed)
self.assertIn(999.0, r.issues)
def test_zero_valid(self):
t = TableData(columns=["c"], rows=[])
self.assertEqual(check_numbers("结果为 0", t), [])
def test_wan_scale_valid(self):
t = TableData(columns=["v"], rows=[[1234567]])
self.assertEqual(check_numbers("约 123 万元", t), [])
def test_decimal_result_numbers(self):
t = TableData(columns=["customer_id", "holding_scale"], rows=[["CUST-A", Decimal("3150000.00")]])
nums = result_numbers(t)
self.assertIn(3150000.0, nums)
def test_decimal_wan_answer_valid(self):
t = TableData(columns=["v"], rows=[[Decimal("3150000.00")]])
self.assertEqual(check_numbers("CUST-DEMO-B 持仓 315.00 万元", t), [])
def test_customer_id_not_extracted(self):
self.assertEqual(extract_numbers("客户 CUST-3001 交易流水 5 笔"), [5.0])
def test_customer_id_adjacent_chinese_not_extracted(self):
self.assertEqual(extract_numbers("客户CUST-3001 交易流水 5 笔"), [5.0])
self.assertNotIn(510300.0, extract_numbers("产品PROD-510300 申购 520000.00 元"))
def test_date_time_not_extracted(self):
self.assertEqual(extract_numbers("520000.00元(9月3日10:00)"), [520000.0])
self.assertEqual(extract_numbers("2026-09-03 10:00 成交 500000 元"), [500000.0])
def test_product_name_digits_not_extracted(self):
nums = extract_numbers("沪深300指数 1838000.00 元")
self.assertNotIn(300.0, nums)
self.assertIn(1838000.0, nums)
if __name__ == "__main__":
unittest.main()