Skip to content

Commit e2a7185

Browse files
committed
Add tests for Bulk module
Cover Bulk.execute (all-success 200, mixed 207, all-error 422, empty 200, result ordering) and Bulk.handler via TestClient (JSON array body + malformed body 400). Bulk was the only shipped module without tests.
1 parent 15992a5 commit e2a7185

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

tests/Firefly.Tests/BulkTests.fs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
module Firefly.Tests.BulkTests
2+
3+
open System.Threading.Tasks
4+
open Xunit
5+
open FsUnit.Xunit
6+
open Firefly
7+
8+
let private bodyString (response: Response) =
9+
match (Internal.materializeJson response).Body with
10+
| ResponseBody.Json bytes -> System.Text.Encoding.UTF8.GetString(bytes)
11+
| ResponseBody.Text s -> s
12+
| _ -> failwith "expected JSON body"
13+
14+
let private okOp (x: int) : Task<Result<int, string>> = task { return Ok(x * 10) }
15+
let private failOp (x: int) : Task<Result<int, string>> = task { return Error $"bad {x}" }
16+
let private evenOp (x: int) : Task<Result<int, string>> = task {
17+
return (if x % 2 = 0 then Ok(x * 10) else Error $"odd {x}")
18+
}
19+
20+
[<Fact>]
21+
let ``execute all success returns 200 with counts`` () = task {
22+
let! response = Bulk.execute okOp [ 1; 2; 3 ]
23+
response.Status |> should equal 200
24+
let body = bodyString response
25+
body |> should haveSubstring "\"succeeded\":3"
26+
body |> should haveSubstring "\"failed\":0"
27+
body |> should haveSubstring "\"total\":3"
28+
body |> should haveSubstring "\"status\":\"success\""
29+
}
30+
31+
[<Fact>]
32+
let ``execute mixed results returns 207 multi-status`` () = task {
33+
let! response = Bulk.execute evenOp [ 2; 3; 4 ] // ok, error, ok
34+
response.Status |> should equal 207
35+
let body = bodyString response
36+
body |> should haveSubstring "\"succeeded\":2"
37+
body |> should haveSubstring "\"failed\":1"
38+
body |> should haveSubstring "\"status\":\"error\""
39+
body |> should haveSubstring "odd 3"
40+
}
41+
42+
[<Fact>]
43+
let ``execute all errors returns 422`` () = task {
44+
let! response = Bulk.execute failOp [ 1; 2 ]
45+
response.Status |> should equal 422
46+
let body = bodyString response
47+
body |> should haveSubstring "\"succeeded\":0"
48+
body |> should haveSubstring "\"failed\":2"
49+
}
50+
51+
[<Fact>]
52+
let ``execute empty list returns 200 with zero counts`` () = task {
53+
let! response = Bulk.execute okOp []
54+
response.Status |> should equal 200
55+
let body = bodyString response
56+
body |> should haveSubstring "\"succeeded\":0"
57+
body |> should haveSubstring "\"failed\":0"
58+
body |> should haveSubstring "\"total\":0"
59+
}
60+
61+
[<Fact>]
62+
let ``execute preserves input order in results`` () = task {
63+
let! response = Bulk.execute okOp [ 5; 6; 7 ]
64+
let body = bodyString response
65+
let i0 = body.IndexOf("\"index\":0")
66+
let i1 = body.IndexOf("\"index\":1")
67+
let i2 = body.IndexOf("\"index\":2")
68+
i0 |> should be (greaterThan -1)
69+
i0 |> should be (lessThan i1)
70+
i1 |> should be (lessThan i2)
71+
}
72+
73+
// --- handler via TestClient ---
74+
75+
let private routes =
76+
Route.start
77+
|> Route.post "/bulk" (Bulk.handler okOp)
78+
79+
[<Fact>]
80+
let ``handler processes a JSON array body`` () = task {
81+
let client = TestClient.create routes
82+
let! r = client |> TestClient.post "/bulk" "[1, 2, 3]"
83+
r.Status |> should equal 200
84+
r.Body |> should haveSubstring "\"succeeded\":3"
85+
}
86+
87+
[<Fact>]
88+
let ``handler returns 400 on malformed body`` () = task {
89+
let client = TestClient.create routes
90+
let! r = client |> TestClient.post "/bulk" "not json"
91+
r.Status |> should equal 400
92+
r.Body |> should haveSubstring "error"
93+
}

tests/Firefly.Tests/Firefly.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<Compile Include="EvlogTests.fs" />
7070
<Compile Include="PipelineTests.fs" />
7171
<Compile Include="EnvTests.fs" />
72+
<Compile Include="BulkTests.fs" />
7273
</ItemGroup>
7374

7475
<ItemGroup>

0 commit comments

Comments
 (0)