chore: 初始化版本控制,添加 .gitignore 纳入撮合自动化测试工程
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
"""资金类字段断言工具:全部走 Decimal,杜绝 float 精度误差。"""
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
from formula.contract_formula import to_decimal
|
||||
|
||||
|
||||
def assert_decimal_equal(actual, expected, places=8):
|
||||
"""断言两个金额/数量在指定精度下相等。
|
||||
|
||||
金额字段统一按 ``places`` 位小数(默认 8 位,撮合系统常用精度)量化后
|
||||
比较,既避免逐位浮点比较的脆弱性,又不放过真实精度丢失。
|
||||
|
||||
:param actual: 实际值(HTTP 响应/落库/缓存中的值,可为字符串)
|
||||
:param expected: 期望值(公式层计算结果)
|
||||
:param places: 比较小数位数
|
||||
"""
|
||||
quantum = Decimal(1).scaleb(-places)
|
||||
actual_q = to_decimal(actual).quantize(quantum)
|
||||
expected_q = to_decimal(expected).quantize(quantum)
|
||||
assert actual_q == expected_q, (
|
||||
"金额不一致: actual=%s, expected=%s (精度 %d 位)" % (actual, expected, places)
|
||||
)
|
||||
|
||||
|
||||
def assert_decimal_ge(actual, expected):
|
||||
"""断言 actual >= expected(Decimal 口径)。"""
|
||||
assert to_decimal(actual) >= to_decimal(expected), (
|
||||
"断言失败: %s 应大于等于 %s" % (actual, expected)
|
||||
)
|
||||
Reference in New Issue
Block a user