diff --git a/AsbCloudWebApi.Tests/ReflectionExtensions.cs b/AsbCloudWebApi.Tests/ReflectionExtensions.cs new file mode 100644 index 00000000..e08a8e38 --- /dev/null +++ b/AsbCloudWebApi.Tests/ReflectionExtensions.cs @@ -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 _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); + } +} diff --git a/AsbCloudWebApi.Tests/UnitTests/Services/WellOperationExport/WellOperationExportServiceTest.cs b/AsbCloudWebApi.Tests/UnitTests/Services/WellOperationExport/WellOperationExportServiceTest.cs index cdfd6cd8..dc000166 100644 --- a/AsbCloudWebApi.Tests/UnitTests/Services/WellOperationExport/WellOperationExportServiceTest.cs +++ b/AsbCloudWebApi.Tests/UnitTests/Services/WellOperationExport/WellOperationExportServiceTest.cs @@ -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 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(); wellOperationRepository = Substitute.For(); @@ -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 GetPropsWithDefaultValue(T dto, params string[] excludeProps) + { + IEnumerable 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; + } + } + } }