From 6ce619ac606794cecad51e6d62508b5bfe6e5eb9 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Mon, 25 Dec 2023 15:24:23 -0500 Subject: [PATCH] WIP on test integration --- src/Tests.CSharp/SqliteDb.cs | 4 +- src/Tests/SqliteTests.fs | 479 +++++++++++++++++------------------ 2 files changed, 238 insertions(+), 245 deletions(-) diff --git a/src/Tests.CSharp/SqliteDb.cs b/src/Tests.CSharp/SqliteDb.cs index a34adc0..876ab46 100644 --- a/src/Tests.CSharp/SqliteDb.cs +++ b/src/Tests.CSharp/SqliteDb.cs @@ -39,12 +39,12 @@ public static class SqliteDb /// /// The table name for the catalog metadata /// - public static readonly string Catalog = "sqlite_master"; + public const string Catalog = "sqlite_master"; /// /// The name of the table used for testing /// - public static readonly string TableName = "test_table"; + public const string TableName = "test_table"; /// /// Create a throwaway database file with the test_table defined diff --git a/src/Tests/SqliteTests.fs b/src/Tests/SqliteTests.fs index 75644ed..56d925a 100644 --- a/src/Tests/SqliteTests.fs +++ b/src/Tests/SqliteTests.fs @@ -12,53 +12,44 @@ type JsonDocument = let emptyDoc = { Id = ""; Value = ""; NumValue = 0; Sub = None } - -open BitBadger.Documents.Sqlite - -/// Database access for these tests -module Db = - - open System - open System.IO - open System.Threading.Tasks - - /// The table name for the catalog metadata - let catalog = "sqlite_master" - - /// The name of the table used for testing - let tableName = "test_table" - - /// A throwaway SQLite database file, which will be deleted when it goes out of scope - type ThrowawaySqliteDb(dbName: string) = - let deleteMe () = - if File.Exists dbName then File.Delete dbName - interface IDisposable with - member _.Dispose() = - deleteMe () - interface IAsyncDisposable with - member _.DisposeAsync() = - deleteMe () - ValueTask.CompletedTask - - /// Create a throwaway database file with the test_table defined - let buildDb () = task { - let dbName = $"""test-db-{Guid.NewGuid().ToString("n")}.db""" - Configuration.useConnectionString $"data source={dbName}" - do! Definition.ensureTable tableName - return new ThrowawaySqliteDb(dbName) - } - - /// A function that always returns true let isTrue<'T> (_ : 'T) = true open BitBadger.Documents +open BitBadger.Documents.Sqlite +open BitBadger.Documents.Tests open Expecto open Microsoft.Data.Sqlite +/// Unit tests for the SQLite library +let unitTests = + testList "Unit" [ + testList "Parameters" [ + test "idParam succeeds" { + let theParam = idParam 7 + Expect.equal theParam.ParameterName "@id" "The parameter name is incorrect" + Expect.equal theParam.Value "7" "The parameter value is incorrect" + } + test "jsonParam succeeds" { + let theParam = jsonParam "@test" {| Nice = "job" |} + Expect.equal theParam.ParameterName "@test" "The parameter name is incorrect" + Expect.equal theParam.Value """{"Nice":"job"}""" "The parameter value is incorrect" + } + test "fieldParam succeeds" { + let theParam = fieldParam 99 + Expect.equal theParam.ParameterName "@field" "The parameter name is incorrect" + Expect.equal theParam.Value 99 "The parameter value is incorrect" + } + test "noParams succeeds" { + Expect.isEmpty noParams "The parameter list should have been empty" + } + ] + // Results are exhaustively executed in the context of other tests + ] + /// These tests each use a fresh copy of a SQLite database -let all = +let integrationTests = let documents = [ { Id = "one"; Value = "FIRST!"; NumValue = 0; Sub = None } { Id = "two"; Value = "another"; NumValue = 10; Sub = Some { Foo = "green"; Bar = "blue" } } @@ -67,7 +58,7 @@ let all = { Id = "five"; Value = "purple"; NumValue = 18; Sub = None } ] let loadDocs () = backgroundTask { - for doc in documents do do! insert Db.tableName doc + for doc in documents do do! insert SqliteDb.TableName doc } testList "Integration" [ testList "Configuration" [ @@ -110,11 +101,11 @@ let all = ] testList "Definition" [ testTask "ensureTable succeeds" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() let itExists (name: string) = task { let! result = Custom.scalar - $"SELECT EXISTS (SELECT 1 FROM {Db.catalog} WHERE name = @name) AS it" + $"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = @name) AS it" [ SqliteParameter("@name", name) ] _.GetInt64(0) return result > 0 @@ -134,97 +125,97 @@ let all = ] testList "insert" [ testTask "succeeds" { - use! db = Db.buildDb () - let! before = Find.all Db.tableName + use! db = SqliteDb.BuildDb() + let! before = Find.all 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! insert Db.tableName testDoc - let! after = Find.all Db.tableName + do! insert SqliteDb.TableName testDoc + let! after = Find.all SqliteDb.TableName Expect.equal after [ testDoc ] "There should have been one document inserted" } testTask "fails for duplicate key" { - use! db = Db.buildDb () - do! insert Db.tableName { emptyDoc with Id = "test" } + use! db = SqliteDb.BuildDb() + do! insert SqliteDb.TableName { emptyDoc with Id = "test" } Expect.throws (fun () -> - insert Db.tableName {emptyDoc with Id = "test" } |> Async.AwaitTask |> Async.RunSynchronously) + insert SqliteDb.TableName {emptyDoc with Id = "test" } |> Async.AwaitTask |> Async.RunSynchronously) "An exception should have been raised for duplicate document ID insert" } ] testList "save" [ testTask "succeeds when a document is inserted" { - use! db = Db.buildDb () - let! before = Find.all Db.tableName + use! db = SqliteDb.BuildDb() + let! before = Find.all 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! save Db.tableName testDoc - let! after = Find.all Db.tableName + do! save SqliteDb.TableName testDoc + let! after = Find.all SqliteDb.TableName Expect.equal after [ testDoc ] "There should have been one document inserted" } testTask "succeeds when a document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() let testDoc = { emptyDoc with Id = "test"; Sub = Some { Foo = "a"; Bar = "b" } } - do! insert Db.tableName testDoc + do! insert SqliteDb.TableName testDoc - let! before = Find.byId Db.tableName "test" + let! before = Find.byId 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! save Db.tableName upd8Doc - let! after = Find.byId Db.tableName "test" + do! save SqliteDb.TableName upd8Doc + let! after = Find.byId SqliteDb.TableName "test" if Option.isNone after then Expect.isTrue false "There should have been a document returned post-update" Expect.equal after.Value upd8Doc "The updated document is not correct" } ] testList "Count" [ testTask "all succeeds" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - let! theCount = Count.all Db.tableName + let! theCount = Count.all SqliteDb.TableName Expect.equal theCount 5L "There should have been 5 matching documents" } testTask "byField succeeds" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - let! theCount = Count.byField Db.tableName "Value" EQ "purple" + let! theCount = Count.byField SqliteDb.TableName "Value" EQ "purple" Expect.equal theCount 2L "There should have been 2 matching documents" } ] testList "Exists" [ testList "byId" [ testTask "succeeds when a document exists" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - let! exists = Exists.byId Db.tableName "three" + let! exists = Exists.byId SqliteDb.TableName "three" Expect.isTrue exists "There should have been an existing document" } testTask "succeeds when a document does not exist" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - let! exists = Exists.byId Db.tableName "seven" + let! exists = Exists.byId SqliteDb.TableName "seven" Expect.isFalse exists "There should not have been an existing document" } ] testList "byField" [ testTask "succeeds when documents exist" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - let! exists = Exists.byField Db.tableName "NumValue" EQ 10 + let! exists = Exists.byField SqliteDb.TableName "NumValue" EQ 10 Expect.isTrue exists "There should have been existing documents" } testTask "succeeds when no matching documents exist" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - let! exists = Exists.byField Db.tableName "Nothing" LT "none" + let! exists = Exists.byField SqliteDb.TableName "Nothing" LT "none" Expect.isFalse exists "There should not have been any existing documents" } ] @@ -232,13 +223,13 @@ let all = testList "Find" [ testList "all" [ testTask "succeeds when there is data" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() - do! insert Db.tableName { Foo = "one"; Bar = "two" } - do! insert Db.tableName { Foo = "three"; Bar = "four" } - do! insert Db.tableName { Foo = "five"; Bar = "six" } + 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 = Find.all Db.tableName + let! results = Find.all SqliteDb.TableName let expected = [ { Foo = "one"; Bar = "two" } { Foo = "three"; Bar = "four" } @@ -247,66 +238,66 @@ let all = Expect.equal results expected "There should have been 3 documents returned" } testTask "succeeds when there is no data" { - use! db = Db.buildDb () - let! results = Find.all Db.tableName + use! db = SqliteDb.BuildDb() + let! results = Find.all SqliteDb.TableName Expect.equal results [] "There should have been no documents returned" } ] testList "byId" [ testTask "succeeds when a document is found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - let! doc = Find.byId Db.tableName "two" + let! doc = Find.byId SqliteDb.TableName "two" Expect.isTrue (Option.isSome doc) "There should have been a document returned" Expect.equal doc.Value.Id "two" "The incorrect document was returned" } testTask "succeeds when a document is not found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - let! doc = Find.byId Db.tableName "three hundred eighty-seven" + let! doc = Find.byId SqliteDb.TableName "three hundred eighty-seven" Expect.isFalse (Option.isSome doc) "There should not have been a document returned" } ] testList "byField" [ testTask "succeeds when documents are found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - let! docs = Find.byField Db.tableName "NumValue" GT 15 + let! docs = Find.byField SqliteDb.TableName "NumValue" GT 15 Expect.equal (List.length docs) 2 "There should have been two documents returned" } testTask "succeeds when documents are not found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - let! docs = Find.byField Db.tableName "NumValue" GT 100 + let! docs = Find.byField SqliteDb.TableName "NumValue" GT 100 Expect.isTrue (List.isEmpty docs) "There should have been no documents returned" } ] testList "firstByField" [ testTask "succeeds when a document is found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - let! doc = Find.firstByField Db.tableName "Value" EQ "another" + let! doc = Find.firstByField SqliteDb.TableName "Value" EQ "another" Expect.isTrue (Option.isSome doc) "There should have been a document returned" Expect.equal doc.Value.Id "two" "The incorrect document was returned" } testTask "succeeds when multiple documents are found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - let! doc = Find.firstByField Db.tableName "Sub.Foo" EQ "green" + let! doc = Find.firstByField SqliteDb.TableName "Sub.Foo" EQ "green" Expect.isTrue (Option.isSome doc) "There should have been a document returned" Expect.contains [ "two"; "four" ] doc.Value.Id "An incorrect document was returned" } testTask "succeeds when a document is not found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - let! doc = Find.firstByField Db.tableName "Value" EQ "absent" + let! doc = Find.firstByField SqliteDb.TableName "Value" EQ "absent" Expect.isFalse (Option.isSome doc) "There should not have been a document returned" } ] @@ -314,36 +305,36 @@ let all = testList "Update" [ testList "full" [ testTask "succeeds when a document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () let testDoc = { emptyDoc with Id = "one"; Sub = Some { Foo = "blue"; Bar = "red" } } - do! Update.full Db.tableName "one" testDoc - let! after = Find.byId Db.tableName "one" + do! Update.full SqliteDb.TableName "one" testDoc + let! after = Find.byId SqliteDb.TableName "one" if Option.isNone after then Expect.isTrue false "There should have been a document returned post-update" Expect.equal after.Value testDoc "The updated document is not correct" } testTask "succeeds when no document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() - let! before = Find.all Db.tableName + let! before = Find.all SqliteDb.TableName Expect.hasCountOf before 0u isTrue "There should have been no documents returned" // This not raising an exception is the test do! Update.full - Db.tableName + SqliteDb.TableName "test" { emptyDoc with Id = "x"; Sub = Some { Foo = "blue"; Bar = "red" } } } ] testList "fullFunc" [ testTask "succeeds when a document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - do! Update.fullFunc Db.tableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None } - let! after = Find.byId Db.tableName "one" + do! Update.fullFunc SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None } + let! after = Find.byId SqliteDb.TableName "one" if Option.isNone after then Expect.isTrue false "There should have been a document returned post-update" Expect.equal @@ -352,90 +343,90 @@ let all = "The updated document is not correct" } testTask "succeeds when no document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() - let! before = Find.all Db.tableName + let! before = Find.all SqliteDb.TableName Expect.hasCountOf before 0u isTrue "There should have been no documents returned" // This not raising an exception is the test - do! Update.fullFunc Db.tableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None } + do! Update.fullFunc SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None } } ] testList "partialById" [ testTask "succeeds when a document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - do! Update.partialById Db.tableName "one" {| NumValue = 44 |} - let! after = Find.byId Db.tableName "one" + do! Update.partialById SqliteDb.TableName "one" {| NumValue = 44 |} + let! after = Find.byId SqliteDb.TableName "one" if Option.isNone after then Expect.isTrue false "There should have been a document returned post-update" Expect.equal after.Value.NumValue 44 "The updated document is not correct" } testTask "succeeds when no document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() - let! before = Find.all Db.tableName + let! before = Find.all SqliteDb.TableName Expect.hasCountOf before 0u isTrue "There should have been no documents returned" // This not raising an exception is the test - do! Update.partialById Db.tableName "test" {| Foo = "green" |} + do! Update.partialById SqliteDb.TableName "test" {| Foo = "green" |} } ] testList "partialByField" [ testTask "succeeds when a document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - do! Update.partialByField Db.tableName "Value" EQ "purple" {| NumValue = 77 |} - let! after = Count.byField Db.tableName "NumValue" EQ 77 + do! Update.partialByField SqliteDb.TableName "Value" EQ "purple" {| NumValue = 77 |} + let! after = Count.byField SqliteDb.TableName "NumValue" EQ 77 Expect.equal after 2L "There should have been 2 documents returned" } testTask "succeeds when no document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() - let! before = Find.all Db.tableName + let! before = Find.all SqliteDb.TableName Expect.hasCountOf before 0u isTrue "There should have been no documents returned" // This not raising an exception is the test - do! Update.partialByField Db.tableName "Value" EQ "burgundy" {| Foo = "green" |} + do! Update.partialByField SqliteDb.TableName "Value" EQ "burgundy" {| Foo = "green" |} } ] ] testList "Delete" [ testList "byId" [ testTask "succeeds when a document is deleted" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - do! Delete.byId Db.tableName "four" - let! remaining = Count.all Db.tableName + do! Delete.byId SqliteDb.TableName "four" + let! remaining = Count.all SqliteDb.TableName Expect.equal remaining 4L "There should have been 4 documents remaining" } testTask "succeeds when a document is not deleted" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - do! Delete.byId Db.tableName "thirty" - let! remaining = Count.all Db.tableName + do! Delete.byId SqliteDb.TableName "thirty" + let! remaining = Count.all SqliteDb.TableName Expect.equal remaining 5L "There should have been 5 documents remaining" } ] testList "byField" [ testTask "succeeds when documents are deleted" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - do! Delete.byField Db.tableName "Value" NE "purple" - let! remaining = Count.all Db.tableName + do! Delete.byField SqliteDb.TableName "Value" NE "purple" + let! remaining = Count.all SqliteDb.TableName Expect.equal remaining 2L "There should have been 2 documents remaining" } testTask "succeeds when documents are not deleted" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - do! Delete.byField Db.tableName "Value" EQ "crimson" - let! remaining = Count.all Db.tableName + do! Delete.byField SqliteDb.TableName "Value" EQ "crimson" + let! remaining = Count.all SqliteDb.TableName Expect.equal remaining 5L "There should have been 5 documents remaining" } ] @@ -443,24 +434,24 @@ let all = testList "Custom" [ testList "single" [ testTask "succeeds when a row is found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () let! doc = Custom.single - $"SELECT data FROM {Db.tableName} WHERE data ->> 'Id' = @id" + $"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id" [ SqliteParameter("@id", "one") ] fromData Expect.isSome doc "There should have been a document returned" Expect.equal doc.Value.Id "one" "The incorrect document was returned" } testTask "succeeds when a row is not found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () let! doc = Custom.single - $"SELECT data FROM {Db.tableName} WHERE data ->> 'Id' = @id" + $"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id" [ SqliteParameter("@id", "eighty") ] fromData Expect.isNone doc "There should not have been a document returned" @@ -468,19 +459,19 @@ let all = ] testList "list" [ testTask "succeeds when data is found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - let! docs = Custom.list (Query.selectFromTable Db.tableName) [] fromData + let! docs = Custom.list (Query.selectFromTable SqliteDb.TableName) [] fromData Expect.hasCountOf docs 5u isTrue "There should have been 5 documents returned" } testTask "succeeds when data is not found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () let! docs = Custom.list - $"SELECT data FROM {Db.tableName} WHERE data ->> 'NumValue' > @value" + $"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value" [ SqliteParameter("@value", 100) ] fromData Expect.isEmpty docs "There should have been no documents returned" @@ -488,28 +479,28 @@ let all = ] testList "nonQuery" [ testTask "succeeds when operating on data" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () - do! Custom.nonQuery $"DELETE FROM {Db.tableName}" [] + do! Custom.nonQuery $"DELETE FROM {SqliteDb.TableName}" [] - let! remaining = Count.all Db.tableName + let! remaining = Count.all SqliteDb.TableName Expect.equal remaining 0L "There should be no documents remaining in the table" } testTask "succeeds when no data matches where clause" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() do! loadDocs () do! Custom.nonQuery - $"DELETE FROM {Db.tableName} WHERE data ->> 'NumValue' > @value" + $"DELETE FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value" [ SqliteParameter("@value", 100) ] - let! remaining = Count.all Db.tableName + let! remaining = Count.all SqliteDb.TableName Expect.equal remaining 5L "There should be 5 documents remaining in the table" } ] testTask "scalar succeeds" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() let! nbr = Custom.scalar "SELECT 5 AS test_value" [] _.GetInt32(0) Expect.equal nbr 5 "The query should have returned the number 5" @@ -517,12 +508,12 @@ let all = ] testList "Extensions" [ testTask "ensureTable succeeds" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () let itExists (name: string) = task { let! result = conn.customScalar - $"SELECT EXISTS (SELECT 1 FROM {Db.catalog} WHERE name = @name) AS it" + $"SELECT EXISTS (SELECT 1 FROM {SqliteDb.Catalog} WHERE name = @name) AS it" [ SqliteParameter("@name", name) ] _.GetInt64(0) return result > 0 @@ -541,23 +532,23 @@ let all = } testList "insert" [ testTask "succeeds" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () - let! before = conn.findAll Db.tableName + let! before = conn.findAll 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 Db.tableName testDoc - let! after = conn.findAll Db.tableName + do! conn.insert SqliteDb.TableName testDoc + let! after = conn.findAll SqliteDb.TableName Expect.equal after [ testDoc ] "There should have been one document inserted" } testTask "fails for duplicate key" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () - do! conn.insert Db.tableName { emptyDoc with Id = "test" } + do! conn.insert SqliteDb.TableName { emptyDoc with Id = "test" } Expect.throws (fun () -> - conn.insert Db.tableName {emptyDoc with Id = "test" } + conn.insert SqliteDb.TableName {emptyDoc with Id = "test" } |> Async.AwaitTask |> Async.RunSynchronously) "An exception should have been raised for duplicate document ID insert" @@ -565,96 +556,96 @@ let all = ] testList "save" [ testTask "succeeds when a document is inserted" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () - let! before = conn.findAll Db.tableName + let! before = conn.findAll 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 Db.tableName testDoc - let! after = conn.findAll Db.tableName + do! conn.save SqliteDb.TableName testDoc + let! after = conn.findAll SqliteDb.TableName Expect.equal after [ testDoc ] "There should have been one document inserted" } testTask "succeeds when a document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () let testDoc = { emptyDoc with Id = "test"; Sub = Some { Foo = "a"; Bar = "b" } } - do! conn.insert Db.tableName testDoc + do! conn.insert SqliteDb.TableName testDoc - let! before = conn.findById Db.tableName "test" + let! before = conn.findById 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 Db.tableName upd8Doc - let! after = conn.findById Db.tableName "test" + do! conn.save SqliteDb.TableName upd8Doc + let! after = conn.findById SqliteDb.TableName "test" if Option.isNone after then Expect.isTrue false "There should have been a document returned post-update" Expect.equal after.Value upd8Doc "The updated document is not correct" } ] testTask "countAll succeeds" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - let! theCount = conn.countAll Db.tableName + let! theCount = conn.countAll SqliteDb.TableName Expect.equal theCount 5L "There should have been 5 matching documents" } testTask "countByField succeeds" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - let! theCount = conn.countByField Db.tableName "Value" EQ "purple" + let! theCount = conn.countByField SqliteDb.TableName "Value" EQ "purple" Expect.equal theCount 2L "There should have been 2 matching documents" } testList "existsById" [ testTask "succeeds when a document exists" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - let! exists = conn.existsById Db.tableName "three" + let! exists = conn.existsById SqliteDb.TableName "three" Expect.isTrue exists "There should have been an existing document" } testTask "succeeds when a document does not exist" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - let! exists = conn.existsById Db.tableName "seven" + let! exists = conn.existsById SqliteDb.TableName "seven" Expect.isFalse exists "There should not have been an existing document" } ] testList "existsByField" [ testTask "succeeds when documents exist" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - let! exists = conn.existsByField Db.tableName "NumValue" EQ 10 + let! exists = conn.existsByField SqliteDb.TableName "NumValue" EQ 10 Expect.isTrue exists "There should have been existing documents" } testTask "succeeds when no matching documents exist" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - let! exists = conn.existsByField Db.tableName "Nothing" EQ "none" + let! exists = conn.existsByField SqliteDb.TableName "Nothing" EQ "none" Expect.isFalse exists "There should not have been any existing documents" } ] testList "findAll" [ testTask "succeeds when there is data" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () - do! insert Db.tableName { Foo = "one"; Bar = "two" } - do! insert Db.tableName { Foo = "three"; Bar = "four" } - do! insert Db.tableName { Foo = "five"; Bar = "six" } + 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 Db.tableName + let! results = conn.findAll SqliteDb.TableName let expected = [ { Foo = "one"; Bar = "two" } { Foo = "three"; Bar = "four" } @@ -663,115 +654,115 @@ let all = Expect.equal results expected "There should have been 3 documents returned" } testTask "succeeds when there is no data" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () - let! results = conn.findAll Db.tableName + let! results = conn.findAll SqliteDb.TableName Expect.equal results [] "There should have been no documents returned" } ] testList "findById" [ testTask "succeeds when a document is found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - let! doc = conn.findById Db.tableName "two" + let! doc = conn.findById SqliteDb.TableName "two" Expect.isTrue (Option.isSome doc) "There should have been a document returned" Expect.equal doc.Value.Id "two" "The incorrect document was returned" } testTask "succeeds when a document is not found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - let! doc = conn.findById Db.tableName "three hundred eighty-seven" + let! doc = conn.findById SqliteDb.TableName "three hundred eighty-seven" Expect.isFalse (Option.isSome doc) "There should not have been a document returned" } ] testList "findByField" [ testTask "succeeds when documents are found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - let! docs = conn.findByField Db.tableName "Sub.Foo" EQ "green" + let! docs = conn.findByField SqliteDb.TableName "Sub.Foo" EQ "green" Expect.equal (List.length docs) 2 "There should have been two documents returned" } testTask "succeeds when documents are not found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - let! docs = conn.findByField Db.tableName "Value" EQ "mauve" + let! docs = conn.findByField SqliteDb.TableName "Value" EQ "mauve" Expect.isTrue (List.isEmpty docs) "There should have been no documents returned" } ] testList "findFirstByField" [ testTask "succeeds when a document is found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - let! doc = conn.findFirstByField Db.tableName "Value" EQ "another" + let! doc = conn.findFirstByField SqliteDb.TableName "Value" EQ "another" Expect.isTrue (Option.isSome doc) "There should have been a document returned" Expect.equal doc.Value.Id "two" "The incorrect document was returned" } testTask "succeeds when multiple documents are found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - let! doc = conn.findFirstByField Db.tableName "Sub.Foo" EQ "green" + let! doc = conn.findFirstByField SqliteDb.TableName "Sub.Foo" EQ "green" Expect.isTrue (Option.isSome doc) "There should have been a document returned" Expect.contains [ "two"; "four" ] doc.Value.Id "An incorrect document was returned" } testTask "succeeds when a document is not found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - let! doc = conn.findFirstByField Db.tableName "Value" EQ "absent" + let! doc = conn.findFirstByField SqliteDb.TableName "Value" EQ "absent" Expect.isFalse (Option.isSome doc) "There should not have been a document returned" } ] testList "updateFull" [ testTask "succeeds when a document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () let testDoc = { emptyDoc with Id = "one"; Sub = Some { Foo = "blue"; Bar = "red" } } - do! conn.updateFull Db.tableName "one" testDoc - let! after = conn.findById Db.tableName "one" + do! conn.updateFull SqliteDb.TableName "one" testDoc + let! after = conn.findById SqliteDb.TableName "one" if Option.isNone after then Expect.isTrue false "There should have been a document returned post-update" Expect.equal after.Value testDoc "The updated document is not correct" } testTask "succeeds when no document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () - let! before = conn.findAll Db.tableName + let! before = conn.findAll SqliteDb.TableName Expect.hasCountOf before 0u isTrue "There should have been no documents returned" // This not raising an exception is the test do! conn.updateFull - Db.tableName + SqliteDb.TableName "test" { emptyDoc with Id = "x"; Sub = Some { Foo = "blue"; Bar = "red" } } } ] testList "updateFullFunc" [ testTask "succeeds when a document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () do! conn.updateFullFunc - Db.tableName + SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None } - let! after = conn.findById Db.tableName "one" + let! after = conn.findById SqliteDb.TableName "one" if Option.isNone after then Expect.isTrue false "There should have been a document returned post-update" Expect.equal @@ -780,125 +771,125 @@ let all = "The updated document is not correct" } testTask "succeeds when no document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () - let! before = conn.findAll Db.tableName + let! before = conn.findAll SqliteDb.TableName Expect.hasCountOf before 0u isTrue "There should have been no documents returned" // This not raising an exception is the test do! conn.updateFullFunc - Db.tableName + SqliteDb.TableName (_.Id) { Id = "one"; Value = "le un"; NumValue = 1; Sub = None } } ] testList "updatePartialById" [ testTask "succeeds when a document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - do! conn.updatePartialById Db.tableName "one" {| NumValue = 44 |} - let! after = conn.findById Db.tableName "one" + do! conn.updatePartialById SqliteDb.TableName "one" {| NumValue = 44 |} + let! after = conn.findById SqliteDb.TableName "one" if Option.isNone after then Expect.isTrue false "There should have been a document returned post-update" Expect.equal after.Value.NumValue 44 "The updated document is not correct" } testTask "succeeds when no document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () - let! before = conn.findAll Db.tableName + let! before = conn.findAll SqliteDb.TableName Expect.hasCountOf before 0u isTrue "There should have been no documents returned" // This not raising an exception is the test - do! conn.updatePartialById Db.tableName "test" {| Foo = "green" |} + do! conn.updatePartialById SqliteDb.TableName "test" {| Foo = "green" |} } ] testList "updatePartialByField" [ testTask "succeeds when a document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - do! conn.updatePartialByField Db.tableName "Value" EQ "purple" {| NumValue = 77 |} - let! after = conn.countByField Db.tableName "NumValue" EQ 77 + do! conn.updatePartialByField SqliteDb.TableName "Value" EQ "purple" {| NumValue = 77 |} + let! after = conn.countByField SqliteDb.TableName "NumValue" EQ 77 Expect.equal after 2L "There should have been 2 documents returned" } testTask "succeeds when no document is updated" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () - let! before = conn.findAll Db.tableName + let! before = conn.findAll SqliteDb.TableName Expect.hasCountOf before 0u isTrue "There should have been no documents returned" // This not raising an exception is the test - do! conn.updatePartialByField Db.tableName "Value" EQ "burgundy" {| Foo = "green" |} + do! conn.updatePartialByField SqliteDb.TableName "Value" EQ "burgundy" {| Foo = "green" |} } ] testList "deleteById" [ testTask "succeeds when a document is deleted" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - do! conn.deleteById Db.tableName "four" - let! remaining = conn.countAll Db.tableName + do! conn.deleteById SqliteDb.TableName "four" + let! remaining = conn.countAll SqliteDb.TableName Expect.equal remaining 4L "There should have been 4 documents remaining" } testTask "succeeds when a document is not deleted" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - do! conn.deleteById Db.tableName "thirty" - let! remaining = conn.countAll Db.tableName + do! conn.deleteById SqliteDb.TableName "thirty" + let! remaining = conn.countAll SqliteDb.TableName Expect.equal remaining 5L "There should have been 5 documents remaining" } ] testList "deleteByField" [ testTask "succeeds when documents are deleted" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - do! conn.deleteByField Db.tableName "Value" NE "purple" - let! remaining = conn.countAll Db.tableName + do! conn.deleteByField SqliteDb.TableName "Value" NE "purple" + let! remaining = conn.countAll SqliteDb.TableName Expect.equal remaining 2L "There should have been 2 documents remaining" } testTask "succeeds when documents are not deleted" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - do! conn.deleteByField Db.tableName "Value" EQ "crimson" - let! remaining = conn.countAll Db.tableName + do! conn.deleteByField SqliteDb.TableName "Value" EQ "crimson" + let! remaining = conn.countAll SqliteDb.TableName Expect.equal remaining 5L "There should have been 5 documents remaining" } ] testList "customSingle" [ testTask "succeeds when a row is found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () let! doc = conn.customSingle - $"SELECT data FROM {Db.tableName} WHERE data ->> 'Id' = @id" + $"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id" [ SqliteParameter("@id", "one") ] fromData Expect.isSome doc "There should have been a document returned" Expect.equal doc.Value.Id "one" "The incorrect document was returned" } testTask "succeeds when a row is not found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () let! doc = conn.customSingle - $"SELECT data FROM {Db.tableName} WHERE data ->> 'Id' = @id" + $"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'Id' = @id" [ SqliteParameter("@id", "eighty") ] fromData Expect.isNone doc "There should not have been a document returned" @@ -906,21 +897,21 @@ let all = ] testList "customList" [ testTask "succeeds when data is found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - let! docs = conn.customList (Query.selectFromTable Db.tableName) [] fromData + let! docs = conn.customList (Query.selectFromTable SqliteDb.TableName) [] fromData Expect.hasCountOf docs 5u isTrue "There should have been 5 documents returned" } testTask "succeeds when data is not found" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () let! docs = conn.customList - $"SELECT data FROM {Db.tableName} WHERE data ->> 'NumValue' > @value" + $"SELECT data FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value" [ SqliteParameter("@value", 100) ] fromData Expect.isEmpty docs "There should have been no documents returned" @@ -928,30 +919,30 @@ let all = ] testList "customNonQuery" [ testTask "succeeds when operating on data" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () - do! conn.customNonQuery $"DELETE FROM {Db.tableName}" [] + do! conn.customNonQuery $"DELETE FROM {SqliteDb.TableName}" [] - let! remaining = conn.countAll Db.tableName + let! remaining = conn.countAll SqliteDb.TableName Expect.equal remaining 0L "There should be no documents remaining in the table" } testTask "succeeds when no data matches where clause" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () do! loadDocs () do! conn.customNonQuery - $"DELETE FROM {Db.tableName} WHERE data ->> 'NumValue' > @value" + $"DELETE FROM {SqliteDb.TableName} WHERE data ->> 'NumValue' > @value" [ SqliteParameter("@value", 100) ] - let! remaining = conn.countAll Db.tableName + let! remaining = conn.countAll SqliteDb.TableName Expect.equal remaining 5L "There should be 5 documents remaining in the table" } ] testTask "customScalar succeeds" { - use! db = Db.buildDb () + use! db = SqliteDb.BuildDb() use conn = Configuration.dbConn () let! nbr = conn.customScalar "SELECT 5 AS test_value" [] _.GetInt32(0) @@ -963,3 +954,5 @@ let all = } ] |> testSequenced + +let all = testList "Sqlite" [ unitTests; integrationTests ]