Add JSON converter tests
This commit is contained in:
1 parent
d1a9606c5c
commit
cfb3c3a287
4 files changed
+269
No files matched your search
@@ -0,0 +1,265 @@
|
|||||||
|
module ConvertersTests
|
||||||
|
|
||||||
|
open Expecto
|
||||||
|
open MyWebLog
|
||||||
|
open MyWebLog.Converters.Json
|
||||||
|
open Newtonsoft.Json
|
||||||
|
|
||||||
|
/// Unit tests for the CategoryIdConverter type
|
||||||
|
let categoryIdConverterTests = testList "CategoryIdConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(CategoryIdConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(CategoryId "test-cat-id", opts)
|
||||||
|
Expect.equal after "\"test-cat-id\"" "Category ID serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<CategoryId>("\"test-cat-id\"", opts)
|
||||||
|
Expect.equal after (CategoryId "test-cat-id") "Category ID not serialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the CommentIdConverter type
|
||||||
|
let commentIdConverterTests = testList "CommentIdConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(CommentIdConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(CommentId "test-id", opts)
|
||||||
|
Expect.equal after "\"test-id\"" "Comment ID serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<CommentId>("\"my-test\"", opts)
|
||||||
|
Expect.equal after (CommentId "my-test") "Comment ID deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the CommentStatusConverter type
|
||||||
|
let commentStatusConverterTests = testList "CommentStatusConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(CommentStatusConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(Approved, opts)
|
||||||
|
Expect.equal after "\"Approved\"" "Comment status serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<CommentStatus>("\"Spam\"", opts)
|
||||||
|
Expect.equal after Spam "Comment status deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the CustomFeedIdConverter type
|
||||||
|
let customFeedIdConverterTests = testList "CustomFeedIdConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(CustomFeedIdConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(CustomFeedId "my-feed", opts)
|
||||||
|
Expect.equal after "\"my-feed\"" "Custom feed ID serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<CustomFeedId>("\"feed-me\"", opts)
|
||||||
|
Expect.equal after (CustomFeedId "feed-me") "Custom feed ID deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the CustomFeedSourceConverter type
|
||||||
|
let customFeedSourceConverterTests = testList "CustomFeedSourceConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(CustomFeedSourceConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(Category (CategoryId "abc-123"), opts)
|
||||||
|
Expect.equal after "\"category:abc-123\"" "Custom feed source serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<CustomFeedSource>("\"tag:testing\"", opts)
|
||||||
|
Expect.equal after (Tag "testing") "Custom feed source deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the ExplicitRating type
|
||||||
|
let explicitRatingConverterTests = testList "ExplicitRatingConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(ExplicitRatingConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(Yes, opts)
|
||||||
|
Expect.equal after "\"yes\"" "Explicit rating serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<ExplicitRating>("\"clean\"", opts)
|
||||||
|
Expect.equal after Clean "Explicit rating deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the MarkupText type
|
||||||
|
let markupTextConverterTests = testList "MarkupTextConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(MarkupTextConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(Html "<h4>test</h4>", opts)
|
||||||
|
Expect.equal after "\"HTML: <h4>test</h4>\"" "Markup text serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<MarkupText>("\"Markdown: #### test\"", opts)
|
||||||
|
Expect.equal after (Markdown "#### test") "Markup text deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the PermalinkConverter type
|
||||||
|
let permalinkConverterTests = testList "PermalinkConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(PermalinkConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(Permalink "2022/test", opts)
|
||||||
|
Expect.equal after "\"2022/test\"" "Permalink serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<Permalink>("\"2023/unit.html\"", opts)
|
||||||
|
Expect.equal after (Permalink "2023/unit.html") "Permalink deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the PageIdConverter type
|
||||||
|
let pageIdConverterTests = testList "PageIdConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(PageIdConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(PageId "test-page", opts)
|
||||||
|
Expect.equal after "\"test-page\"" "Page ID serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<PageId>("\"page-test\"", opts)
|
||||||
|
Expect.equal after (PageId "page-test") "Page ID deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the PodcastMedium type
|
||||||
|
let podcastMediumConverterTests = testList "PodcastMediumConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(PodcastMediumConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(Audiobook, opts)
|
||||||
|
Expect.equal after "\"audiobook\"" "Podcast medium serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<PodcastMedium>("\"newsletter\"", opts)
|
||||||
|
Expect.equal after Newsletter "Podcast medium deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the PostIdConverter type
|
||||||
|
let postIdConverterTests = testList "PostIdConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(PostIdConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(PostId "test-post", opts)
|
||||||
|
Expect.equal after "\"test-post\"" "Post ID serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<PostId>("\"post-test\"", opts)
|
||||||
|
Expect.equal after (PostId "post-test") "Post ID deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the TagMapIdConverter type
|
||||||
|
let tagMapIdConverterTests = testList "TagMapIdConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(TagMapIdConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(TagMapId "test-map", opts)
|
||||||
|
Expect.equal after "\"test-map\"" "Tag map ID serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<TagMapId>("\"map-test\"", opts)
|
||||||
|
Expect.equal after (TagMapId "map-test") "Tag map ID deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the ThemeAssetIdConverter type
|
||||||
|
let themeAssetIdConverterTests = testList "ThemeAssetIdConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(ThemeAssetIdConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(ThemeAssetId (ThemeId "test", "unit.jpg"), opts)
|
||||||
|
Expect.equal after "\"test/unit.jpg\"" "Theme asset ID serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<ThemeAssetId>("\"theme/test.png\"", opts)
|
||||||
|
Expect.equal after (ThemeAssetId (ThemeId "theme", "test.png")) "Theme asset ID deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the ThemeIdConverter type
|
||||||
|
let themeIdConverterTests = testList "ThemeIdConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(ThemeIdConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(ThemeId "test-theme", opts)
|
||||||
|
Expect.equal after "\"test-theme\"" "Theme ID serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<ThemeId>("\"theme-test\"", opts)
|
||||||
|
Expect.equal after (ThemeId "theme-test") "Theme ID deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the UploadIdConverter type
|
||||||
|
let uploadIdConverterTests = testList "UploadIdConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(UploadIdConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(UploadId "test-up", opts)
|
||||||
|
Expect.equal after "\"test-up\"" "Upload ID serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<UploadId>("\"up-test\"", opts)
|
||||||
|
Expect.equal after (UploadId "up-test") "Upload ID deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the WebLogIdConverter type
|
||||||
|
let webLogIdConverterTests = testList "WebLogIdConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(WebLogIdConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(WebLogId "test-web", opts)
|
||||||
|
Expect.equal after "\"test-web\"" "Web log ID serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<WebLogId>("\"web-test\"", opts)
|
||||||
|
Expect.equal after (WebLogId "web-test") "Web log ID deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// Unit tests for the WebLogUserIdConverter type
|
||||||
|
let webLogUserIdConverterTests = testList "WebLogUserIdConverter" [
|
||||||
|
let opts = JsonSerializerSettings()
|
||||||
|
opts.Converters.Add(WebLogUserIdConverter())
|
||||||
|
test "succeeds when serializing" {
|
||||||
|
let after = JsonConvert.SerializeObject(WebLogUserId "test-user", opts)
|
||||||
|
Expect.equal after "\"test-user\"" "Web log user ID serialized incorrectly"
|
||||||
|
}
|
||||||
|
test "succeeds when deserializing" {
|
||||||
|
let after = JsonConvert.DeserializeObject<WebLogUserId>("\"user-test\"", opts)
|
||||||
|
Expect.equal after (WebLogUserId "user-test") "Web log user ID deserialized incorrectly"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
/// All tests for the Data.Converters file
|
||||||
|
let all = testList "Converters" [
|
||||||
|
categoryIdConverterTests
|
||||||
|
commentIdConverterTests
|
||||||
|
commentStatusConverterTests
|
||||||
|
customFeedIdConverterTests
|
||||||
|
customFeedSourceConverterTests
|
||||||
|
explicitRatingConverterTests
|
||||||
|
markupTextConverterTests
|
||||||
|
permalinkConverterTests
|
||||||
|
pageIdConverterTests
|
||||||
|
podcastMediumConverterTests
|
||||||
|
postIdConverterTests
|
||||||
|
tagMapIdConverterTests
|
||||||
|
themeAssetIdConverterTests
|
||||||
|
themeIdConverterTests
|
||||||
|
uploadIdConverterTests
|
||||||
|
webLogIdConverterTests
|
||||||
|
webLogUserIdConverterTests
|
||||||
|
]
|
||||||
@@ -8,7 +8,9 @@
|
|||||||
<Compile Include="SupportTypesTests.fs" />
|
<Compile Include="SupportTypesTests.fs" />
|
||||||
<Compile Include="DataTypesTests.fs" />
|
<Compile Include="DataTypesTests.fs" />
|
||||||
<Compile Include="ViewModelsTests.fs" />
|
<Compile Include="ViewModelsTests.fs" />
|
||||||
|
<Compile Include="ConvertersTests.fs" />
|
||||||
<Compile Include="Program.fs" />
|
<Compile Include="Program.fs" />
|
||||||
|
<Content Include="root-weblog.json" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -2,6 +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 ]
|
||||||
]
|
]
|
||||||
|
|
||||||
[<EntryPoint>]
|
[<EntryPoint>]
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
{"WebLog":{"Id":"uSitJEuD3UyzWC9jgOHc8g","Name":"Root WebLog","Slug":"root-weblog","Subtitle":"This is the main one","DefaultPage":"posts","PostsPerPage":9,"ThemeId":"default","UrlBase":"http://localhost:8081","TimeZone":"America/Denver","Rss":{"IsFeedEnabled":true,"FeedName":"feed","ItemsInFeed":7,"IsCategoryEnabled":true,"IsTagEnabled":true,"Copyright":"CC40-NC-BY","CustomFeeds":[{"Id":"isPQ6drbDEydxohQzaiYtQ","Source":"tag:podcast","Path":"podcast-feed","Podcast":{"Title":"Root Podcast","ItemsInFeed":23,"Summary":"All things that happen in the domain root","DisplayedAuthor":"Podcaster Extraordinaire","Email":"podcaster@example.com","ImageUrl":"images/cover-art.png","AppleCategory":"Fiction","AppleSubcategory":"Drama","Explicit":"no","DefaultMediaType":"audio/mpeg","MediaBaseUrl":"https://media.example.com/root/","PodcastGuid":"10fd7f79-c719-4e1d-9da7-10405dd4fd96","FundingUrl":"https://example.com/support-us","FundingText":"Support Our Work","Medium":"newsletter"}}]},"AutoHtmx":true,"Uploads":"Database","RedirectRules":[]},"Users":[{"Id":"5EM2rimH9kONpmd2zQkiVA","WebLogId":"uSitJEuD3UyzWC9jgOHc8g","Email":"root@example.com","FirstName":"Root","LastName":"Owner","PreferredName":"Admin","PasswordHash":"AQAAAAIAAYagAAAAEEnq9J9lKZoMQZaTOJHKIQo44skDdzDigzqS+o6myMop38YuHfm/vNs9b/WpYjsOxg==","AccessLevel":"Administrator","CreatedOn":"2024-01-20T21:49:03Z","LastSeenOn":"2024-01-20T22:25:03Z"},{"Id":"GPbJaSOwTkKt14ZKYyveKA","WebLogId":"uSitJEuD3UyzWC9jgOHc8g","Email":"editor@example.com","FirstName":"Ed","LastName":"It-Or","PreferredName":"Edits","PasswordHash":"AQAAAAIAAYagAAAAEA8E3NwJkZO+q35FTmUT0wMNB8IpBOSVACKQcccXpaWfZJMRmZzjPEzd4j/f9h+rEA==","AccessLevel":"Editor","CreatedOn":"2024-01-20T21:58:42Z"},{"Id":"iIRNLSeY0EanxRPyqGuwVg","WebLogId":"uSitJEuD3UyzWC9jgOHc8g","Email":"author@example.com","FirstName":"Author","LastName":"Dude","PreferredName":"Mister","PasswordHash":"AQAAAAIAAYagAAAAEBYNf1sR/pjaX2dZgqlvpH/Tqpz2h/CG3rsk/wH2ReTysjpK/gxSqht7IULWXM7KHQ==","Url":"https://example.com/author","AccessLevel":"Author","CreatedOn":"2024-01-20T21:53:27Z"}],"Theme":{"Id":"default","Name":"myWebLog Default Theme","Version":"2.1.0","Templates":[{"Name":"single-post","Text":"{%- assign post = model.posts | first -%}\n<h1>{{ post.title }}</h1>\n<h4 class=\"item-meta text-muted\">\n {% if post.published_on -%}\n Published {{ post.published_on | date: \"dddd, MMMM d, yyyy\" }}\n at {{ post.published_on | date: \"h:mm tt\" | downcase }}\n {%- else -%}\n **DRAFT**\n {% endif %}\n by {{ model.authors | value: post.author_id }}\n {%- if is_editor or is_author and user_id == post.author_id %}\n • <a hx-boost=false href=\"{{ post | edit_post_link }}\">Edit Post</a>\n {%- endif %}\n</h4>\n<div>\n <article class=\"container mt-3\">\n <div>{{ post.text }}</div>\n {%- assign cat_count = post.category_ids | size -%}\n {% if cat_count > 0 -%}\n <h4 class=\"item-meta text-muted\">\n Categorized under\n {% for cat_id in post.category_ids -%}\n {% assign cat = categories | where: \"Id\", cat_id | first %}\n <span class=text-nowrap>\n <a href=\"{{ cat | category_link }}\" title=\"Categorized under “{{ cat.name | escape }}”\">\n {{ cat.name }}\n </a>\n </span>\n {% unless forloop.last %} • {% endunless %}\n {%- endfor %}\n </h4>\n {%- endif %}\n {%- assign tag_count = post.tags | size -%}\n {% if tag_count > 0 -%}\n <h4 class=\"item-meta text-muted\">\n Tagged\n {% for tag in post.tags %}\n <span class=text-nowrap>\n <a href=\"{{ tag | tag_link }}\" title=\"Posts tagged “{{ tag | escape }}”\" rel=\"tag\">{{ tag }}</a>\n </span>\n {% unless forloop.last %} • {% endunless %}\n {%- endfor %}\n </h4>\n {%- endif %}\n </article>\n <div>\n <nav aria-label=pagination>\n <ul class=\"pagination justify-content-evenly mt-5\">\n {% if model.newer_link -%}\n <li class=page-item>\n <a class=page-link href=\"{{ model.newer_link.value | relative_link }}\">{{ model.newer_name.value }}</a>\n <span class=text-muted>« Previous Post</span>\n {% endif %}\n {% if model.older_link -%}\n <li class=\"page-item text-end\">\n <a class=page-link href=\"{{ model.older_link.value | relative_link }}\">{{ model.older_name.value }}</a>\n <span class=text-muted>Next Post »</span>\n {%- endif -%}\n </ul>\n </nav>\n </div>\n</div>\n"},{"Name":"single-page","Text":"<h2 class=py-3>{{ page.title }}</h2>\n<article class=\"container\">{{ page.text }}</article>\n"},{"Name":"layout","Text":"<!DOCTYPE html>\n<html lang=en>\n <meta charset=utf-8>\n <meta name=viewport content=\"width=device-width\">\n <link rel=stylesheet href=https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css\n integrity=sha384-1BmE4kWBq78iYhFldvKLine truncated
|
||||||
Reference in new issue
Block a user