1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
namespace FundLab.Api.Tests
open System
open Xunit
open FundLab.Api
module StockCashflowSupport =
let quote (code: string) (price: decimal) : StockQuote =
{ Code = code; Name = Some "贵州茅台"; Price = Some price; Currency = "CNY"; Market = Some "sh"; Adjust = Some "none"; Suspended = Some false }
let stubQuoteProbe (quotes: Map<string, decimal>) =
{ new IStockQuoteProbe with
member _.GetQuote(code, _) =
match quotes.TryFind code with
| Some price -> Ok(quote code price)
| None -> Error(MarketDataCollectorUnavailable(sprintf "no valid quote for %s" code)) }
let probes (quotes: Map<string, decimal>) =
{ NavDates = NavDateProbeSupport.stubNavDateProbe
NavSeries = NavDateProbeSupport.stubNavSeriesProbe
BondQuotes = NavDateProbeSupport.stubBondQuoteProbe
StockQuotes = stubQuoteProbe quotes
StockDaily = NavDateProbeSupport.stubStockDailyProbe }
[<Collection("postgres")>]
type StockCashflowTests(fixture: PostgresFixture) =
let sharedRepository =
lazy
let value = FundRepository(fixture.ConnectionString)
value.EnsureSchema()
value
let repository () = sharedRepository.Value
let createFund () =
let command =
{
Name = "股票现金账本测试 FOF"
InitialCash = 1000000.00m
InitialUnitNav = 1.00000000m
IsSynthetic = true
}
let key = fixture.Key(sprintf "stock-cashflow-fund-%s" (Guid.NewGuid().ToString("N")))
match repository().CreateFund(key, command) with
| FundWriteResult.Created fund -> fund.Id
| other -> failwithf "unexpected fund creation result: %A" other
let app (quotes: Map<string, decimal>) =
App.createApplicationWithProbes (repository ()) (StockCashflowSupport.probes quotes)
let buy (quotes: Map<string, decimal>) fundId body idempotencyKey =
PersistenceTestHelpers.invoke
(app quotes)
"POST"
(sprintf "/api/funds/%O/stock-trades" fundId)
[ "Authorization", "Bearer test-token"; "Idempotency-Key", idempotencyKey ]
body
let sell (quotes: Map<string, decimal>) fundId body idempotencyKey =
PersistenceTestHelpers.invoke
(app quotes)
"POST"
(sprintf "/api/funds/%O/stock-sells" fundId)
[ "Authorization", "Bearer test-token"; "Idempotency-Key", idempotencyKey ]
body
let recordCashflow (quotes: Map<string, decimal>) fundId body idempotencyKey =
PersistenceTestHelpers.invoke
(app quotes)
"POST"
(sprintf "/api/funds/%O/stock-cashflows" fundId)
[ "Authorization", "Bearer test-token"; "Idempotency-Key", idempotencyKey ]
body
let readCashflows (quotes: Map<string, decimal>) fundId =
PersistenceTestHelpers.invoke
(app quotes)
"GET"
(sprintf "/api/funds/%O/stock-cashflows" fundId)
[ "Authorization", "Bearer test-token" ]
""
[<Fact>]
member _.``buying a stock records a buy cashflow event``() =
let fundId = createFund ()
let quotes = Map.ofList [ "600519", 1252.57m ]
let buyStatus, _ = buy quotes fundId """{"instrumentCode":"600519","quantity":"100"}""" (fixture.Key "stock-cashflow-buy")
Assert.Equal(201, buyStatus)
let status, body = readCashflows quotes fundId
Assert.Equal(200, status)
Assert.Contains("\"eventType\":\"buy\"", body)
Assert.Contains("\"instrumentCode\":\"600519\"", body)
Assert.Contains("\"amount\":\"125257.00\"", body)
[<Fact>]
member _.``selling a stock records a sell cashflow event``() =
let fundId = createFund ()
let quotes = Map.ofList [ "600519", 1252.57m ]
let buyStatus, _ = buy quotes fundId """{"instrumentCode":"600519","quantity":"100"}""" (fixture.Key "stock-cashflow-sell-buy")
Assert.Equal(201, buyStatus)
let sellStatus, _ = sell quotes fundId """{"instrumentCode":"600519","quantity":"40"}""" (fixture.Key "stock-cashflow-sell")
Assert.Equal(201, sellStatus)
let _, body = readCashflows quotes fundId
Assert.Contains("\"eventType\":\"sell\"", body)
Assert.Contains("\"amount\":\"50102.80\"", body)
[<Fact>]
member _.``a recorded dividend cashflow is idempotent``() =
let fundId = createFund ()
let quotes = Map.ofList [ "600519", 1252.57m ]
let key = fixture.Key "stock-cashflow-dividend"
let body =
"""{"instrumentCode":"600519","eventType":"dividend","eventDate":"2026-06-20","quantity":"100","amount":"250.00"}"""
let firstStatus, firstBody = recordCashflow quotes fundId body key
Assert.Equal(201, firstStatus)
Assert.Contains("\"eventType\":\"dividend\"", firstBody)
Assert.Contains("\"amount\":\"250.00\"", firstBody)
let secondStatus, _ = recordCashflow quotes fundId body key
Assert.Equal(200, secondStatus)
let _, events = readCashflows quotes fundId
Assert.Contains("\"eventType\":\"dividend\"", events)
[<Fact>]
member _.``a malformed event type is rejected``() =
let fundId = createFund ()
let quotes = Map.ofList [ "600519", 1252.57m ]
let status, body =
recordCashflow
quotes
fundId
"""{"instrumentCode":"600519","eventType":"interest","eventDate":"2026-06-20","amount":"1.00"}"""
(fixture.Key "stock-cashflow-bad-type")
Assert.Equal(400, status)
Assert.Contains("INVALID_STOCK_CASHFLOW_REQUEST", body)
|