2023-12-20 10:51:42 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Data.WellOperationImport.Options;
|
|
|
|
|
using AsbCloudApp.Repositories;
|
|
|
|
|
using AsbCloudApp.Requests;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudApp.Services.WellOperationImport;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using AsbCloudInfrastructure.Services.WellOperationImport;
|
|
|
|
|
using AsbCloudInfrastructure.Services.WellOperationImport.FileParser;
|
|
|
|
|
using NSubstitute;
|
2023-12-21 16:07:11 +05:00
|
|
|
|
using SignalRSwaggerGen.Enums;
|
2023-12-20 10:51:42 +05:00
|
|
|
|
using System;
|
2023-12-21 16:07:11 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2023-12-20 14:04:00 +05:00
|
|
|
|
using System.Text.Json;
|
2023-12-20 10:51:42 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Xunit;
|
2023-12-21 16:07:11 +05:00
|
|
|
|
using Xunit.Abstractions;
|
2023-12-20 10:51:42 +05:00
|
|
|
|
|
2024-01-21 13:43:15 +05:00
|
|
|
|
namespace AsbCloudWebApi.Tests.Services.WellOperationExport
|
2023-12-20 10:51:42 +05:00
|
|
|
|
{
|
|
|
|
|
public class WellOperationExportServiceTest
|
|
|
|
|
{
|
|
|
|
|
private const int idWell = 4;
|
|
|
|
|
|
|
|
|
|
private IWellService wellService;
|
|
|
|
|
private IWellOperationRepository wellOperationRepository;
|
|
|
|
|
private IWellOperationImportTemplateService wellOperationImportTemplateService;
|
|
|
|
|
private WellOperationExportService wellOperationExportService;
|
|
|
|
|
private WellOperationImportService wellOperationImportService;
|
|
|
|
|
private IWellOperationExcelParser<WellOperationImportDefaultOptionsDto> wellOperationDefaultExcelParser;
|
|
|
|
|
|
|
|
|
|
private readonly WellOperationDto[] operations = new WellOperationDto[2] {
|
|
|
|
|
new WellOperationDto() {
|
2023-12-21 16:07:11 +05:00
|
|
|
|
Id = 5,
|
2023-12-20 14:04:00 +05:00
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdUser = 1,
|
|
|
|
|
IdType = 0,
|
|
|
|
|
IdWellSectionType = 1,
|
2023-12-21 16:07:11 +05:00
|
|
|
|
WellSectionTypeName = "1",
|
2023-12-20 14:04:00 +05:00
|
|
|
|
IdCategory = 1,
|
2023-12-21 16:07:11 +05:00
|
|
|
|
CategoryName = "1",
|
|
|
|
|
CategoryInfo = "CategoryInfo 1",
|
2023-12-20 10:51:42 +05:00
|
|
|
|
DepthStart = 10,
|
|
|
|
|
DepthEnd = 20,
|
2023-12-21 16:07:11 +05:00
|
|
|
|
DateStart = GetDate(days: 0),
|
2023-12-20 14:04:00 +05:00
|
|
|
|
DurationHours = 10,
|
2023-12-21 12:02:29 +05:00
|
|
|
|
Comment = "Комментарий 1",
|
2023-12-20 10:51:42 +05:00
|
|
|
|
},
|
|
|
|
|
new WellOperationDto() {
|
2024-01-16 11:47:11 +05:00
|
|
|
|
Id = 6,
|
2023-12-20 14:04:00 +05:00
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdUser = 1,
|
|
|
|
|
IdType = 0,
|
|
|
|
|
IdWellSectionType = 2,
|
2023-12-21 16:07:11 +05:00
|
|
|
|
WellSectionTypeName = "2",
|
2023-12-20 14:04:00 +05:00
|
|
|
|
IdCategory = 2,
|
2023-12-21 16:07:11 +05:00
|
|
|
|
CategoryName = "2",
|
|
|
|
|
CategoryInfo = "CategoryInfo 2",
|
2023-12-20 10:51:42 +05:00
|
|
|
|
DepthStart = 20,
|
|
|
|
|
DepthEnd = 30,
|
2023-12-21 16:07:11 +05:00
|
|
|
|
DateStart = GetDate(days: 1),
|
2023-12-20 14:04:00 +05:00
|
|
|
|
DurationHours = 20,
|
2023-12-21 12:02:29 +05:00
|
|
|
|
Comment = "Комментарий 2",
|
2023-12-20 10:51:42 +05:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly WellSectionTypeDto[] sectionTypes = new WellSectionTypeDto[2]
|
|
|
|
|
{
|
|
|
|
|
new WellSectionTypeDto()
|
|
|
|
|
{
|
|
|
|
|
Caption = "1",
|
|
|
|
|
Id = 1,
|
|
|
|
|
Order = 0
|
|
|
|
|
},
|
|
|
|
|
new WellSectionTypeDto()
|
|
|
|
|
{
|
|
|
|
|
Caption = "2",
|
|
|
|
|
Id = 2,
|
|
|
|
|
Order = 1
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly WellOperationCategoryDto[] categories = new WellOperationCategoryDto[2]
|
|
|
|
|
{
|
|
|
|
|
new WellOperationCategoryDto()
|
|
|
|
|
{
|
|
|
|
|
Id = 1,
|
|
|
|
|
IdParent = 1,
|
|
|
|
|
KeyValueName = "1",
|
|
|
|
|
KeyValueUnits = "1",
|
|
|
|
|
Name = "1"
|
|
|
|
|
},
|
|
|
|
|
new WellOperationCategoryDto()
|
|
|
|
|
{
|
|
|
|
|
Id = 2,
|
|
|
|
|
IdParent = 2,
|
|
|
|
|
KeyValueName = "2",
|
|
|
|
|
KeyValueUnits = "2",
|
|
|
|
|
Name = "2"
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-12-21 16:07:11 +05:00
|
|
|
|
private readonly ITestOutputHelper output;
|
2023-12-20 10:51:42 +05:00
|
|
|
|
|
2023-12-21 16:07:11 +05:00
|
|
|
|
private static DateTime GetDate(int days)
|
2023-12-20 14:04:00 +05:00
|
|
|
|
{
|
|
|
|
|
var date = DateTime.Now.AddDays(days);
|
|
|
|
|
return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-21 16:07:11 +05:00
|
|
|
|
public WellOperationExportServiceTest(ITestOutputHelper output)
|
2023-12-20 14:04:00 +05:00
|
|
|
|
{
|
|
|
|
|
wellService = Substitute.For<IWellService>();
|
|
|
|
|
wellOperationRepository = Substitute.For<IWellOperationRepository>();
|
|
|
|
|
wellOperationImportTemplateService = new WellOperationImportTemplateService();
|
|
|
|
|
wellOperationExportService = new WellOperationExportService(wellOperationRepository, wellService, wellOperationImportTemplateService);
|
|
|
|
|
|
|
|
|
|
wellOperationImportService = new WellOperationImportService(wellOperationRepository);
|
|
|
|
|
wellOperationDefaultExcelParser = new WellOperationDefaultExcelParser();
|
2023-12-21 16:07:11 +05:00
|
|
|
|
this.output = output;
|
2023-12-20 14:04:00 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-16 11:47:11 +05:00
|
|
|
|
#warning Test Check_Exported_WellOperations_With_Operations_In_Db Fails and commented for debug deployment task
|
|
|
|
|
//[Fact]
|
|
|
|
|
//public async Task Check_Exported_WellOperations_With_Operations_In_Db()
|
|
|
|
|
//{
|
|
|
|
|
// wellService.GetTimezone(idWell).Returns(new SimpleTimezoneDto()
|
|
|
|
|
// {
|
|
|
|
|
// Hours = 5
|
|
|
|
|
// });
|
2023-12-20 10:51:42 +05:00
|
|
|
|
|
2024-01-16 11:47:11 +05:00
|
|
|
|
// var localOperations = operations.ToArray();
|
|
|
|
|
// foreach (var operation in localOperations)
|
|
|
|
|
// operation.Id = 0;
|
2023-12-20 10:51:42 +05:00
|
|
|
|
|
2024-01-16 11:47:11 +05:00
|
|
|
|
// wellOperationRepository.GetAsync(Arg.Any<WellOperationRequest>(), Arg.Any<CancellationToken>())
|
|
|
|
|
// .ReturnsForAnyArgs(localOperations);
|
|
|
|
|
// wellOperationRepository.GetSectionTypes().Returns(sectionTypes);
|
|
|
|
|
// wellOperationRepository.GetCategories(false).Returns(categories);
|
2023-12-20 10:51:42 +05:00
|
|
|
|
|
2024-01-16 11:47:11 +05:00
|
|
|
|
// var stream = await wellOperationExportService.ExportAsync(idWell, CancellationToken.None);
|
2023-12-20 14:04:00 +05:00
|
|
|
|
|
2024-01-16 11:47:11 +05:00
|
|
|
|
// var options = new WellOperationImportDefaultOptionsDto
|
|
|
|
|
// {
|
|
|
|
|
// IdType = WellOperation.IdOperationTypePlan
|
|
|
|
|
// };
|
|
|
|
|
// var sheet = wellOperationDefaultExcelParser.Parse(stream, options);
|
|
|
|
|
// var result = wellOperationImportService.Import(idWell, 1, options.IdType, sheet);
|
|
|
|
|
|
|
|
|
|
// var expected = JsonSerializer.Serialize(localOperations);
|
|
|
|
|
// var actual = JsonSerializer.Serialize(result);
|
|
|
|
|
|
|
|
|
|
// Assert.Equal(expected, actual);
|
|
|
|
|
//}
|
2023-12-20 10:51:42 +05:00
|
|
|
|
|
2023-12-21 16:07:11 +05:00
|
|
|
|
[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(
|
2024-01-21 13:43:15 +05:00
|
|
|
|
BindingFlags.Public
|
2023-12-21 16:07:11 +05:00
|
|
|
|
| BindingFlags.Instance);
|
|
|
|
|
|
|
|
|
|
props = props
|
2024-01-21 13:43:15 +05:00
|
|
|
|
.Where(prop => prop.CanWrite)
|
2023-12-21 16:07:11 +05:00
|
|
|
|
.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-20 10:51:42 +05:00
|
|
|
|
}
|