mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 14:13:12 +00:00
104 lines
2.8 KiB
C#
104 lines
2.8 KiB
C#
using FluentAssertions;
|
|
using SeaHaven.Services.DTOs;
|
|
using SeaHaven.Services.Validation;
|
|
using Xunit;
|
|
|
|
namespace SeaHaven.Services.Tests;
|
|
|
|
public class CreateAssetValidationTests
|
|
{
|
|
private readonly CreateAssetValidation _validator = new();
|
|
|
|
[Fact]
|
|
public void Validate_WhenNameEmpty_Fails()
|
|
{
|
|
var result = _validator.Validate(new CreateAssetDTO { Name = "" });
|
|
|
|
result.IsValid.Should().BeFalse();
|
|
result.Errors.Should().Contain(e => e.PropertyName == nameof(CreateAssetDTO.Name));
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_WhenNameTooLong_Fails()
|
|
{
|
|
var result = _validator.Validate(new CreateAssetDTO { Name = new string('a', 201) });
|
|
|
|
result.IsValid.Should().BeFalse();
|
|
result.Errors.Should().Contain(e => e.PropertyName == nameof(CreateAssetDTO.Name));
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_WhenLocationIdZero_Fails()
|
|
{
|
|
var result = _validator.Validate(new CreateAssetDTO { Name = "Pump", LocationId = 0 });
|
|
|
|
result.IsValid.Should().BeFalse();
|
|
result.Errors.Should().Contain(e => e.PropertyName == nameof(CreateAssetDTO.LocationId));
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_WhenValid_Passes()
|
|
{
|
|
var result = _validator.Validate(new CreateAssetDTO { Name = "Pump", LocationId = 5 });
|
|
|
|
result.IsValid.Should().BeTrue();
|
|
}
|
|
}
|
|
|
|
public class CreateContactValidationTests
|
|
{
|
|
private readonly CreateContactValidation _validator = new();
|
|
|
|
private static CreateContactDTO Valid() => new()
|
|
{
|
|
FirstName = "Ada",
|
|
LastName = "Lovelace",
|
|
Email = "ada@example.com"
|
|
};
|
|
|
|
[Fact]
|
|
public void Validate_WhenNamesMissing_Fails()
|
|
{
|
|
var dto = Valid();
|
|
dto.FirstName = "";
|
|
dto.LastName = "";
|
|
|
|
var result = _validator.Validate(dto);
|
|
|
|
result.IsValid.Should().BeFalse();
|
|
result.Errors.Should().Contain(e => e.PropertyName == nameof(CreateContactDTO.FirstName));
|
|
result.Errors.Should().Contain(e => e.PropertyName == nameof(CreateContactDTO.LastName));
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_WhenEmailMalformed_Fails()
|
|
{
|
|
var dto = Valid();
|
|
dto.Email = "not-an-email";
|
|
|
|
var result = _validator.Validate(dto);
|
|
|
|
result.IsValid.Should().BeFalse();
|
|
result.Errors.Should().Contain(e => e.PropertyName == nameof(CreateContactDTO.Email));
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_WhenPhoneTooLong_Fails()
|
|
{
|
|
var dto = Valid();
|
|
dto.Phone = new string('1', 21);
|
|
|
|
var result = _validator.Validate(dto);
|
|
|
|
result.IsValid.Should().BeFalse();
|
|
result.Errors.Should().Contain(e => e.PropertyName == nameof(CreateContactDTO.Phone));
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_WhenValid_Passes()
|
|
{
|
|
var result = _validator.Validate(Valid());
|
|
|
|
result.IsValid.Should().BeTrue();
|
|
}
|
|
}
|