summaryrefslogtreecommitdiff
path: root/tests/FundLab.Api.Tests
diff options
context:
space:
mode:
authorSomhairle H. Marisol <[email protected]>2026-09-22 21:18:35 +0800
committerSomhairle H. Marisol <[email protected]>2026-09-22 21:18:35 +0800
commit877e03183b5acaff84526cb9fe978280cadd8a76 (patch)
tree69e81ebe38de13883bad392e9fd0fa61ecaaa9b5 /tests/FundLab.Api.Tests
parent248b50c8fd057c87a852f8306fc4585905880abc (diff)
downloadfund-lab-877e03183b5acaff84526cb9fe978280cadd8a76.tar.gz
Backfill the missing cash debit for pre-3d-34 stock buys (3d-39)
Diffstat (limited to 'tests/FundLab.Api.Tests')
-rw-r--r--tests/FundLab.Api.Tests/CashReconciliationTests.fs36
1 files changed, 36 insertions, 0 deletions
diff --git a/tests/FundLab.Api.Tests/CashReconciliationTests.fs b/tests/FundLab.Api.Tests/CashReconciliationTests.fs
index fa14bff..b31b358 100644
--- a/tests/FundLab.Api.Tests/CashReconciliationTests.fs
+++ b/tests/FundLab.Api.Tests/CashReconciliationTests.fs
@@ -366,6 +366,42 @@ type CashReconciliationTests(fixture: PostgresFixture) =
Assert.Equal(0.00m, (repository().GetCashReconciliation fundId |> Result.defaultWith failwith).Difference)
[<Fact>]
+ member _.``backfill debits cash for a pre-3d-34 stock buy that never reduced available cash``() =
+ let fundId = createFund 10000.00m
+
+ // a manual stock buy from before the 3d-34 debit fix: no ledger event and no debit
+ buyStockNoDebit fundId "600519" 100m 10.00m (fixture.Key "cash-pre34-buy")
+
+ // simulate a database that predates the ledger
+ deleteLedgerEvents fundId
+ Assert.Equal(0L, ledgerEventCount fundId)
+
+ let before =
+ repository().GetFund fundId |> Option.defaultWith (fun () -> failwith "fund was not found")
+
+ Assert.Equal(10000.00m, before.AvailableCash)
+
+ repository().BackfillCashLedger() |> ignore
+
+ let after =
+ repository().GetFund fundId |> Option.defaultWith (fun () -> failwith "fund was not found")
+
+ // the rebuilt stock_buy event is paired with the missing 1000.00 debit
+ Assert.Equal(9000.00m, after.AvailableCash)
+ Assert.Equal(1L, ledgerEventCount fundId)
+ Assert.Equal(0.00m, (repository().GetCashReconciliation fundId |> Result.defaultWith failwith).Difference)
+
+ // re-running must not debit twice or write another event
+ repository().BackfillCashLedger() |> ignore
+
+ let rerun =
+ repository().GetFund fundId |> Option.defaultWith (fun () -> failwith "fund was not found")
+
+ Assert.Equal(9000.00m, rerun.AvailableCash)
+ Assert.Equal(1L, ledgerEventCount fundId)
+ Assert.Equal(0.00m, (repository().GetCashReconciliation fundId |> Result.defaultWith failwith).Difference)
+
+ [<Fact>]
member _.``manual stock buy debits available cash and records a stock_buy event``() =
let fundId = createFund 10000.00m
buyStock fundId "600519" 100m 10.00m (fixture.Key "cash-manual-stock-buy")