Skip to content

Commit df3c32c

Browse files
committed
Added more tests and set fallback for BlogPostsPerPage
1 parent 0e6f147 commit df3c32c

File tree

3 files changed

+69
-54
lines changed

3 files changed

+69
-54
lines changed

src/LinkDotNet.Blog.Web/AppConfigurationFactory.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,33 @@ public static class AppConfigurationFactory
88
{
99
public static AppConfiguration Create(IConfiguration config)
1010
{
11-
var social = config.GetSection("Social").Get<Social>();
12-
var introduction = config.GetSection("Introduction").Get<Introduction>();
11+
var social = config.GetSection(nameof(Social)).Get<Social>();
12+
var introduction = config.GetSection(nameof(Introduction)).Get<Introduction>();
1313
var profileInformation = config.GetSection("AboutMeProfileInformation").Get<ProfileInformation>();
1414
var giscus = config.GetSection("Giscus").Get<GiscusConfiguration>();
1515
var disqus = config.GetSection("Disqus").Get<DisqusConfiguration>();
16+
var blogPostPerPage = GetBlogPostPerPage(config[nameof(AppConfiguration.BlogPostsPerPage)]);
1617
var configuration = new AppConfiguration
1718
{
18-
BlogName = config["BlogName"],
19-
BlogBrandUrl = config["BlogBrandUrl"],
19+
BlogName = config[nameof(AppConfiguration.BlogName)],
20+
BlogBrandUrl = config[nameof(AppConfiguration.BlogBrandUrl)],
2021
Social = social,
2122
Introduction = introduction,
22-
ConnectionString = config["ConnectionString"],
23-
DatabaseName = config["DatabaseName"],
24-
BlogPostsPerPage = int.Parse(config["BlogPostsPerPage"]),
23+
ConnectionString = config[nameof(AppConfiguration.ConnectionString)],
24+
DatabaseName = config[nameof(AppConfiguration.DatabaseName)],
25+
BlogPostsPerPage = blogPostPerPage,
2526
ProfileInformation = profileInformation,
2627
GiscusConfiguration = giscus,
2728
DisqusConfiguration = disqus,
28-
KofiToken = config["KofiToken"],
29-
GithubSponsorName = config["GithubSponsorName"],
29+
KofiToken = config[nameof(AppConfiguration.KofiToken)],
30+
GithubSponsorName = config[nameof(AppConfiguration.GithubSponsorName)],
3031
};
3132

3233
return configuration;
3334
}
35+
36+
private static int GetBlogPostPerPage(string configValue)
37+
{
38+
return int.TryParse(configValue, out var blogPostPerPage) ? blogPostPerPage : 10;
39+
}
3440
}

src/LinkDotNet.Blog.Web/Features/ShowBlogPost/Components/DonationSection.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
@if (AppConfiguration.IsGithubSponsorAvailable)
99
{
1010
<GithubSponsor Name="@AppConfiguration.GithubSponsorName"></GithubSponsor>
11-
}
11+
}

