Add converter / utils tests

This commit is contained in:
Daniel J. Summers 2024-01-21 09:17:01 -05:00
parent cfb3c3a287
commit ffe6713b5e
7 changed files with 161 additions and 35 deletions

View File

@ -151,8 +151,8 @@ module Json =
ThemeIdConverter() ThemeIdConverter()
UploadIdConverter() UploadIdConverter()
WebLogIdConverter() WebLogIdConverter()
WebLogUserIdConverter() WebLogUserIdConverter() ]
] |> List.iter ser.Converters.Add |> List.iter ser.Converters.Add
// NodaTime // NodaTime
let _ = ser.ConfigureForNodaTime DateTimeZoneProviders.Tzdb let _ = ser.ConfigureForNodaTime DateTimeZoneProviders.Tzdb
// Handles DUs with no associated data, as well as option fields // Handles DUs with no associated data, as well as option fields

View File

@ -45,4 +45,10 @@
<Compile Include="PostgresData.fs" /> <Compile Include="PostgresData.fs" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>MyWebLog.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project> </Project>

View File

@ -24,14 +24,6 @@ let diffLists<'T, 'U when 'U: equality> oldItems newItems (f: 'T -> 'U) =
let diff compList = fun item -> not (compList |> List.exists (fun other -> f item = f other)) let diff compList = fun item -> not (compList |> List.exists (fun other -> f item = f other))
List.filter (diff newItems) oldItems, List.filter (diff oldItems) newItems List.filter (diff newItems) oldItems, List.filter (diff oldItems) newItems
/// Find meta items added and removed
let diffMetaItems (oldItems: MetaItem list) newItems =
diffLists oldItems newItems (fun item -> $"{item.Name}|{item.Value}")
/// Find the permalinks added and removed
let diffPermalinks (oldLinks: Permalink list) newLinks =
diffLists oldLinks newLinks string
/// Find the revisions added and removed /// Find the revisions added and removed
let diffRevisions (oldRevs: Revision list) newRevs = let diffRevisions (oldRevs: Revision list) newRevs =
diffLists oldRevs newRevs (fun rev -> $"{rev.AsOf.ToUnixTimeTicks()}|{rev.Text}") diffLists oldRevs newRevs (fun rev -> $"{rev.AsOf.ToUnixTimeTicks()}|{rev.Text}")

View File

