Add F# SQLite Json tests
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
module SqliteExtensionTests
|
||||
|
||||
open System.IO
|
||||
open System.Text.Json
|
||||
open BitBadger.Documents
|
||||
open BitBadger.Documents.Sqlite
|
||||
@@ -10,10 +11,44 @@ open Types
|
||||
|
||||
/// Integration tests for the F# extensions on the SqliteConnection data type
|
||||
let integrationTests =
|
||||
let loadDocs () = backgroundTask {
|
||||
for doc in testDocuments do do! insert SqliteDb.TableName doc
|
||||
let loadDocs (conn: SqliteConnection) = backgroundTask {
|
||||
for doc in testDocuments do do! conn.insert SqliteDb.TableName doc
|
||||
}
|
||||
testList "Sqlite.Extensions" [
|
||||
|
||||
/// Set up a stream writer for a test
|
||||
let writeStream (stream: Stream) =
|
||||
let writer = new StreamWriter(stream)
|
||||
writer.AutoFlush <- true
|
||||
writer
|
||||
|
||||
/// Get the text of the given stream
|
||||
let streamText (stream: Stream) =
|
||||
stream.Position <- 0L
|
||||
use reader = new StreamReader(stream)
|
||||
reader.ReadToEnd()
|
||||
|
||||
/// Verify a JSON array begins with "[" and ends with "]"
|
||||
let 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 verifyEmpty json =
|
||||
Expect.equal json "[]" "There should be no documents returned"
|
||||
|
||||
/// Verify an empty JSON document
|
||||
let 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 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}"
|
||||
|
||||
ftestList "Sqlite.Extensions" [
|
||||
testTask "ensureTable succeeds" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
@@ -108,7 +143,7 @@ let integrationTests =
|
||||
testTask "countAll succeeds" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! theCount = conn.countAll SqliteDb.TableName
|
||||
Expect.equal theCount 5L "There should have been 5 matching documents"
|
||||
@@ -116,7 +151,7 @@ let integrationTests =
|
||||
testTask "countByFields succeeds" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! theCount = conn.countByFields SqliteDb.TableName Any [ Field.Equal "Value" "purple" ]
|
||||
Expect.equal theCount 2L "There should have been 2 matching documents"
|
||||
@@ -125,7 +160,7 @@ let integrationTests =
|
||||
testTask "succeeds when a document exists" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! exists = conn.existsById SqliteDb.TableName "three"
|
||||
Expect.isTrue exists "There should have been an existing document"
|
||||
@@ -133,7 +168,7 @@ let integrationTests =
|
||||
testTask "succeeds when a document does not exist" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! exists = conn.existsById SqliteDb.TableName "seven"
|
||||
Expect.isFalse exists "There should not have been an existing document"
|
||||
@@ -143,7 +178,7 @@ let integrationTests =
|
||||
testTask "succeeds when documents exist" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! exists = conn.existsByFields SqliteDb.TableName Any [ Field.Equal "NumValue" 10 ]
|
||||
Expect.isTrue exists "There should have been existing documents"
|
||||
@@ -151,7 +186,7 @@ let integrationTests =
|
||||
testTask "succeeds when no matching documents exist" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! exists = conn.existsByFields SqliteDb.TableName Any [ Field.Equal "Nothing" "none" ]
|
||||
Expect.isFalse exists "There should not have been any existing documents"
|
||||
@@ -185,7 +220,7 @@ let integrationTests =
|
||||
testTask "succeeds when ordering numerically" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! results = conn.findAllOrdered<JsonDocument> SqliteDb.TableName [ Field.Named "n:NumValue" ]
|
||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
||||
@@ -197,7 +232,7 @@ let integrationTests =
|
||||
testTask "succeeds when ordering numerically descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! results = conn.findAllOrdered<JsonDocument> SqliteDb.TableName [ Field.Named "n:NumValue DESC" ]
|
||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
||||
@@ -209,7 +244,7 @@ let integrationTests =
|
||||
testTask "succeeds when ordering alphabetically" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! results = conn.findAllOrdered<JsonDocument> SqliteDb.TableName [ Field.Named "Id DESC" ]
|
||||
Expect.hasLength results 5 "There should have been 5 documents returned"
|
||||
@@ -223,7 +258,7 @@ let integrationTests =
|
||||
testTask "succeeds when a document is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! doc = conn.findById<string, JsonDocument> SqliteDb.TableName "two"
|
||||
Expect.isSome doc "There should have been a document returned"
|
||||
@@ -232,7 +267,7 @@ let integrationTests =
|
||||
testTask "succeeds when a document is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! doc = conn.findById<string, JsonDocument> SqliteDb.TableName "three hundred eighty-seven"
|
||||
Expect.isNone doc "There should not have been a document returned"
|
||||
@@ -242,7 +277,7 @@ let integrationTests =
|
||||
testTask "succeeds when documents are found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! docs = conn.findByFields<JsonDocument> SqliteDb.TableName Any [ Field.Equal "Sub.Foo" "green" ]
|
||||
Expect.hasLength docs 2 "There should have been two documents returned"
|
||||
@@ -250,7 +285,7 @@ let integrationTests =
|
||||
testTask "succeeds when documents are not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! docs = conn.findByFields<JsonDocument> SqliteDb.TableName Any [ Field.Equal "Value" "mauve" ]
|
||||
Expect.isEmpty docs "There should have been no documents returned"
|
||||
@@ -260,7 +295,7 @@ let integrationTests =
|
||||
testTask "succeeds when sorting ascending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! docs =
|
||||
conn.findByFieldsOrdered<JsonDocument>
|
||||
@@ -271,7 +306,7 @@ let integrationTests =
|
||||
testTask "succeeds when sorting descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! docs =
|
||||
conn.findByFieldsOrdered<JsonDocument>
|
||||
@@ -284,7 +319,7 @@ let integrationTests =
|
||||
testTask "succeeds when a document is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! doc = conn.findFirstByFields<JsonDocument> SqliteDb.TableName Any [ Field.Equal "Value" "another" ]
|
||||
Expect.isSome doc "There should have been a document returned"
|
||||
@@ -293,7 +328,7 @@ let integrationTests =
|
||||
testTask "succeeds when multiple documents are found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! doc = conn.findFirstByFields<JsonDocument> SqliteDb.TableName Any [ Field.Equal "Sub.Foo" "green" ]
|
||||
Expect.isSome doc "There should have been a document returned"
|
||||
@@ -302,7 +337,7 @@ let integrationTests =
|
||||
testTask "succeeds when a document is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! doc = conn.findFirstByFields<JsonDocument> SqliteDb.TableName Any [ Field.Equal "Value" "absent" ]
|
||||
Expect.isNone doc "There should not have been a document returned"
|
||||
@@ -312,7 +347,7 @@ let integrationTests =
|
||||
testTask "succeeds when sorting ascending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! doc =
|
||||
conn.findFirstByFieldsOrdered<JsonDocument>
|
||||
@@ -323,7 +358,7 @@ let integrationTests =
|
||||
testTask "succeeds when sorting descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! doc =
|
||||
conn.findFirstByFieldsOrdered<JsonDocument>
|
||||
@@ -332,11 +367,500 @@ let integrationTests =
|
||||
Expect.equal "four" doc.Value.Id "An incorrect document was returned"
|
||||
}
|
||||
]
|
||||
testList "jsonAll" [
|
||||
testTask "succeeds when there is data" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
|
||||
do! conn.insert SqliteDb.TableName { Foo = "one"; Bar = "two" }
|
||||
do! conn.insert SqliteDb.TableName { Foo = "three"; Bar = "four" }
|
||||
do! conn.insert SqliteDb.TableName { Foo = "five"; Bar = "six" }
|
||||
|
||||
let! json = conn.jsonAll 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()
|
||||
use conn = Configuration.dbConn ()
|
||||
let! json = conn.jsonAll SqliteDb.TableName
|
||||
verifyEmpty json
|
||||
}
|
||||
]
|
||||
testList "jsonAllOrdered" [
|
||||
testTask "succeeds when ordering numerically" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json = conn.jsonAllOrdered 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()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json = conn.jsonAllOrdered 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()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json = conn.jsonAllOrdered 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 "jsonById" [
|
||||
testTask "succeeds when a document is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json = conn.jsonById 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()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json = conn.jsonById SqliteDb.TableName "three hundred eighty-seven"
|
||||
verifyNoDoc json
|
||||
}
|
||||
]
|
||||
testList "jsonByFields" [
|
||||
testTask "succeeds when documents are found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json = conn.jsonByFields 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()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json = conn.jsonByFields 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()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json = conn.jsonByFields SqliteDb.TableName Any [ Field.Greater "NumValue" 100 ]
|
||||
verifyEmpty json
|
||||
}
|
||||
testTask "succeeds for InArray when matching documents exist" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! conn.ensureTable SqliteDb.TableName
|
||||
for doc in ArrayDocument.TestDocuments do do! conn.insert SqliteDb.TableName doc
|
||||
|
||||
let! json =
|
||||
conn.jsonByFields 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()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! conn.ensureTable SqliteDb.TableName
|
||||
for doc in ArrayDocument.TestDocuments do do! conn.insert SqliteDb.TableName doc
|
||||
|
||||
let! json =
|
||||
conn.jsonByFields SqliteDb.TableName All [ Field.InArray "Values" SqliteDb.TableName [ "j" ] ]
|
||||
verifyEmpty json
|
||||
}
|
||||
]
|
||||
testList "jsonByFieldsOrdered" [
|
||||
testTask "succeeds when sorting ascending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json =
|
||||
conn.jsonByFieldsOrdered 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()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json =
|
||||
conn.jsonByFieldsOrdered
|
||||
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()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json =
|
||||
conn.jsonByFieldsOrdered
|
||||
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()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json =
|
||||
conn.jsonByFieldsOrdered
|
||||
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 "jsonFirstByFields" [
|
||||
testTask "succeeds when a document is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json = conn.jsonFirstByFields 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()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json = conn.jsonFirstByFields 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()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json = conn.jsonFirstByFields SqliteDb.TableName Any [ Field.Equal "Value" "absent" ]
|
||||
verifyNoDoc json
|
||||
}
|
||||
]
|
||||
testList "jsonFirstByFieldsOrdered" [
|
||||
testTask "succeeds when sorting ascending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json =
|
||||
conn.jsonFirstByFieldsOrdered
|
||||
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()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json =
|
||||
conn.jsonFirstByFieldsOrdered
|
||||
SqliteDb.TableName Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar DESC" ]
|
||||
Expect.equal json JsonDocument.four "An incorrect document was returned"
|
||||
}
|
||||
]
|
||||
testList "writeJsonAll" [
|
||||
testTask "succeeds when there is data" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
|
||||
do! conn.insert SqliteDb.TableName { Foo = "one"; Bar = "two" }
|
||||
do! conn.insert SqliteDb.TableName { Foo = "three"; Bar = "four" }
|
||||
do! conn.insert SqliteDb.TableName { Foo = "five"; Bar = "six" }
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonAll 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"
|
||||
}
|
||||
testTask "succeeds when there is no data" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonAll SqliteDb.TableName writer
|
||||
verifyEmpty (streamText stream)
|
||||
}
|
||||
]
|
||||
testList "writeJsonAllOrdered" [
|
||||
testTask "succeeds when ordering numerically" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonAllOrdered 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"
|
||||
}
|
||||
testTask "succeeds when ordering numerically descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonAllOrdered 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"
|
||||
}
|
||||
testTask "succeeds when ordering alphabetically" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonAllOrdered 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"
|
||||
}
|
||||
]
|
||||
testList "writeJsonById" [
|
||||
testTask "succeeds when a document is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonById SqliteDb.TableName writer "two"
|
||||
Expect.equal (streamText stream) JsonDocument.two "The incorrect document was returned"
|
||||
}
|
||||
testTask "succeeds when a document is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonById SqliteDb.TableName writer "three hundred eighty-seven"
|
||||
verifyNoDoc (streamText stream)
|
||||
}
|
||||
]
|
||||
testList "writeJsonByFields" [
|
||||
testTask "succeeds when documents are found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonByFields 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"
|
||||
}
|
||||
testTask "succeeds when documents are found using IN with numeric field" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonByFields SqliteDb.TableName writer All [ Field.In "NumValue" [ 2; 4; 6; 8 ] ]
|
||||
Expect.equal (streamText stream) $"[{JsonDocument.three}]" "There should have been one document returned"
|
||||
}
|
||||
testTask "succeeds when documents are not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonByFields SqliteDb.TableName writer Any [ Field.Greater "NumValue" 100 ]
|
||||
verifyEmpty (streamText stream)
|
||||
}
|
||||
testTask "succeeds for InArray when matching documents exist" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! conn.ensureTable SqliteDb.TableName
|
||||
for doc in ArrayDocument.TestDocuments do do! conn.insert SqliteDb.TableName doc
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonByFields
|
||||
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"
|
||||
}
|
||||
testTask "succeeds for InArray when no matching documents exist" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! conn.ensureTable SqliteDb.TableName
|
||||
for doc in ArrayDocument.TestDocuments do do! conn.insert SqliteDb.TableName doc
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonByFields
|
||||
SqliteDb.TableName writer All [ Field.InArray "Values" SqliteDb.TableName [ "j" ] ]
|
||||
verifyEmpty (streamText stream)
|
||||
}
|
||||
]
|
||||
testList "writeJsonByFieldsOrdered" [
|
||||
testTask "succeeds when sorting ascending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonByFieldsOrdered
|
||||
SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id" ]
|
||||
Expect.equal
|
||||
(streamText stream) $"[{JsonDocument.five},{JsonDocument.four}]" "Incorrect documents were returned"
|
||||
}
|
||||
testTask "succeeds when sorting descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonByFieldsOrdered
|
||||
SqliteDb.TableName writer Any [ Field.Greater "NumValue" 15 ] [ Field.Named "Id DESC" ]
|
||||
Expect.equal
|
||||
(streamText stream) $"[{JsonDocument.four},{JsonDocument.five}]" "Incorrect documents were returned"
|
||||
}
|
||||
testTask "succeeds when sorting case-sensitively" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonByFieldsOrdered
|
||||
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"
|
||||
}
|
||||
testTask "succeeds when sorting case-insensitively" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonByFieldsOrdered
|
||||
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"
|
||||
}
|
||||
]
|
||||
testList "writeJsonFirstByFields" [
|
||||
testTask "succeeds when a document is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Value" "another" ]
|
||||
Expect.equal (streamText stream) JsonDocument.two "The incorrect document was returned"
|
||||
}
|
||||
testTask "succeeds when multiple documents are found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonFirstByFields 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 ]
|
||||
}
|
||||
testTask "succeeds when a document is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Value" "absent" ]
|
||||
verifyNoDoc (streamText stream)
|
||||
}
|
||||
]
|
||||
testList "writeJsonFirstByFieldsOrdered" [
|
||||
testTask "succeeds when sorting ascending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonFirstByFieldsOrdered
|
||||
SqliteDb.TableName writer Any [ Field.Equal "Sub.Foo" "green" ] [ Field.Named "Sub.Bar" ]
|
||||
Expect.equal (streamText stream) JsonDocument.two "An incorrect document was returned"
|
||||
}
|
||||
testTask "succeeds when sorting descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeJsonFirstByFieldsOrdered
|
||||
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"
|
||||
}
|
||||
]
|
||||
testList "updateById" [
|
||||
testTask "succeeds when a document is updated" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let testDoc = { emptyDoc with Id = "one"; Sub = Some { Foo = "blue"; Bar = "red" } }
|
||||
do! conn.updateById SqliteDb.TableName "one" testDoc
|
||||
@@ -363,10 +887,10 @@ let integrationTests =
|
||||
testTask "succeeds when a document is updated" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
do! conn.updateByFunc
|
||||
SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
||||
SqliteDb.TableName _.Id { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
||||
let! after = conn.findById<string, JsonDocument> SqliteDb.TableName "one"
|
||||
if Option.isNone after then
|
||||
Expect.isTrue false "There should have been a document returned post-update"
|
||||
@@ -384,14 +908,14 @@ let integrationTests =
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! conn.updateByFunc
|
||||
SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
||||
SqliteDb.TableName _.Id { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
|
||||
}
|
||||
]
|
||||
testList "patchById" [
|
||||
testTask "succeeds when a document is updated" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
do! conn.patchById SqliteDb.TableName "one" {| NumValue = 44 |}
|
||||
let! after = conn.findById<string, JsonDocument> SqliteDb.TableName "one"
|
||||
@@ -414,7 +938,7 @@ let integrationTests =
|
||||
testTask "succeeds when a document is updated" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
do! conn.patchByFields SqliteDb.TableName Any [ Field.Equal "Value" "purple" ] {| NumValue = 77 |}
|
||||
let! after = conn.countByFields SqliteDb.TableName Any [ Field.Equal "NumValue" 77 ]
|
||||
@@ -435,7 +959,7 @@ let integrationTests =
|
||||
testTask "succeeds when fields are removed" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
do! conn.removeFieldsById SqliteDb.TableName "two" [ "Sub"; "Value" ]
|
||||
try
|
||||
@@ -448,7 +972,7 @@ let integrationTests =
|
||||
testTask "succeeds when a field is not removed" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! conn.removeFieldsById SqliteDb.TableName "two" [ "AFieldThatIsNotThere" ]
|
||||
@@ -465,7 +989,7 @@ let integrationTests =
|
||||
testTask "succeeds when a field is removed" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
do! conn.removeFieldsByFields SqliteDb.TableName Any [ Field.Equal "NumValue" 17 ] [ "Sub" ]
|
||||
try
|
||||
@@ -478,7 +1002,7 @@ let integrationTests =
|
||||
testTask "succeeds when a field is not removed" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
// This not raising an exception is the test
|
||||
do! conn.removeFieldsByFields SqliteDb.TableName Any [ Field.Equal "NumValue" 17 ] [ "Nothing" ]
|
||||
@@ -496,7 +1020,7 @@ let integrationTests =
|
||||
testTask "succeeds when a document is deleted" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
do! conn.deleteById SqliteDb.TableName "four"
|
||||
let! remaining = conn.countAll SqliteDb.TableName
|
||||
@@ -505,7 +1029,7 @@ let integrationTests =
|
||||
testTask "succeeds when a document is not deleted" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
do! conn.deleteById SqliteDb.TableName "thirty"
|
||||
let! remaining = conn.countAll SqliteDb.TableName
|
||||
@@ -516,7 +1040,7 @@ let integrationTests =
|
||||
testTask "succeeds when documents are deleted" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
do! conn.deleteByFields SqliteDb.TableName Any [ Field.NotEqual "Value" "purple" ]
|
||||
let! remaining = conn.countAll SqliteDb.TableName
|
||||
@@ -525,18 +1049,103 @@ let integrationTests =
|
||||
testTask "succeeds when documents are not deleted" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
do! conn.deleteByFields SqliteDb.TableName Any [ Field.Equal "Value" "crimson" ]
|
||||
let! remaining = conn.countAll SqliteDb.TableName
|
||||
Expect.equal remaining 5L "There should have been 5 documents remaining"
|
||||
}
|
||||
]
|
||||
testList "customList" [
|
||||
testTask "succeeds when data is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! docs = conn.customList (Query.find SqliteDb.TableName) [] fromData<JsonDocument>
|
||||
Expect.hasLength docs 5 "There should have been 5 documents returned"
|
||||
}
|
||||
testTask "succeeds when data is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! docs =
|
||||
conn.customList
|
||||
$"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 "customJsonArray" [
|
||||
testTask "succeeds when data is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! json = conn.customJsonArray (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()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! docs =
|
||||
conn.customJsonArray
|
||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value"
|
||||
[ SqliteParameter("@value", 100) ]
|
||||
jsonFromData
|
||||
Expect.equal docs "[]" "There should have been no documents returned"
|
||||
}
|
||||
]
|
||||
testList "writeCustomJsonArray" [
|
||||
testTask "succeeds when data is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeCustomJsonArray (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 `[`"
|
||||
}
|
||||
testTask "succeeds when data is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs conn
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! conn.writeCustomJsonArray
|
||||
$"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"
|
||||
}
|
||||
]
|
||||
testList "customSingle" [
|
||||
testTask "succeeds when a row is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! doc =
|
||||
conn.customSingle
|
||||
@@ -549,7 +1158,7 @@ let integrationTests =
|
||||
testTask "succeeds when a row is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
let! doc =
|
||||
conn.customSingle
|
||||
@@ -559,33 +1168,37 @@ let integrationTests =
|
||||
Expect.isNone doc "There should not have been a document returned"
|
||||
}
|
||||
]
|
||||
testList "customList" [
|
||||
testTask "succeeds when data is found" {
|
||||
testList "customJsonSingle" [
|
||||
testTask "succeeds when a row is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
|
||||
let! docs = conn.customList (Query.find SqliteDb.TableName) [] fromData<JsonDocument>
|
||||
Expect.hasLength docs 5 "There should have been 5 documents returned"
|
||||
do! loadDocs conn
|
||||
|
||||
let! json =
|
||||
conn.customJsonSingle
|
||||
$"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()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
|
||||
let! docs =
|
||||
conn.customList
|
||||
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value"
|
||||
[ SqliteParameter("@value", 100) ]
|
||||
fromData<JsonDocument>
|
||||
Expect.isEmpty docs "There should have been no documents returned"
|
||||
do! loadDocs conn
|
||||
|
||||
let! json =
|
||||
conn.customJsonSingle
|
||||
$"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 "customNonQuery" [
|
||||
testTask "succeeds when operating on data" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
do! conn.customNonQuery $"DELETE FROM {SqliteDb.TableName}" []
|
||||
|
||||
@@ -595,7 +1208,7 @@ let integrationTests =
|
||||
testTask "succeeds when no data matches where clause" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use conn = Configuration.dbConn ()
|
||||
do! loadDocs ()
|
||||
do! loadDocs conn
|
||||
|
||||
do! conn.customNonQuery
|
||||
$"DELETE FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
module SqliteTests
|
||||
|
||||
open System.IO
|
||||
open System.Text.Json
|
||||
open BitBadger.Documents
|
||||
open BitBadger.Documents.Sqlite
|
||||
@@ -135,6 +136,19 @@ 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) =
|
||||
let writer = new StreamWriter(stream)
|
||||
writer.AutoFlush <- true
|
||||
writer
|
||||
|
||||
/// 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 +165,85 @@ 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()
|
||||
use writer = writeStream stream
|
||||
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 `[`"
|
||||
}
|
||||
testTask "succeeds when data is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
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"
|
||||
}
|
||||
]
|
||||
testList "single" [
|
||||
testTask "succeeds when a row is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
@@ -176,24 +269,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 +750,467 @@ 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()
|
||||
use writer = writeStream stream
|
||||
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"
|
||||
}
|
||||
testTask "succeeds when there is no data" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! Json.writeAll SqliteDb.TableName writer
|
||||
verifyEmpty (streamText stream)
|
||||
}
|
||||
]
|
||||
testList "writeAllOrdered" [
|
||||
testTask "succeeds when ordering numerically" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
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"
|
||||
}
|
||||
testTask "succeeds when ordering numerically descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
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"
|
||||
}
|
||||
testTask "succeeds when ordering alphabetically" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
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"
|
||||
}
|
||||
]
|
||||
testList "writeById" [
|
||||
testTask "succeeds when a document is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! Json.writeById SqliteDb.TableName writer "two"
|
||||
Expect.equal (streamText stream) JsonDocument.two "The incorrect document was returned"
|
||||
}
|
||||
testTask "succeeds when a document is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! Json.writeById SqliteDb.TableName writer "three hundred eighty-seven"
|
||||
verifyNoDoc (streamText stream)
|
||||
}
|
||||
]
|
||||
testList "writeByFields" [
|
||||
testTask "succeeds when documents are found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
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"
|
||||
}
|
||||
testTask "succeeds when documents are found using IN with numeric field" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
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"
|
||||
}
|
||||
testTask "succeeds when documents are not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! Json.writeByFields SqliteDb.TableName writer Any [ Field.Greater "NumValue" 100 ]
|
||||
verifyEmpty (streamText stream)
|
||||
}
|
||||
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()
|
||||
use writer = writeStream stream
|
||||
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"
|
||||
}
|
||||
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()
|
||||
use writer = writeStream stream
|
||||
do! Json.writeByFields SqliteDb.TableName writer All [ Field.InArray "Values" SqliteDb.TableName [ "j" ] ]
|
||||
verifyEmpty (streamText stream)
|
||||
}
|
||||
]
|
||||
testList "writeByFieldsOrdered" [
|
||||
testTask "succeeds when sorting ascending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
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"
|
||||
}
|
||||
testTask "succeeds when sorting descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
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"
|
||||
}
|
||||
testTask "succeeds when sorting case-sensitively" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
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"
|
||||
}
|
||||
testTask "succeeds when sorting case-insensitively" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
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"
|
||||
}
|
||||
]
|
||||
testList "writeFirstByFields" [
|
||||
testTask "succeeds when a document is found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! Json.writeFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Value" "another" ]
|
||||
Expect.equal (streamText stream) JsonDocument.two "The incorrect document was returned"
|
||||
}
|
||||
testTask "succeeds when multiple documents are found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
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 ]
|
||||
}
|
||||
testTask "succeeds when a document is not found" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
do! Json.writeFirstByFields SqliteDb.TableName writer Any [ Field.Equal "Value" "absent" ]
|
||||
verifyNoDoc (streamText stream)
|
||||
}
|
||||
]
|
||||
testList "writeFirstByFieldsOrdered" [
|
||||
testTask "succeeds when sorting ascending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
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"
|
||||
}
|
||||
testTask "succeeds when sorting descending" {
|
||||
use! db = SqliteDb.BuildDb()
|
||||
do! loadDocs ()
|
||||
|
||||
use stream = new MemoryStream()
|
||||
use writer = writeStream stream
|
||||
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"
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
/// Integration tests for the Update module of the SQLite library
|
||||
let updateTests = testList "Update" [
|
||||
testList "byId" [
|
||||
@@ -682,7 +1240,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 +1255,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 }
|
||||
}
|
||||
]
|
||||
]
|
||||
@@ -844,7 +1402,7 @@ let deleteTests = testList "Delete" [
|
||||
]
|
||||
|
||||
/// All tests for the SQLite library
|
||||
let all = testList "Sqlite" [
|
||||
let all = ftestList "Sqlite" [
|
||||
testList "Unit" [ queryTests; parametersTests ]
|
||||
testSequenced <| testList "Integration" [
|
||||
configurationTests
|
||||
@@ -854,6 +1412,7 @@ let all = testList "Sqlite" [
|
||||
countTests
|
||||
existsTests
|
||||
findTests
|
||||
jsonTests
|
||||
updateTests
|
||||
patchTests
|
||||
removeFieldsTests
|
||||
|
||||
@@ -26,6 +26,22 @@ type JsonDocument =
|
||||
NumValue: int
|
||||
Sub: SubDocument option }
|
||||
|
||||
module JsonDocument =
|
||||
/// The JSON for document ID `one`
|
||||
let one = """{"Id":"one","Value":"FIRST!","NumValue":0,"Sub":null}"""
|
||||
|
||||
/// The JSON for document ID `two`
|
||||
let two = """{"Id":"two","Value":"another","NumValue":10,"Sub":{"Foo":"green","Bar":"blue"}}"""
|
||||
|
||||
/// The JSON for document ID `three`
|
||||
let three = """{"Id":"three","Value":"","NumValue":4,"Sub":null}"""
|
||||
|
||||
/// The JSON for document ID `four`
|
||||
let four = """{"Id":"four","Value":"purple","NumValue":17,"Sub":{"Foo":"green","Bar":"red"}}"""
|
||||
|
||||
/// The JSON for document ID `five`
|
||||
let five = """{"Id":"five","Value":"purple","NumValue":18,"Sub":null}"""
|
||||
|
||||
|
||||
/// An empty JsonDocument
|
||||
let emptyDoc = { Id = ""; Value = ""; NumValue = 0; Sub = None }
|
||||
|
||||
Reference in New Issue
Block a user