Add C# SQLite Json tests

This commit is contained in:
2025-04-07 21:37:11 -04:00
parent 812ef06af5
commit 7a18ec53e5
5 changed files with 1394 additions and 122 deletions

View File

@@ -14,7 +14,7 @@ let integrationTests =
let loadDocs (conn: SqliteConnection) = backgroundTask {
for doc in testDocuments do do! conn.insert SqliteDb.TableName doc
}
/// Set up a stream writer for a test
let writeStream (stream: Stream) =
let writer = new StreamWriter(stream)
@@ -48,7 +48,7 @@ let integrationTests =
let theDocs = docs |> String.concat " | "
Expect.isTrue false $"Could not find any of |{theDocs}| in {json}"
ftestList "Sqlite.Extensions" [
testList "Sqlite.Extensions" [
testTask "ensureTable succeeds" {
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
@@ -57,12 +57,12 @@ let integrationTests =
$"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = @name) AS it"
[ SqliteParameter("@name", name) ]
toExists
let! exists = itExists "ensured"
let! alsoExists = itExists "idx_ensured_key"
Expect.isFalse exists "The table should not exist already"
Expect.isFalse alsoExists "The key index should not exist already"
do! conn.ensureTable "ensured"
let! exists' = itExists "ensured"
let! alsoExists' = itExists "idx_ensured_key"
@@ -77,10 +77,10 @@ let integrationTests =
$"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = 'idx_ensured_test') AS it"
[]
toExists
let! exists = indexExists ()
Expect.isFalse exists "The index should not exist already"
do! conn.ensureTable "ensured"
do! conn.ensureFieldIndex "ensured" "test" [ "Name"; "Age" ]
let! exists' = indexExists ()
@@ -92,7 +92,7 @@ let integrationTests =
use conn = Configuration.dbConn ()
let! before = conn.findAll<SubDocument> SqliteDb.TableName
Expect.equal before [] "There should be no documents in the table"
let testDoc = { emptyDoc with Id = "turkey"; Sub = Some { Foo = "gobble"; Bar = "gobble" } }
do! conn.insert SqliteDb.TableName testDoc
let! after = conn.findAll<JsonDocument> SqliteDb.TableName
@@ -116,7 +116,7 @@ let integrationTests =
use conn = Configuration.dbConn ()
let! before = conn.findAll<JsonDocument> SqliteDb.TableName
Expect.equal before [] "There should be no documents in the table"
let testDoc = { emptyDoc with Id = "test"; Sub = Some { Foo = "a"; Bar = "b" } }
do! conn.save SqliteDb.TableName testDoc
let! after = conn.findAll<JsonDocument> SqliteDb.TableName
@@ -127,11 +127,11 @@ let integrationTests =
use conn = Configuration.dbConn ()
let testDoc = { emptyDoc with Id = "test"; Sub = Some { Foo = "a"; Bar = "b" } }
do! conn.insert SqliteDb.TableName testDoc
let! before = conn.findById<string, JsonDocument> SqliteDb.TableName "test"
if Option.isNone before then Expect.isTrue false "There should have been a document returned"
Expect.equal before.Value testDoc "The document is not correct"
let upd8Doc = { testDoc with Sub = Some { Foo = "c"; Bar = "d" } }
do! conn.save SqliteDb.TableName upd8Doc
let! after = conn.findById<string, JsonDocument> SqliteDb.TableName "test"
@@ -144,7 +144,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
let! theCount = conn.countAll SqliteDb.TableName
Expect.equal theCount 5L "There should have been 5 matching documents"
}
@@ -152,7 +152,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
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"
}
@@ -161,7 +161,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
let! exists = conn.existsById SqliteDb.TableName "three"
Expect.isTrue exists "There should have been an existing document"
}
@@ -169,7 +169,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
let! exists = conn.existsById SqliteDb.TableName "seven"
Expect.isFalse exists "There should not have been an existing document"
}
@@ -179,7 +179,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
let! exists = conn.existsByFields SqliteDb.TableName Any [ Field.Equal "NumValue" 10 ]
Expect.isTrue exists "There should have been existing documents"
}
@@ -187,7 +187,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
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"
}
@@ -196,11 +196,11 @@ let integrationTests =
testTask "succeeds when there is data" {
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
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! results = conn.findAll<SubDocument> SqliteDb.TableName
let expected = [
{ Foo = "one"; Bar = "two" }
@@ -221,7 +221,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
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"
Expect.equal
@@ -233,7 +233,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
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"
Expect.equal
@@ -245,7 +245,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
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"
Expect.equal
@@ -259,7 +259,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
let! doc = conn.findById<string, JsonDocument> SqliteDb.TableName "two"
Expect.isSome doc "There should have been a document returned"
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
@@ -268,7 +268,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
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"
}
@@ -278,7 +278,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
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"
}
@@ -286,7 +286,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
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"
}
@@ -320,7 +320,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
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"
Expect.equal doc.Value.Id "two" "The incorrect document was returned"
@@ -329,7 +329,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
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"
Expect.contains [ "two"; "four" ] doc.Value.Id "An incorrect document was returned"
@@ -338,7 +338,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
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"
}
@@ -861,7 +861,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
let testDoc = { emptyDoc with Id = "one"; Sub = Some { Foo = "blue"; Bar = "red" } }
do! conn.updateById SqliteDb.TableName "one" testDoc
let! after = conn.findById<string, JsonDocument> SqliteDb.TableName "one"
@@ -872,10 +872,10 @@ let integrationTests =
testTask "succeeds when no document is updated" {
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
let! before = conn.findAll<JsonDocument> SqliteDb.TableName
Expect.isEmpty before "There should have been no documents returned"
// This not raising an exception is the test
do! conn.updateById
SqliteDb.TableName
@@ -888,7 +888,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
do! conn.updateByFunc
SqliteDb.TableName _.Id { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
let! after = conn.findById<string, JsonDocument> SqliteDb.TableName "one"
@@ -902,10 +902,10 @@ let integrationTests =
testTask "succeeds when no document is updated" {
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
let! before = conn.findAll<JsonDocument> SqliteDb.TableName
Expect.isEmpty before "There should have been no documents returned"
// This not raising an exception is the test
do! conn.updateByFunc
SqliteDb.TableName _.Id { Id = "one"; Value = "le un"; NumValue = 1; Sub = None }
@@ -916,7 +916,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
do! conn.patchById SqliteDb.TableName "one" {| NumValue = 44 |}
let! after = conn.findById<string, JsonDocument> SqliteDb.TableName "one"
if Option.isNone after then
@@ -926,10 +926,10 @@ let integrationTests =
testTask "succeeds when no document is updated" {
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
let! before = conn.findAll<SubDocument> SqliteDb.TableName
Expect.isEmpty before "There should have been no documents returned"
// This not raising an exception is the test
do! conn.patchById SqliteDb.TableName "test" {| Foo = "green" |}
}
@@ -939,7 +939,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
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 ]
Expect.equal after 2L "There should have been 2 documents returned"
@@ -947,10 +947,10 @@ let integrationTests =
testTask "succeeds when no document is updated" {
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
let! before = conn.findAll<SubDocument> SqliteDb.TableName
Expect.isEmpty before "There should have been no documents returned"
// This not raising an exception is the test
do! conn.patchByFields SqliteDb.TableName Any [ Field.Equal "Value" "burgundy" ] {| Foo = "green" |}
}
@@ -960,7 +960,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
do! conn.removeFieldsById SqliteDb.TableName "two" [ "Sub"; "Value" ]
try
let! _ = conn.findById<string, JsonDocument> SqliteDb.TableName "two"
@@ -973,14 +973,14 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
// This not raising an exception is the test
do! conn.removeFieldsById SqliteDb.TableName "two" [ "AFieldThatIsNotThere" ]
}
testTask "succeeds when no document is matched" {
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
// This not raising an exception is the test
do! conn.removeFieldsById SqliteDb.TableName "two" [ "Value" ]
}
@@ -990,7 +990,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
do! conn.removeFieldsByFields SqliteDb.TableName Any [ Field.Equal "NumValue" 17 ] [ "Sub" ]
try
let! _ = conn.findById<string, JsonDocument> SqliteDb.TableName "four"
@@ -1003,14 +1003,14 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
// This not raising an exception is the test
do! conn.removeFieldsByFields SqliteDb.TableName Any [ Field.Equal "NumValue" 17 ] [ "Nothing" ]
}
testTask "succeeds when no document is matched" {
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
// This not raising an exception is the test
do! conn.removeFieldsByFields
SqliteDb.TableName Any [ Field.NotEqual "Abracadabra" "apple" ] [ "Value" ]
@@ -1021,7 +1021,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
do! conn.deleteById SqliteDb.TableName "four"
let! remaining = conn.countAll SqliteDb.TableName
Expect.equal remaining 4L "There should have been 4 documents remaining"
@@ -1030,7 +1030,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
do! conn.deleteById SqliteDb.TableName "thirty"
let! remaining = conn.countAll SqliteDb.TableName
Expect.equal remaining 5L "There should have been 5 documents remaining"
@@ -1041,7 +1041,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
do! conn.deleteByFields SqliteDb.TableName Any [ Field.NotEqual "Value" "purple" ]
let! remaining = conn.countAll SqliteDb.TableName
Expect.equal remaining 2L "There should have been 2 documents remaining"
@@ -1050,7 +1050,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
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"
@@ -1061,7 +1061,7 @@ let integrationTests =
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"
}
@@ -1069,7 +1069,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
let! docs =
conn.customList
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value"
@@ -1146,7 +1146,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
let! doc =
conn.customSingle
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id"
@@ -1159,7 +1159,7 @@ let integrationTests =
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
do! loadDocs conn
let! doc =
conn.customSingle
$"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id"
@@ -1221,7 +1221,7 @@ let integrationTests =
testTask "customScalar succeeds" {
use! db = SqliteDb.BuildDb()
use conn = Configuration.dbConn ()
let! nbr = conn.customScalar "SELECT 5 AS test_value" [] _.GetInt32(0)
Expect.equal nbr 5 "The query should have returned the number 5"
}

View File

@@ -1402,7 +1402,7 @@ let deleteTests = testList "Delete" [
]
/// All tests for the SQLite library
let all = ftestList "Sqlite" [
let all = testList "Sqlite" [
testList "Unit" [ queryTests; parametersTests ]
testSequenced <| testList "Integration" [
configurationTests