From 78c1c5e68e13f07a12a5fe9170b99fb7c3f4dc04 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Tue, 30 Jan 2024 19:14:59 -0500 Subject: [PATCH] Add framework for post tests - SQLite is failing on fresh environment - Remove no equality/no comparison on meta/revision - Rename RethinkDB test file --- src/MyWebLog.Domain/SupportTypes.fs | 4 +- src/MyWebLog.Tests/Data/CategoryDataTests.fs | 3 ++ src/MyWebLog.Tests/Data/PageDataTests.fs | 3 ++ src/MyWebLog.Tests/Data/PostDataTests.fs | 52 +++++++++++++++++++ src/MyWebLog.Tests/Data/PostgresDataTests.fs | 18 +++++-- ...ethinkDbTests.fs => RethinkDbDataTests.fs} | 12 ++++- src/MyWebLog.Tests/Data/SQLiteDataTests.fs | 19 ++++++- src/MyWebLog.Tests/MyWebLog.Tests.fsproj | 3 +- src/MyWebLog.Tests/Program.fs | 2 +- 9 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 src/MyWebLog.Tests/Data/PostDataTests.fs rename src/MyWebLog.Tests/Data/{RethinkDbTests.fs => RethinkDbDataTests.fs} (96%) diff --git a/src/MyWebLog.Domain/SupportTypes.fs b/src/MyWebLog.Domain/SupportTypes.fs index 53df258..b4ecde4 100644 --- a/src/MyWebLog.Domain/SupportTypes.fs +++ b/src/MyWebLog.Domain/SupportTypes.fs @@ -314,7 +314,7 @@ type MarkupText = /// An item of metadata -[] +[] type MetaItem = { /// The name of the metadata value Name: string @@ -329,7 +329,7 @@ type MetaItem = { /// A revision of a page or post -[] +[] type Revision = { /// When this revision was saved AsOf: Instant diff --git a/src/MyWebLog.Tests/Data/CategoryDataTests.fs b/src/MyWebLog.Tests/Data/CategoryDataTests.fs index 9368545..b1233cd 100644 --- a/src/MyWebLog.Tests/Data/CategoryDataTests.fs +++ b/src/MyWebLog.Tests/Data/CategoryDataTests.fs @@ -1,3 +1,6 @@ +/// +/// Integration tests for implementations +/// module CategoryDataTests open Expecto diff --git a/src/MyWebLog.Tests/Data/PageDataTests.fs b/src/MyWebLog.Tests/Data/PageDataTests.fs index 45fda8a..03159b8 100644 --- a/src/MyWebLog.Tests/Data/PageDataTests.fs +++ b/src/MyWebLog.Tests/Data/PageDataTests.fs @@ -1,3 +1,6 @@ +/// +/// Integration tests for implementations +/// module PageDataTests open System diff --git a/src/MyWebLog.Tests/Data/PostDataTests.fs b/src/MyWebLog.Tests/Data/PostDataTests.fs new file mode 100644 index 0000000..45f2063 --- /dev/null +++ b/src/MyWebLog.Tests/Data/PostDataTests.fs @@ -0,0 +1,52 @@ +/// +/// Integration tests for implementations +/// +module PostDataTests + +open Expecto +open MyWebLog +open MyWebLog.Data +open NodaTime + +/// The ID of the root web log +let rootId = WebLogId "uSitJEuD3UyzWC9jgOHc8g" + +let ``Add succeeds`` (data: IData) = task { + let post = + { Id = PostId "a-new-post" + WebLogId = WebLogId "test" + AuthorId = WebLogUserId "test-author" + Status = Published + Title = "A New Test Post" + Permalink = Permalink "2020/test-post.html" + PublishedOn = Some (Noda.epoch + Duration.FromMinutes 1L) + UpdatedOn = Noda.epoch + Duration.FromMinutes 3L + Template = Some "fancy" + Text = "

