module PageDataTests open System open Expecto open MyWebLog open MyWebLog.Data open NodaTime /// The ID of the root web log let rootId = WebLogId "uSitJEuD3UyzWC9jgOHc8g" /// The ID of the "A cool page" page let coolPageId = PageId "hgc_BLEZ50SoAWLuPNISvA" /// The published and updated time of the "A cool page" page let coolPagePublished = Instant.FromDateTimeOffset(DateTimeOffset.Parse "2024-01-20T22:14:28Z") /// The ID of the "Yet Another Page" page let otherPageId = PageId "KouRjvSmm0Wz6TMD8xf67A" let ``Add succeeds`` (data: IData) = task { let page = { Id = PageId "added-page" WebLogId = WebLogId "test" AuthorId = WebLogUserId "the-author" Title = "A New Page" Permalink = Permalink "2024/the-page.htm" PublishedOn = Noda.epoch + Duration.FromDays 3 UpdatedOn = Noda.epoch + Duration.FromDays 3 + Duration.FromMinutes 2L IsInPageList = true Template = Some "new-page-template" Text = "
It really is cool!
\n" "Text is incorrect" Expect.hasLength pg.Metadata 2 "There should be 2 metadata items on this page" Expect.equal pg.Metadata[0].Name "Cool" "Meta item 0 name is incorrect" Expect.equal pg.Metadata[0].Value "true" "Meta item 0 value is incorrect" Expect.equal pg.Metadata[1].Name "Warm" "Meta item 1 name is incorrect" Expect.equal pg.Metadata[1].Value "false" "Meta item 1 value is incorrect" Expect.isEmpty pg.Revisions "Revisions should not have been retrieved" Expect.isEmpty pg.PriorPermalinks "Prior permalinks should not have been retrieved" } let ``FindById succeeds when a page is not found (incorrect weblog)`` (data: IData) = task { let! page = data.Page.FindById coolPageId (WebLogId "wrong") Expect.isNone page "The page should not have been retrieved" } let ``FindById succeeds when a page is not found (bad page ID)`` (data: IData) = task { let! page = data.Page.FindById (PageId "missing") rootId Expect.isNone page "The page should not have been retrieved" } let ``FindByPermalink succeeds when a page is found`` (data: IData) = task { let! page = data.Page.FindByPermalink (Permalink "a-cool-page.html") rootId Expect.isSome page "A page should have been returned" let pg = page.Value Expect.equal pg.Id coolPageId "The wrong page was retrieved" } let ``FindByPermalink succeeds when a page is not found (incorrect weblog)`` (data: IData) = task { let! page = data.Page.FindByPermalink (Permalink "a-cool-page.html") (WebLogId "wrong") Expect.isNone page "The page should not have been retrieved" } let ``FindByPermalink succeeds when a page is not found (no such permalink)`` (data: IData) = task { let! page = data.Page.FindByPermalink (Permalink "1970/no-www-then.html") rootId Expect.isNone page "The page should not have been retrieved" } let ``FindCurrentPermalink succeeds when a page is found`` (data: IData) = task { let! link = data.Page.FindCurrentPermalink [ Permalink "a-cool-pg.html"; Permalink "a-cool-pg.html/" ] rootId Expect.isSome link "A permalink should have been returned" Expect.equal link (Some (Permalink "a-cool-page.html")) "The wrong permalink was retrieved" } let ``FindCurrentPermalink succeeds when a page is not found`` (data: IData) = task { let! link = data.Page.FindCurrentPermalink [ Permalink "blah/"; Permalink "blah" ] rootId Expect.isNone link "A permalink should not have been returned" } let ``FindFullById succeeds when a page is found`` (data: IData) = task { let! page = data.Page.FindFullById coolPageId rootId Expect.isSome page "A page should have been returned" let pg = page.Value Expect.equal pg.Id coolPageId "The wrong page was retrieved" Expect.equal pg.WebLogId rootId "The page's web log did not match the called parameter" Expect.hasLength pg.Revisions 1 "There should be 1 revision" Expect.equal pg.Revisions[0].AsOf coolPagePublished "Revision 0 as-of is incorrect" Expect.equal pg.Revisions[0].Text (Markdown "# A Cool Page\n\nIt really is cool!") "Revision 0 text is incorrect" Expect.hasLength pg.PriorPermalinks 1 "There should be 1 prior permalink" Expect.equal pg.PriorPermalinks[0] (Permalink "a-cool-pg.html") "Prior permalink 0 is incorrect" } let ``FindFullById succeeds when a page is not found`` (data: IData) = task { let! page = data.Page.FindFullById (PageId "not-there") rootId Expect.isNone page "A page should not have been retrieved" } let ``FindFullByWebLog succeeds when pages are found`` (data: IData) = task { let! pages = data.Page.FindFullByWebLog rootId Expect.hasLength pages 2 "There should have been 2 pages returned" pages |> List.iter (fun pg -> Expect.contains [ coolPageId; otherPageId ] pg.Id $"Page ID {pg.Id} unexpected" if pg.Id = coolPageId then Expect.isNonEmpty pg.Metadata "Metadata should have been retrieved" Expect.isNonEmpty pg.PriorPermalinks "Prior permalinks should have been retrieved" Expect.isNonEmpty pg.Revisions "Revisions should have been retrieved") } let ``FindFullByWebLog succeeds when pages are not found`` (data: IData) = task { let! pages = data.Page.FindFullByWebLog (WebLogId "does-not-exist") Expect.isEmpty pages "No pages should have been retrieved" } let ``FindListed succeeds when pages are found`` (data: IData) = task { let! pages = data.Page.FindListed rootId Expect.hasLength pages 1 "There should have been 1 page returned" Expect.equal pages[0].Id otherPageId "An unexpected page was returned" Expect.equal pages[0].Text "" "Text should not have been returned" Expect.isEmpty pages[0].PriorPermalinks "Prior permalinks should not have been retrieved" Expect.isEmpty pages[0].Revisions "Revisions should not have been retrieved" } let ``FindListed succeeds when pages are not found`` (data: IData) = task { let! pages = data.Page.FindListed (WebLogId "none") Expect.isEmpty pages "No pages should have been retrieved" }