mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 14:11:10 +00:00
Replaces FluentValidation and Mediator command/queries handling with custom implementations.
This commit is contained in:
1 parent
9a5c7473f9
commit
2d1c6b1ab8
297 files changed
+3619
-3045
No files matched your search
-16
@@ -1,16 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Mediator;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Behaviors.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="ValidationBehavior"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ValidationBehaviorFixture : IRequest<ErrorOr<ValidationBehaviorTestResponse>>
|
||||
{
|
||||
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Behaviors.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture test class for validation behavior responses.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ValidationBehaviorTestResponse
|
||||
{
|
||||
}
|
||||
-99
@@ -1,99 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Lumina.Application.Common.Behaviors;
|
||||
using Lumina.Application.UnitTests.Common.Behaviors.Fixtures;
|
||||
using Mediator;
|
||||
using NSubstitute;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Behaviors;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="ValidationBehavior"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ValidationBehaviorTests
|
||||
{
|
||||
private readonly IValidator<ValidationBehaviorFixture> _mockValidator;
|
||||
private readonly MessageHandlerDelegate<ValidationBehaviorFixture, ErrorOr<ValidationBehaviorTestResponse>> _nextDelegate;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ValidationBehaviorTests"/> class.
|
||||
/// </summary>
|
||||
public ValidationBehaviorTests()
|
||||
{
|
||||
_mockValidator = Substitute.For<IValidator<ValidationBehaviorFixture>>();
|
||||
_nextDelegate = Substitute.For<MessageHandlerDelegate<ValidationBehaviorFixture, ErrorOr<ValidationBehaviorTestResponse>>>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenValidatorIsNull_ShouldInvokeNextDelegate()
|
||||
{
|
||||
// Arrange
|
||||
ValidationBehavior<ValidationBehaviorFixture, ErrorOr<ValidationBehaviorTestResponse>> behavior = new();
|
||||
ValidationBehaviorFixture request = new();
|
||||
ValidationBehaviorTestResponse expectedResponse = new();
|
||||
_nextDelegate.Invoke(request, Arg.Any<CancellationToken>()).Returns(new ValueTask<ErrorOr<ValidationBehaviorTestResponse>>(ErrorOrFactory.From(expectedResponse)));
|
||||
|
||||
// Act
|
||||
ErrorOr<ValidationBehaviorTestResponse> result = await behavior.Handle(request, _nextDelegate, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsError);
|
||||
Assert.Equal(expectedResponse, result.Value);
|
||||
await _nextDelegate.Received(1).Invoke(request, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenValidationSucceeds_ShouldInvokeNextDelegate()
|
||||
{
|
||||
// Arrange
|
||||
ValidationBehavior<ValidationBehaviorFixture, ErrorOr<ValidationBehaviorTestResponse>> behavior = new(_mockValidator);
|
||||
ValidationBehaviorFixture request = new();
|
||||
ValidationBehaviorTestResponse expectedResponse = new();
|
||||
_mockValidator.ValidateAsync(request, Arg.Any<CancellationToken>()).Returns(new ValidationResult());
|
||||
_nextDelegate.Invoke(request, Arg.Any<CancellationToken>()).Returns(new ValueTask<ErrorOr<ValidationBehaviorTestResponse>>(ErrorOrFactory.From(expectedResponse)));
|
||||
|
||||
// Act
|
||||
ErrorOr<ValidationBehaviorTestResponse> result = await behavior.Handle(request, _nextDelegate, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsError);
|
||||
Assert.Equal(expectedResponse, result.Value);
|
||||
await _mockValidator.Received(1).ValidateAsync(request, Arg.Any<CancellationToken>());
|
||||
await _nextDelegate.Received(1).Invoke(request, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WhenValidationFails_ShouldReturnErrors()
|
||||
{
|
||||
// Arrange
|
||||
ValidationBehavior<ValidationBehaviorFixture, ErrorOr<ValidationBehaviorTestResponse>> behavior = new(_mockValidator);
|
||||
ValidationBehaviorFixture request = new();
|
||||
List<ValidationFailure> validationFailures =
|
||||
[
|
||||
new ValidationFailure("Property1", "Error message 1"),
|
||||
new ValidationFailure("Property2", "Error message 2")
|
||||
];
|
||||
_mockValidator.ValidateAsync(request, Arg.Any<CancellationToken>()).Returns(new ValidationResult(validationFailures));
|
||||
|
||||
// Act
|
||||
ErrorOr<ValidationBehaviorTestResponse> result = await behavior.Handle(request, _nextDelegate, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsError);
|
||||
Assert.Equal(2, result.Errors.Count);
|
||||
Assert.Equal(ErrorType.Validation, result.Errors[0].Type);
|
||||
Assert.Equal("Error message 1", result.Errors[0].Description);
|
||||
Assert.Equal(ErrorType.Validation, result.Errors[1].Type);
|
||||
Assert.Equal("Error message 2", result.Errors[1].Description);
|
||||
await _mockValidator.Received(1).ValidateAsync(request, Arg.Any<CancellationToken>());
|
||||
await _nextDelegate.DidNotReceive().Invoke(request, Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -30,7 +30,7 @@ public static class ValidatorTestExtensions
|
||||
/// <param name="expectedError">The error that should not be present in the validation results.</param>
|
||||
public static void ShouldNotHaveValidationError(this List<Error> errors, Error expectedError)
|
||||
{
|
||||
Assert.DoesNotContain(errors, error => error.Code == expectedError.Code);
|
||||
Assert.DoesNotContain(errors, error => error.Description == expectedError.Description);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -40,7 +40,7 @@ public static class ValidatorTestExtensions
|
||||
/// <param name="expectedError">The error that should be present in the validation results.</param>
|
||||
public static void ShouldHaveValidationError(this List<Error> errors, Error expectedError)
|
||||
{
|
||||
Assert.Contains(errors, error => error.Code == expectedError.Code);
|
||||
Assert.Contains(errors, error => error.Description == expectedError.Description);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user