v4.1 (#11)
- Add `Json` module to return JSON strings and write JSON as it's read to a `PipeWriter` - Add `docfx`-based documentation to allow how-to docs and API docs to be generated on the same site Reviewed-on: #11
This commit was merged in pull request #11.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
module SqliteTests
|
||||
|
||||
open System.IO
|
||||
open System.IO.Pipelines
|
||||
open System.Text.Json
|
||||
open BitBadger.Documents
|
||||
open BitBadger.Documents.Sqlite
|
||||
@@ -135,6 +137,17 @@ let loadDocs () = backgroundTask {
|
||||
for doc in testDocuments do do! insert SqliteDb.TableName doc
|
||||
}
|
||||
|
||||
/// Set up a stream writer for a test
|
||||
let writeStream (stream: Stream) =
|
||||
PipeWriter.Create(stream, StreamPipeWriterOptions(leaveOpen = true))
|
||||
|
||||
/// Get the text of the given stream
|
||||
let streamText (stream: Stream) =
|
||||
stream.Position <- 0L
|
||||
use reader = new StreamReader(stream)
|
||||
reader.ReadToEnd()
|
||||
|
||||
|
||||
/// Integration tests for the Configuration module of the SQLite library
|
||||
let configurationTests = testList "Configuration" [
|
||||
test "useConnectionString / connectionString succeed" {
|
||||
@@ -151,6 +164,89 @@ let configurationTests = testList "Configuration" [
|
||||
|
||||
/// Integration tests for the Custom module of the SQLite library
|
||||
let customTests = testList "Custom" [
|
||||
testList "list" [
|
||||
testTask "succeeds when data is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! docs = Custom.list (Query.find SqliteDb.TableName) [] fromData<JsonDocument>
|
||||
Expect.hasCountOf docs 5u (fun _ -> true) "There should have been 5 documents returned"
|
||||
}
|
||||
testTask "succeeds when data is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! docs =
|
||||
Custom.list
|
||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value"
|
||||
[ SqliteParameter("@value", 100) ]
|
||||
fromData<JsonDocument>
|
||||
Expect.isEmpty docs "There should have been no documents returned"
|
||||
}
|
||||
]
|
||||
testList "jsonArray" [
|
||||
testTask "succeeds when data is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json = Custom.jsonArray (Query.find SqliteDb.TableName) [] jsonFromData
|
||||
Expect.stringStarts json "[" "The JSON array should have started with `[`"
|
||||
Expect.stringContains json JsonDocument.one "Document ID `one` should have been found"
|
||||
Expect.stringContains json JsonDocument.two "Document ID `two` should have been found"
|
||||
Expect.stringContains json JsonDocument.three "Document ID `three` should have been found"
|
||||
Expect.stringContains json JsonDocument.four "Document ID `four` should have been found"
|
||||
Expect.stringContains json JsonDocument.five "Document ID `five` should have been found"
|
||||
Expect.stringEnds json "]" "The JSON array should have ended with `[`"
|
||||
}
|
||||
testTask "succeeds when data is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! docs =
|
||||
Custom.jsonArray
|
||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value"
|
||||
[ SqliteParameter("@value", 100) ]
|
||||
jsonFromData
|
||||
Expect.equal docs "[]" "There should have been no documents returned"
|
||||
}
|
||||
]
|
||||
testList "writeJsonArray" [
|
||||
testTask "succeeds when data is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Custom.writeJsonArray (Query.find SqliteDb.TableName) [] writer jsonFromData
|
||||
let json = streamText stream
|
||||
Expect.stringStarts json "[" "The JSON array should have started with `[`"
|
||||
Expect.stringContains json JsonDocument.one "Document ID `one` should have been found"
|
||||
Expect.stringContains json JsonDocument.two "Document ID `two` should have been found"
|
||||
Expect.stringContains json JsonDocument.three "Document ID `three` should have been found"
|
||||
Expect.stringContains json JsonDocument.four "Document ID `four` should have been found"
|
||||
Expect.stringContains json JsonDocument.five "Document ID `five` should have been found"
|
||||
Expect.stringEnds json "]" "The JSON array should have ended with `[`"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds when data is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Custom.writeJsonArray
|
||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value"
|
||||
[ SqliteParameter("@value", 100) ]
|
||||
writer
|
||||
jsonFromData
|
||||
Expect.equal (streamText stream) "[]" "There should have been no documents returned"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
]
|
||||
testList "single" [
|
||||
testTask "succeeds when a row is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
@@ -176,24 +272,28 @@ let customTests = testList "Custom" [
|
||||
Expect.isNone doc "There should not have been a document returned"
|
||||
}
|
||||
]
|
||||
testList "list" [
|
||||
testTask "succeeds when data is found" {
|
||||
testList "jsonSingle" [
|
||||
testTask "succeeds when a row is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! docs = Custom.list (Query.find SqliteDb.TableName) [] fromData<JsonDocument>
|
||||
Expect.hasCountOf docs 5u (fun _ -> true) "There should have been 5 documents returned"
|
||||
let! json =
|
||||
Custom.jsonSingle
|
||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id"
|
||||
[ SqliteParameter("@id", "one") ]
|
||||
jsonFromData
|
||||
Expect.equal json JsonDocument.one "The JSON document is incorrect"
|
||||
}
|
||||
testTask "succeeds when data is not found" {
|
||||
testTask "succeeds when a row is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! docs =
|
||||
Custom.list
|
||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value"
|
||||
[ SqliteParameter("@value", 100) ]
|
||||
fromData<JsonDocument>
|
||||
Expect.isEmpty docs "There should have been no documents returned"
|
||||
let! json =
|
||||
Custom.jsonSingle
|
||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id"
|
||||
[ SqliteParameter("@id", "eighty") ]
|
||||
jsonFromData
|
||||
Expect.equal json "{}" "There should not have been a document returned"
|
||||
}
|
||||
]
|
||||
testList "nonQuery" [
|
||||
@@ -653,6 +753,533 @@ let findTests = testList "Find" [
|
||||
]
|
||||
]
|
||||
|
||||
/// Verify a JSON array begins with "[" and ends with "]"
|
||||
let private verifyBeginEnd json =
|
||||
Expect.stringStarts json "[" "The array should have started with `[`"
|
||||
Expect.stringEnds json "]" "The array should have ended with `]`"
|
||||
|
||||
/// Verify an empty JSON array
|
||||
let private verifyEmpty json =
|
||||
Expect.equal json "[]" "There should be no documents returned"
|
||||
|
||||
/// Verify an empty JSON document
|
||||
let private verifyNoDoc json =
|
||||
Expect.equal json "{}" "There should be no document returned"
|
||||
|
||||
/// Verify the presence of any of the given documents in the given JSON
|
||||
let private verifyAny (json: string) (docs: string list) =
|
||||
match docs |> List.tryFind json.Contains with
|
||||
| Some _ -> ()
|
||||
| None ->
|
||||
let theDocs = docs |> String.concat " | "
|
||||
Expect.isTrue false $"Could not find any of |{theDocs}| in {json}"
|
||||
|
||||
/// Integration tests for the Json module of the SQLite library
|
||||
let jsonTests = testList "Json" [
|
||||
testList "all" [
|
||||
testTask "succeeds when there is data" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
|
||||
do! insert SqliteDb.TableName { Foo = "one"; Bar = "two" }
|
||||
do! insert SqliteDb.TableName { Foo = "three"; Bar = "four" }
|
||||
do! insert SqliteDb.TableName { Foo = "five"; Bar = "six" }
|
||||
|
||||
let! json = Json.all SqliteDb.TableName
|
||||
verifyBeginEnd json
|
||||
Expect.stringContains json """{"Foo":"one","Bar":"two"}""" "The first document was not found"
|
||||
Expect.stringContains json """{"Foo":"three","Bar":"four"}""" "The second document was not found"
|
||||
Expect.stringContains json """{"Foo":"five","Bar":"six"}""" "The third document was not found"
|
||||
}
|
||||
testTask "succeeds when there is no data" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
let! json = Json.all SqliteDb.TableName
|
||||
verifyEmpty json
|
||||
}
|
||||
]
|
||||
testList "allOrdered" [
|
||||
testTask "succeeds when ordering numerically" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json = Json.allOrdered SqliteDb.TableName [ Field.Named "n:NumValue" ]
|
||||
Expect.equal
|
||||
json
|
||||
$"[{JsonDocument.one},{JsonDocument.three},{JsonDocument.two},{JsonDocument.four},{JsonDocument.five}]"
|
||||
"The documents were not ordered correctly"
|
||||
}
|
||||
testTask "succeeds when ordering numerically descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json = Json.allOrdered SqliteDb.TableName [ Field.Named "n:NumValue DESC" ]
|
||||
Expect.equal
|
||||
json
|
||||
$"[{JsonDocument.five},{JsonDocument.four},{JsonDocument.two},{JsonDocument.three},{JsonDocument.one}]"
|
||||
"The documents were not ordered correctly"
|
||||
}
|
||||
testTask "succeeds when ordering alphabetically" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json = Json.allOrdered SqliteDb.TableName [ Field.Named "Id DESC" ]
|
||||
Expect.equal
|
||||
json
|
||||
$"[{JsonDocument.two},{JsonDocument.three},{JsonDocument.one},{JsonDocument.four},{JsonDocument.five}]"
|
||||
"The documents were not ordered correctly"
|
||||
}
|
||||
]
|
||||
testList "byId" [
|
||||
testTask "succeeds when a document is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json = Json.byId SqliteDb.TableName "two"
|
||||
Expect.equal json JsonDocument.two "The incorrect document was returned"
|
||||
}
|
||||
testTask "succeeds when a document is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json = Json.byId SqliteDb.TableName "three hundred eighty-seven"
|
||||
verifyNoDoc json
|
||||
}
|
||||
]
|
||||
testList "byFields" [
|
||||
testTask "succeeds when documents are found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json = Json.byFields SqliteDb.TableName Any [ Field.Greater "NumValue" 15 ]
|
||||
verifyBeginEnd json
|
||||
Expect.stringContains json JsonDocument.four "Document `four` should have been returned"
|
||||
Expect.stringContains json JsonDocument.five "Document `five` should have been returned"
|
||||
}
|
||||
testTask "succeeds when documents are found using IN with numeric field" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json = Json.byFields SqliteDb.TableName All [ Field.In "NumValue" [ 2; 4; 6; 8 ] ]
|
||||
Expect.equal json $"[{JsonDocument.three}]" "There should have been one document returned"
|
||||
}
|
||||
testTask "succeeds when documents are not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json = Json.byFields SqliteDb.TableName Any [ Field.Greater "NumValue" 100 ]
|
||||
verifyEmpty json
|
||||
}
|
||||
testTask "succeeds for InArray when matching documents exist" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! Definition.ensureTable SqliteDb.TableName
|
||||
for doc in ArrayDocument.TestDocuments do do! insert SqliteDb.TableName doc
|
||||
|
||||
let! json = Json.byFields SqliteDb.TableName All [ Field.InArray "Values" SqliteDb.TableName [ "c" ] ]
|
||||
verifyBeginEnd json
|
||||
Expect.stringContains
|
||||
json """{"Id":"first","Values":["a","b","c"]}""" "Document `first` should have been returned"
|
||||
Expect.stringContains
|
||||
json """{"Id":"second","Values":["c","d","e"]}""" "Document `second` should have been returned"
|
||||
}
|
||||
testTask "succeeds for InArray when no matching documents exist" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! Definition.ensureTable SqliteDb.TableName
|
||||
for doc in ArrayDocument.TestDocuments do do! insert SqliteDb.TableName doc
|
||||
|
||||
let! json = Json.byFields SqliteDb.TableName All [ Field.InArray "Values" SqliteDb.TableName [ "j" ] ]
|
||||
verifyEmpty json
|
||||
}
|
||||
]
|
||||
testList "byFieldsOrdered" [
|
||||
testTask "succeeds when sorting ascending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json = Json.byFieldsOrdered SqliteDb.TableName Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id" ]
|
||||
Expect.equal json $"[{JsonDocument.five},{JsonDocument.four}]" "Incorrect documents were returned"
|
||||
}
|
||||
testTask "succeeds when sorting descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json =
|
||||
Json.byFieldsOrdered SqliteDb.TableName Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id DESC" ]
|
||||
Expect.equal json $"[{JsonDocument.four},{JsonDocument.five}]" "Incorrect documents were returned"
|
||||
}
|
||||
testTask "succeeds when sorting case-sensitively" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json =
|
||||
Json.byFieldsOrdered SqliteDb.TableName All [ Field.LessOrEqual "NumValue" 10 ] [ Field.Named "Value" ]
|
||||
Expect.equal
|
||||
json $"[{JsonDocument.three},{JsonDocument.one},{JsonDocument.two}]" "Documents not ordered correctly"
|
||||
}
|
||||
testTask "succeeds when sorting case-insensitively" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json =
|
||||
Json.byFieldsOrdered
|
||||
SqliteDb.TableName All [ Field.LessOrEqual "NumValue" 10 ] [ Field.Named "i:Value" ]
|
||||
Expect.equal
|
||||
json $"[{JsonDocument.three},{JsonDocument.two},{JsonDocument.one}]" "Documents not ordered correctly"
|
||||
}
|
||||
]
|
||||
testList "firstByFields" [
|
||||
testTask "succeeds when a document is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json = Json.firstByFields SqliteDb.TableName Any [ Field.Equal "Value" "another" ]
|
||||
Expect.equal json JsonDocument.two "The incorrect document was returned"
|
||||
}
|
||||
testTask "succeeds when multiple documents are found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json = Json.firstByFields SqliteDb.TableName Any [ Field.Equal "Sub.Foo" "green" ]
|
||||
Expect.notEqual json "{}" "There should have been a document returned"
|
||||
verifyAny json [ JsonDocument.two; JsonDocument.four ]
|
||||
}
|
||||
testTask "succeeds when a document is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json = Json.firstByFields SqliteDb.TableName Any [ Field.Equal "Value" "absent" ]
|
||||
verifyNoDoc json
|
||||
}
|
||||
]
|
||||
testList "firstByFieldsOrdered" [
|
||||
testTask "succeeds when sorting ascending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json =
|
||||
Json.firstByFieldsOrdered
|
||||
SqliteDb.TableName Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar" ]
|
||||
Expect.equal json JsonDocument.two "An incorrect document was returned"
|
||||
}
|
||||
testTask "succeeds when sorting descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
let! json =
|
||||
Json.firstByFieldsOrdered
|
||||
SqliteDb.TableName Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar DESC" ]
|
||||
Expect.equal json JsonDocument.four "An incorrect document was returned"
|
||||
}
|
||||
]
|
||||
testList "writeAll" [
|
||||
testTask "succeeds when there is data" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
|
||||
do! insert SqliteDb.TableName { Foo = "one"; Bar = "two" }
|
||||
do! insert SqliteDb.TableName { Foo = "three"; Bar = "four" }
|
||||
do! insert SqliteDb.TableName { Foo = "five"; Bar = "six" }
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeAll SqliteDb.TableName writer
|
||||
let json = streamText stream
|
||||
verifyBeginEnd json
|
||||
Expect.stringContains json """{"Foo":"one","Bar":"two"}""" "The first document was not found"
|
||||
Expect.stringContains json """{"Foo":"three","Bar":"four"}""" "The second document was not found"
|
||||
Expect.stringContains json """{"Foo":"five","Bar":"six"}""" "The third document was not found"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds when there is no data" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeAll SqliteDb.TableName writer
|
||||
verifyEmpty (streamText stream)
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
]
|
||||
testList "writeAllOrdered" [
|
||||
testTask "succeeds when ordering numerically" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeAllOrdered SqliteDb.TableName writer [ Field.Named "n:NumValue" ]
|
||||
Expect.equal
|
||||
(streamText stream)
|
||||
$"[{JsonDocument.one},{JsonDocument.three},{JsonDocument.two},{JsonDocument.four},{JsonDocument.five}]"
|
||||
"The documents were not ordered correctly"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds when ordering numerically descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeAllOrdered SqliteDb.TableName writer [ Field.Named "n:NumValue DESC" ]
|
||||
Expect.equal
|
||||
(streamText stream)
|
||||
$"[{JsonDocument.five},{JsonDocument.four},{JsonDocument.two},{JsonDocument.three},{JsonDocument.one}]"
|
||||
"The documents were not ordered correctly"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds when ordering alphabetically" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeAllOrdered SqliteDb.TableName writer [ Field.Named "Id DESC" ]
|
||||
Expect.equal
|
||||
(streamText stream)
|
||||
$"[{JsonDocument.two},{JsonDocument.three},{JsonDocument.one},{JsonDocument.four},{JsonDocument.five}]"
|
||||
"The documents were not ordered correctly"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
]
|
||||
testList "writeById" [
|
||||
testTask "succeeds when a document is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeById SqliteDb.TableName writer "two"
|
||||
Expect.equal (streamText stream) JsonDocument.two "The incorrect document was returned"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds when a document is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeById SqliteDb.TableName writer "three hundred eighty-seven"
|
||||
verifyNoDoc (streamText stream)
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
]
|
||||
testList "writeByFields" [
|
||||
testTask "succeeds when documents are found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeByFields SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ]
|
||||
let json = streamText stream
|
||||
verifyBeginEnd json
|
||||
Expect.stringContains json JsonDocument.four "Document `four` should have been returned"
|
||||
Expect.stringContains json JsonDocument.five "Document `five` should have been returned"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds when documents are found using IN with numeric field" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeByFields SqliteDb.TableName writer All [ Field.In "NumValue" [ 2; 4; 6; 8 ] ]
|
||||
Expect.equal
|
||||
(streamText stream) $"[{JsonDocument.three}]" "There should have been one document returned"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds when documents are not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeByFields SqliteDb.TableName writer Any [ Field.Greater "NumValue" 100 ]
|
||||
verifyEmpty (streamText stream)
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds for InArray when matching documents exist" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! Definition.ensureTable SqliteDb.TableName
|
||||
for doc in ArrayDocument.TestDocuments do do! insert SqliteDb.TableName doc
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeByFields
|
||||
SqliteDb.TableName writer All [ Field.InArray "Values" SqliteDb.TableName [ "c" ] ]
|
||||
let json = streamText stream
|
||||
verifyBeginEnd json
|
||||
Expect.stringContains
|
||||
json """{"Id":"first","Values":["a","b","c"]}""" "Document `first` should have been returned"
|
||||
Expect.stringContains
|
||||
json """{"Id":"second","Values":["c","d","e"]}""" "Document `second` should have been returned"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds for InArray when no matching documents exist" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! Definition.ensureTable SqliteDb.TableName
|
||||
for doc in ArrayDocument.TestDocuments do do! insert SqliteDb.TableName doc
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeByFields
|
||||
SqliteDb.TableName writer All [ Field.InArray "Values" SqliteDb.TableName [ "j" ] ]
|
||||
verifyEmpty (streamText stream)
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
]
|
||||
testList "writeByFieldsOrdered" [
|
||||
testTask "succeeds when sorting ascending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeByFieldsOrdered
|
||||
SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id" ]
|
||||
Expect.equal
|
||||
(streamText stream) $"[{JsonDocument.five},{JsonDocument.four}]" "Incorrect documents were returned"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds when sorting descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeByFieldsOrdered
|
||||
SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id DESC" ]
|
||||
Expect.equal
|
||||
(streamText stream) $"[{JsonDocument.four},{JsonDocument.five}]" "Incorrect documents were returned"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds when sorting case-sensitively" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeByFieldsOrdered
|
||||
SqliteDb.TableName writer All [ Field.LessOrEqual "NumValue" 10 ] [ Field.Named "Value" ]
|
||||
Expect.equal
|
||||
(streamText stream)
|
||||
$"[{JsonDocument.three},{JsonDocument.one},{JsonDocument.two}]"
|
||||
"Documents not ordered correctly"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds when sorting case-insensitively" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeByFieldsOrdered
|
||||
SqliteDb.TableName writer All [ Field.LessOrEqual "NumValue" 10 ] [ Field.Named "i:Value" ]
|
||||
Expect.equal
|
||||
(streamText stream)
|
||||
$"[{JsonDocument.three},{JsonDocument.two},{JsonDocument.one}]"
|
||||
"Documents not ordered correctly"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
]
|
||||
testList "writeFirstByFields" [
|
||||
testTask "succeeds when a document is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Value" "another" ]
|
||||
Expect.equal (streamText stream) JsonDocument.two "The incorrect document was returned"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds when multiple documents are found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ]
|
||||
let json = streamText stream
|
||||
Expect.notEqual json "{}" "There should have been a document returned"
|
||||
verifyAny json [ JsonDocument.two; JsonDocument.four ]
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds when a document is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Value" "absent" ]
|
||||
verifyNoDoc (streamText stream)
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
]
|
||||
testList "writeFirstByFieldsOrdered" [
|
||||
testTask "succeeds when sorting ascending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeFirstByFieldsOrdered
|
||||
SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar" ]
|
||||
Expect.equal (streamText stream) JsonDocument.two "An incorrect document was returned"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
testTask "succeeds when sorting descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
let writer = writeStream stream
|
||||
try
|
||||
do! Json.writeFirstByFieldsOrdered
|
||||
SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar DESC" ]
|
||||
Expect.equal (streamText stream) JsonDocument.four "An incorrect document was returned"
|
||||
finally
|
||||
writer.Complete()
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
/// Integration tests for the Update module of the SQLite library
|
||||
let updateTests = testList "Update" [
|
||||
testList "byId" [
|
||||
@@ -682,7 +1309,7 @@ let updateTests = testList "Update" [
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
do! Update.byFunc SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
||||
do! Update.byFunc SqliteDb.TableName _.Id { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
||||
let! after = Find.byId<string, JsonDocument> SqliteDb.TableName "one"
|
||||
Expect.isSome after "There should have been a document returned post-update"
|
||||
Expect.equal
|
||||
@@ -697,7 +1324,7 @@ let updateTests = testList "Update" [
|
||||
Expect.isEmpty before "There should have been no documents returned"
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! Update.byFunc SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
||||
do! Update.byFunc SqliteDb.TableName _.Id { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
||||
}
|
||||
]
|
||||
]
|
||||
@@ -854,6 +1481,7 @@ let all = testList "Sqlite" [
|
||||
countTests
|
||||
existsTests
|
||||
findTests
|
||||
jsonTests
|
||||
updateTests
|
||||
patchTests
|
||||
removeFieldsTests
|
||||
|
||||
Reference in New Issue
Block a user