Add framework for SQLite data tests

This commit is contained in:
Daniel J. Summers 2024-01-21 14:26:02 -05:00
parent ffe6713b5e
commit 1345074c71
7 changed files with 88 additions and 6 deletions

View File

@ -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<SQLiteData>, 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<SQLiteData>, 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"

View File

@ -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"
}

View File

@ -10,6 +10,8 @@
<Compile Include="ViewModelsTests.fs" />
<Compile Include="ConvertersTests.fs" />
<Compile Include="UtilsTests.fs" />
<Compile Include="CategoryDataTests.fs" />
<Compile Include="SQLiteDataTests.fs" />
<Compile Include="Program.fs" />
<Content Include="root-weblog.json" />
</ItemGroup>

View File

@ -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 ]
]
[<EntryPoint>]

View File

@ -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<SQLiteData>(), 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

View File

@ -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)

View File

@ -42,4 +42,10 @@
<None Include=".\wwwroot\upload\*" CopyToOutputDirectory="Always" />
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>MyWebLog.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>