Skip to content

Commit

Permalink
Add GetTagsQueryHandlerTests tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisnabi committed Jun 3, 2024
1 parent 4d093fd commit d83a3de
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Blogger.Application/Articles/GetTags/GetTagsQuery.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace Blogger.Application.Articles.GetTags;
public record GetTagsQuery()
: IRequest<IReadOnlyList<GetTagsQueryResponse>>;
: IRequest<IReadOnlyCollection<GetTagsQueryResponse>>;
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
namespace Blogger.Application.Articles.GetTags;

public class GetTagsQueryHandler(IArticleRepository articleRepository)
: IRequestHandler<GetTagsQuery, IReadOnlyList<GetTagsQueryResponse>>
: IRequestHandler<GetTagsQuery, IReadOnlyCollection<GetTagsQueryResponse>>
{
private readonly IArticleRepository _articleRepository = articleRepository;

public async Task<IReadOnlyList<GetTagsQueryResponse>> Handle(GetTagsQuery request, CancellationToken cancellationToken)
public async Task<IReadOnlyCollection<GetTagsQueryResponse>> Handle(GetTagsQuery request, CancellationToken cancellationToken)
{
var tags = await _articleRepository.GetTagsAsync(cancellationToken);

return tags.GroupBy(x => x)
.Select(x => new GetTagsQueryResponse(x.Key, x.Count()))
.ToImmutableList();
return [.. tags.Select(x => new GetTagsQueryResponse(x.Tag, x.Count))];
}
}
2 changes: 1 addition & 1 deletion src/Blogger.Domain/ArticleAggregate/IArticleRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public interface IArticleRepository

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

Task<IReadOnlyCollection<TagModel>> GetTagsAsync(CancellationToken cancellationToken);

Task<IReadOnlyList<Tag>> GetTagsAsync(CancellationToken cancellationToken);
Task SaveChangesAsync(CancellationToken cancellationToken);
void Delete(Article draft);
Task<Article?> GetDraftByIdAsync(ArticleId draftId, CancellationToken cancellationToken);
Expand Down
2 changes: 2 additions & 0 deletions src/Blogger.Domain/ArticleAggregate/Models/TagModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
namespace Blogger.Domain.ArticleAggregate.Models;
public sealed record TagModel(Tag Tag, int Count);
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,18 @@ public async Task<IReadOnlyCollection<Article>> GetLatestArticlesAsync(Tag tag,

return [.. articles];
}




public async Task<IReadOnlyList<Tag>> GetTagsAsync(CancellationToken cancellationToken)

public async Task<IReadOnlyCollection<TagModel>> GetTagsAsync(CancellationToken cancellationToken)
{
var tags = await bloggerDbContext.Articles
.AsNoTracking()
.Where(x => x.Status == ArticleStatus.Published)
.SelectMany(x => x.Tags)
.GroupBy(x => x.Value)
.Select(x => new TagModel(Tag.Create(x.Key), x.Count()))
.ToListAsync(cancellationToken);

return tags.ToImmutableList();
return[.. tags];
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System.Collections.Immutable;
using Blogger.Application.Articles.GetTags;
using Blogger.Domain.ArticleAggregate;
using Blogger.Infrastructure.Persistence.Repositories;
using Blogger.IntegrationTests.Fixtures;

using FluentAssertions;

namespace Blogger.IntegrationTests.Articles;
public class GetTagsQueryHandlerTests : IClassFixture<BloggerDbContextFixture>
{
private readonly BloggerDbContextFixture _fixture;

public GetTagsQueryHandlerTests(BloggerDbContextFixture fixture)
{
_fixture = fixture;
}

[Fact]
public async Task Handle_ShouldReturnTags_WhenTagsExist()
{
// Arrange
var articleRepository = new ArticleRepository(_fixture.BuildDbContext(Guid.NewGuid().ToString()));
var sut = new GetTagsQueryHandler(articleRepository);

var article_1 = Article.CreateArticle("Title 1", "Test Body", "Test Summary", [Tag.Create("tag1"), Tag.Create("tag3")]);
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 GetTagsQuery();

// Act
var response = (await sut.Handle(request, CancellationToken.None)).ToImmutableList();

// Assert
response.Should().NotBeNull();
response.Should().HaveCount(3);

response[0].Tag.Value.Should().Be("tag1");
response[0].Count.Should().Be(2);

response[1].Tag.Value.Should().Be("tag3");
response[1].Count.Should().Be(1);

response[2].Tag.Value.Should().Be("tag4");
response[2].Count.Should().Be(1);
}

[Fact]
public async Task Handle_ShouldReturnEmpty_WhenNoTagsExist()
{
// Arrange
var articleRepository = new ArticleRepository(_fixture.BuildDbContext(Guid.NewGuid().ToString()));
var sut = new GetTagsQueryHandler(articleRepository);

var request = new GetTagsQuery();

// Act
var response = await sut.Handle(request, CancellationToken.None);

// Assert
response.Should().NotBeNull();
response.Should().BeEmpty();
}
}

0 comments on commit d83a3de

Please sign in to comment.