fix(ledger): preserve exact decimal amount in manual and unified responses

Co-authored-by: genichin <4282227+genichin@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-09-09 12:41:36 +00:00
committed by GitHub
co-authored by genichin
parent 7fc3386f3a
commit fde5b33f05
5 changed files with 32 additions and 8 deletions
+14 -2
View File
@@ -199,6 +199,7 @@ class _FakeRepo:
side=action.lower(),
quantity=quantity,
price=price,
price_decimal=amount_decimal if action == "DIVIDEND" else Decimal(str(price)),
fee_amount=fee,
tax_amount=tax,
executed_at=executed_at,
@@ -505,7 +506,7 @@ def test_dividend_updates_only_profit_buffer_and_records_executed_at_and_tax() -
assert resp.status_code == 201
body = resp.json()
assert body["action"] == "DIVIDEND"
assert body["amount"] == 120.0
assert body["amount"] == "120"
assert body["tax"] == 10.0
assert body["executed_at"] == "2026-09-01T00:00:00Z"
assert body["cash_balance_after"] == 1000.0
@@ -593,11 +594,14 @@ def test_dividend_rejects_invalid_amount_tax_and_unrelated_fields_without_state_
)
invalid_payloads = [
{"action": "DIVIDEND", "amount": 1},
{"action": "DIVIDEND", "amount": 0, "executed_at": "2026-09-01"},
{"action": "DIVIDEND", "amount": -1, "executed_at": "2026-09-01"},
{"action": "DIVIDEND", "amount": 1, "executed_at": "2026-13-40"},
{"action": "DIVIDEND", "amount": 1, "tax": -1, "executed_at": "2026-09-01"},
{"action": "DIVIDEND", "amount": 1, "tax": "0.000000001", "executed_at": "2026-09-01"},
{"action": "DIVIDEND", "amount": 1, "tax": "1000000000000.00000000", "executed_at": "2026-09-01"},
{"action": "DIVIDEND", "amount": 1, "executed_at": "2026-09-01", "fee": 0},
{"action": "DIVIDEND", "amount": 1, "executed_at": "2026-09-01", "quantity": 1},
{"action": "DIVIDEND", "amount": 1, "executed_at": "2026-09-01", "price": 1},
{"action": "DIVIDEND", "amount": 1, "executed_at": "2026-09-01", "target_ledger_entry_id": 1},
@@ -712,6 +716,14 @@ def test_dividend_duplicate_check_does_not_collapse_decimals_that_can_alias_in_f
headers=_auth(),
)
assert second.status_code == 201
assert first.json()["amount"] == "90000000.00000001"
assert second.json()["amount"] == "90000000.00000002"
history = client.get(f"/v1/portfolios/{_PORTFOLIO_ID}/ledger?source=manual", headers=_auth())
assert history.status_code == 200
amounts = [item["amount"] for item in history.json()["items"] if item["action"] == "DIVIDEND"]
assert "90000000.00000001" in amounts
assert "90000000.00000002" in amounts
def test_reversed_dividend_is_excluded_from_future_duplicate_candidate_matching() -> None:
@@ -891,7 +903,7 @@ def test_unified_ledger_includes_dividend_fields() -> None:
item = resp.json()["items"][0]
assert item["source"] == "manual"
assert item["action"] == "DIVIDEND"
assert item["amount"] == 33.0
assert item["amount"] == "33"
assert item["price"] == 33.0
assert item["executed_at"] == "2026-09-01T00:00:00Z"
assert item["tax_amount"] == 3.0