DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/AttributeTest/DateValidationAttributeTest.cs

109 lines
3.6 KiB
C#
Raw Normal View History

2023-03-24 12:34:57 +05:00
using AsbCloudApp.ValidationAttributes;
using System;
using Xunit;
namespace AsbCloudWebApi.Tests.AttributeTest
{
public class DateValidationAttributeTest
{
[Fact]
public void AllowNull_true_on_null_valid()
{
var attribute = new DateValidationAttribute { AllowNull = true };
var result = attribute.IsValid(null);
Assert.True(result);
}
[Fact]
public void AllowNull_false_on_null_invalid()
{
var attribute = new DateValidationAttribute { AllowNull = false };
var result = attribute.IsValid(null);
Assert.False(result);
}
[Fact]
public void IsDateOnly_true_on_empty_timePart_valid()
{
var attribute = new DateValidationAttribute { IsDateOnly = true };
var date = new DateTime(2023, 01, 01, 00, 00, 00);
var result = attribute.IsValid(date);
Assert.True(result);
}
[Fact]
public void IsDateOnly_true_on_timePart_invalid()
{
var attribute = new DateValidationAttribute { IsDateOnly = true };
var date = new DateTime(2023, 01, 01, 01, 01, 01);
var result = attribute.IsValid(date);
Assert.False(result);
}
[Fact]
public void IsDateOnly_true_on_utc_invalid()
{
var attribute = new DateValidationAttribute { IsDateOnly = true };
var date = new DateTime(2023, 01, 01, 00, 00, 00, DateTimeKind.Utc);
var result = attribute.IsValid(date);
Assert.False(result);
}
[Fact]
public void AllowUtc_true_on_unspecified_valid()
{
var attribute = new DateValidationAttribute { AllowUtc = true };
var date = new DateTime(2023, 01, 01, 00, 00, 00, DateTimeKind.Unspecified);
var result = attribute.IsValid(date);
Assert.True(result);
}
[Fact]
public void AllowUtc_true_on_utc_valid()
{
var attribute = new DateValidationAttribute { AllowUtc = true };
var date = new DateTime(2023, 01, 01, 00, 00, 00, DateTimeKind.Utc);
var result = attribute.IsValid(date);
Assert.True(result);
}
[Fact]
public void AllowUtc_false_on_utc_invalid()
{
var attribute = new DateValidationAttribute { AllowUtc = false };
var date = new DateTime(2023, 01, 01, 00, 00, 00, DateTimeKind.Utc);
var result = attribute.IsValid(date);
Assert.False(result);
}
[Fact]
public void AllowUtc_false_on_unspecified_valid()
{
var attribute = new DateValidationAttribute { AllowUtc = false };
var date = new DateTime(2023, 01, 01, 00, 00, 00, DateTimeKind.Unspecified);
var result = attribute.IsValid(date);
Assert.True(result);
}
[Fact]
public void GtDate_on_same_date_invalid()
{
var gtDate = "2023-01-01T00:00:00";
var date = DateTime.Parse(gtDate);
var attribute = new DateValidationAttribute { GtDate = gtDate };
var result = attribute.IsValid(date);
Assert.False(result);
}
[Fact]
public void GtDate_on_greater_date_valid()
{
var gtDate = "2023-01-01T00:00:00";
var date = DateTime.Parse(gtDate).AddMilliseconds(1);
var attribute = new DateValidationAttribute { GtDate = gtDate };
var result = attribute.IsValid(date);
Assert.True(result);
}
}
}