@ -1,6 +1,7 @@
module ConvertersTests module ConvertersTests
open Expecto open Expecto
open Microsoft.FSharpLu.Json
open MyWebLog open MyWebLog
open MyWebLog.Converters.Json open MyWebLog.Converters.Json
open Newtonsoft.Json open Newtonsoft.Json
@ -243,6 +244,35 @@ let webLogUserIdConverterTests = testList "WebLogUserIdConverter" [
} }
] ]
open NodaTime.Serialization.JsonNet
/// Unit tests for the Json.configure function
let configureTests = test "Json.configure succeeds" {
let has typ (converter: JsonConverter) = converter.GetType() = typ
let ser = configure (JsonSerializer.Create())
Expect.hasCountOf ser.Converters 1u (has typeof<CategoryIdConverter>) "Category ID converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<CommentIdConverter>) "Comment ID converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<CommentStatusConverter>) "Comment status converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<CustomFeedIdConverter>) "Custom feed ID converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<CustomFeedSourceConverter>) "Custom feed source converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<ExplicitRatingConverter>) "Explicit rating converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<MarkupTextConverter>) "Markup text converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<PermalinkConverter>) "Permalink converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<PageIdConverter>) "Page ID converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<PodcastMediumConverter>) "Podcast medium converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<PostIdConverter>) "Post ID converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<TagMapIdConverter>) "Tag map ID converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<ThemeAssetIdConverter>) "Theme asset ID converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<ThemeIdConverter>) "Theme ID converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<UploadIdConverter>) "Upload ID converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<WebLogIdConverter>) "Web log ID converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<WebLogUserIdConverter>) "Web log user ID converter not found"
Expect.hasCountOf ser.Converters 1u (has typeof<CompactUnionJsonConverter>) "F# type converter not found"
Expect.hasCountOf ser.Converters 1u (has (NodaConverters.InstantConverter.GetType())) "NodaTime converter not found"
Expect.equal ser.NullValueHandling NullValueHandling.Ignore "Null handling set incorrectly"
Expect.equal ser.MissingMemberHandling MissingMemberHandling.Ignore "Missing member handling set incorrectly"
}
/// All tests for the Data.Converters file /// All tests for the Data.Converters file
let all = testList "Converters" [ let all = testList "Converters" [
categoryIdConverterTests categoryIdConverterTests
@ -262,4 +292,5 @@ let all = testList "Converters" [
uploadIdConverterTests uploadIdConverterTests
webLogIdConverterTests webLogIdConverterTests
webLogUserIdConverterTests webLogUserIdConverterTests
configureTests
] ]

View File

@ -9,6 +9,7 @@
<Compile Include="DataTypesTests.fs" /> <Compile Include="DataTypesTests.fs" />
<Compile Include="ViewModelsTests.fs" /> <Compile Include="ViewModelsTests.fs" />
<Compile Include="ConvertersTests.fs" /> <Compile Include="ConvertersTests.fs" />
<Compile Include="UtilsTests.fs" />
<Compile Include="Program.fs" /> <Compile Include="Program.fs" />
<Content Include="root-weblog.json" /> <Content Include="root-weblog.json" />
</ItemGroup> </ItemGroup>

View File

@ -2,7 +2,7 @@
let allTests = testList "MyWebLog" [ let allTests = testList "MyWebLog" [
testList "Domain" [ SupportTypesTests.all; DataTypesTests.all; ViewModelsTests.all ] testList "Domain" [ SupportTypesTests.all; DataTypesTests.all; ViewModelsTests.all ]
testList "Data" [ ConvertersTests.all ] testList "Data" [ ConvertersTests.all; UtilsTests.all ]
] ]
[<EntryPoint>] [<EntryPoint>]

View File

@ -0,0 +1,96 @@
module UtilsTests
open Expecto
open MyWebLog
open MyWebLog.Data
open NodaTime
/// Unit tests for the orderByHierarchy function
let orderByHierarchyTests = test "orderByHierarchy succeeds" {
let rawCats =
[ { Category.Empty with Id = CategoryId "a"; Name = "Audio"; Slug = "audio"; ParentId = Some (CategoryId "p") }
{ Category.Empty with
Id = CategoryId "b"
Name = "Breaking"
Description = Some "Breaking News"
Slug = "breaking"
ParentId = Some (CategoryId "n") }
{ Category.Empty with Id = CategoryId "l"; Name = "Local"; Slug = "local"; ParentId = Some (CategoryId "b") }
{ Category.Empty with Id = CategoryId "n"; Name = "News"; Slug = "news" }
{ Category.Empty with Id = CategoryId "p"; Name = "Podcast"; Slug = "podcast" }
{ Category.Empty with Id = CategoryId "v"; Name = "Video"; Slug = "vid"; ParentId = Some (CategoryId "p") } ]
let cats = Utils.orderByHierarchy rawCats None None [] |> List.ofSeq
Expect.equal cats.Length 6 "There should have been 6 categories"
Expect.equal cats[0].Id "n" "The first top-level category should have been News"
Expect.equal cats[0].Slug "news" "Slug for News not filled properly"
Expect.isEmpty cats[0].ParentNames "Parent names for News not filled properly"
Expect.equal cats[1].Id "b" "Breaking should have been just below News"
Expect.equal cats[1].Slug "news/breaking" "Slug for Breaking not filled properly"
Expect.equal cats[1].Name "Breaking" "Name not filled properly"
Expect.equal cats[1].Description (Some "Breaking News") "Description not filled properly"
Expect.equal cats[1].ParentNames [| "News" |] "Parent names for Breaking not filled properly"
Expect.equal cats[2].Id "l" "Local should have been just below Breaking"
Expect.equal cats[2].Slug "news/breaking/local" "Slug for Local not filled properly"
Expect.equal cats[2].ParentNames [| "News"; "Breaking" |] "Parent names for Local not filled properly"
Expect.equal cats[3].Id "p" "Podcast should have been the next top-level category"
Expect.equal cats[3].Slug "podcast" "Slug for Podcast not filled properly"
Expect.isEmpty cats[3].ParentNames "Parent names for Podcast not filled properly"
Expect.equal cats[4].Id "a" "Audio should have been just below Podcast"
Expect.equal cats[4].Slug "podcast/audio" "Slug for Audio not filled properly"
Expect.equal cats[4].ParentNames [| "Podcast" |] "Parent names for Audio not filled properly"
Expect.equal cats[5].Id "v" "Video should have been below Audio"
Expect.equal cats[5].Slug "podcast/vid" "Slug for Video not filled properly"
Expect.equal cats[5].ParentNames [| "Podcast" |] "Parent names for Video not filled properly"
Expect.hasCountOf cats 6u (fun it -> it.PostCount = 0) "All post counts should have been 0"
}
/// Unit tests for the diffLists function
let diffListsTests = testList "diffLists" [
test "succeeds with identical lists" {
let removed, added = Utils.diffLists [ 1; 2; 3 ] [ 1; 2; 3 ] id
Expect.isEmpty removed "There should have been no removed items returned"
Expect.isEmpty added "There should have been no added items returned"
}
test "succeeds with differing lists" {
let removed, added = Utils.diffLists [ 1; 2; 3 ] [ 3; 4; 5 ] string
Expect.equal removed [ 1; 2 ] "Removed items incorrect"
Expect.equal added [ 4; 5 ] "Added items incorrect"
}
]
/// Unit tests for the diffRevisions function
let diffRevisionsTests = testList "diffRevisions" [
test "succeeds with identical lists" {
let oldItems =
[ { AsOf = Noda.epoch + Duration.FromDays 3; Text = Html "<p>test" }
{ AsOf = Noda.epoch; Text = Html "<p>test test" } ]
let newItems =
[ { AsOf = Noda.epoch; Text = Html "<p>test test" }
{ AsOf = Noda.epoch + Duration.FromDays 3; Text = Html "<p>test" } ]
let removed, added = Utils.diffRevisions oldItems newItems
Expect.isEmpty removed "There should have been no removed items returned"
Expect.isEmpty added "There should have been no added items returned"
}
test "succeeds with differing lists" {
let oldItems =
[ { AsOf = Noda.epoch + Duration.FromDays 3; Text = Html "<p>test" }
{ AsOf = Noda.epoch + Duration.FromDays 2; Text = Html "<p>tests" }
{ AsOf = Noda.epoch; Text = Html "<p>test test" } ]
let newItems =
[ { AsOf = Noda.epoch + Duration.FromDays 4; Text = Html "<p>tests" }
{ AsOf = Noda.epoch + Duration.FromDays 3; Text = Html "<p>test" }
{ AsOf = Noda.epoch; Text = Html "<p>test test" } ]
let removed, added = Utils.diffRevisions oldItems newItems
Expect.equal removed.Length 1 "There should be 1 removed item"
Expect.equal removed[0].AsOf (Noda.epoch + Duration.FromDays 2) "Expected removed item incorrect"
Expect.equal added.Length 1 "There should be 1 added item"
Expect.equal added[0].AsOf (Noda.epoch + Duration.FromDays 4) "Expected added item incorrect"
}
]
/// All tests for the Utils file
let all = testList "Utils" [
orderByHierarchyTests
diffListsTests
diffRevisionsTests
]