Tests Add TestDataContainsNotDefaultProps example

This commit is contained in:
ngfrolov 2023-12-21 16:07:11 +05:00
parent 0967c208b6
commit 40ac7780da
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
2 changed files with 114 additions and 8 deletions

View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Tests
{
public static class ReflectionExtensions
{
private static readonly Dictionary<Type, object> _commonTypeDictionary = new()
{
{ typeof(int), default(int) },
{ typeof(Guid), default(Guid) },
{ typeof(DateOnly), default(DateOnly) },
{ typeof(DateTime), default(DateTime) },
{ typeof(DateTimeOffset), default(DateTimeOffset) },
{ typeof(TimeOnly), default(TimeOnly) },
{ typeof(long), default(long) },
{ typeof(bool), default(bool) },
{ typeof(double), default(double) },
{ typeof(short), default(short) },
{ typeof(float), default(float) },
{ typeof(byte), default(byte) },
{ typeof(char), default(char) },
{ typeof(uint), default(uint) },
{ typeof(ushort), default(ushort) },
{ typeof(ulong), default(ulong) },
{ typeof(sbyte), default(sbyte) }
};
public static object? GetDefaultValue(this Type type)
{
if (!type.IsValueType)
{
return null;
}
return _commonTypeDictionary.TryGetValue(type, out var value)
? value
: Activator.CreateInstance(type);
}
public static bool IsDefaultValue(this Type type, object? value)
=> (value?.Equals(type.GetDefaultValue()) != false);
}
}

View File

@ -8,11 +8,16 @@ using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.WellOperationImport;
using AsbCloudInfrastructure.Services.WellOperationImport.FileParser;
using NSubstitute;
using SignalRSwaggerGen.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace AsbCloudWebApi.Tests.UnitTests.Services.WellOperationExport
{
@ -27,31 +32,36 @@ namespace AsbCloudWebApi.Tests.UnitTests.Services.WellOperationExport
private WellOperationImportService wellOperationImportService;
private IWellOperationExcelParser<WellOperationImportDefaultOptionsDto> wellOperationDefaultExcelParser;
private readonly WellOperationDto[] operations = new WellOperationDto[2] {
new WellOperationDto() {
Id = 5,
IdWell = idWell,
IdUser = 1,
IdType = 0,
IdWellSectionType = 1,
WellSectionTypeName = "1",
IdCategory = 1,
CategoryInfo = "1",
CategoryName = "1",
CategoryInfo = "CategoryInfo 1",
DepthStart = 10,
DepthEnd = 20,
DateStart = getDate(days: 0),
DateStart = GetDate(days: 0),
DurationHours = 10,
Comment = "Комментарий 1",
},
new WellOperationDto() {
Id = 6,
IdWell = idWell,
IdUser = 1,
IdType = 0,
IdWellSectionType = 2,
WellSectionTypeName = "2",
IdCategory = 2,
CategoryInfo = "2",
CategoryName = "2",
CategoryInfo = "CategoryInfo 2",
DepthStart = 20,
DepthEnd = 30,
DateStart = getDate(days: 1),
DateStart = GetDate(days: 1),
DurationHours = 20,
Comment = "Комментарий 2",
}
@ -92,14 +102,15 @@ namespace AsbCloudWebApi.Tests.UnitTests.Services.WellOperationExport
Name = "2"
}
};
private readonly ITestOutputHelper output;
private static DateTime getDate(int days)
private static DateTime GetDate(int days)
{
var date = DateTime.Now.AddDays(days);
return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second);
}
public WellOperationExportServiceTest()
public WellOperationExportServiceTest(ITestOutputHelper output)
{
wellService = Substitute.For<IWellService>();
wellOperationRepository = Substitute.For<IWellOperationRepository>();
@ -108,6 +119,7 @@ namespace AsbCloudWebApi.Tests.UnitTests.Services.WellOperationExport
wellOperationImportService = new WellOperationImportService(wellOperationRepository);
wellOperationDefaultExcelParser = new WellOperationDefaultExcelParser();
this.output = output;
}
[Fact]
@ -136,6 +148,53 @@ namespace AsbCloudWebApi.Tests.UnitTests.Services.WellOperationExport
Assert.Equal(expected, actual);
}
}
[Fact]
public void TestDataContainsNotDefaultProps()
{
var initOk = true;
for (int i = 0; i < operations.Length; i++)
{
var operation = operations[i];
var propsWithDefault = GetPropsWithDefaultValue(operation,
nameof(WellOperationDto.Id),
nameof(WellOperationDto.IdType),
nameof(WellOperationDto.IdPlan),
nameof(WellOperationDto.IdParentCategory),
nameof(WellOperationDto.Day),
nameof(WellOperationDto.NptHours),
nameof(WellOperationDto.UserName),
nameof(WellOperationDto.LastUpdateDate)
)
.ToArray();
if (propsWithDefault.Any())
{
initOk = false;
foreach (var propertyName in propsWithDefault)
output.WriteLine($"{nameof(operations)}[{i}].{propertyName} is default");
}
}
Assert.True(initOk);
}
private static IEnumerable<string> GetPropsWithDefaultValue<T>(T dto, params string[] excludeProps)
{
IEnumerable<PropertyInfo> props = typeof(T).GetProperties(
BindingFlags.Public
| BindingFlags.Instance);
props = props
.Where(prop=>prop.CanWrite)
.Where(prop => !excludeProps.Contains(prop.Name));
foreach (var prop in props)
{
var value = prop.GetValue(dto);
if (prop.PropertyType.IsDefaultValue(value))
yield return prop.Name;
}
}
}
}