From 1345074c71a3c76153e79f18a602f5d7c5169577 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sun, 21 Jan 2024 14:26:02 -0500 Subject: [PATCH] Add framework for SQLite data tests --- src/MyWebLog.Data/SQLiteData.fs | 8 ++-- src/MyWebLog.Tests/CategoryDataTests.fs | 15 ++++++ src/MyWebLog.Tests/MyWebLog.Tests.fsproj | 2 + src/MyWebLog.Tests/Program.fs | 2 +- src/MyWebLog.Tests/SQLiteDataTests.fs | 59 ++++++++++++++++++++++++ src/MyWebLog/Maintenance.fs | 2 +- src/MyWebLog/MyWebLog.fsproj | 6 +++ 7 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 src/MyWebLog.Tests/CategoryDataTests.fs create mode 100644 src/MyWebLog.Tests/SQLiteDataTests.fs diff --git a/src/MyWebLog.Data/SQLiteData.fs b/src/MyWebLog.Data/SQLiteData.fs index 52f6de2..424f282 100644 --- a/src/MyWebLog.Data/SQLiteData.fs +++ b/src/MyWebLog.Data/SQLiteData.fs @@ -1,5 +1,6 @@ namespace MyWebLog.Data +open System open System.Threading.Tasks open BitBadger.Documents open BitBadger.Documents.Sqlite @@ -221,8 +222,7 @@ type SQLiteData(conn: SqliteConnection, log: ILogger, ser: JsonSeria TranscriptCaptions = Map.tryBoolean "transcript_captions" epRdr SeasonNumber = Map.tryInt "season_number" epRdr SeasonDescription = Map.tryString "season_description" epRdr - EpisodeNumber = Map.tryString "episode_number" epRdr - |> Option.map System.Double.Parse + EpisodeNumber = Map.tryString "episode_number" epRdr |> Option.map Double.Parse EpisodeDescription = Map.tryString "episode_description" epRdr } } |> List.ofSeq epRdr.Close() @@ -235,8 +235,8 @@ type SQLiteData(conn: SqliteConnection, log: ILogger, ser: JsonSeria cmd.Parameters.Clear()) logStep "Migrating dates/times" - let inst (dt: System.DateTime) = - System.DateTime(dt.Ticks, System.DateTimeKind.Utc) + let inst (dt: DateTime) = + DateTime(dt.Ticks, DateTimeKind.Utc) |> (Instant.FromDateTimeUtc >> Noda.toSecondsPrecision) // page.updated_on, page.published_on cmd.CommandText <- "SELECT id, updated_on, published_on FROM page" diff --git a/src/MyWebLog.Tests/CategoryDataTests.fs b/src/MyWebLog.Tests/CategoryDataTests.fs new file mode 100644 index 0000000..01872a1 --- /dev/null +++ b/src/MyWebLog.Tests/CategoryDataTests.fs @@ -0,0 +1,15 @@ +module CategoryDataTests + +open Expecto +open MyWebLog +open MyWebLog.Data + +/// Tests for the Add method +let addTests (data: IData) = task { + let category = + { Category.Empty with Id = CategoryId "added-cat"; WebLogId = WebLogId "test"; Name = "Added"; Slug = "added" } + do! data.Category.Add category + let! stored = data.Category.FindById (CategoryId "added-cat") (WebLogId "test") + Expect.isSome stored "The category should have been added" +} + diff --git a/src/MyWebLog.Tests/MyWebLog.Tests.fsproj b/src/MyWebLog.Tests/MyWebLog.Tests.fsproj index a0433b0..8887ed0 100644 --- a/src/MyWebLog.Tests/MyWebLog.Tests.fsproj +++ b/src/MyWebLog.Tests/MyWebLog.Tests.fsproj @@ -10,6 +10,8 @@ + + diff --git a/src/MyWebLog.Tests/Program.fs b/src/MyWebLog.Tests/Program.fs index eda78d1..b01944f 100644 --- a/src/MyWebLog.Tests/Program.fs +++ b/src/MyWebLog.Tests/Program.fs @@ -2,7 +2,7 @@ let allTests = testList "MyWebLog" [ testList "Domain" [ SupportTypesTests.all; DataTypesTests.all; ViewModelsTests.all ] - testList "Data" [ ConvertersTests.all; UtilsTests.all ] + testList "Data" [ ConvertersTests.all; UtilsTests.all; SQLiteDataTests.all ] ] [] diff --git a/src/MyWebLog.Tests/SQLiteDataTests.fs b/src/MyWebLog.Tests/SQLiteDataTests.fs new file mode 100644 index 0000000..073115e --- /dev/null +++ b/src/MyWebLog.Tests/SQLiteDataTests.fs @@ -0,0 +1,59 @@ +module SQLiteDataTests + +open BitBadger.Documents +open Expecto +open Microsoft.Extensions.Logging.Abstractions +open MyWebLog +open MyWebLog.Converters +open MyWebLog.Data +open Newtonsoft.Json + +/// JSON serializer +let ser = Json.configure (JsonSerializer.CreateDefault()) + +/// Create a SQLiteData instance for testing +let mkData () = + Sqlite.Configuration.useConnectionString "Data Source=./test-db.db" + let conn = Sqlite.Configuration.dbConn () + SQLiteData(conn, NullLogger(), ser) :> IData + +/// Dispose the connection associated with the SQLiteData instance +let dispose (data: IData) = + (data :?> SQLiteData).Conn.Dispose() + +/// Set up the environment for the SQLite tests +let environmentSetUp = testList "Environment" [ + testTask "creating database" { + let data = mkData () + try + do! data.StartUp() + do! Maintenance.Backup.restoreBackup "root-weblog.json" None false data + finally dispose data + } +] + +/// Integration tests for the Category implementation in SQLite +let categoryTests = testList "Category" [ + testTask "Add succeeds" { + let data = mkData () + try do! CategoryDataTests.addTests data + finally dispose data + } +] + + +open System.IO + +/// Delete the SQLite database +let environmentCleanUp = test "Clean Up" { + File.Delete "test-db.db" + Expect.isFalse (File.Exists "test-db.db") "The test SQLite database should have been deleted" +} + +/// All SQLite data tests +let all = + testList "SQLiteData" + [ environmentSetUp + categoryTests + environmentCleanUp ] + |> testSequenced diff --git a/src/MyWebLog/Maintenance.fs b/src/MyWebLog/Maintenance.fs index 54ab9fc..1612add 100644 --- a/src/MyWebLog/Maintenance.fs +++ b/src/MyWebLog/Maintenance.fs @@ -395,7 +395,7 @@ module Backup = } /// Decide whether to restore a backup - let private restoreBackup fileName newUrlBase promptForOverwrite data = task { + let internal restoreBackup fileName newUrlBase promptForOverwrite data = task { let serializer = getSerializer false use stream = new FileStream(fileName, FileMode.Open) diff --git a/src/MyWebLog/MyWebLog.fsproj b/src/MyWebLog/MyWebLog.fsproj index a270487..4707fa3 100644 --- a/src/MyWebLog/MyWebLog.fsproj +++ b/src/MyWebLog/MyWebLog.fsproj @@ -42,4 +42,10 @@ + + + <_Parameter1>MyWebLog.Tests + + +