DD.WellWorkover.Cloud/AsbCloudInfrastructure.Tests/Services/WellOperationServiceTest.cs

118 lines
4.6 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Data.WellOperation;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.WellOperationService.WellOperationService;
using NSubstitute;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
2024-08-19 10:57:31 +05:00
namespace AsbCloudInfrastructure.Tests.Services;
public class WellOperationServiceTest
{
private const int idWell = 1;
private readonly IWellOperationService wellOperationServiceMock = Substitute.For<IWellOperationService>();
private readonly IWellOperationRepository wellOperationRepositoryMock = Substitute.For<IWellOperationRepository>();
private readonly IWellService wellServiceMock = Substitute.For<IWellService>();
private readonly IWellOperationCategoryRepository wellOperationCategoryRepositoryMock = Substitute.For<IWellOperationCategoryRepository>();
2024-08-19 10:51:09 +05:00
private readonly SimpleTimezoneDto timeZone = new()
{
Hours = 5,
};
2024-08-19 10:51:09 +05:00
private readonly WellOperationBaseDto operation = new WellOperationBaseDto()
{
IdWell = idWell,
Id = 1,
IdType = WellOperation.IdOperationTypeFact,
DepthStart = 1,
DepthEnd = 2,
DateStart = DateTimeOffset.UtcNow,
DurationHours = 3,
2024-08-19 10:51:09 +05:00
IdCategory = WellOperationCategory.NonProductiveTimeSubIds.First(),
IdWellSectionType = 1
};
public WellOperationServiceTest()
{
wellServiceMock
.GetTimezone(Arg.Any<int>())
.Returns(timeZone);
wellOperationServiceMock = new WellOperationService(wellServiceMock, wellOperationRepositoryMock, wellOperationCategoryRepositoryMock);
}
[Fact]
public async Task GetTvdAsync_ShouldReturn_Success()
{
2024-08-19 10:51:09 +05:00
//arrange
var id = operation.Id;
//плановая операция
var operationPlanRotor = new WellOperationBaseDto(operation)
{
Id = id++,
IdType = WellOperation.IdOperationTypePlan,
IdCategory = WellOperationCategory.IdRotor,
};
//фактическая операция, с нпв
var operationFactWithNpt = new WellOperationBaseDto(operation)
{
Id = id++,
DateStart = operationPlanRotor.DateStart.AddDays(1),
};
//фактическая операция, у которой есть плановая для сопоставления
var operationFactRotor = new WellOperationBaseDto(operation)
{
Id = id++,
IdPlan = operationPlanRotor.Id,
DateStart = operationFactWithNpt.DateStart.AddHours(operationFactWithNpt.DurationHours),
IdCategory = WellOperationCategory.IdRotor,
};
WellOperationBaseDto[] factOperations = [operationFactWithNpt, operationFactRotor];
WellOperationBaseDto[] operations = [operationFactRotor, operationFactWithNpt, operationPlanRotor];
wellOperationRepositoryMock
.GetAll(Arg.Any<WellOperationRepositoryRequest>(), Arg.Any<CancellationToken>())
.Returns(operations);
//act
var data = await wellOperationServiceMock.GetTvdAsync(idWell, CancellationToken.None);
2024-08-19 10:51:09 +05:00
//assert
var plans = data
.Where(pfb => pfb.Plan is not null)
.Select(pfb => pfb.Plan!)
.ToArray();
2024-08-19 10:51:09 +05:00
var facts = data
.Where(pfb => pfb.Fact is not null)
.Select(pfb => pfb.Fact!)
.ToArray();
2024-08-19 10:51:09 +05:00
var retrievedOperationPlanRotor = plans.First(o => o.Id == operationPlanRotor.Id);
Assert.Equal(0, retrievedOperationPlanRotor.Day);
Assert.Equal(0, retrievedOperationPlanRotor.NptHours);
Assert.Equal(operationPlanRotor.IdCategory, retrievedOperationPlanRotor.IdCategory);
var retrievedOperationFactWithNpt = facts.First(o => o.Id == operationFactWithNpt.Id);
Assert.Equal(0, retrievedOperationFactWithNpt.Day);
Assert.Equal(operationFactWithNpt.IdCategory, retrievedOperationFactWithNpt.IdCategory);
var retrievedOperationFactRotor = facts.First(o => o.Id == operationFactRotor.Id);
2024-08-19 10:57:31 +05:00
var expectedDay = operationFactWithNpt.DurationHours / 24;
2024-08-19 10:51:09 +05:00
Assert.Equal(expectedDay, retrievedOperationFactRotor.Day, 0.001);
Assert.Equal(operationFactWithNpt.DurationHours, retrievedOperationFactRotor.NptHours, 0.001);
Assert.Equal(retrievedOperationPlanRotor.Id, retrievedOperationFactRotor.IdPlan);
Assert.Equal(operationFactRotor.IdCategory, retrievedOperationFactRotor.IdCategory);
}
}