Skip to content

Commit 2930d00

Browse files
committed
Added missing method to IBlogProvider.
Added more tests.
1 parent 3dd2f7b commit 2930d00

3 files changed

Lines changed: 89 additions & 18 deletions

File tree

Source/Dove.Blog.Logic/BlogProvider.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace Dove.Blog.Logic;
1515
public interface IBlogProvider
1616
{
1717
Task<IEnumerable<(string category, int posts)>> GetCategories();
18-
Task<Post> GetPost(string? postTitle);
1918
Task<IEnumerable<string>> GetTags();
19+
Task<Post> GetPost(string? postTitle);
20+
Task<IEnumerable<PostSummary>> GetPosts(string? category = null, params string[] tags);
2021
}
2122

2223
public class BlogProvider(IDataProvider dataProvider, IMemoryCache memoryCache, ILogger<BlogProvider> logger) : IBlogProvider
@@ -97,21 +98,21 @@ public async Task<IEnumerable<PostSummary>> GetPosts(string? category = null, pa
9798
posts = posts.Where(p => p.Categories != null && p.Categories.Contains(category))
9899
.ToList();
99100
}
101+
100102
if (tags.Length > 0)
101103
{
102-
posts = posts.Where(p => p.Tags != null && p.Tags.Intersect(tags).Any())
104+
posts = posts.Where(p => p.Tags!.Any(t => tags.Contains(t)))
103105
.ToList();
104106
}
105107

