From 4d093fd79ff96f922260d0dca15ac5e2dca7d2fe Mon Sep 17 00:00:00 2001 From: thisisnabi Date: Mon, 3 Jun 2024 12:53:18 +0330 Subject: [PATCH] Add GetTaggedArticlesQueryHandlerTests tests --- .../GetTaggedArticlesQuery.cs | 2 +- .../GetTaggedArticlesQueryHandler.cs | 7 +- .../ArticleAggregate/IArticleRepository.cs | 9 +-- .../Repositories/ArticleRepository.cs | 25 +++---- .../GetTaggedArticlesQueryHandlerTests.cs | 66 +++++++++++++++++++ 5 files changed, 89 insertions(+), 20 deletions(-) create mode 100644 tests/Blogger.IntegrationTests/Articles/GetTaggedArticlesQueryHandlerTests.cs diff --git a/src/Blogger.Application/Articles/GetTaggedArticles/GetTaggedArticlesQuery.cs b/src/Blogger.Application/Articles/GetTaggedArticles/GetTaggedArticlesQuery.cs index eb7c374..f5e3034 100644 --- a/src/Blogger.Application/Articles/GetTaggedArticles/GetTaggedArticlesQuery.cs +++ b/src/Blogger.Application/Articles/GetTaggedArticles/GetTaggedArticlesQuery.cs @@ -1,3 +1,3 @@ namespace Blogger.Application.Articles.GetTaggedArticles; public record GetTaggedArticlesQuery(Tag Tag) - : IRequest>; \ No newline at end of file + : IRequest>; \ No newline at end of file diff --git a/src/Blogger.Application/Articles/GetTaggedArticles/GetTaggedArticlesQueryHandler.cs b/src/Blogger.Application/Articles/GetTaggedArticles/GetTaggedArticlesQueryHandler.cs index ade090d..41ff03a 100644 --- a/src/Blogger.Application/Articles/GetTaggedArticles/GetTaggedArticlesQueryHandler.cs +++ b/src/Blogger.Application/Articles/GetTaggedArticles/GetTaggedArticlesQueryHandler.cs @@ -1,15 +1,14 @@ namespace Blogger.Application.Articles.GetTaggedArticles; public class GetTaggedArticlesQueryHandler(IArticleRepository articleRepository) - : IRequestHandler> + : IRequestHandler> { private readonly IArticleRepository _articleRepository = articleRepository; - public async Task> Handle(GetTaggedArticlesQuery request, CancellationToken cancellationToken) + public async Task> 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)]; } } diff --git a/src/Blogger.Domain/ArticleAggregate/IArticleRepository.cs b/src/Blogger.Domain/ArticleAggregate/IArticleRepository.cs index f6e4011..1be15d9 100644 --- a/src/Blogger.Domain/ArticleAggregate/IArticleRepository.cs +++ b/src/Blogger.Domain/ArticleAggregate/IArticleRepository.cs @@ -17,12 +17,13 @@ public interface IArticleRepository Task> GetPopularArticlesAsync(int size, CancellationToken cancellationToken); Task> GetPopularTagsAsync(int size,CancellationToken cancellationToken); - + + Task> GetLatestArticlesAsync(Tag tag, CancellationToken cancellationToken); + + Task> GetTagsAsync(CancellationToken cancellationToken); Task SaveChangesAsync(CancellationToken cancellationToken); void Delete(Article draft); Task GetDraftByIdAsync(ArticleId draftId, CancellationToken cancellationToken); - - Task> GetLatestArticlesAsync(Tag tag, CancellationToken cancellationToken); - + } diff --git a/src/Blogger.Infrastructure/Persistence/Repositories/ArticleRepository.cs b/src/Blogger.Infrastructure/Persistence/Repositories/ArticleRepository.cs index 2927757..6c7fe65 100644 --- a/src/Blogger.Infrastructure/Persistence/Repositories/ArticleRepository.cs +++ b/src/Blogger.Infrastructure/Persistence/Repositories/ArticleRepository.cs @@ -72,6 +72,20 @@ public async Task> GetPopularTagsAsync(int size, Cancel return [.. (await topSizeTags.ToListAsync(cancellationToken)).Select(x => Tag.Create(x.Tag))]; } + + public async Task> 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> GetTagsAsync(CancellationToken cancellationToken) { @@ -97,17 +111,6 @@ public async Task> GetTagsAsync(CancellationToken cancellatio - public async Task> 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); diff --git a/tests/Blogger.IntegrationTests/Articles/GetTaggedArticlesQueryHandlerTests.cs b/tests/Blogger.IntegrationTests/Articles/GetTaggedArticlesQueryHandlerTests.cs new file mode 100644 index 0000000..98f10e1 --- /dev/null +++ b/tests/Blogger.IntegrationTests/Articles/GetTaggedArticlesQueryHandlerTests.cs @@ -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 +{ + 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(); + } + +}