-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GetPopularArticlesHandlerTests test
- Loading branch information
1 parent
5201fbd
commit 2e9418a
Showing
6 changed files
with
125 additions
and
15 deletions.
There are no files selected for viewing
7 changes: 3 additions & 4 deletions
7
src/Blogger.Application/Articles/GetPopularArticles/GetPopularArticlesHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
namespace Blogger.Application.Articles.GetPopularArticles; | ||
|
||
public class GetPopularArticlesHandler(IArticleRepository articleRepository) | ||
: IRequestHandler<GetPopularArticlesQuery, IReadOnlyList<GetPopularArticlesQueryResponse>> | ||
: IRequestHandler<GetPopularArticlesQuery, IReadOnlyCollection<GetPopularArticlesQueryResponse>> | ||
{ | ||
private readonly IArticleRepository _articleRepository = articleRepository; | ||
|
||
public async Task<IReadOnlyList<GetPopularArticlesQueryResponse>> Handle(GetPopularArticlesQuery request, CancellationToken cancellationToken) | ||
public async Task<IReadOnlyCollection<GetPopularArticlesQueryResponse>> Handle(GetPopularArticlesQuery request, CancellationToken cancellationToken) | ||
{ | ||
var articles = await _articleRepository.GetPopularArticlesAsync(request.Size, cancellationToken); | ||
|
||
return articles.Select(x => new GetPopularArticlesQueryResponse(x.Id, x.Title)) | ||
.ToImmutableList(); | ||
return [.. articles.Select(x => new GetPopularArticlesQueryResponse(x.Id, x.Title))]; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/Blogger.Application/Articles/GetPopularArticles/GetPopularArticlesQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
namespace Blogger.Application.Articles.GetPopularArticles; | ||
public record GetPopularArticlesQuery(int Size) | ||
: IRequest<IReadOnlyList<GetPopularArticlesQueryResponse>>; | ||
: IRequest<IReadOnlyCollection<GetPopularArticlesQueryResponse>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
tests/Blogger.IntegrationTests/Articles/GetPopularArticlesHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using Blogger.Application.Articles.GetPopularArticles; | ||
using Blogger.Domain.ArticleAggregate; | ||
using Blogger.Infrastructure.Persistence.Repositories; | ||
using Blogger.IntegrationTests.Fixtures; | ||
|
||
using FluentAssertions; | ||
|
||
namespace Blogger.IntegrationTests.Articles; | ||
public class GetPopularArticlesHandlerTests : IClassFixture<BloggerDbContextFixture> | ||
{ | ||
private readonly BloggerDbContextFixture _fixture; | ||
|
||
public GetPopularArticlesHandlerTests(BloggerDbContextFixture fixture) | ||
{ | ||
_fixture = fixture; | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_ShouldReturnPopularArticles_WhenArticlesExist() | ||
{ | ||
// Arrange | ||
var articleRepository = new ArticleRepository(_fixture.BuildDbContext(Guid.NewGuid().ToString())); | ||
var _sut = new GetPopularArticlesHandler(articleRepository); | ||
|
||
var article_1 = Article.CreateArticle("Title 1", "Test Body", "Test Summary", [Tag.Create("tag1"), Tag.Create("tag2")]); | ||
var article_2 = Article.CreateArticle("Title 2", "Test Body", "Test Summary", [Tag.Create("tag1"), Tag.Create("tag2")]); | ||
var article_3 = Article.CreateArticle("Title 3", "Test Body", "Test Summary", [Tag.Create("tag1"), Tag.Create("tag2")]); | ||
|
||
var like_1 = Like.Create("127.0.0.1", DateTime.UtcNow); | ||
var like_2 = Like.Create("127.0.0.2", DateTime.UtcNow); | ||
var like_3 = Like.Create("127.0.0.3", DateTime.UtcNow); | ||
var like_4 = Like.Create("127.0.0.4", DateTime.UtcNow); | ||
var like_5 = Like.Create("127.0.0.1", DateTime.UtcNow); | ||
var like_6 = Like.Create("127.0.0.2", DateTime.UtcNow); | ||
var like_7 = Like.Create("127.0.0.3", DateTime.UtcNow); | ||
var like_8 = Like.Create("127.0.0.4", DateTime.UtcNow); | ||
var like_9 = Like.Create("127.0.0.1", DateTime.UtcNow); | ||
|
||
article_1.Like(like_1); | ||
article_1.Like(like_2); | ||
articleRepository.Add(article_1); | ||
|
||
article_2.Like(like_3); | ||
article_2.Like(like_4); | ||
article_2.Like(like_5); | ||
articleRepository.Add(article_2); | ||
|
||
article_3.Like(like_6); | ||
article_3.Like(like_7); | ||
article_3.Like(like_8); | ||
article_3.Like(like_9); | ||
articleRepository.Add(article_3); | ||
await articleRepository.SaveChangesAsync(CancellationToken.None); | ||
|
||
var request = new GetPopularArticlesQuery(2); | ||
|
||
// Act | ||
var response = await _sut.Handle(request, CancellationToken.None); | ||
|
||
// Assert | ||
response.Should().NotBeNull(); | ||
response.Should().HaveCount(2); | ||
|
||
var firstArticleResponse = response.First(); | ||
firstArticleResponse.ArticleId.Should().Be(article_3.Id); | ||
|
||
var secondArticleResponse = response.Last(); | ||
secondArticleResponse.ArticleId.Should().Be(article_2.Id); | ||
} | ||
|
||
|
||
[Fact] | ||
public async Task Handle_ShouldReturnEmpty_WhenNoArticlesExist() | ||
{ | ||
// Arrange | ||
var articleRepository = new ArticleRepository(_fixture.BuildDbContext(Guid.NewGuid().ToString())); | ||
var _sut = new GetPopularArticlesHandler(articleRepository); | ||
|
||
var request = new GetPopularArticlesQuery(2); | ||
|
||
// Act | ||
var response = await _sut.Handle(request, CancellationToken.None); | ||
|
||
// Assert | ||
response.Should().NotBeNull(); | ||
response.Should().BeEmpty(); | ||
} | ||
|
||
|
||
} |