chore: 初始化版本控制,添加 .gitignore 纳入撮合自动化测试工程
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import pytest
|
||||
from decimal import Decimal
|
||||
from unittest.mock import patch, MagicMock
|
||||
from formula.contract_formula import (
|
||||
to_decimal, side_to_direction, open_value, init_margin, fee,
|
||||
frozen_margin, maintain_margin, unrealized_pnl, margin_balance,
|
||||
is_burst, bankruptcy_price, liquidation_price, SIDE_LONG, SIDE_SHORT
|
||||
)
|
||||
|
||||
|
||||
class TestContractFormula:
|
||||
|
||||
@patch("formula.contract_formula.init_margin")
|
||||
@patch("formula.contract_formula.fee")
|
||||
def test_frozen_margin_returns_sum_of_init_margin_and_fee(self, mock_fee, mock_init_margin):
|
||||
"""验证 frozen_margin 返回 init_margin 和 fee 的和,且参数正确传递。"""
|
||||
# 模拟 init_margin 返回 Decimal('100')
|
||||
mock_init_margin.return_value = Decimal('100')
|
||||
# 模拟 fee 返回 Decimal('20')
|
||||
mock_fee.return_value = Decimal('20')
|
||||
|
||||
result = frozen_margin(price="10", quantity="5", margin_rate="0.1",
|
||||
maker_fee_rate="0.001", taker_fee_rate="0.002")
|
||||
assert result == Decimal('120')
|
||||
|
||||
# 验证 init_margin 被用正确参数调用
|
||||
mock_init_margin.assert_called_once_with("10", "5", "0.1")
|
||||
# 验证 fee 被用正确参数调用
|
||||
mock_fee.assert_called_once_with("10", "5", "0.001", "0.002")
|
||||
|
||||
@patch("formula.contract_formula.side_to_direction")
|
||||
@patch("formula.contract_formula.to_decimal")
|
||||
def test_unrealized_pnl_calculates_correctly(self, mock_to_decimal, mock_side_to_direction):
|
||||
"""验证 unrealized_pnl 返回正确的未实现盈亏,并使用了方向转换。"""
|
||||
# 模拟 side_to_direction 返回 1(做多)
|
||||
mock_side_to_direction.return_value = SIDE_LONG
|
||||
# 模拟 to_decimal 依次返回 Decimal('10'), Decimal('12'), Decimal('5')
|
||||
# 注意 to_decimal 会被调用多次(mark_price, open_price, quantity)
|
||||
mock_to_decimal.side_effect = [Decimal('12'), Decimal('10'), Decimal('5')]
|
||||
|
||||
result = unrealized_pnl(side="bid", open_price="10", mark_price="12", quantity="5")
|
||||
assert result == Decimal('10')
|
||||
|
||||
# 验证 side_to_direction 被调用
|
||||
mock_side_to_direction.assert_called_once_with("bid")
|
||||
# 验证 to_decimal 被调用了三次
|
||||
assert mock_to_decimal.call_count == 3
|
||||
|
||||
def test_is_burst_returns_true_when_margin_balance_less_or_equal_maintain(self):
|
||||
"""验证保证金余额 <= 维持保证金时返回 True,否则返回 False。"""
|
||||
# 针对爆仓场景:合理设置参数使得余额小于维持保证金
|
||||
# 使用真实计算,不 mock 内部函数,以验证整体逻辑
|
||||
# 做多,开仓价10,数量1,保证金率0.1,维持率0.05,额外保证金0
|
||||
# 标记价格跌至0,未实现盈亏为 -10,起始保证金1,余额=0+1-10=-9,维持保证金=10*1*0.05=0.5,-9 <= 0.5 → True
|
||||
assert is_burst(extra_margin="0", side="bid", open_price="10", mark_price="0",
|
||||
quantity="1", margin_rate="0.1", maintain_rate="0.05") is True
|
||||
|
||||
# 标记价格未跌,未实现盈亏0,余额=1,维持保证金=0.5,1 > 0.5 → False
|
||||
assert is_burst(extra_margin="0", side="bid", open_price="10", mark_price="10",
|
||||
quantity="1", margin_rate="0.1", maintain_rate="0.05") is False
|
||||
|
||||
@patch("formula.contract_formula.side_to_direction")
|
||||
def test_bankruptcy_price_returns_correct_formula(self, mock_side_to_direction):
|
||||
"""验证破产价格公式,做多和做空方向分别计算。"""
|
||||
# 测试做多方向
|
||||
mock_side_to_direction.return_value = SIDE_LONG
|
||||
result_long = bankruptcy_price(side="bid", open_price="100", quantity="10",
|
||||
margin_rate="0.2", extra_margin="5")
|
||||
# 期望:100 * (1 - 0.2) - 5/10 = 80 - 0.5 = 79.5
|
||||
assert result_long == Decimal('79.5')
|
||||
|
||||
# 测试做空方向
|
||||
mock_side_to_direction.return_value = SIDE_SHORT
|
||||
result_short = bankruptcy_price(side="ask", open_price="100", quantity="10",
|
||||
margin_rate="0.2", extra_margin="5")
|
||||
# 期望:100 * (1 + 0.2) + 5/10 = 120 + 0.5 = 120.5
|
||||
assert result_short == Decimal('120.5')
|
||||
|
||||
# 验证 side_to_direction 被调用了两次
|
||||
assert mock_side_to_direction.call_count == 2
|
||||
|
||||
def test_side_to_direction_raises_value_error_for_invalid_side(self):
|
||||
"""验证 side_to_direction 对非法输入抛出 ValueError。"""
|
||||
with pytest.raises(ValueError, match="非法的持仓方向"):
|
||||
side_to_direction("invalid")
|
||||
with pytest.raises(ValueError, match="非法的持仓方向"):
|
||||
side_to_direction(3)
|
||||
with pytest.raises(ValueError, match="非法的持仓方向"):
|
||||
side_to_direction(None)
|
||||
Reference in New Issue
Block a user