DD.WellWorkover.Cloud/AsbCloudWebApi.IntegrationTests/Repository/DataSaubStatRepositoryTest.cs

141 lines
4.8 KiB
C#
Raw Normal View History

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
{
private static readonly DataSaubStatDto[] statDtos = new DataSaubStatDto[2]
{
new DataSaubStatDto()
{
IdTelemetry = 1,
DateEnd = new DateTimeOffset(2024, 1, 1, 20, 25, 0, TimeSpan.Zero),
DateStart = new DateTimeOffset(2024, 1, 1, 20, 15, 0, TimeSpan.Zero),
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 = 9.0,
RotorTorque = 9.0,
RotorTorqueSp = 9.0,
RotorTorqueLimitMax = 9.0,
Speed = 10.0
},
new DataSaubStatDto()
{
IdTelemetry = 1,
DateEnd = new DateTimeOffset(2024, 2, 2, 20, 25, 0, TimeSpan.Zero),
DateStart = new DateTimeOffset(2024, 2, 2, 20, 15, 0, TimeSpan.Zero),
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,
Name = "Категория 2"
};
private readonly IDataSaubStatRepository dataSaubStatRepository;
public DataSaubStatRepositoryTest(WebAppFactoryFixture factory) : base(factory)
{
var dbSet = dbContext.Set<DataSaubStat>();
var categories = dbContext.Set<WellOperationCategory>();
categories.RemoveRange(categories);
dbSet.RemoveRange(dbSet);
dbContext.SaveChanges();
categories.Add(category);
var entities = statDtos.Select(stat => stat.Adapt<DataSaubStat>());
dbSet.AddRange(entities);
dbContext.SaveChanges();
dataSaubStatRepository = scope.ServiceProvider.GetRequiredService<IDataSaubStatRepository>();
}
[Fact]
public async Task GetLastDatesAsync_returns_success()
{
//act
var telemetryIds = statDtos.Select(stat => stat.IdTelemetry).ToArray();
var result = await dataSaubStatRepository.GetLastDatesAsync(telemetryIds, CancellationToken.None);
var expected = statDtos.Max(stat => stat.DateEnd);
var actual = result.Any() ? result.First().DateEnd : DateTimeOffset.UnixEpoch;
//assert
Assert.True((expected - actual).Ticks == 0.0);
}
[Fact]
public async Task InsertRangeAsync_returns_success()
{
//act
var dbSet = dbContext.Set<DataSaubStat>();
dbSet.RemoveRange(dbSet);
dbContext.SaveChanges();
var result = await dataSaubStatRepository.InsertRangeAsync(statDtos, CancellationToken.None);
//assert
Assert.Equal(statDtos.Length, result);
var statDtosFromDb = dbSet.Select(stat => stat.Adapt<DataSaubStatDto>()).ToArray();
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);
}
}
}
}