Skip to content

Commit

Permalink
fix(comments): throw NotFoundException if no matching nodes were found
Browse files Browse the repository at this point in the history
  • Loading branch information
undrcrxwn committed Feb 19, 2024
1 parent 26fa72c commit 2ac81c2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ CASE WHEN COUNT(reply) > 0 THEN COLLECT(DISTINCT {
}
""",
new { id = id.ToString() });

if (await data.PeekAsync() is null)
throw new NotFoundException();

var record = await data.SingleAsync();
return record[0].Adapt<CommentDto>();
Expand Down Expand Up @@ -104,6 +107,9 @@ OPTIONAL MATCH (deepReplyAuthor:Author)<-[:AUTHORED_BY]-(deepReply:Comment)-[:RE
count
});

if (await data.PeekAsync() is null)
throw new NotFoundException();

var record = await data.SingleAsync();
return record[0].Adapt<Page<CommentDto>>();
});
Expand Down Expand Up @@ -145,6 +151,9 @@ public async Task<CommentDto> CreateAsync(Guid authorId, Guid discussionId, stri
content
});

if (await data.PeekAsync() is null)
throw new NotFoundException();

var record = await data.SingleAsync();
return record[0].Adapt<CommentDto>();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
using CrowdParlay.Social.Application.Abstractions;
using CrowdParlay.Social.Application.Exceptions;
using CrowdParlay.Social.IntegrationTests.Fixtures;
using Microsoft.Extensions.DependencyInjection;

namespace CrowdParlay.Social.IntegrationTests.Tests;

public class CommentsRepositoryTests : IClassFixture<WebApplicationContext>
{
private readonly HttpClient _client;
private readonly IServiceProvider _services;

public CommentsRepositoryTests(WebApplicationContext context)
{
_client = context.Client;
_services = context.Services;
}
public CommentsRepositoryTests(WebApplicationContext context) => _services = context.Services;

[Fact(DisplayName = "Create comment")]
public async Task CreateComment()
Expand Down Expand Up @@ -112,4 +108,33 @@ public async Task SearchComments()
page.Items.Should().BeEquivalentTo(new[] { comment1, comment2 });
page.Items.First().FirstRepliesAuthors.Should().BeEquivalentTo(new[] { author4, author2, author1 });
}

[Fact(DisplayName = "Get comment with unknown ID")]
public async Task GetComment_WithUnknownId_ThrowsNotFoundException()
{
// Arrange
await using var scope = _services.CreateAsyncScope();
var comments = scope.ServiceProvider.GetRequiredService<ICommentRepository>();

// Act
Func<Task> getComment = async () => await comments.GetByIdAsync(Guid.NewGuid());

// Assert
await getComment.Should().ThrowAsync<NotFoundException>();
}

[Fact(DisplayName = "Create comment with unknown author and discussion")]
public async Task CreateComment_WithUnknownAuthorAndDiscussion_ThrowsNotFoundException()
{
// Arrange
await using var scope = _services.CreateAsyncScope();
var comments = scope.ServiceProvider.GetRequiredService<ICommentRepository>();

// Act
Func<Task> createComment = async () =>
await comments.CreateAsync(Guid.NewGuid(), Guid.NewGuid(), "Comment content");

// Assert
await createComment.Should().ThrowAsync<NotFoundException>();
}
}

0 comments on commit 2ac81c2

Please sign in to comment.