Автотест (окончание)

This commit is contained in:
Olga Nemt 2024-10-14 10:51:39 +05:00
parent 00d489a849
commit ea0f8311ab
6 changed files with 66 additions and 296 deletions

View File

@ -7,11 +7,15 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<None Remove="Services\DataSaubStat\test1.csv" />
<None Remove="Services\DataSaubStat\test1_result.csv" />
<None Remove="Services\Trajectory\PlannedTrajectoryTemplate.xlsx" /> <None Remove="Services\Trajectory\PlannedTrajectoryTemplate.xlsx" />
<None Remove="Services\Trajectory\Templates\TrajectoryFactManualTemplate.xlsx" /> <None Remove="Services\Trajectory\Templates\TrajectoryFactManualTemplate.xlsx" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Services\DataSaubStat\test1.csv" />
<EmbeddedResource Include="Services\DataSaubStat\test1_result.csv" />
<EmbeddedResource Include="Services\Trajectory\Templates\TrajectoryFactManualTemplate.xlsx" /> <EmbeddedResource Include="Services\Trajectory\Templates\TrajectoryFactManualTemplate.xlsx" />
<EmbeddedResource Include="Services\Trajectory\Templates\TrajectoryPlanTemplate.xlsx" /> <EmbeddedResource Include="Services\Trajectory\Templates\TrajectoryPlanTemplate.xlsx" />
</ItemGroup> </ItemGroup>

View File

@ -0,0 +1,16 @@
using AsbCloudApp.Data;
using CsvHelper.Configuration;
namespace AsbCloudInfrastructure.Tests.MapData
{
public class DataSaubStatDrillingQualityDtoMap : ClassMap<DataSaubStatDrillingQualityDto>
{
public DataSaubStatDrillingQualityDtoMap()
{
Map(m => m.IdTelemetry).Name("id_telemetry");
Map(m => m.DepthStart).Name("depth_start");
Map(m => m.DepthEnd).Name("depth_end");
Map(m => m.DepthDrillingQuality).Name("depth_drilling_quality");
}
}
}

View File

