From e639b723f1c76c2dff081709f06f371129d212f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=BD=D0=BE=D0=B9=20=D0=B8=D1=88?= =?UTF-8?q?=D0=B0=D0=BA?= <69521267+undrcrxwn@users.noreply.github.com> Date: Sat, 23 Mar 2024 11:46:07 +0300 Subject: [PATCH] fix(discussions): return Not Found if no discussions found by author --- .../Services/DiscussionsRepository.cs | 8 +-- .../Tests/DiscussionsRepositoryTests.cs | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 tests/CrowdParlay.Social.IntegrationTests/Tests/DiscussionsRepositoryTests.cs diff --git a/src/CrowdParlay.Social.Infrastructure.Persistence/Services/DiscussionsRepository.cs b/src/CrowdParlay.Social.Infrastructure.Persistence/Services/DiscussionsRepository.cs index ad29021..abbe333 100644 --- a/src/CrowdParlay.Social.Infrastructure.Persistence/Services/DiscussionsRepository.cs +++ b/src/CrowdParlay.Social.Infrastructure.Persistence/Services/DiscussionsRepository.cs @@ -59,8 +59,8 @@ public async Task> GetAllAsync() } """); - var record = await data.SingleAsync(); - return record[0].Adapt>(); + var records = await data.ToListAsync(); + return records.Select(x => x[0]).Adapt>(); }); } @@ -85,8 +85,8 @@ public async Task> GetByAuthorAsync(Guid authorId) } """, new { authorId = authorId.ToString() }); - var record = await data.SingleAsync(); - return record[0].Adapt>(); + var records = await data.ToListAsync(); + return records.Select(x => x[0]).Adapt>(); }); } diff --git a/tests/CrowdParlay.Social.IntegrationTests/Tests/DiscussionsRepositoryTests.cs b/tests/CrowdParlay.Social.IntegrationTests/Tests/DiscussionsRepositoryTests.cs new file mode 100644 index 0000000..08e83cc --- /dev/null +++ b/tests/CrowdParlay.Social.IntegrationTests/Tests/DiscussionsRepositoryTests.cs @@ -0,0 +1,54 @@ +namespace CrowdParlay.Social.IntegrationTests.Tests; + +public class DiscussionsRepositoryTests(WebApplicationContext context) : IClassFixture +{ + private readonly IServiceProvider _services = context.Services; + + [Fact(DisplayName = "Get discussions by author")] + public async Task GetDiscussionsByAuthor() + { + // Arrange + await using var scope = _services.CreateAsyncScope(); + var authors = scope.ServiceProvider.GetRequiredService(); + var discussions = scope.ServiceProvider.GetRequiredService(); + + var author = await authors.CreateAsync( + id: Guid.NewGuid(), + username: "compartmental", + displayName: "Степной ишак", + avatarUrl: null); + + DiscussionDto[] expected = + [ + await discussions.CreateAsync(author.Id, "Discussion 1", "bla bla bla"), + await discussions.CreateAsync(author.Id, "Discussion 2", "numa numa e") + ]; + + // Act + var response = await discussions.GetByAuthorAsync(author.Id); + + // Assert + response.Should().BeEquivalentTo(expected); + } + + [Fact(DisplayName = "Get discussions by author of no discussions")] + public async Task GetNoDiscussionsByAuthor() + { + // Arrange + await using var scope = _services.CreateAsyncScope(); + var authors = scope.ServiceProvider.GetRequiredService(); + var discussions = scope.ServiceProvider.GetRequiredService(); + + var author = await authors.CreateAsync( + id: Guid.NewGuid(), + username: "compartmental", + displayName: "Степной ишак", + avatarUrl: null); + + // Act + var response = await discussions.GetByAuthorAsync(author.Id); + + // Assert + response.Should().BeEmpty(); + } +} \ No newline at end of file