|
| 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 | +} |
0 commit comments