Skip to content

Commit

Permalink
Add GetPopularTagsQueryHandlerTests tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisnabi committed Jun 3, 2024
1 parent 2e9418a commit 24837c3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public async Task<IReadOnlyList<GetPopularTagsQueryResponse>>
Handle(GetPopularTagsQuery request, CancellationToken cancellationToken)
{
var tags = await _articleRepository.GetPopularTagsAsync(request.Size, cancellationToken);
return tags.Select(x => new GetPopularTagsQueryResponse(x))
.ToImmutableList();
return [.. tags.Select(x => new GetPopularTagsQueryResponse(x))];
}
}
3 changes: 2 additions & 1 deletion src/Blogger.Domain/ArticleAggregate/IArticleRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public interface IArticleRepository

Task<IReadOnlyCollection<Article>> GetPopularArticlesAsync(int size, CancellationToken cancellationToken);

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

Task<IReadOnlyList<Tag>> GetTagsAsync(CancellationToken cancellationToken);
Task SaveChangesAsync(CancellationToken cancellationToken);
void Delete(Article draft);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ public async Task<IReadOnlyCollection<Article>> GetPopularArticlesAsync(int size

return [.. articles];
}



public async Task<IReadOnlyList<Tag>> GetPopularTagsAsync(int size, CancellationToken cancellationToken)

public async Task<IReadOnlyCollection<Tag>> GetPopularTagsAsync(int size, CancellationToken cancellationToken)
{
var topSizeTags = bloggerDbContext.Articles
.AsNoTracking()
Expand All @@ -72,9 +70,7 @@ public async Task<IReadOnlyList<Tag>> GetPopularTagsAsync(int size, Cancellation
}).OrderByDescending(x => x.Count)
.Take(size);

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

public async Task<IReadOnlyList<Tag>> GetTagsAsync(CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Blogger.Application.Articles.GetPopularTags;
using Blogger.Domain.ArticleAggregate;
using Blogger.Infrastructure.Persistence.Repositories;
using Blogger.IntegrationTests.Fixtures;

using FluentAssertions;

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

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

[Fact]
public async Task Handle_ShouldReturnPopularTags_WhenTagsExist()
{
// Arrange
var articleRepository = new ArticleRepository(_fixture.BuildDbContext(Guid.NewGuid().ToString()));
var sut = new GetPopularTagsQueryHandler(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("tag4")]);
var article_3 = Article.CreateArticle("Title 3", "Test Body", "Test Summary", [Tag.Create("tag4"), Tag.Create("tag2")]);
var article_4 = Article.CreateArticle("Title 4", "Test Body", "Test Summary", [Tag.Create("tag4"), Tag.Create("tag2")]);
var article_5 = Article.CreateArticle("Title 5", "Test Body", "Test Summary", [Tag.Create("tag2"), Tag.Create("tag3")]);

articleRepository.Add(article_1);
articleRepository.Add(article_2);
articleRepository.Add(article_3);
articleRepository.Add(article_4);
articleRepository.Add(article_5);

await articleRepository.SaveChangesAsync(CancellationToken.None);


var request = new GetPopularTagsQuery(2);

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

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

response[0].Tag.Value.Should().Be("tag2");
response[1].Tag.Value.Should().Be("tag4");
}

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


var request = new GetPopularTagsQuery(2);

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

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

0 comments on commit 24837c3

Please sign in to comment.