-
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 GetTaggedArticlesQueryHandlerTests tests
- Loading branch information
1 parent
24837c3
commit 4d093fd
Showing
5 changed files
with
89 additions
and
20 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
src/Blogger.Application/Articles/GetTaggedArticles/GetTaggedArticlesQuery.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.GetTaggedArticles; | ||
public record GetTaggedArticlesQuery(Tag Tag) | ||
: IRequest<IReadOnlyList<GetTaggedArticlesQueryResponse>>; | ||
: IRequest<IReadOnlyCollection<GetTaggedArticlesQueryResponse>>; |
7 changes: 3 additions & 4 deletions
7
src/Blogger.Application/Articles/GetTaggedArticles/GetTaggedArticlesQueryHandler.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.GetTaggedArticles; | ||
|
||
public class GetTaggedArticlesQueryHandler(IArticleRepository articleRepository) | ||
: IRequestHandler<GetTaggedArticlesQuery, IReadOnlyList<GetTaggedArticlesQueryResponse>> | ||
: IRequestHandler<GetTaggedArticlesQuery, IReadOnlyCollection<GetTaggedArticlesQueryResponse>> | ||
{ | ||
private readonly IArticleRepository _articleRepository = articleRepository; | ||
|
||
public async Task<IReadOnlyList<GetTaggedArticlesQueryResponse>> Handle(GetTaggedArticlesQuery request, CancellationToken cancellationToken) | ||
public async Task<IReadOnlyCollection<GetTaggedArticlesQueryResponse>> Handle(GetTaggedArticlesQuery request, CancellationToken cancellationToken) | ||
{ | ||
var articles = await _articleRepository.GetLatestArticlesAsync(request.Tag, cancellationToken); | ||
|
||
return articles.Select(x => (GetTaggedArticlesQueryResponse)x) | ||
.ToImmutableList(); | ||
return [.. articles.Select(x => (GetTaggedArticlesQueryResponse)x)]; | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
tests/Blogger.IntegrationTests/Articles/GetTaggedArticlesQueryHandlerTests.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,66 @@ | ||
using Blogger.Application.Articles.GetTaggedArticles; | ||
using Blogger.Domain.ArticleAggregate; | ||
using Blogger.Infrastructure.Persistence.Repositories; | ||
using Blogger.IntegrationTests.Fixtures; | ||
using FluentAssertions; | ||
|
||
namespace Blogger.IntegrationTests.Articles; | ||
public class GetTaggedArticlesQueryHandlerTests : IClassFixture<BloggerDbContextFixture> | ||
{ | ||
private readonly BloggerDbContextFixture _fixture; | ||
|
||
public GetTaggedArticlesQueryHandlerTests(BloggerDbContextFixture fixture) | ||
{ | ||
_fixture = fixture; | ||
} | ||
|
||
|
||
[Fact] | ||
public async Task Handle_ShouldReturnTaggedArticles_WhenArticlesExist() | ||
{ | ||
// Arrange | ||
var tag = Tag.Create("testTag"); | ||
var articleRepository = new ArticleRepository(_fixture.BuildDbContext(Guid.NewGuid().ToString())); | ||
var sut = new GetTaggedArticlesQueryHandler(articleRepository); | ||
|
||
var article_1 = Article.CreateArticle("Title 1", "Test Body", "Test Summary", [Tag.Create("tag1"), Tag.Create("testTag")]); | ||
var article_2 = Article.CreateArticle("Title 2", "Test Body", "Test Summary", [Tag.Create("tag1"), Tag.Create("tag4")]); | ||
|
||
articleRepository.Add(article_1); | ||
articleRepository.Add(article_2); | ||
|
||
await articleRepository.SaveChangesAsync(CancellationToken.None); | ||
|
||
var request = new GetTaggedArticlesQuery(tag); | ||
|
||
// Act | ||
var response = await sut.Handle(request, CancellationToken.None); | ||
|
||
// Assert | ||
response.Should().NotBeNull(); | ||
response.Should().HaveCount(1); | ||
|
||
response.First().ArticleId.Should().Be(article_1.Id); | ||
response.First().Tags.Should().Contain(tag); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_ShouldReturnEmpty_WhenNoArticlesExist() | ||
{ | ||
// Arrange | ||
var tag = Tag.Create("testTag"); | ||
var articleRepository = new ArticleRepository(_fixture.BuildDbContext(Guid.NewGuid().ToString())); | ||
var sut = new GetTaggedArticlesQueryHandler(articleRepository); | ||
|
||
|
||
var request = new GetTaggedArticlesQuery(tag); | ||
|
||
// Act | ||
var response = await sut.Handle(request, CancellationToken.None); | ||
|
||
// Assert | ||
response.Should().NotBeNull(); | ||
response.Should().BeEmpty(); | ||
} | ||
|
||
} |