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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
namespace FundLab.Api.Tests
open System
open System.Threading
open Xunit
open FundLab.Api
module FundValuationSupport =
let stockQuote (code: string) (price: decimal) : StockQuote =
{ Code = code; Name = Some "贵州茅台"; Price = Some price; Currency = "CNY" }
let bondQuote (code: string) (price: decimal) : BondQuote =
{
Code = code
SourceRevision = "akshare-test/hs-cov-spot"
Name = Some "南航转债"
Price = Some price
CleanPrice = Some price
AccruedInterest = None
Date = None
MaturityDate = None
}
let stockProbeReturning (prices: Map<string, decimal>) =
{ new IStockQuoteProbe with
member _.GetQuote(code, _) =
match prices.TryFind code with
| Some price -> Ok(stockQuote code price)
| None -> Error(MarketDataCollectorUnavailable(sprintf "no valid quote for %s" code)) }
let bondProbeReturning (prices: Map<string, decimal>) =
{ new IBondQuoteProbe with
member _.GetQuote(code, _) =
match prices.TryFind code with
| Some price -> Ok(bondQuote code price)
| None -> Error(MarketDataCollectorUnavailable(sprintf "no valid quote for %s" code)) }
let probes (stockPrices: Map<string, decimal>) (bondPrices: Map<string, decimal>) =
{ NavDates = NavDateProbeSupport.stubNavDateProbe
NavSeries = NavDateProbeSupport.stubNavSeriesProbe
BondQuotes = bondProbeReturning bondPrices
StockQuotes = stockProbeReturning stockPrices
StockDaily = NavDateProbeSupport.stubStockDailyProbe }
let emptyStockDailyProbe =
{ new IStockDailyProbe with
member _.RecentDaily(_, _, _) = Ok [] }
let stockDailyProbeReturning (bars: Map<string, StockDailyObservation list>) =
{ new IStockDailyProbe with
member _.RecentDaily(code, _, _) =
match bars.TryFind code with
| Some observations -> Ok observations
| None -> Error(MarketDataCollectorUnavailable(sprintf "no daily bars for %s" code)) }
let probesWithDaily
(stockPrices: Map<string, decimal>)
(bondPrices: Map<string, decimal>)
(stockDaily: IStockDailyProbe)
=
{ NavDates = NavDateProbeSupport.stubNavDateProbe
NavSeries = NavDateProbeSupport.stubNavSeriesProbe
BondQuotes = bondProbeReturning bondPrices
StockQuotes = stockProbeReturning stockPrices
StockDaily = stockDaily }
[<Collection("postgres")>]
type FundValuationTests(fixture: PostgresFixture) =
let sharedRepository =
lazy
let value = FundRepository(fixture.ConnectionString)
value.EnsureSchema()
value
let repository () = sharedRepository.Value
let createFund (initialCash: decimal) =
let command =
{
Name = "组合估值测试 FOF"
InitialCash = initialCash
InitialUnitNav = 1.00000000m
IsSynthetic = true
}
let key = fixture.Key(sprintf "valuation-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 buyStock fundId code quantity price =
let key = fixture.Key(sprintf "valuation-stock-%s" (Guid.NewGuid().ToString("N")))
match
repository().CreateStockTrade(
key,
fundId,
{ InstrumentCode = code; StockName = Some "贵州茅台"; Quantity = quantity; Price = price }
)
with
| StockTradeWriteResult.StockTradeCreated _ -> ()
| other -> failwithf "unexpected stock trade result: %A" other
let buyBond fundId code quantity price =
let key = fixture.Key(sprintf "valuation-bond-%s" (Guid.NewGuid().ToString("N")))
match
repository().CreateBondTrade(
key,
fundId,
{ InstrumentCode = code; BondName = Some "南航转债"; Quantity = quantity; Price = price }
)
with
| BondTradeWriteResult.BondTradeCreated _ -> ()
| other -> failwithf "unexpected bond trade result: %A" other
let app stockPrices bondPrices =
App.createApplicationWithProbes (repository ()) (FundValuationSupport.probes stockPrices bondPrices)
let getValuation stockPrices bondPrices fundId =
PersistenceTestHelpers.invoke
(app stockPrices bondPrices)
"GET"
(sprintf "/api/funds/%O/valuation" fundId)
[ "Authorization", "Bearer test-token" ]
""
[<Fact>]
member _.``mixed valuation prices live positions and flags the unavailable one``() =
let fundId = createFund 10000.00m
buyStock fundId "600519" 100m 1000.00m
buyBond fundId "110075" 10m 100.00m
let status, body =
getValuation (Map.ofList [ "600519", 1252.57m ]) (Map.ofList [ "110075", 106.368m ]) fundId
Assert.Equal(200, status)
Assert.Contains("\"cash\":\"10000.00\"", body)
Assert.Contains("\"positionsMarketValue\":\"126320.68\"", body)
Assert.Contains("\"portfolioValue\":\"136320.68\"", body)
Assert.Contains("\"pricedPositions\":2", body)
Assert.Contains("\"unavailablePositions\":0", body)
[<Fact>]
member _.``all-unavailable valuation keeps cash intact and marks positions unavailable without zeroing``() =
let fundId = createFund 5000.00m
buyStock fundId "600519" 100m 1000.00m
buyBond fundId "110075" 10m 100.00m
let status, body = getValuation Map.empty Map.empty fundId
Assert.Equal(200, status)
Assert.Contains("\"cash\":\"5000.00\"", body)
Assert.Contains("\"positionsMarketValue\":\"0.00\"", body)
Assert.Contains("\"portfolioValue\":\"5000.00\"", body)
Assert.Contains("\"unavailablePositions\":2", body)
Assert.Contains("\"status\":\"unavailable\"", body)
Assert.Contains("\"quantity\":\"100.00000000\"", body)
Assert.Contains("\"quantity\":\"10.00000000\"", body)
[<Fact>]
member _.``partial failure values available positions and does not inflate on missing price``() =
let fundId = createFund 2000.00m
buyStock fundId "600519" 100m 1000.00m
buyBond fundId "110075" 10m 100.00m
let status, body = getValuation (Map.ofList [ "600519", 10.00m ]) Map.empty fundId
Assert.Equal(200, status)
Assert.Contains("\"positionsMarketValue\":\"1000.00\"", body)
Assert.Contains("\"portfolioValue\":\"3000.00\"", body)
Assert.Contains("\"pricedPositions\":1", body)
Assert.Contains("\"unavailablePositions\":1", body)
[<Fact>]
member _.``without probes every position is marked unavailable and excluded from market value``() =
let fundId = createFund 0.00m
buyStock fundId "600519" 100m 1000.00m
let status, body =
PersistenceTestHelpers.invoke
(App.createApplication (repository ()))
"GET"
(sprintf "/api/funds/%O/valuation" fundId)
[ "Authorization", "Bearer test-token" ]
""
Assert.Equal(200, status)
Assert.Contains("\"positionsMarketValue\":\"0.00\"", body)
Assert.Contains("\"portfolioValue\":\"0.00\"", body)
Assert.Contains("\"status\":\"unavailable\"", body)
Assert.Contains("\"quantity\":\"100.00000000\"", body)
Assert.Contains("\"unavailablePositions\":1", body)
[<Fact>]
member _.``valuation of a missing fund answers 404``() =
let status, body = getValuation Map.empty Map.empty (Guid.NewGuid())
Assert.Equal(404, status)
Assert.Contains("FUND_NOT_FOUND", body)
[<Fact>]
member _.``refresh persists a stock daily close and valuation prefers that snapshot over a live quote``() =
let fundId = createFund 10000.00m
buyStock fundId "601398" 100m 1000.00m
let daily : StockDailyObservation =
{
BarDate = DateOnly(2026, 9, 21)
Close = 1111.00m
Volume = None
Amount = None
}
let dailyProbe =
FundValuationSupport.stockDailyProbeReturning (Map.ofList [ "601398", [ daily ] ])
let appWithDaily =
App.createApplicationWithProbes
(repository ())
(FundValuationSupport.probesWithDaily Map.empty Map.empty dailyProbe)
let refreshStatus, refreshBody =
PersistenceTestHelpers.invoke
appWithDaily
"POST"
(sprintf "/api/funds/%O/market-data/refresh" fundId)
[ "Authorization", "Bearer test-token" ]
""
Assert.Equal(200, refreshStatus)
Assert.Contains("\"asOfDate\":\"2026-09-21\"", refreshBody)
Assert.Contains("\"instrumentCode\":\"601398\"", refreshBody)
Assert.Contains("\"assetClass\":\"stock\"", refreshBody)
Assert.Contains("\"snapshotDate\":\"2026-09-21\"", refreshBody)
Assert.Contains("\"price\":\"1111.00000000\"", refreshBody)
Assert.Contains("\"failures\":[]", refreshBody)
// A deliberately different live quote must not override the persisted close.
let status, body = getValuation (Map.ofList [ "601398", 9999.00m ]) Map.empty fundId
Assert.Equal(200, status)
Assert.Contains("\"positionsMarketValue\":\"111100.00\"", body)
Assert.Contains("\"priceSource\":\"snapshot\"", body)
[<Fact>]
member _.``refresh marks instruments without market data as failures and never fabricates a price``() =
let fundId = createFund 5000.00m
buyStock fundId "600000" 100m 1000.00m
buyBond fundId "110059" 10m 100.00m
let appMissing =
App.createApplicationWithProbes
(repository ())
(FundValuationSupport.probesWithDaily Map.empty Map.empty FundValuationSupport.emptyStockDailyProbe)
let refreshStatus, refreshBody =
PersistenceTestHelpers.invoke
appMissing
"POST"
(sprintf "/api/funds/%O/market-data/refresh" fundId)
[ "Authorization", "Bearer test-token" ]
""
Assert.Equal(200, refreshStatus)
Assert.Contains("\"refreshed\":[]", refreshBody)
Assert.Contains("\"instrumentCode\":\"600000\"", refreshBody)
Assert.Contains("\"reason\":\"no daily bar on or before the refresh date\"", refreshBody)
Assert.Contains("\"instrumentCode\":\"110059\"", refreshBody)
Assert.Contains("\"reason\":\"no valid quote for 110059\"", refreshBody)
let status, body = getValuation Map.empty Map.empty fundId
Assert.Equal(200, status)
Assert.Contains("\"positionsMarketValue\":\"0.00\"", body)
Assert.Contains("\"unavailablePositions\":2", body)
Assert.Contains("\"status\":\"unavailable\"", body)
[<Fact>]
member _.``refresh persists a bond valuation price and valuation prefers that snapshot``() =
let fundId = createFund 8000.00m
buyBond fundId "113050" 10m 100.00m
let appWithBond =
App.createApplicationWithProbes
(repository ())
(FundValuationSupport.probes Map.empty (Map.ofList [ "113050", 106.368m ]))
let refreshStatus, refreshBody =
PersistenceTestHelpers.invoke
appWithBond
"POST"
(sprintf "/api/funds/%O/market-data/refresh" fundId)
[ "Authorization", "Bearer test-token" ]
""
Assert.Equal(200, refreshStatus)
Assert.Contains("\"assetClass\":\"bond\"", refreshBody)
Assert.Contains("\"price\":\"106.36800000\"", refreshBody)
let status, body = getValuation Map.empty (Map.ofList [ "113050", 200.00m ]) fundId
Assert.Equal(200, status)
Assert.Contains("\"positionsMarketValue\":\"1063.68\"", body)
Assert.Contains("\"priceSource\":\"snapshot\"", body)
|