106-
107-
108108
return posts.Select(p =>
109109
{
110+
var brief = p.Content?.Length > 200 ? p.Content?.Substring(0, 200) : p.Content;
110111
return new PostSummary
111112
{
112113
Slug = p.Slug,
113114
Title = p.Title,
114-
Summary = p.Summary ?? _htmlRegex.Replace(p.Content?.Substring(0, 200) + " ...", string.Empty),
115+
Summary = p.Summary ?? _htmlRegex.Replace(brief + " ...", string.Empty),
115116
Author = p.Author,
116117
Posted = p.Posted,
117118
Updated = p.Updated
@@ -188,10 +189,4 @@ private string Slugify(string text)
188189

189190
return finalized.ToLower();
190191
}
191-
192-
private static string RemoveHTMLTagsCompiled(string html)
193-
{
194-
return _htmlRegex.Replace(html, string.Empty);
195-
}
196-
197192
}
Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
using Dove.Blog.Abstractions;
2+
using Dove.Blog.Data;
23
using Dove.Blog.Logic;
34
using FluentAssertions;
45
using Microsoft.Extensions.Caching.Memory;
56
using Microsoft.Extensions.Logging;
67
using NSubstitute;
8+
using System;
9+
using Xunit;
710

811
namespace Dove.Blog.Tests;
912

10-
public class CategoryProviderTests
13+
public class BlogProviderTests
1114
{
12-
[Fact]
13-
public async Task GetCategories_Success_TestAsync()
15+
private readonly BlogProvider _provider;
16+
17+
public BlogProviderTests()
1418
{
1519
var memoryCache = Substitute.For<IMemoryCache>();
1620
var logger = Substitute.For<ILogger<BlogProvider>>();
@@ -20,19 +24,88 @@ public async Task GetCategories_Success_TestAsync()
2024
fileProvider.ReadPageContent(Arg.Is<string>(x => x == "Posts/First-Blog-Post")).Returns(FirstBlogPost);
2125
fileProvider.ReadPageContent(Arg.Is<string>(x => x == "Posts/Second-Blog-Post")).Returns(SecondBlogPost);
2226
fileProvider.ReadPageContent(Arg.Is<string>(x => x == "Posts/Third-Blog-Post")).Returns(ThirdBlogPost);
23-
24-
var provider = new BlogProvider(fileProvider, memoryCache, logger);
25-
var categories = await provider.GetCategories();
27+
28+
_provider = new BlogProvider(fileProvider, memoryCache, logger);
29+
}
30+
31+
[Fact]
32+
public async Task GetCategories_Success_TestAsync()
33+
{
34+
var categories = await _provider.GetCategories();
2635

2736
categories.Should().NotBeNull();
2837
categories.Should().HaveCount(4);
2938
categories.Should().BeEquivalentTo([("General", 2), ("Yahoo", 1), ("Recipes", 1), ("Test", 1)]);
3039
}
3140

41+
[Fact]
42+
public async Task GetTags_Success_TestAsync()
43+
{
44+
var tags = await _provider.GetTags();
45+
tags.Should().NotBeNull();
46+
tags.Should().HaveCount(2);
47+
tags.Should().BeEquivalentTo(["blog", "test"]);
48+
}
49+
50+
[Fact]
51+
public async Task GetPosts_No_Filters_Success_TestAsync()
52+
{
53+
var posts = await _provider.GetPosts();
54+
posts.Should().NotBeNull();
55+
posts.Should().HaveCount(3);
56+
}
57+
58+
59+
[Fact]
60+
public async Task GetPosts_Filter_On_Category_Success_TestAsync()
61+
{
62+
var posts = await _provider.GetPosts("General");
63+
posts.Should().NotBeNull();
64+
posts.Should().HaveCount(2);
65+
}
66+
67+
68+
[Fact]
69+
public async Task GetPosts_Filter_On_Tags_Success_TestAsync()
70+
{
71+
var posts = await _provider.GetPosts(category: null, "test", "blog");
72+
posts.Should().NotBeNull();
73+
posts.Should().HaveCount(3);
74+
}
75+
76+
[Fact]
77+
public async Task GetPost_Success_TestAsync()
78+
{
79+
var expected = new Post
80+
{
81+
Title = "First Post",
82+
Author = "tdue21",
83+
Slug = "first-blog-post",
84+
Categories = ["General"],
85+
Tags = ["blog", "test"],
86+
Content = "<p>Bla bla bla</p>\n",
87+
Posted = new DateTimeOffset(2023, 10, 1, 0, 0, 0, TimeSpan.Zero),
88+
};
89+
var post = await _provider.GetPost("First-Blog-Post");
90+
post.Should().NotBeNull();
91+
post.Should().BeEquivalentTo(expected, options => options
92+
.Excluding(x => x.Posted)
93+
.Excluding(x => x.Updated)
94+
.Excluding(x => x.Summary));
95+
// Timezone difference can be up to 2 hours
96+
post.Posted.Should().BeCloseTo(expected.Posted, TimeSpan.FromHours(2));
97+
}
98+
3299
private const string FirstBlogPost = @"---
33100
title: First Post
101+
author: tdue21
102+
slug: first-blog-post
103+
posted: 2023-10-01
34104
categories:
35105
- General
106+
tags:
107+
- blog
108+
- test
36109
---
37110
Bla bla bla";
38111

@@ -41,6 +114,8 @@ public async Task GetCategories_Success_TestAsync()
41114
categories:
42115
- Yahoo
43116
- Recipes
117+
tags:
118+
- blog
44119
---
45120
Bla bla bla";
46121

@@ -49,6 +124,8 @@ public async Task GetCategories_Success_TestAsync()
49124
categories:
50125
- General
51126
- Test
127+
tags:
128+
- test
52129
---
53130
Bla bla bla";
54131
}

Source/Dove.Blog.slnx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@
88
<Project Path="Dove.Blog.Logic/Dove.Blog.Logic.csproj" />
99
<Project Path="Dove.Blog.Tests/Dove.Blog.Tests.csproj" Id="01a5c447-5567-4f52-9ba6-3da4908ae3d3" />
1010
<Project Path="Dove.Blog.WebApp/Dove.Blog.WebApp.csproj" />
11-
<Project Path="RazorApp/RazorApp.csproj" Id="e1b9081c-ab4e-1179-9faa-1688b5d6edc4" />
1211
</Solution>

0 commit comments

Comments
 (0)