Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/Web.Tests/Handlers/CreateBlogPostHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,35 @@ public async Task Handle_Success_DoesNotCallInvalidateById()
await _cache.Received(1).InvalidateAllAsync(Arg.Any<CancellationToken>());
await _cache.DidNotReceive().InvalidateByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
}

[Fact]
public async Task Handle_OperationCanceled_Rethrows()
{
// Arrange
var command = new CreateBlogPostCommand("Title", "Content", "Author");
_repo.AddAsync(Arg.Any<BlogPost>(), Arg.Any<CancellationToken>())
.ThrowsAsync(new OperationCanceledException());

// Act
Func<Task> act = () => _handler.Handle(command, CancellationToken.None);

// Assert
await act.Should().ThrowAsync<OperationCanceledException>();
}

[Fact]
public async Task Handle_UnexpectedException_ReturnsUnexpectedErrorResult()
{
// Arrange
var command = new CreateBlogPostCommand("Title", "Content", "Author");
_repo.AddAsync(Arg.Any<BlogPost>(), Arg.Any<CancellationToken>())
.ThrowsAsync(new TimeoutException("db timeout"));

// Act
var result = await _handler.Handle(command, CancellationToken.None);

// Assert
result.Failure.Should().BeTrue();
result.Error.Should().Be("An unexpected error occurred.");
}
}
33 changes: 33 additions & 0 deletions tests/Web.Tests/Handlers/DeleteBlogPostHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,37 @@ public async Task Handle_ConcurrentDelete_ReturnsConcurrencyErrorCode()
result.Failure.Should().BeTrue();
result.ErrorCode.Should().Be(ResultErrorCode.Concurrency);
}

[Fact]
public async Task Handle_OperationCanceled_Rethrows()
{
// Arrange
var id = Guid.NewGuid();
var command = new DeleteBlogPostCommand(id);
_repo.DeleteAsync(id, Arg.Any<CancellationToken>())
.ThrowsAsync(new OperationCanceledException());

// Act
Func<Task> act = () => _handler.Handle(command, CancellationToken.None);

// Assert
await act.Should().ThrowAsync<OperationCanceledException>();
}

[Fact]
public async Task Handle_UnexpectedException_ReturnsUnexpectedErrorResult()
{
// Arrange
var id = Guid.NewGuid();
var command = new DeleteBlogPostCommand(id);
_repo.DeleteAsync(id, Arg.Any<CancellationToken>())
.ThrowsAsync(new TimeoutException("db timeout"));

// Act
var result = await _handler.Handle(command, CancellationToken.None);

// Assert
result.Failure.Should().BeTrue();
result.Error.Should().Be("An unexpected error occurred.");
}
}
70 changes: 70 additions & 0 deletions tests/Web.Tests/Handlers/EditBlogPostHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,74 @@ public async Task HandleGetById_CacheServiceThrows_ReturnsFailResult()
result.Failure.Should().BeTrue();
result.Error.Should().Contain("redis down");
}

[Fact]
public async Task HandleEdit_OperationCanceled_Rethrows()
{
// Arrange
var post = BlogPost.Create("Title", "Content", "Author");
var command = new EditBlogPostCommand(post.Id, "New Title", "New Content");
_repo.GetByIdAsync(post.Id, Arg.Any<CancellationToken>())
.ThrowsAsync(new OperationCanceledException());

// Act
Func<Task> act = () => _handler.Handle(command, CancellationToken.None);

// Assert
await act.Should().ThrowAsync<OperationCanceledException>();
}

[Fact]
public async Task HandleEdit_UnexpectedException_ReturnsUnexpectedErrorResult()
{
// Arrange
var post = BlogPost.Create("Title", "Content", "Author");
var command = new EditBlogPostCommand(post.Id, "New Title", "New Content");
_repo.GetByIdAsync(post.Id, Arg.Any<CancellationToken>())
.ThrowsAsync(new TimeoutException("db timeout"));

// Act
var result = await _handler.Handle(command, CancellationToken.None);

// Assert
result.Failure.Should().BeTrue();
result.Error.Should().Be("An unexpected error occurred.");
}

[Fact]
public async Task HandleGetById_OperationCanceled_Rethrows()
{
// Arrange
var id = Guid.NewGuid();
_cache.GetOrFetchByIdAsync(
id,
Arg.Any<Func<Task<BlogPostDto?>>>(),
Arg.Any<CancellationToken>())
.ThrowsAsync(new OperationCanceledException());

// Act
Func<Task> act = () => _handler.Handle(new GetBlogPostByIdQuery(id), CancellationToken.None);

// Assert
await act.Should().ThrowAsync<OperationCanceledException>();
}

[Fact]
public async Task HandleGetById_UnexpectedException_ReturnsUnexpectedErrorResult()
{
// Arrange
var id = Guid.NewGuid();
_cache.GetOrFetchByIdAsync(
id,
Arg.Any<Func<Task<BlogPostDto?>>>(),
Arg.Any<CancellationToken>())
.ThrowsAsync(new TimeoutException("db timeout"));

// Act
var result = await _handler.Handle(new GetBlogPostByIdQuery(id), CancellationToken.None);

// Assert
result.Failure.Should().BeTrue();
result.Error.Should().Be("An unexpected error occurred.");
}
}
33 changes: 33 additions & 0 deletions tests/Web.Tests/Handlers/GetBlogPostsHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,37 @@ public async Task Handle_CacheServiceThrows_ReturnsFailResult()
result.Failure.Should().BeTrue();
result.Error.Should().Contain("redis down");
}

[Fact]
public async Task Handle_OperationCanceled_Rethrows()
{
// Arrange
_cache.GetOrFetchAllAsync(
Arg.Any<Func<Task<IReadOnlyList<BlogPostDto>>>>(),
Arg.Any<CancellationToken>())
.ThrowsAsync(new OperationCanceledException());

// Act
Func<Task> act = () => _handler.Handle(new GetBlogPostsQuery(), CancellationToken.None);

// Assert
await act.Should().ThrowAsync<OperationCanceledException>();
}

[Fact]
public async Task Handle_UnexpectedException_ReturnsUnexpectedErrorResult()
{
// Arrange
_cache.GetOrFetchAllAsync(
Arg.Any<Func<Task<IReadOnlyList<BlogPostDto>>>>(),
Arg.Any<CancellationToken>())
.ThrowsAsync(new TimeoutException("db timeout"));

// Act
var result = await _handler.Handle(new GetBlogPostsQuery(), CancellationToken.None);

// Assert
result.Failure.Should().BeTrue();
result.Error.Should().Be("An unexpected error occurred.");
}
}
Loading
Loading