32 lines
1.3 KiB
Python
32 lines
1.3 KiB
Python
from decimal import Decimal
|
|
|
|
from app.domain.corporate_actions import calculate_stock_consolidation
|
|
|
|
|
|
def test_stock_consolidation_preserves_cost_and_scales_wac():
|
|
result = calculate_stock_consolidation(Decimal(1452), Decimal(726), Decimal("1086.23"))
|
|
assert result.average_price_after == Decimal("2172.46")
|
|
assert result.total_cost_basis == Decimal("1577205.96000000")
|
|
assert result.ratio == Decimal("0.5")
|
|
|
|
def test_stock_consolidation_rejects_fractional_result():
|
|
import pytest
|
|
|
|
from app.domain.corporate_actions import CorporateActionError
|
|
with pytest.raises(CorporateActionError) as exc:
|
|
calculate_stock_consolidation(Decimal(3), Decimal("1.5"), Decimal(10))
|
|
assert exc.value.code == "FRACTIONAL_RESULT_UNSUPPORTED"
|
|
|
|
|
|
def test_stock_consolidation_allows_stored_wac_rounding_residue():
|
|
result = calculate_stock_consolidation(
|
|
Decimal(1452),
|
|
Decimal(726),
|
|
Decimal("1086.234145936639256198347107438016528926"),
|
|
)
|
|
|
|
assert result.average_price_before == Decimal("1086.23414594")
|
|
assert result.average_price_after == Decimal("2172.46829187")
|
|
assert result.total_cost_basis == Decimal("1577211.97990000")
|
|
assert result.total_cost_basis - result.average_price_after * result.quantity_after == Decimal("0.00000238")
|