summaryrefslogtreecommitdiff
path: root/tests/FundLab.Api.Tests
diff options
context:
space:
mode:
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")