@ -1,13 +1,7 @@
using AsbCloudApp.Data.SAUB; using AsbCloudApp.Data.SAUB;
using CsvHelper.Configuration; using CsvHelper.Configuration;
using AsbCloudInfrastructure.Services;
using CsvHelper.Configuration.Attributes;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Text.Json.Serialization;
namespace AsbCloudDb.Model namespace AsbCloudInfrastructure.Tests.MapData
{ {
public class TelemetryDataSaubMap : ClassMap<TelemetryDataSaubDto> public class TelemetryDataSaubMap : ClassMap<TelemetryDataSaubDto>
{ {

View File

@ -1,21 +1,21 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Data.DetectedOperation;
using AsbCloudApp.Data.SAUB; using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Repositories; using AsbCloudApp.Repositories;
using AsbCloudApp.Requests; using AsbCloudApp.Requests;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services; using AsbCloudInfrastructure.Services;
using AsbCloudInfrastructure.Services.SAUB; using AsbCloudInfrastructure.Tests.MapData;
using CsvHelper; using CsvHelper;
using Mapster; using Mapster;
using NSubstitute; using NSubstitute;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Formats.Asn1;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Xunit; using Xunit;
@ -25,323 +25,77 @@ namespace AsbCloudInfrastructure.Tests.Services;
public class DataSaubStatDtillingQualityServiceTest public class DataSaubStatDtillingQualityServiceTest
{ {
private const string UsingTemplateFile = "AsbCloudInfrastructure/Tests/Services/DataSaubStat"; private const string UsingTemplateFile = "AsbCloudInfrastructure/Tests/Services/DataSaubStat";
private readonly int Gap = 5;
private readonly IDataSaubStatRepository<DataSaubStatDrillingQualityDto> dataSaubStatRepositoryMock = Substitute.For<IDataSaubStatRepository<DataSaubStatDrillingQualityDto>>(); private readonly IDataSaubStatRepository<DataSaubStatDrillingQualityDto> dataSaubStatRepositoryMock = Substitute.For<IDataSaubStatRepository<DataSaubStatDrillingQualityDto>>();
private readonly ITelemetryDataCache<TelemetryDataSaubDto> telemetryDataCacheMock = Substitute.For<ITelemetryDataCache<TelemetryDataSaubDto>>(); private readonly ITelemetryDataCache<TelemetryDataSaubDto> telemetryDataCacheMock = Substitute.For<ITelemetryDataCache<TelemetryDataSaubDto>>();
private readonly IDetectedOperationRepository detectedOperationRepositoryMock = Substitute.For<IDetectedOperationRepository>();
private readonly ITelemetryDataSaubService dataSaubServiceMock = Substitute.For<ITelemetryDataSaubService>(); private readonly ITelemetryDataSaubService dataSaubServiceMock = Substitute.For<ITelemetryDataSaubService>();
private DataSaubStatDrillingQualityService dataSaubStatService; private DataSaubStatDrillingQualityService dataSaubStatService;
private int[] idTelemetries = [1];
private IEnumerable<DataSaubStatDrillingQualityDto> dataSaubStatDrillingQualityDtos = new List<DataSaubStatDrillingQualityDto>()
{
new DataSaubStatDrillingQualityDto {
Id = 1,
DateEnd = DateTime.UtcNow,
DateStart = DateTime.UtcNow.AddHours(-1),
DepthEnd = 2,
DepthStart = 1,
IdFeedRegulator = 1,
IdTelemetry = 1
},
new DataSaubStatDrillingQualityDto {
Id = 2,
DateEnd = DateTime.UtcNow,
DateStart = DateTime.UtcNow.AddHours(-1),
DepthEnd = 3,
DepthStart = 2,
IdFeedRegulator = 2,
IdTelemetry = 2,
},
new DataSaubStatDrillingQualityDto {
Id = 3,
DateEnd = DateTime.UtcNow,
DateStart = DateTime.UtcNow.AddHours(-1),
DepthEnd = 4,
DepthStart = 3,
IdFeedRegulator = 3,
IdTelemetry = 3,
},
};
private List<TelemetryDataSaubDto> telemetryDataSaubDtos = new List<TelemetryDataSaubDto> {
new TelemetryDataSaubDto()
{
IdTelemetry = 1,
DateTime = DateTime.UtcNow.AddMinutes(-30),
AxialLoad = 800,
AxialLoadLimitMax = 1,
AxialLoadSp = 1,
BitDepth = 1,
BlockPosition = 1,
BlockPositionMax = 1,
BlockPositionMin = 1,
BlockSpeed = 1,
BlockSpeedSp = 1,
BlockSpeedSpDevelop = 1,
BlockSpeedSpRotor = 1,
BlockSpeedSpSlide = 1,
Flow = 1,
FlowDeltaLimitMax = 1,
FlowIdle = 1,
HookWeight = 1,
HookWeightIdle = 1,
HookWeightLimitMax = 1,
HookWeightLimitMin = 1,
IdFeedRegulator = 1,
IdUser = 1,
Mode = 1,
Mse = 1,
MseState = 1,
Pressure = 1,
PressureDeltaLimitMax = 1,
PressureIdle = 1,
PressureSp = 1,
PressureSpDevelop = 1,
PressureSpRotor = 1,
PressureSpSlide = 1,
Pump0Flow = 1,
Pump1Flow = 1,
Pump2Flow = 1,
RotorSpeed = 1,
RotorTorque = 1,
RotorTorqueIdle = 1,
RotorTorqueSp = 1,
RotorTorqueLimitMax = 1,
WellDepth = 800,
}
};
public DataSaubStatDtillingQualityServiceTest() public DataSaubStatDtillingQualityServiceTest()
{ {
telemetryDataCacheMock
.GetIds(Arg.Any<TelemetryDataRequest>())
.Returns(idTelemetries);
dataSaubStatRepositoryMock
.GetLastsAsync(Arg.Any<int[]>(), Arg.Any<CancellationToken>())
.Returns(dataSaubStatDrillingQualityDtos);
dataSaubStatService = new DataSaubStatDrillingQualityService( dataSaubStatService = new DataSaubStatDrillingQualityService(
dataSaubStatRepositoryMock, dataSaubStatRepositoryMock,
telemetryDataCacheMock, telemetryDataCacheMock,
dataSaubServiceMock); dataSaubServiceMock);
} }
private TelemetryDataSaubDto CreateTelemetryDataSaubItem(
TelemetryDataSaubDto telemetrySaubDto,
float wellDepth,
float bitDepth,
short idFeedRegulator,
DateTime dateTime)
{
var telemetrySaubDto1 = telemetrySaubDto.Adapt<TelemetryDataSaubDto>();
telemetrySaubDto1.WellDepth = wellDepth;
telemetrySaubDto1.BitDepth = bitDepth;
telemetrySaubDto1.IdFeedRegulator = idFeedRegulator;
telemetrySaubDto1.DateTime = dateTime;
return telemetrySaubDto1;
}
[Fact] [Fact]
public async Task Create_1DataSaubStatItems_ShouldReturn__Success() public async Task Create_DataSaubStatDrillingQuality_From_CSVFile_Test1_ShouldReturn_Success()
{ {
var telemetryDataSaubDtos2 = new List<TelemetryDataSaubDto>(); var resourceName = Assembly.GetExecutingAssembly()
string fileName = @"C:\Users\admin\source\repos\AsbCloudServer\AsbCloudInfrastructure.Tests\Services\DataSaubStat\test.csv"; .GetManifestResourceNames()
using (var reader = new StreamReader(fileName)) .Where(r => r.Contains("DataSaubStat.test1.csv"))
.FirstOrDefault();
if (String.IsNullOrEmpty(resourceName))
Assert.Fail("No mock data for test");
using var stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(resourceName)!;
var actual = Enumerable.Empty<DataSaubStatDrillingQualityDto>();
using (var reader = new StreamReader(stream))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{ {
try csv.Context.RegisterClassMap<TelemetryDataSaubMap>();
{ var records = csv.GetRecords<TelemetryDataSaubDto>().ToList();
csv.Context.RegisterClassMap<TelemetryDataSaubMap>();
var records = csv.GetRecords<TelemetryDataSaubDto>().ToList();
dataSaubServiceMock
dataSaubServiceMock
.Get(Arg.Any<int>(), Arg.Any<bool>(), Arg.Any<DateTimeOffset>(), Arg.Any<DateTimeOffset>(), Arg.Any<int>(), Arg.Any<CancellationToken>()) .Get(Arg.Any<int>(), Arg.Any<bool>(), Arg.Any<DateTimeOffset>(), Arg.Any<DateTimeOffset>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(records); .Returns(records);
//act //act
var result = await dataSaubStatService.CreateStatDrillingQualityForTelemetry(1, DateTimeOffset.UnixEpoch, CancellationToken.None); actual = await dataSaubStatService.CreateStatDrillingQualityForTelemetry(1, DateTimeOffset.UnixEpoch, CancellationToken.None);
//assert
Assert.Equal(3, result.Count());
}
catch (Exception ex)
{
;
}
} }
//var telemetrySaubDto = telemetryDataSaubDtos.FirstOrDefault(); resourceName = Assembly.GetExecutingAssembly()
//if (telemetrySaubDto != null) .GetManifestResourceNames()
//{ .Where(r => r.Contains("DataSaubStat.test1_result.csv"))
// //idFeedRegulator depthWell .FirstOrDefault();
// //1 800
// //1 900
// //1 1000
// //1 900
// //1 1200
// //1 1300
// //1 1100
// //1 1500
// //2 1600
// //Îæèäàåìûå ðåçóëüòàòû äëÿ idFeedRegulator:
// // êîëè÷åñòâî âîçâðàùàåìûõ çàïèñåé = 3
// // îáùàÿ ãëóáèíà = (1000 - 800) + (1300 - 900) + (1600 - 1100) = 1100
// var telemetrySaubDto1 = CreateTelemetryDataSaubItem(
// telemetrySaubDto,
// wellDepth: 900,
// bitDepth: 900,
// idFeedRegulator: 1,
// dateTime: DateTime.UtcNow.AddMinutes(20));
// telemetryDataSaubDtos.Add(telemetrySaubDto1);
// var telemetrySaubDto2 = CreateTelemetryDataSaubItem( if (String.IsNullOrEmpty(resourceName))
// telemetrySaubDto, Assert.Fail("No mock data for test");
// wellDepth: 1000,
// bitDepth: 1000,
// idFeedRegulator: 1,
// dateTime: DateTime.UtcNow.AddMinutes(20));
// telemetryDataSaubDtos.Add(telemetrySaubDto2);
// var telemetrySaubDto3 = CreateTelemetryDataSaubItem( using var stream2 = Assembly.GetExecutingAssembly()
// telemetrySaubDto, .GetManifestResourceStream(resourceName)!;
// wellDepth: 900,
// bitDepth: 900,
// idFeedRegulator: 1,
// dateTime: DateTime.UtcNow.AddMinutes(20));
// telemetryDataSaubDtos.Add(telemetrySaubDto3);
// var telemetrySaubDto4 = CreateTelemetryDataSaubItem( using (var reader = new StreamReader(stream2))
// telemetrySaubDto, using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
// wellDepth: 1200, {
// bitDepth: 1200, csv.Context.RegisterClassMap<DataSaubStatDrillingQualityDtoMap>();
// idFeedRegulator: 1, var expected = csv.GetRecords<DataSaubStatDrillingQualityDto>().ToList();
// dateTime: DateTime.UtcNow.AddMinutes(20));
// telemetryDataSaubDtos.Add(telemetrySaubDto4);
// var telemetrySaubDto5 = CreateTelemetryDataSaubItem( Assert.Equal(expected.Count(), actual.Count());
// telemetrySaubDto,
// wellDepth: 1300,
// bitDepth: 1300,
// idFeedRegulator: 1,
// dateTime: DateTime.UtcNow.AddMinutes(20));
// telemetryDataSaubDtos.Add(telemetrySaubDto5);
// var telemetrySaubDto6 = CreateTelemetryDataSaubItem( var expectedItem = expected.FirstOrDefault()!;
// telemetrySaubDto, var actualItem = actual.FirstOrDefault()!;
// wellDepth: 1100,
// bitDepth: 1100,
// idFeedRegulator: 1,
// dateTime: DateTime.UtcNow.AddMinutes(20));
// telemetryDataSaubDtos.Add(telemetrySaubDto6);
// var telemetrySaubDto7 = CreateTelemetryDataSaubItem( Assert.True(Math.Abs(expectedItem.DepthStart - actualItem.DepthStart) < 0.000001);
// telemetrySaubDto, Assert.True(Math.Abs(expectedItem.DepthEnd - actualItem.DepthEnd) < 0.000001);
// wellDepth: 1500, Assert.True(Math.Abs(expectedItem.DepthDrillingQuality - actualItem.DepthDrillingQuality) < 0.000001);
// bitDepth: 1500, }
// idFeedRegulator: 1,
// dateTime: DateTime.UtcNow.AddMinutes(20));
// telemetryDataSaubDtos.Add(telemetrySaubDto7);
// var telemetrySaubDto8 = CreateTelemetryDataSaubItem(
// telemetrySaubDto,
// wellDepth: 1600,
// bitDepth: 1600,
// idFeedRegulator: 2,
// dateTime: DateTime.UtcNow.AddMinutes(20));
// telemetryDataSaubDtos.Add(telemetrySaubDto8);
//}
} }
private TelemetryDataSaubDto Convert(TelemetryDataSaub src, double timezoneOffset)
{
var mapper = MapsterForUnitTests.GetMapper();
var res = new TelemetryDataSaubDto();
var dto = mapper.Map(src, res);
//var telemetryUser = telemetryUserService.GetOrDefault(src.IdTelemetry, src.IdUser ?? int.MinValue);
dto.User = "dfsdf";
//telemetryUser?.MakeDisplayName();
dto.DateTime = src.DateTime.ToRemoteDateTime(timezoneOffset);
dto.BitDepth = src.BitDepth <= src.WellDepth
? src.BitDepth
: src.WellDepth;
return dto;
}
//[Fact]
//public async Task Create_2DataSaubStatItems_ShouldReturn__Success()
//{
// var insertedDataSaubStatCount = 2;
// var detectedOperationDto = detectedOperationDtos.FirstOrDefault();
// if (detectedOperationDto != null)
// {
// var detectedOperationDto1 = detectedOperationDto.Adapt<DetectedOperationDto>();
// detectedOperationDto1.DateStart = DateTimeOffset.UtcNow.AddMinutes(1);
// detectedOperationDto1.DateEnd = DateTimeOffset.UtcNow.AddHours(1);
// detectedOperationDtos.Add(detectedOperationDto1);
// }
// var telemetryDataSaubDto = telemetryDataSaubDtos.LastOrDefault();
// if (telemetryDataSaubDto != null)
// {
// var telemetryDataSaubDto1 = telemetryDataSaubDto.Adapt<TelemetryDataSaubDto>();
// telemetryDataSaubDto1.DateTime = DateTime.UtcNow.AddMinutes(10);
// telemetryDataSaubDto1.WellDepth = telemetryDataSaubDto.WellDepth + 1;
// var telemetryDataSaubDto2 = telemetryDataSaubDto.Adapt<TelemetryDataSaubDto>();
// telemetryDataSaubDto2.DateTime = DateTime.UtcNow.AddMinutes(20);
// telemetryDataSaubDto2.WellDepth = telemetryDataSaubDto1.WellDepth + 1;
// var telemetryDataSaubDto3 = telemetryDataSaubDto.Adapt<TelemetryDataSaubDto>();
// telemetryDataSaubDto3.DateTime = DateTime.UtcNow.AddMinutes(30);
// telemetryDataSaubDto3.WellDepth = telemetryDataSaubDto2.WellDepth + 1;
// telemetryDataSaubDtos.Add(telemetryDataSaubDto1);
// telemetryDataSaubDtos.Add(telemetryDataSaubDto2);
// telemetryDataSaubDtos.Add(telemetryDataSaubDto3);
// }
// detectedOperationRepositoryMock
// .Get(Arg.Any<DetectedOperationByTelemetryRequest>(), Arg.Any<CancellationToken>())
// .Returns(detectedOperationDtos);
// dataSaubStatRepositoryMock
// .InsertRangeAsync(Arg.Any<IEnumerable<DataSaubStatDto>>(), Arg.Any<CancellationToken>())
// .Returns(insertedDataSaubStatCount);
// Action<string, double?> action = (message, percent) =>
// {
// //assert
// Assert.NotNull(percent);
// Assert.InRange(percent.Value, 0.0, 1.0);
// };
// //act
// await dataSaubStatService.CreateStatAsync(Gap, action, CancellationToken.None);
// //assert
// await dataSaubStatRepositoryMock.Received().InsertRangeAsync(
// Arg.Is<IEnumerable<DataSaubStatDto>>(l => l.Count() == insertedDataSaubStatCount),
// Arg.Any<CancellationToken>());
//}
} }
internal class TestDto
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}

View File

@ -0,0 +1,2 @@
"id_telemetry","depth_start","depth_end","depth_drilling_quality"
419,46,251.279998779297,0.0
1 id_telemetry depth_start depth_end depth_drilling_quality
2 419 46 251.279998779297 0.0