Make Json calls and stream writes async

This commit is contained in:
2025-04-05 19:33:47 -04:00
parent 74961c4ba7
commit 120a59ff7f
4 changed files with 203 additions and 155 deletions

View File

@@ -336,16 +336,16 @@ let customTests = testList "Custom" [
use db = PostgresDb.BuildDb()
do! loadDocs ()
let docs = Custom.jsonArray (Query.find PostgresDb.TableName) [] jsonFromData
let! docs = Custom.jsonArray (Query.find PostgresDb.TableName) [] jsonFromData
Expect.stringStarts docs "[" "The JSON array should have started with `[`"
Expect.hasLength (docs.Split "{\"Id\":") 6 "There should have been 5 documents returned"
Expect.hasLength ((string docs).Split "{\"Id\":") 6 "There should have been 5 documents returned"
Expect.stringEnds docs "]" "The JSON array should have ended with `[`"
}
testTask "succeeds when data is not found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
let docs =
let! docs =
Custom.jsonArray
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
[ "@path", Sql.string "$.NumValue ? (@ > 100)" ]
@@ -360,7 +360,7 @@ let customTests = testList "Custom" [
use stream = new MemoryStream()
use writer = writeStream stream
Custom.writeJsonArray (Query.find PostgresDb.TableName) [] writer jsonFromData
do! Custom.writeJsonArray (Query.find PostgresDb.TableName) [] writer jsonFromData
let docs = streamText stream
Expect.stringStarts docs "[" "The JSON array should have started with `[`"
@@ -373,11 +373,11 @@ let customTests = testList "Custom" [
use stream = new MemoryStream()
use writer = writeStream stream
Custom.writeJsonArray
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
[ "@path", Sql.string "$.NumValue ? (@ > 100)" ]
writer
jsonFromData
do! Custom.writeJsonArray
$"SELECT data FROM {PostgresDb.TableName} WHERE data @? @path::jsonpath"
[ "@path", Sql.string "$.NumValue ? (@ > 100)" ]
writer
jsonFromData
Expect.equal (streamText stream) "[]" "There should have been no documents returned"
}
@@ -412,7 +412,7 @@ let customTests = testList "Custom" [
use db = PostgresDb.BuildDb()
do! loadDocs ()
let doc =
let! doc =
Custom.jsonSingle
$"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id"
[ "@id", Sql.string "one"]
@@ -425,7 +425,7 @@ let customTests = testList "Custom" [
use db = PostgresDb.BuildDb()
do! loadDocs ()
let doc =
let! doc =
Custom.jsonSingle
$"SELECT data FROM {PostgresDb.TableName} WHERE data ->> 'Id' = @id"
[ "@id", Sql.string "eighty" ]
@@ -1113,16 +1113,16 @@ let private verifyBeginEnd json =
Expect.stringEnds json "]" "The array should have ended with `]`"
/// Verify the presence of a document by its ID
let private verifyDocById docId json =
let private verifyDocById json docId =
Expect.stringContains json $"{{\"Id\": \"%s{docId}\"," $"Document `{docId}` not present"
/// Verify the presence of a document by its ID
let private verifySingleById docId json =
let private verifySingleById json docId =
verifyBeginEnd json
Expect.stringContains json $"{{\"Id\": \"%s{docId}\"," $"Document `{docId}` not present"
/// Verify the presence of any of the given document IDs in the given JSON
let private verifyAnyById (docIds: string list) (json: string) =
let private verifyAnyById (json: string) (docIds: string list) =
match docIds |> List.tryFind (fun it -> json.Contains $"{{\"Id\": \"{it}\"") with
| Some _ -> ()
| None ->
@@ -1132,7 +1132,7 @@ let private verifyAnyById (docIds: string list) (json: string) =
/// Verify the JSON for `all` returning data
let private verifyAllData json =
verifyBeginEnd json
for docId in [ "one"; "two"; "three"; "four"; "five" ] do verifyDocById docId json
[ "one"; "two"; "three"; "four"; "five" ] |> List.iter (verifyDocById json)
/// Verify an empty JSON array
let private verifyEmpty json =
@@ -1143,7 +1143,7 @@ let private verifyNoDoc json =
Expect.equal json "{}" "There should be no document returned"
/// Verify the JSON for an ordered query
let private verifyExpectedOrder idFirst idSecond idThird idFourth idFifth (json: string) =
let private verifyExpectedOrder (json: string) idFirst idSecond idThird idFourth idFifth =
let firstIdx = json.IndexOf $"{{\"Id\": \"%s{idFirst}\","
let secondIdx = json.IndexOf $"{{\"Id\": \"%s{idSecond}\","
verifyBeginEnd json
@@ -1170,31 +1170,33 @@ let jsonTests = testList "Json" [
testTask "succeeds when there is data" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.all PostgresDb.TableName |> verifyAllData
let! json = Json.all PostgresDb.TableName
verifyAllData json
}
testTask "succeeds when there is no data" {
use db = PostgresDb.BuildDb()
Json.all PostgresDb.TableName |> verifyEmpty
let! json = Json.all PostgresDb.TableName
verifyEmpty json
}
]
testList "allOrdered" [
testTask "succeeds when ordering numerically" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.allOrdered PostgresDb.TableName [ Field.Named "n:NumValue" ]
|> verifyExpectedOrder "one" "three" (Some "two") (Some "four") (Some "five")
let! json = Json.allOrdered PostgresDb.TableName [ Field.Named "n:NumValue" ]
verifyExpectedOrder json "one" "three" (Some "two") (Some "four") (Some "five")
}
testTask "succeeds when ordering numerically descending" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.allOrdered PostgresDb.TableName [ Field.Named "n:NumValue DESC" ]
|> verifyExpectedOrder "five" "four" (Some "two") (Some "three") (Some "one")
let! json = Json.allOrdered PostgresDb.TableName [ Field.Named "n:NumValue DESC" ]
verifyExpectedOrder json "five" "four" (Some "two") (Some "three") (Some "one")
}
testTask "succeeds when ordering alphabetically" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.allOrdered PostgresDb.TableName [ Field.Named "Id DESC" ]
|> verifyExpectedOrder "two" "three" (Some "one") (Some "four") (Some "five")
let! json = Json.allOrdered PostgresDb.TableName [ Field.Named "Id DESC" ]
verifyExpectedOrder json "two" "three" (Some "one") (Some "four") (Some "five")
}
]
testList "byId" [
@@ -1202,64 +1204,70 @@ let jsonTests = testList "Json" [
use db = PostgresDb.BuildDb()
do! loadDocs ()
let json = Json.byId PostgresDb.TableName "two"
let! json = Json.byId PostgresDb.TableName "two"
Expect.stringStarts json """{"Id": "two",""" "An incorrect document was returned"
Expect.stringEnds json "}" "JSON should have ended with this document"
}
testTask "succeeds when a document is not found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.byId PostgresDb.TableName "three hundred eighty-seven" |> verifyNoDoc
let! json = Json.byId PostgresDb.TableName "three hundred eighty-seven"
verifyNoDoc json
}
]
testList "byFields" [
testTask "succeeds when documents are found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.byFields PostgresDb.TableName All [ Field.In "Value" [ "purple"; "blue" ]; Field.Exists "Sub" ]
|> verifySingleById "four"
let! json =
Json.byFields PostgresDb.TableName All [ Field.In "Value" [ "purple"; "blue" ]; Field.Exists "Sub" ]
verifySingleById json "four"
}
testTask "succeeds when documents are found using IN with numeric field" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.byFields PostgresDb.TableName All [ Field.In "NumValue" [ 2; 4; 6; 8 ] ] |> verifySingleById "three"
let! json = Json.byFields PostgresDb.TableName All [ Field.In "NumValue" [ 2; 4; 6; 8 ] ]
verifySingleById json "three"
}
testTask "succeeds when documents are not found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.byFields PostgresDb.TableName All [ Field.Equal "Value" "mauve"; Field.NotEqual "NumValue" 40 ]
|> verifyEmpty
let! json =
Json.byFields PostgresDb.TableName All [ Field.Equal "Value" "mauve"; Field.NotEqual "NumValue" 40 ]
verifyEmpty json
}
testTask "succeeds for InArray when matching documents exist" {
use db = PostgresDb.BuildDb()
do! Definition.ensureTable PostgresDb.TableName
for doc in ArrayDocument.TestDocuments do do! insert PostgresDb.TableName doc
let json = Json.byFields PostgresDb.TableName All [ Field.InArray "Values" PostgresDb.TableName [ "c" ] ]
let! json = Json.byFields PostgresDb.TableName All [ Field.InArray "Values" PostgresDb.TableName [ "c" ] ]
verifyBeginEnd json
verifyDocById "first" json
verifyDocById "second" json
verifyDocById json "first"
verifyDocById json "second"
}
testTask "succeeds for InArray when no matching documents exist" {
use db = PostgresDb.BuildDb()
do! Definition.ensureTable PostgresDb.TableName
for doc in ArrayDocument.TestDocuments do do! insert PostgresDb.TableName doc
Json.byFields PostgresDb.TableName All [ Field.InArray "Values" PostgresDb.TableName [ "j" ] ]
|> verifyEmpty
let! json = Json.byFields PostgresDb.TableName All [ Field.InArray "Values" PostgresDb.TableName [ "j" ] ]
verifyEmpty json
}
]
testList "byFieldsOrdered" [
testTask "succeeds when sorting ascending" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.byFieldsOrdered PostgresDb.TableName All [ Field.Equal "Value" "purple" ] [ Field.Named "Id" ]
|> verifyExpectedOrder "five" "four" None None None
let! json =
Json.byFieldsOrdered PostgresDb.TableName All [ Field.Equal "Value" "purple" ] [ Field.Named "Id" ]
verifyExpectedOrder json "five" "four" None None None
}
testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.byFieldsOrdered PostgresDb.TableName All [ Field.Equal "Value" "purple" ] [ Field.Named "Id DESC" ]
|> verifyExpectedOrder "four" "five" None None None
let! json =
Json.byFieldsOrdered PostgresDb.TableName All [ Field.Equal "Value" "purple" ] [ Field.Named "Id DESC" ]
verifyExpectedOrder json "four" "five" None None None
}
]
testList "byContains" [
@@ -1267,15 +1275,16 @@ let jsonTests = testList "Json" [
use db = PostgresDb.BuildDb()
do! loadDocs ()
let json = Json.byContains PostgresDb.TableName {| Sub = {| Foo = "green" |} |}
let! json = Json.byContains PostgresDb.TableName {| Sub = {| Foo = "green" |} |}
verifyBeginEnd json
verifyDocById "two" json
verifyDocById "four" json
verifyDocById json "two"
verifyDocById json "four"
}
testTask "succeeds when documents are not found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.byContains PostgresDb.TableName {| Value = "mauve" |} |> verifyEmpty
let! json = Json.byContains PostgresDb.TableName {| Value = "mauve" |}
verifyEmpty json
}
]
testList "byContainsOrdered" [
@@ -1283,14 +1292,17 @@ let jsonTests = testList "Json" [
testTask "succeeds when sorting ascending" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.byContainsOrdered PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar" ]
|> verifyExpectedOrder "two" "four" None None None
let! json =
Json.byContainsOrdered PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar" ]
verifyExpectedOrder json "two" "four" None None None
}
testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.byContainsOrdered PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar DESC" ]
|> verifyExpectedOrder "four" "two" None None None
let! json =
Json.byContainsOrdered
PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Sub.Bar DESC" ]
verifyExpectedOrder json "four" "two" None None None
}
]
testList "byJsonPath" [
@@ -1298,16 +1310,17 @@ let jsonTests = testList "Json" [
use db = PostgresDb.BuildDb()
do! loadDocs ()
let json = Json.byJsonPath PostgresDb.TableName "$.NumValue ? (@ < 15)"
let! json = Json.byJsonPath PostgresDb.TableName "$.NumValue ? (@ < 15)"
verifyBeginEnd json
verifyDocById "one" json
verifyDocById "two" json
verifyDocById "three" json
verifyDocById json "one"
verifyDocById json "two"
verifyDocById json "three"
}
testTask "succeeds when documents are not found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.byJsonPath PostgresDb.TableName "$.NumValue ? (@ < 0)" |> verifyEmpty
let! json = Json.byJsonPath PostgresDb.TableName "$.NumValue ? (@ < 0)"
verifyEmpty json
}
]
testList "byJsonPathOrdered" [
@@ -1315,113 +1328,127 @@ let jsonTests = testList "Json" [
testTask "succeeds when sorting ascending" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.byJsonPathOrdered PostgresDb.TableName "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue" ]
|> verifyExpectedOrder "one" "three" (Some "two") None None
let! json = Json.byJsonPathOrdered PostgresDb.TableName "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue" ]
verifyExpectedOrder json "one" "three" (Some "two") None None
}
testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.byJsonPathOrdered PostgresDb.TableName "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue DESC" ]
|> verifyExpectedOrder "two" "three" (Some "one") None None
let! json =
Json.byJsonPathOrdered PostgresDb.TableName "$.NumValue ? (@ < 15)" [ Field.Named "n:NumValue DESC" ]
verifyExpectedOrder json "two" "three" (Some "one") None None
}
]
testList "firstByFields" [
testTask "succeeds when a document is found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByFields PostgresDb.TableName Any [ Field.Equal "Value" "another" ]
|> verifyDocById "two"
let! json = Json.firstByFields PostgresDb.TableName Any [ Field.Equal "Value" "another" ]
verifyDocById json "two"
}
testTask "succeeds when multiple documents are found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByFields PostgresDb.TableName Any [ Field.Equal "Value" "purple" ]
|> verifyAnyById [ "five"; "four" ]
let! json = Json.firstByFields PostgresDb.TableName Any [ Field.Equal "Value" "purple" ]
verifyAnyById json [ "five"; "four" ]
}
testTask "succeeds when a document is not found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByFields PostgresDb.TableName Any [ Field.Equal "Value" "absent" ] |> verifyNoDoc
let! json = Json.firstByFields PostgresDb.TableName Any [ Field.Equal "Value" "absent" ]
verifyNoDoc json
}
]
testList "firstByFieldsOrdered" [
testTask "succeeds when sorting ascending" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByFieldsOrdered PostgresDb.TableName Any [ Field.Equal "Value" "purple" ] [ Field.Named "Id" ]
|> verifyDocById "five"
let! json =
Json.firstByFieldsOrdered PostgresDb.TableName Any [ Field.Equal "Value" "purple" ] [ Field.Named "Id" ]
verifyDocById json "five"
}
testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByFieldsOrdered
PostgresDb.TableName Any [ Field.Equal "Value" "purple" ] [ Field.Named "Id DESC" ]
|> verifyDocById "four"
let! json =
Json.firstByFieldsOrdered
PostgresDb.TableName Any [ Field.Equal "Value" "purple" ] [ Field.Named "Id DESC" ]
verifyDocById json "four"
}
]
testList "firstByContains" [
testTask "succeeds when a document is found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByContains PostgresDb.TableName {| Value = "another" |} |> verifyDocById "two"
let! json = Json.firstByContains PostgresDb.TableName {| Value = "another" |}
verifyDocById json "two"
}
testTask "succeeds when multiple documents are found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByContains PostgresDb.TableName {| Sub = {| Foo = "green" |} |} |> verifyAnyById [ "two"; "four" ]
let! json = Json.firstByContains PostgresDb.TableName {| Sub = {| Foo = "green" |} |}
verifyAnyById json [ "two"; "four" ]
}
testTask "succeeds when a document is not found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByContains PostgresDb.TableName {| Value = "absent" |} |> verifyNoDoc
let! json = Json.firstByContains PostgresDb.TableName {| Value = "absent" |}
verifyNoDoc json
}
]
testList "firstByContainsOrdered" [
testTask "succeeds when sorting ascending" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByContainsOrdered PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Value" ]
|> verifyDocById "two"
let! json =
Json.firstByContainsOrdered PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Value" ]
verifyDocById json "two"
}
testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByContainsOrdered
PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Value DESC" ]
|> verifyDocById "four"
let! json =
Json.firstByContainsOrdered
PostgresDb.TableName {| Sub = {| Foo = "green" |} |} [ Field.Named "Value DESC" ]
verifyDocById json "four"
}
]
testList "firstByJsonPath" [
testTask "succeeds when a document is found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByJsonPath PostgresDb.TableName """$.Value ? (@ == "FIRST!")""" |> verifyDocById "one"
let! json = Json.firstByJsonPath PostgresDb.TableName """$.Value ? (@ == "FIRST!")"""
verifyDocById json "one"
}
testTask "succeeds when multiple documents are found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByJsonPath PostgresDb.TableName """$.Sub.Foo ? (@ == "green")"""
|> verifyAnyById [ "two"; "four" ]
let! json = Json.firstByJsonPath PostgresDb.TableName """$.Sub.Foo ? (@ == "green")"""
verifyAnyById json [ "two"; "four" ]
}
testTask "succeeds when a document is not found" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByJsonPath PostgresDb.TableName """$.Id ? (@ == "nope")""" |> verifyNoDoc
let! json = Json.firstByJsonPath PostgresDb.TableName """$.Id ? (@ == "nope")"""
verifyNoDoc json
}
]
testList "firstByJsonPathOrdered" [
testTask "succeeds when sorting ascending" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByJsonPathOrdered PostgresDb.TableName """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar" ]
|> verifyDocById "two"
let! json =
Json.firstByJsonPathOrdered
PostgresDb.TableName """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar" ]
verifyDocById json "two"
}
testTask "succeeds when sorting descending" {
use db = PostgresDb.BuildDb()
do! loadDocs ()
Json.firstByJsonPathOrdered
PostgresDb.TableName """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar DESC" ]
|> verifyDocById "four"
let! json =
Json.firstByJsonPathOrdered
PostgresDb.TableName """$.Sub.Foo ? (@ == "green")""" [ Field.Named "Sub.Bar DESC" ]
verifyDocById json "four"
}
]
]