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; namespace AsbCloudInfrastructure.Tests.Services; public class WellOperationServiceTest { private const int idWell = 1; private readonly IWellOperationService wellOperationServiceMock = Substitute.For(); private readonly IWellOperationRepository wellOperationRepositoryMock = Substitute.For(); private readonly IWellService wellServiceMock = Substitute.For(); private readonly IWellOperationCategoryRepository wellOperationCategoryRepositoryMock = Substitute.For(); private readonly SimpleTimezoneDto timeZone = new() { Hours = 5, }; private readonly WellOperationBaseDto operation = new WellOperationBaseDto() { IdWell = idWell, Id = 1, IdType = WellOperation.IdOperationTypeFact, DepthStart = 1, DepthEnd = 2, DateStart = DateTimeOffset.UtcNow, DurationHours = 3, IdCategory = WellOperationCategory.NonProductiveTimeSubIds.First(), IdWellSectionType = 1 }; public WellOperationServiceTest() { wellServiceMock .GetTimezone(Arg.Any()) .Returns(timeZone); wellOperationServiceMock = new WellOperationService(wellServiceMock, wellOperationRepositoryMock, wellOperationCategoryRepositoryMock); } [Fact] public async Task GetTvdAsync_ShouldReturn_Success() { //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(), Arg.Any()) .Returns(operations); //act var data = await wellOperationServiceMock.GetTvdAsync(idWell, CancellationToken.None); //assert var plans = data .Where(pfb => pfb.Plan is not null) .Select(pfb => pfb.Plan!) .ToArray(); var facts = data .Where(pfb => pfb.Fact is not null) .Select(pfb => pfb.Fact!) .ToArray(); 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); var expectedDay = operationFactWithNpt.DurationHours / 24; 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); } }