11using Dove . Blog . Abstractions ;
2+ using Dove . Blog . Data ;
23using Dove . Blog . Logic ;
34using FluentAssertions ;
45using Microsoft . Extensions . Caching . Memory ;
56using Microsoft . Extensions . Logging ;
67using NSubstitute ;
8+ using System ;
9+ using Xunit ;
710
811namespace 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 = @"---
33100title: First Post
101+ author: tdue21
102+ slug: first-blog-post
103+ posted: 2023-10-01
34104categories:
35105- General
106+ tags:
107+ - blog
108+ - test
36109---
37110Bla bla bla" ;
38111
@@ -41,6 +114,8 @@ public async Task GetCategories_Success_TestAsync()
41114categories:
42115- Yahoo
43116- Recipes
117+ tags:
118+ - blog
44119---
45120Bla bla bla" ;
46121
@@ -49,6 +124,8 @@ public async Task GetCategories_Success_TestAsync()
49124categories:
50125- General
51126- Test
127+ tags:
128+ - test
52129---
53130Bla bla bla" ;
54131}
0 commit comments