2024-08-17 16:38:40 +02:00
|
|
|
using FluentAssertions;
|
2025-06-12 17:43:22 +02:00
|
|
|
using FluentValidation.Results;
|
2024-08-17 16:38:40 +02:00
|
|
|
using Vegasco.WebApi.Cars;
|
|
|
|
|
|
|
|
|
|
namespace WebApi.Tests.Unit.Cars;
|
|
|
|
|
|
|
|
|
|
public sealed class CreateCarRequestValidatorTests
|
|
|
|
|
{
|
|
|
|
|
private readonly CreateCar.Validator _sut = new();
|
|
|
|
|
|
|
|
|
|
private readonly CreateCar.Request _validRequest = new("Ford Focus");
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ValidateAsync_ShouldBeValid_WhenRequestIsValid()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
|
|
|
|
|
// Act
|
2025-06-12 17:43:22 +02:00
|
|
|
ValidationResult? result = await _sut.ValidateAsync(_validRequest);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
result.IsValid.Should().BeTrue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(1)]
|
|
|
|
|
[InlineData(50)]
|
|
|
|
|
public async Task ValidateAsync_ShouldBeValid_WhenNameIsJustWithinTheLimits(int nameLength)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2025-06-12 17:43:22 +02:00
|
|
|
CreateCar.Request request = _validRequest with { Name = new string('s', nameLength) };
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
// Act
|
2025-06-12 17:43:22 +02:00
|
|
|
ValidationResult? result = await _sut.ValidateAsync(request);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
result.IsValid.Should().BeTrue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ValidateAsync_ShouldNotBeValid_WhenNameIsEmpty()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
2025-06-12 17:43:22 +02:00
|
|
|
CreateCar.Request request = _validRequest with { Name = "" };
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
// Act
|
2025-06-12 17:43:22 +02:00
|
|
|
ValidationResult? result = await _sut.ValidateAsync(request);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
result.IsValid.Should().BeFalse();
|
|
|
|
|
result.Errors.Should().ContainSingle()
|
|
|
|
|
.Which
|
|
|
|
|
.PropertyName.Should().Be(nameof(CreateCar.Request.Name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ValidateAsync_ShouldNotBeValid_WhenNameIsTooLong()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
const int nameMaxLength = 50;
|
2025-06-12 17:43:22 +02:00
|
|
|
CreateCar.Request request = _validRequest with { Name = new string('s', nameMaxLength + 1) };
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
// Act
|
2025-06-12 17:43:22 +02:00
|
|
|
ValidationResult? result = await _sut.ValidateAsync(request);
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
result.IsValid.Should().BeFalse();
|
|
|
|
|
result.Errors.Should().ContainSingle()
|
|
|
|
|
.Which
|
|
|
|
|
.PropertyName.Should().Be(nameof(CreateCar.Request.Name));
|
|
|
|
|
}
|
|
|
|
|
}
|