DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/Services/WellOperationServiceTest.cs
2024-08-19 10:01:07 +05:00

133 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 Mapster;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace AsbCloudWebApi.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>();
private readonly SimpleTimezoneDto timeZone = new SimpleTimezoneDto()
{
Hours = 5,
};
private readonly WellOperationBaseDto wellOperation = new WellOperationBaseDto()
{
IdWell = idWell,
Id = 1,
IdType = WellOperation.IdOperationTypeFact,
DepthStart = 1,
DepthEnd = 2,
DateStart = DateTimeOffset.UtcNow,
DurationHours = 3,
IdCategory = WellOperationCategory.NonProductiveTimeSubIds.FirstOrDefault(),
IdWellSectionType = 1
};
private readonly List<WellOperationBaseDto> wellOperations = new List<WellOperationBaseDto>();
private readonly IEnumerable<PlanFactPredictBase<WellOperationDto>> tvds = new List<PlanFactPredictBase<WellOperationDto>>()
{
new PlanFactPredictBase<WellOperationDto>()
{
Fact = new WellOperationDto(),
Plan = new WellOperationDto(),
Predict = new WellOperationDto()
}
};
public WellOperationServiceTest()
{
//wellOperationServiceMock.GetTvdAsync(Arg.Any<int>(), Arg.Any<CancellationToken>())
// .Returns(tvds);
wellServiceMock
.GetTimezone(Arg.Any<int>())
.Returns(timeZone);
//фактическая операция, у которой нет плановой для сопоставления
var wellOperation1 = wellOperation.Adapt<WellOperationBaseDto>();
//фактическая операция, у которой есть плановая для сопоставления
var wellOperation2 = wellOperation1.Adapt<WellOperationBaseDto>();
wellOperation2.Id = wellOperation1.Id + 1;
wellOperation2.IdPlan = wellOperation1.Id;
wellOperation2.IdType = WellOperation.IdOperationTypeFact;
wellOperation2.DateStart = wellOperation1.DateStart.AddDays(1);
//плановая операция
var wellOperation3 = wellOperation1.Adapt<WellOperationBaseDto>();
wellOperation3.Id = wellOperation1.Id + 2;
wellOperation3.IdType = WellOperation.IdOperationTypePlan;
wellOperation3.DateStart = wellOperation1.DateStart.AddDays(2);
wellOperations.Add(wellOperation1);
wellOperations.Add(wellOperation2);
wellOperations.Add(wellOperation3);
wellOperationRepositoryMock.GetAll(Arg.Any<WellOperationRepositoryRequest>(), Arg.Any<CancellationToken>())
.Returns(wellOperations);
wellOperationServiceMock = new WellOperationService(wellServiceMock, wellOperationRepositoryMock, wellOperationCategoryRepositoryMock);
}
[Fact]
public async Task GetTvdAsync_ShouldReturn_Success()
{
var factOperations = wellOperations
.Where(o => o.IdType == WellOperation.IdOperationTypeFact);
//act
var data = await wellOperationServiceMock.GetTvdAsync(idWell, CancellationToken.None);
var actualFactNptHours = data
.Where(o => o.Fact != null)
.Select(o => o.Fact!.NptHours)
.ToArray();
var factOperationsWithNpt = factOperations
.Where(o => WellOperationCategory.NonProductiveTimeSubIds.Contains(o.IdCategory));
var expectedFactNptHours = factOperations
.Select(dto =>
{
var nptHours = factOperationsWithNpt
.Where(o => o.DateStart <= dto.DateStart)
.Sum(e => e.DurationHours);
return nptHours;
});
var actualFactDays = data
.Where(o => o.Fact != null)
.Select(o => o.Fact!.Day)
.ToArray();
var firstWellOperation = factOperations.MinBy(e => e.DateStart)!;
var expectedFactDays = factOperations
.Select(dto =>
{
var day = (dto.DateStart - firstWellOperation.DateStart).TotalDays;
return day;
});
//assert
Assert.Equal(expectedFactNptHours, actualFactNptHours);
Assert.Equal(expectedFactDays, actualFactDays);
}
}