-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#169) Application: add event handler tests
- Loading branch information
1 parent
32a5fab
commit c90332c
Showing
6 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...ions/src/MiniSpace.Services.Reactions.Application/Exceptions/PostAlreadyAddedException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace MiniSpace.Services.Reactions.Application.Exceptions | ||
{ | ||
public class PostAlreadyAddedException : AppException | ||
{ | ||
public override string Code { get; } = "post_already_added"; | ||
public Guid PostId { get; } | ||
Check warning on line 6 in MiniSpace.Services.Reactions/src/MiniSpace.Services.Reactions.Application/Exceptions/PostAlreadyAddedException.cs
|
||
|
||
public PostAlreadyAddedException(Guid postId) | ||
: base($"Post with id: {postId} was already added.") | ||
{ | ||
PostId = postId; | ||
} | ||
} | ||
} |
Empty file.
Empty file.
64 changes: 64 additions & 0 deletions
64
...rvices.Reactions.Application.UnitTests/Events/External/Handlers/PostCreatedHandlerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Xunit; | ||
using Moq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using MiniSpace.Services.Reactions.Application.Exceptions; | ||
using MiniSpace.Services.Reactions.Application.Services; | ||
using MiniSpace.Services.Reactions.Core.Entities; | ||
using MiniSpace.Services.Reactions.Core.Repositories; | ||
using MiniSpace.Services.Reactions.Application.Commands.Handlers; | ||
using MiniSpace.Services.Reactions.Application.Commands; | ||
using MiniSpace.Services.Reactions.Infrastructure.Contexts; | ||
using System.Threading; | ||
using FluentAssertions; | ||
using MiniSpace.Services.Reactions.Application.Events.External.Handlers; | ||
using MiniSpace.Services.Reactions.Application.Events.External; | ||
using System.ComponentModel.Design; | ||
using Convey.CQRS.Commands; | ||
using MiniSpace.Services.Reactions.Application.Events; | ||
|
||
namespace MiniSpace.Services.Reactions.Application.UnitTests.Events.External.Handlers | ||
{ | ||
public class PostCreatedHandlerTest | ||
{ | ||
private readonly PostCreatedHandler _postDeletedHandler; | ||
private readonly Mock<IPostRepository> _postRepositoryMock; | ||
|
||
public PostCreatedHandlerTest() | ||
{ | ||
_postRepositoryMock = new(); | ||
_postDeletedHandler = new PostCreatedHandler(_postRepositoryMock.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task HandleAsync_ValidData_ShouldNotThrowExeption() | ||
{ | ||
// Arrange | ||
var postId = Guid.NewGuid(); | ||
var post = new PostCreated(postId); | ||
|
||
_postRepositoryMock.Setup(repo => repo.ExistsAsync(postId)) | ||
.ReturnsAsync(false); | ||
|
||
// Act & Assert | ||
Func<Task> act = async () => await _postDeletedHandler.HandleAsync(post, new CancellationToken()); | ||
await act.Should().NotThrowAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task HandleAsync_PostAlreadyCreated_ShouldThrowPostAlreadyExistsException() | ||
{ | ||
// Arrange | ||
var postId = Guid.NewGuid(); | ||
var post = new PostCreated(postId); | ||
|
||
_postRepositoryMock.Setup(repo => repo.ExistsAsync(postId)) | ||
.ReturnsAsync(true); | ||
|
||
// Act & Assert | ||
Func<Task> act = async () => await _postDeletedHandler.HandleAsync(post, new CancellationToken()); | ||
await act.Should().ThrowAsync<PostAlreadyAddedException>(); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...ces.Reactions.Application.UnitTests/Events/External/Handlers/StudentCreatedHandlerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Xunit; | ||
using Moq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using MiniSpace.Services.Reactions.Application.Exceptions; | ||
using MiniSpace.Services.Reactions.Application.Services; | ||
using MiniSpace.Services.Reactions.Core.Entities; | ||
using MiniSpace.Services.Reactions.Core.Repositories; | ||
using MiniSpace.Services.Reactions.Application.Commands.Handlers; | ||
using MiniSpace.Services.Reactions.Application.Commands; | ||
using MiniSpace.Services.Reactions.Infrastructure.Contexts; | ||
using System.Threading; | ||
using FluentAssertions; | ||
using MiniSpace.Services.Reactions.Application.Events.External.Handlers; | ||
using MiniSpace.Services.Reactions.Application.Events.External; | ||
using System.ComponentModel.Design; | ||
|
||
namespace MiniSpace.Services.Reactions.Application.UnitTests.Events.External.Handlers | ||
{ | ||
public class StudentCreatedHandlerTest | ||
{ | ||
private readonly StudentCreatedHandler _studentCreatedHandler; | ||
private readonly Mock<IStudentRepository> _studentRepositoryMock; | ||
|
||
public StudentCreatedHandlerTest() | ||
{ | ||
_studentRepositoryMock = new Mock<IStudentRepository>(); | ||
_studentCreatedHandler = new StudentCreatedHandler(_studentRepositoryMock.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task HandleAsync_ValidData_ShouldNotThrowExeption() | ||
{ | ||
// Arrange | ||
var studentId = Guid.NewGuid(); | ||
var @event = new StudentCreated(studentId, "Jan Kowalski"); | ||
var cancellationToken = new CancellationToken(); | ||
|
||
_studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId)) | ||
.ReturnsAsync(false); | ||
|
||
// Act & Assert | ||
Func<Task> act = async () => await _studentCreatedHandler.HandleAsync(@event, cancellationToken); | ||
await act.Should().NotThrowAsync(); | ||
|
||
} | ||
|
||
[Fact] | ||
public async Task HandleAsync_StudentAlreadyCreated_ShuldThrowStudentAlreadyAddedException() | ||
{ | ||
// Arrange | ||
var studentId = Guid.NewGuid(); | ||
var @event = new StudentCreated(studentId, "Jan Kowalski"); | ||
var cancellationToken = new CancellationToken(); | ||
|
||
_studentRepositoryMock.Setup(repo => repo.ExistsAsync(studentId)) | ||
.ReturnsAsync(true); | ||
|
||
// Act & Assert | ||
Func<Task> act = async () => await _studentCreatedHandler.HandleAsync(@event, cancellationToken); | ||
await act.Should().ThrowAsync<StudentAlreadyAddedException>(); | ||
} | ||
} | ||
} |