tests/LinkDotNet.Blog.UnitTests/Web/AppConfigurationFactoryTests.cs

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@ public class AppConfigurationFactoryTests
1010
public void ShouldMapFromAppConfiguration()
1111
{
1212
var inMemorySettings = new Dictionary<string, string>
13-
{
14-
{ "BlogName", "UnitTest" },
15-
{ "BlogBrandUrl", "http://localhost" },
16-
{ "Social:GithubAccountUrl", "github" },
17-
{ "Social:LinkedInAccountUrl", "linkedIn" },
18-
{ "Social:TwitterAccountUrl", "twitter" },
19-
{ "ConnectionString", "cs" },
20-
{ "DatabaseName", "db" },
21-
{ "Introduction:BackgroundUrl", "someurl" },
22-
{ "Introduction:ProfilePictureUrl", "anotherurl" },
23-
{ "Introduction:Description", "desc" },
24-
{ "BlogPostsPerPage", "5" },
25-
{ "AboutMeProfileInformation:Name", "Steven" },
26-
{ "AboutMeProfileInformation:Heading", "Dev" },
27-
{ "AboutMeProfileInformation:ProfilePictureUrl", "Url" },
28-
{ "Giscus:Repository", "repo" },
29-
{ "Giscus:RepositoryId", "repoid" },
30-
{ "Giscus:Category", "general" },
31-
{ "Giscus:CategoryId", "generalid" },
32-
{ "Disqus:Shortname", "blog" },
33-
{ "KofiToken", "ABC" },
34-
{ "GithubSponsorName", "linkdotnet" },
35-
};
13+
{
14+
{ "BlogName", "UnitTest" },
15+
{ "BlogBrandUrl", "http://localhost" },
16+
{ "Social:GithubAccountUrl", "github" },
17+
{ "Social:LinkedInAccountUrl", "linkedIn" },
18+
{ "Social:TwitterAccountUrl", "twitter" },
19+
{ "ConnectionString", "cs" },
20+
{ "DatabaseName", "db" },
21+
{ "Introduction:BackgroundUrl", "someurl" },
22+
{ "Introduction:ProfilePictureUrl", "anotherurl" },
23+
{ "Introduction:Description", "desc" },
24+
{ "BlogPostsPerPage", "5" },
25+
{ "AboutMeProfileInformation:Name", "Steven" },
26+
{ "AboutMeProfileInformation:Heading", "Dev" },
27+
{ "AboutMeProfileInformation:ProfilePictureUrl", "Url" },
28+
{ "Giscus:Repository", "repo" },
29+
{ "Giscus:RepositoryId", "repoid" },
30+
{ "Giscus:Category", "general" },
31+
{ "Giscus:CategoryId", "generalid" },
32+
{ "Disqus:Shortname", "blog" },
33+
{ "KofiToken", "ABC" },
34+
{ "GithubSponsorName", "linkdotnet" },
35+
};
3636
var configuration = new ConfigurationBuilder()
3737
.AddInMemoryCollection(inMemorySettings)
3838
.Build();
@@ -80,15 +80,14 @@ public void ShouldSetGithubLinkedAccountAccordingToValueSet(
8080
bool twitterAvailable)
8181
{
8282
var inMemorySettings = new Dictionary<string, string>
83-
{
84-
{ "Introduction:BackgroundUrl", "someurl" },
85-
{ "Introduction:ProfilePictureUrl", "anotherurl" },
86-
{ "Introduction:Description", "desc" },
87-
{ "Social:GithubAccountUrl", githubUrl },
88-
{ "Social:LinkedInAccountUrl", linkedInUrl },
89-
{ "Social:TwitterAccountUrl", twitterUrl },
90-
{ "BlogPostsPerPage", "2" },
91-
};
83+
{
84+
{ "Introduction:BackgroundUrl", "someurl" },
85+
{ "Introduction:ProfilePictureUrl", "anotherurl" },
86+
{ "Introduction:Description", "desc" },
87+
{ "Social:GithubAccountUrl", githubUrl },
88+
{ "Social:LinkedInAccountUrl", linkedInUrl },
89+
{ "Social:TwitterAccountUrl", twitterUrl },
90+
};
9291
var configuration = new ConfigurationBuilder()
9392
.AddInMemoryCollection(inMemorySettings)
9493
.Build();
@@ -104,12 +103,11 @@ public void ShouldSetGithubLinkedAccountAccordingToValueSet(
104103
public void ShouldSetIsAboutMeEnabledToFalseWhenNoInformation()
105104
{
106105
var inMemorySettings = new Dictionary<string, string>
107-
{
108-
{ "Introduction:BackgroundUrl", "someurl" },
109-
{ "Introduction:ProfilePictureUrl", "anotherurl" },
110-
{ "Introduction:Description", "desc" },
111-
{ "BlogPostsPerPage", "2" },
112-
};
106+
{
107+
{ "Introduction:BackgroundUrl", "someurl" },
108+
{ "Introduction:ProfilePictureUrl", "anotherurl" },
109+
{ "Introduction:Description", "desc" },
110+
};
113111
var configuration = new ConfigurationBuilder()
114112
.AddInMemoryCollection(inMemorySettings)
115113
.Build();
@@ -123,12 +121,11 @@ public void ShouldSetIsAboutMeEnabledToFalseWhenNoInformation()
123121
public void ShouldSetCommentPluginsToFalseWhenNoInformation()
124122
{
125123
var inMemorySettings = new Dictionary<string, string>
126-
{
127-
{ "Introduction:BackgroundUrl", "someurl" },
128-
{ "Introduction:ProfilePictureUrl", "anotherurl" },
129-
{ "Introduction:Description", "desc" },
130-
{ "BlogPostsPerPage", "2" },
131-
};
124+
{
125+
{ "Introduction:BackgroundUrl", "someurl" },
126+
{ "Introduction:ProfilePictureUrl", "anotherurl" },
127+
{ "Introduction:Description", "desc" },
128+
};
132129
var configuration = new ConfigurationBuilder()
133130
.AddInMemoryCollection(inMemorySettings)
134131
.Build();
@@ -138,4 +135,16 @@ public void ShouldSetCommentPluginsToFalseWhenNoInformation()
138135
appConfiguration.IsGiscusEnabled.Should().BeFalse();
139136
appConfiguration.IsDisqusEnabled.Should().BeFalse();
140137
}
138+
139+
[Fact]
140+
public void ShouldSetDefaultBlogPostPerPageIfNotSet()
141+
{
142+
var configuration = new ConfigurationBuilder()
143+
.AddInMemoryCollection(new Dictionary<string, string>())
144+
.Build();
145+
146+
var appConfiguration = AppConfigurationFactory.Create(configuration);
147+
148+
appConfiguration.BlogPostsPerPage.Should().Be(10);
149+
}
141150
}

0 commit comments

Comments
 (0)