2024-02-05 16:43:56 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Repositories;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.IntegrationTests.Repository
|
|
|
|
|
{
|
|
|
|
|
public class DataSaubStatRepositoryTest : BaseIntegrationTest
|
|
|
|
|
{
|
2024-02-07 13:28:02 +05:00
|
|
|
|
private static readonly TimeSpan timeSpan = TimeSpan.FromHours(1);
|
|
|
|
|
|
2024-02-05 16:43:56 +05:00
|
|
|
|
private static readonly DataSaubStatDto[] statDtos = new DataSaubStatDto[2]
|
|
|
|
|
{
|
|
|
|
|
new DataSaubStatDto()
|
|
|
|
|
{
|
|
|
|
|
IdTelemetry = 1,
|
2024-02-07 13:28:02 +05:00
|
|
|
|
DateEnd = new DateTimeOffset(2024, 1, 1, 20, 25, 0, timeSpan),
|
|
|
|
|
DateStart = new DateTimeOffset(2024, 1, 1, 20, 15, 0, timeSpan),
|
2024-02-05 16:43:56 +05:00
|
|
|
|
AxialLoad = 10.0,
|
|
|
|
|
AxialLoadLimitMax = 10.0,
|
|
|
|
|
AxialLoadSp = 10.0,
|
|
|
|
|
BlockSpeedSp = 1000,
|
|
|
|
|
DepthEnd = 10.0,
|
|
|
|
|
DepthStart = 5.0,
|
|
|
|
|
EnabledSubsystems = 1,
|
|
|
|
|
Flow = 10.0,
|
|
|
|
|
HasOscillation = true,
|
|
|
|
|
Id = default,
|
|
|
|
|
IdCategory = 2,
|
|
|
|
|
IdFeedRegulator = 1,
|
|
|
|
|
Pressure = 10.0,
|
|
|
|
|
PressureIdle = 10.0,
|
|
|
|
|
PressureSp = 10.0,
|
2024-02-06 10:29:01 +05:00
|
|
|
|
RotorSpeed = 9.0,
|
|
|
|
|
RotorTorque = 9.0,
|
|
|
|
|
RotorTorqueSp = 9.0,
|
|
|
|
|
RotorTorqueLimitMax = 9.0,
|
2024-02-05 16:43:56 +05:00
|
|
|
|
Speed = 10.0
|
|
|
|
|
},
|
|
|
|
|
new DataSaubStatDto()
|
|
|
|
|
{
|
2024-02-06 10:29:01 +05:00
|
|
|
|
IdTelemetry = 1,
|
2024-02-07 13:28:02 +05:00
|
|
|
|
DateEnd = new DateTimeOffset(2024, 2, 2, 20, 25, 0, timeSpan),
|
|
|
|
|
DateStart = new DateTimeOffset(2024, 2, 2, 20, 15, 0, timeSpan),
|
2024-02-05 16:43:56 +05:00
|
|
|
|
AxialLoad = 10.0,
|
|
|
|
|
AxialLoadLimitMax = 10.0,
|
|
|
|
|
AxialLoadSp = 10.0,
|
|
|
|
|
BlockSpeedSp = 1000,
|
|
|
|
|
DepthEnd = 10.0,
|
|
|
|
|
DepthStart = 5.0,
|
|
|
|
|
EnabledSubsystems = 1,
|
|
|
|
|
Flow = 10.0,
|
|
|
|
|
HasOscillation = true,
|
|
|
|
|
Id = default,
|
|
|
|
|
IdCategory = 2,
|
|
|
|
|
IdFeedRegulator = 1,
|
|
|
|
|
Pressure = 10.0,
|
|
|
|
|
PressureIdle = 10.0,
|
|
|
|
|
PressureSp = 10.0,
|
|
|
|
|
RotorSpeed = 10.0,
|
|
|
|
|
RotorTorque = 10.0,
|
|
|
|
|
RotorTorqueSp = 10.0,
|
|
|
|
|
RotorTorqueLimitMax = 10.0,
|
|
|
|
|
Speed = 10.0
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
private static readonly WellOperationCategory category = new()
|
|
|
|
|
{
|
|
|
|
|
Id = 2,
|
|
|
|
|
IdParent = null,
|
2024-02-06 10:29:01 +05:00
|
|
|
|
Name = "Категория 2"
|
2024-02-05 16:43:56 +05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly IDataSaubStatRepository dataSaubStatRepository;
|
|
|
|
|
|
|
|
|
|
public DataSaubStatRepositoryTest(WebAppFactoryFixture factory) : base(factory)
|
|
|
|
|
{
|
|
|
|
|
dataSaubStatRepository = scope.ServiceProvider.GetRequiredService<IDataSaubStatRepository>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetLastDatesAsync_returns_success()
|
|
|
|
|
{
|
2024-02-06 11:44:29 +05:00
|
|
|
|
//prepare
|
|
|
|
|
dbContext.CleanupDbSet<DataSaubStat>();
|
|
|
|
|
|
|
|
|
|
var dbSetSaubStat = dbContext.Set<DataSaubStat>();
|
|
|
|
|
var dbSetCategories = dbContext.Set<WellOperationCategory>();
|
|
|
|
|
|
2024-02-07 13:28:02 +05:00
|
|
|
|
var entities = statDtos.Select(stat => ConvertToEntity(stat));
|
2024-02-06 11:44:29 +05:00
|
|
|
|
|
|
|
|
|
dbSetCategories.Add(category);
|
|
|
|
|
dbSetSaubStat.AddRange(entities);
|
|
|
|
|
dbContext.SaveChanges();
|
|
|
|
|
|
2024-02-05 16:43:56 +05:00
|
|
|
|
//act
|
|
|
|
|
var telemetryIds = statDtos.Select(stat => stat.IdTelemetry).ToArray();
|
2024-02-06 11:44:29 +05:00
|
|
|
|
var result = await dataSaubStatRepository.GetLastsAsync(telemetryIds, CancellationToken.None);
|
2024-02-05 16:43:56 +05:00
|
|
|
|
|
|
|
|
|
var expected = statDtos.Max(stat => stat.DateEnd);
|
2024-02-06 11:44:29 +05:00
|
|
|
|
var actual = result.First().DateEnd;
|
2024-02-05 16:43:56 +05:00
|
|
|
|
|
|
|
|
|
//assert
|
2024-02-06 10:29:01 +05:00
|
|
|
|
Assert.True((expected - actual).Ticks == 0.0);
|
2024-02-05 16:43:56 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task InsertRangeAsync_returns_success()
|
|
|
|
|
{
|
2024-02-06 11:44:29 +05:00
|
|
|
|
//prepare
|
|
|
|
|
dbContext.CleanupDbSet<DataSaubStat>();
|
|
|
|
|
|
2024-02-05 16:43:56 +05:00
|
|
|
|
//act
|
2024-02-06 10:29:01 +05:00
|
|
|
|
var dbSet = dbContext.Set<DataSaubStat>();
|
|
|
|
|
dbSet.RemoveRange(dbSet);
|
2024-02-07 13:28:02 +05:00
|
|
|
|
|
|
|
|
|
var dbSetCategories = dbContext.Set<WellOperationCategory>();
|
|
|
|
|
dbSetCategories.Add(category);
|
|
|
|
|
|
2024-02-06 10:29:01 +05:00
|
|
|
|
dbContext.SaveChanges();
|
2024-02-05 16:43:56 +05:00
|
|
|
|
|
2024-02-06 10:29:01 +05:00
|
|
|
|
var result = await dataSaubStatRepository.InsertRangeAsync(statDtos, CancellationToken.None);
|
2024-02-06 11:44:29 +05:00
|
|
|
|
|
2024-02-05 16:43:56 +05:00
|
|
|
|
//assert
|
|
|
|
|
Assert.Equal(statDtos.Length, result);
|
2024-02-06 11:44:29 +05:00
|
|
|
|
|
2024-02-07 13:28:02 +05:00
|
|
|
|
var statDtosFromDb = dbSet.Select(stat => ConvertToDto(stat, timeSpan)).ToArray();
|
2024-02-06 10:29:01 +05:00
|
|
|
|
|
|
|
|
|
var excludedProps = new[] {
|
|
|
|
|
nameof(DataSaubStat.Telemetry),
|
|
|
|
|
nameof(DataSaubStat.Id),
|
|
|
|
|
nameof(DataSaubStat.OperationCategory)
|
|
|
|
|
};
|
|
|
|
|
foreach (var statDtoFromDb in statDtosFromDb)
|
|
|
|
|
{
|
|
|
|
|
var statDto = statDtos
|
|
|
|
|
.Where(stat => stat.DateStart == statDtoFromDb.DateStart)
|
|
|
|
|
.Where(stat => stat.DateEnd == statDtoFromDb.DateEnd)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
MatchHelper.Match(statDtoFromDb, statDto, excludedProps);
|
|
|
|
|
}
|
2024-02-07 13:28:02 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static DataSaubStat ConvertToEntity(DataSaubStatDto stat)
|
|
|
|
|
{
|
|
|
|
|
var entity = stat.Adapt<DataSaubStat>();
|
|
|
|
|
entity.DateStart = entity.DateStart.ToUniversalTime();
|
|
|
|
|
entity.DateEnd = entity.DateEnd.ToUniversalTime();
|
|
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static DataSaubStatDto ConvertToDto(DataSaubStat stat, TimeSpan timeSpan)
|
|
|
|
|
{
|
|
|
|
|
var dto = stat.Adapt<DataSaubStatDto>();
|
|
|
|
|
dto.DateStart = dto.DateStart.ToOffset(timeSpan);
|
|
|
|
|
dto.DateEnd = dto.DateEnd.ToOffset(timeSpan);
|
2024-02-06 10:29:01 +05:00
|
|
|
|
|
2024-02-07 13:28:02 +05:00
|
|
|
|
return dto;
|
2024-02-05 16:43:56 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|