Skip to content

Commit

Permalink
Add GetTaggedArticlesQueryHandlerTests tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisnabi committed Jun 3, 2024
1 parent 24837c3 commit 4d093fd
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 20 deletions.
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>>;
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)];
}
}
9 changes: 5 additions & 4 deletions src/Blogger.Domain/ArticleAggregate/IArticleRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ public interface IArticleRepository
Task<IReadOnlyCollection<Article>> GetPopularArticlesAsync(int size, CancellationToken cancellationToken);

Task<IReadOnlyCollection<Tag>> GetPopularTagsAsync(int size,CancellationToken cancellationToken);


Task<IReadOnlyCollection<Article>> GetLatestArticlesAsync(Tag tag, CancellationToken cancellationToken);


Task<IReadOnlyList<Tag>> GetTagsAsync(CancellationToken cancellationToken);
Task SaveChangesAsync(CancellationToken cancellationToken);
void Delete(Article draft);
Task<Article?> GetDraftByIdAsync(ArticleId draftId, CancellationToken cancellationToken);

Task<IReadOnlyList<Article>> GetLatestArticlesAsync(Tag tag, CancellationToken cancellationToken);


}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ public async Task<IReadOnlyCollection<Tag>> GetPopularTagsAsync(int size, Cancel

return [.. (await topSizeTags.ToListAsync(cancellationToken)).Select(x => Tag.Create(x.Tag))];
}

public async Task<IReadOnlyCollection<Article>> GetLatestArticlesAsync(Tag tag, CancellationToken cancellationToken)
{
var articles = await bloggerDbContext.Articles
.Where(x => x.Status == ArticleStatus.Published)
.Where(x => x.Tags.Any(x => x.Value == tag.Value))
.OrderByDescending(x => x.PublishedOnUtc)
.ToListAsync(cancellationToken);

return [.. articles];
}




public async Task<IReadOnlyList<Tag>> GetTagsAsync(CancellationToken cancellationToken)
{
Expand All @@ -97,17 +111,6 @@ public async Task<IReadOnlyList<Tag>> GetTagsAsync(CancellationToken cancellatio



public async Task<IReadOnlyList<Article>> GetLatestArticlesAsync(Tag tag, CancellationToken cancellationToken)
{
var que = await bloggerDbContext.Articles
.Where(x => x.Status == ArticleStatus.Published)
.Where(x => x.Tags.Any(x => x.Value == tag.Value))
.OrderByDescending(x => x.PublishedOnUtc)
.ToListAsync(cancellationToken);

return que.ToImmutableList();
}

public async Task SaveChangesAsync(CancellationToken cancellationToken)
{
await bloggerDbContext.SaveChangesAsync(cancellationToken);
Expand Down
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();
}

}

0 comments on commit 4d093fd

Please sign in to comment.