From 877e03183b5acaff84526cb9fe978280cadd8a76 Mon Sep 17 00:00:00 2001 From: "Somhairle H. Marisol" Date: Tue, 22 Sep 2026 21:18:35 +0800 Subject: Backfill the missing cash debit for pre-3d-34 stock buys (3d-39) --- tests/FundLab.Api.Tests/CashReconciliationTests.fs | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'tests/FundLab.Api.Tests') 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 @@ -365,6 +365,42 @@ type CashReconciliationTests(fixture: PostgresFixture) = Assert.Equal(expected, ledgerEventCount fundId) Assert.Equal(0.00m, (repository().GetCashReconciliation fundId |> Result.defaultWith failwith).Difference) + [] + 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) + [] member _.``manual stock buy debits available cash and records a stock_buy event``() = let fundId = createFund 10000.00m -- cgit v1.2.3