Test text here" + CategoryIds = [ CategoryId "a"; CategoryId "b" ] + Tags = [ "x"; "y"; "zed" ] + Episode = Some { Episode.Empty with Media = "test-ep.mp3" } + Metadata = [ { Name = "Meta"; Value = "Data" } ] + PriorPermalinks = [ Permalink "2020/test-post-a.html" ] + Revisions = [ { AsOf = Noda.epoch + Duration.FromMinutes 1L; Text = Html "

Test text here" } ] } + do! data.Post.Add post + let! stored = data.Post.FindFullById post.Id post.WebLogId + Expect.isSome stored "The added post should have been retrieved" + let it = stored.Value + Expect.equal it.Id post.Id "ID not saved properly" + Expect.equal it.WebLogId post.WebLogId "Web log ID not saved properly" + Expect.equal it.AuthorId post.AuthorId "Author ID not saved properly" + Expect.equal it.Status post.Status "Status not saved properly" + Expect.equal it.Title post.Title "Title not saved properly" + Expect.equal it.Permalink post.Permalink "Permalink not saved properly" + Expect.equal it.PublishedOn post.PublishedOn "Published On not saved properly" + Expect.equal it.UpdatedOn post.UpdatedOn "Updated On not saved properly" + Expect.equal it.Template post.Template "Template not saved properly" + Expect.equal it.Text post.Text "Text not saved properly" + Expect.equal it.CategoryIds post.CategoryIds "Category IDs not saved properly" + Expect.equal it.Tags post.Tags "Tags not saved properly" + Expect.equal it.Episode post.Episode "Episode not saved properly" + Expect.equal it.Metadata post.Metadata "Metadata items not saved properly" + Expect.equal it.PriorPermalinks post.PriorPermalinks "Prior permalinks not saved properly" + Expect.equal it.Revisions post.Revisions "Revisions not saved properly" +} diff --git a/src/MyWebLog.Tests/Data/PostgresDataTests.fs b/src/MyWebLog.Tests/Data/PostgresDataTests.fs index 2d0b4ba..7b4dc1c 100644 --- a/src/MyWebLog.Tests/Data/PostgresDataTests.fs +++ b/src/MyWebLog.Tests/Data/PostgresDataTests.fs @@ -23,19 +23,19 @@ let mkData () = /// The host for the PostgreSQL test database (defaults to localhost) let testHost = - RethinkDbTests.env "PG_HOST" "localhost" + RethinkDbDataTests.env "PG_HOST" "localhost" /// The database name for the PostgreSQL test database (defaults to postgres) let testDb = - RethinkDbTests.env "PG_DB" "postgres" + RethinkDbDataTests.env "PG_DB" "postgres" /// The user ID for the PostgreSQL test database (defaults to postgres) let testUser = - RethinkDbTests.env "PG_USER" "postgres" + RethinkDbDataTests.env "PG_USER" "postgres" /// The password for the PostgreSQL test database (defaults to postgres) let testPw = - RethinkDbTests.env "PG_PW" "postgres" + RethinkDbDataTests.env "PG_PW" "postgres" /// Create a fresh environment from the root backup let freshEnvironment () = task { @@ -219,6 +219,15 @@ let pageTests = testList "Page" [ ] ] +/// Integration tests for the Post implementation in PostgreSQL +let postTests = testList "Post" [ + testTask "Add succeeds" { + // We'll need the root website categories restored for these tests + do! freshEnvironment () + do! PostDataTests.``Add succeeds`` (mkData ()) + } +] + /// Drop the throwaway PostgreSQL database let environmentCleanUp = test "Clean Up" { if db.IsSome then db.Value.Dispose() @@ -230,5 +239,6 @@ let all = [ environmentSetUp categoryTests pageTests + postTests environmentCleanUp ] |> testSequenced diff --git a/src/MyWebLog.Tests/Data/RethinkDbTests.fs b/src/MyWebLog.Tests/Data/RethinkDbDataTests.fs similarity index 96% rename from src/MyWebLog.Tests/Data/RethinkDbTests.fs rename to src/MyWebLog.Tests/Data/RethinkDbDataTests.fs index 5304c12..6ac7d6e 100644 --- a/src/MyWebLog.Tests/Data/RethinkDbTests.fs +++ b/src/MyWebLog.Tests/Data/RethinkDbDataTests.fs @@ -1,4 +1,4 @@ -module RethinkDbTests +module RethinkDbDataTests open System open Expecto @@ -218,6 +218,15 @@ let pageTests = testList "Page" [ ] ] +/// Integration tests for the Post implementation in RethinkDB +let postTests = testList "Post" [ + testTask "Add succeeds" { + // We'll need the root website categories restored for these tests + do! freshEnvironment () + do! PostDataTests.``Add succeeds`` data.Value + } +] + /// Drop the throwaway RethinkDB database let environmentCleanUp = testTask "Clean Up" { do! disposeData () @@ -229,5 +238,6 @@ let all = [ environmentSetUp categoryTests pageTests + postTests environmentCleanUp ] |> testSequenced diff --git a/src/MyWebLog.Tests/Data/SQLiteDataTests.fs b/src/MyWebLog.Tests/Data/SQLiteDataTests.fs index 1be47af..06dc659 100644 --- a/src/MyWebLog.Tests/Data/SQLiteDataTests.fs +++ b/src/MyWebLog.Tests/Data/SQLiteDataTests.fs @@ -14,7 +14,7 @@ let ser = Json.configure (JsonSerializer.CreateDefault()) /// The test database name let dbName = - RethinkDbTests.env "SQLITE_DB" "test-db.db" + RethinkDbDataTests.env "SQLITE_DB" "test-db.db" /// Create a SQLiteData instance for testing let mkData () = @@ -30,13 +30,17 @@ let dispose (data: IData) = let freshEnvironment (data: IData option) = task { let env = match data with - | Some d -> d + | Some d -> + System.Console.WriteLine "Existing data" + d | None -> + System.Console.WriteLine $"No data; deleting {dbName}" File.Delete dbName mkData () do! env.StartUp() // This exercises Restore for all implementations; all tests are dependent on it working as expected do! Maintenance.Backup.restoreBackup "root-weblog.json" None false false env + return env } /// Set up the environment for the SQLite tests @@ -284,6 +288,16 @@ let pageTests = testList "Page" [ ] ] +/// Integration tests for the Post implementation in SQLite +let postTests = testList "Post" [ + testTask "Add succeeds" { + // We'll need the root website categories restored for these tests + let! data = freshEnvironment None + try do! PostDataTests.``Add succeeds`` data + finally dispose data + } +] + /// Delete the SQLite database let environmentCleanUp = test "Clean Up" { File.Delete dbName @@ -296,5 +310,6 @@ let all = [ environmentSetUp categoryTests pageTests + postTests environmentCleanUp ] |> testSequenced diff --git a/src/MyWebLog.Tests/MyWebLog.Tests.fsproj b/src/MyWebLog.Tests/MyWebLog.Tests.fsproj index 1ef9297..e6c8dab 100644 --- a/src/MyWebLog.Tests/MyWebLog.Tests.fsproj +++ b/src/MyWebLog.Tests/MyWebLog.Tests.fsproj @@ -12,7 +12,8 @@ - + + diff --git a/src/MyWebLog.Tests/Program.fs b/src/MyWebLog.Tests/Program.fs index 43077ee..f5fc99b 100644 --- a/src/MyWebLog.Tests/Program.fs +++ b/src/MyWebLog.Tests/Program.fs @@ -5,7 +5,7 @@ let allTests = testList "MyWebLog" [ testList "Data" [ ConvertersTests.all UtilsTests.all - RethinkDbTests.all + RethinkDbDataTests.all SQLiteDataTests.all PostgresDataTests.all ]