49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
|
|
// Copyright (c) The Geekeey Authors
|
||
|
|
// SPDX-License-Identifier: EUPL-1.2
|
||
|
|
|
||
|
|
namespace Geekeey.Request.Validation.Tests;
|
||
|
|
|
||
|
|
internal sealed class ValidationSeverityTests
|
||
|
|
{
|
||
|
|
[Test]
|
||
|
|
public async Task I_can_have_warning_problems_not_invalidate_by_default()
|
||
|
|
{
|
||
|
|
var validator = new PropertyValidator<Person, string?>(person
|
||
|
|
=> person.Name, rule => rule.Must(value => !string.IsNullOrWhiteSpace(value), "Name is required.")
|
||
|
|
.WithSeverity(Severity.Warning));
|
||
|
|
|
||
|
|
var result = validator.Validate(new Person { Name = "" });
|
||
|
|
|
||
|
|
await Assert.That(result.Problems).Count().IsEqualTo(1);
|
||
|
|
await Assert.That(result.IsValid).IsTrue();
|
||
|
|
await Assert.That(result.IsValidFor(Severity.Warning)).IsFalse();
|
||
|
|
await Assert.That(result.IsValidFor(Severity.Info)).IsFalse();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Test]
|
||
|
|
public async Task I_can_have_info_problems_only_invalidate_at_info_threshold()
|
||
|
|
{
|
||
|
|
var validator = new PropertyValidator<Person, string?>(person
|
||
|
|
=> person.Name, rule => rule.Must(value => !string.IsNullOrWhiteSpace(value), "Name is required.")
|
||
|
|
.WithSeverity(Severity.Info));
|
||
|
|
|
||
|
|
var result = validator.Validate(new Person { Name = "" });
|
||
|
|
|
||
|
|
await Assert.That(result.Problems).Count().IsEqualTo(1);
|
||
|
|
await Assert.That(result.IsValid).IsTrue();
|
||
|
|
await Assert.That(result.IsValidFor(Severity.Warning)).IsTrue();
|
||
|
|
await Assert.That(result.IsValidFor(Severity.Info)).IsFalse();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Test]
|
||
|
|
public async Task I_can_have_error_problems_invalidate_by_default()
|
||
|
|
{
|
||
|
|
var validator = new PropertyValidator<Person, string?>(person
|
||
|
|
=> person.Name, rule => rule.Must(value => !string.IsNullOrWhiteSpace(value), "Name is required."));
|
||
|
|
|
||
|
|
var result = validator.Validate(new Person { Name = "" });
|
||
|
|
|
||
|
|
await Assert.That(result.IsValid).IsFalse();
|
||
|
|
await Assert.That(result.IsValidFor(Severity.Error)).IsFalse();
|
||
|
|
}
|
||
|
|
}
|