diff --git a/AsbCloudApp/Data/WellOperation/WellOperationBaseDto.cs b/AsbCloudApp/Data/WellOperation/WellOperationBaseDto.cs index 61625da8..9f3095f0 100644 --- a/AsbCloudApp/Data/WellOperation/WellOperationBaseDto.cs +++ b/AsbCloudApp/Data/WellOperation/WellOperationBaseDto.cs @@ -91,6 +91,33 @@ public class WellOperationBaseDto : ItemInfoDto, [StringLength(4096, ErrorMessage = "Комментарий не может быть длиннее 4096 символов")] public string? Comment { get; set; } + /// + /// ctor + /// + public WellOperationBaseDto() { } + + /// + /// ctor + /// + public WellOperationBaseDto(WellOperationBaseDto wellOperation) + { + Id = wellOperation.Id ; + IdWell = wellOperation.IdWell ; + IdWellSectionType = wellOperation.IdWellSectionType ; + IdType = wellOperation.IdType ; + IdCategory = wellOperation.IdCategory ; + DepthStart = wellOperation.DepthStart ; + DepthEnd = wellOperation.DepthEnd ; + DateStart = wellOperation.DateStart ; + DurationHours = wellOperation.DurationHours ; + WellSectionTypeCaption = wellOperation.WellSectionTypeCaption ; + OperationCategoryName = wellOperation.OperationCategoryName ; + IdPlan = wellOperation.IdPlan ; + IdParentCategory = wellOperation.IdParentCategory ; + CategoryInfo = wellOperation.CategoryInfo ; + Comment = wellOperation.Comment; + } + /// /// Валидация даты /// @@ -104,4 +131,5 @@ public class WellOperationBaseDto : ItemInfoDto, $"{nameof(DateStart)}: DateStart не может быть меньше {gtDate}", new[] { nameof(DateStart) }); } + } diff --git a/AsbCloudWebApi.Tests/Services/WellOperationServiceTest.cs b/AsbCloudWebApi.Tests/Services/WellOperationServiceTest.cs index 48b52e2f..b1d943c7 100644 --- a/AsbCloudWebApi.Tests/Services/WellOperationServiceTest.cs +++ b/AsbCloudWebApi.Tests/Services/WellOperationServiceTest.cs @@ -5,10 +5,8 @@ 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; @@ -23,11 +21,12 @@ public class WellOperationServiceTest private readonly IWellService wellServiceMock = Substitute.For(); private readonly IWellOperationCategoryRepository wellOperationCategoryRepositoryMock = Substitute.For(); - private readonly SimpleTimezoneDto timeZone = new SimpleTimezoneDto() + private readonly SimpleTimezoneDto timeZone = new() { Hours = 5, }; - private readonly WellOperationBaseDto wellOperation = new WellOperationBaseDto() + + private readonly WellOperationBaseDto operation = new WellOperationBaseDto() { IdWell = idWell, Id = 1, @@ -36,97 +35,83 @@ public class WellOperationServiceTest DepthEnd = 2, DateStart = DateTimeOffset.UtcNow, DurationHours = 3, - IdCategory = WellOperationCategory.NonProductiveTimeSubIds.FirstOrDefault(), + IdCategory = WellOperationCategory.NonProductiveTimeSubIds.First(), IdWellSectionType = 1 }; - private readonly List wellOperations = new List(); - - private readonly IEnumerable> tvds = new List>() - { - new PlanFactPredictBase() - { - Fact = new WellOperationDto(), - Plan = new WellOperationDto(), - Predict = new WellOperationDto() - } - }; - public WellOperationServiceTest() { - //wellOperationServiceMock.GetTvdAsync(Arg.Any(), Arg.Any()) - // .Returns(tvds); wellServiceMock .GetTimezone(Arg.Any()) .Returns(timeZone); - //фактическая операция, у которой нет плановой для сопоставления - var wellOperation1 = wellOperation.Adapt(); - - //фактическая операция, у которой есть плановая для сопоставления - var wellOperation2 = wellOperation1.Adapt(); - wellOperation2.Id = wellOperation1.Id + 1; - wellOperation2.IdPlan = wellOperation1.Id; - wellOperation2.IdType = WellOperation.IdOperationTypeFact; - wellOperation2.DateStart = wellOperation1.DateStart.AddDays(1); - - //плановая операция - var wellOperation3 = wellOperation1.Adapt(); - 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(), Arg.Any()) - .Returns(wellOperations); wellOperationServiceMock = new WellOperationService(wellServiceMock, wellOperationRepositoryMock, wellOperationCategoryRepositoryMock); } [Fact] public async Task GetTvdAsync_ShouldReturn_Success() { - var factOperations = wellOperations - .Where(o => o.IdType == WellOperation.IdOperationTypeFact); + //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); - 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) + //assert + var plans = data + .Where(pfb => pfb.Plan is not null) + .Select(pfb => pfb.Plan!) .ToArray(); - var firstWellOperation = factOperations.MinBy(e => e.DateStart)!; + var facts = data + .Where(pfb => pfb.Fact is not null) + .Select(pfb => pfb.Fact!) + .ToArray(); - var expectedFactDays = factOperations - .Select(dto => - { - var day = (dto.DateStart - firstWellOperation.DateStart).TotalDays; - return day; - }); + 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); - //assert - Assert.Equal(expectedFactNptHours, actualFactNptHours); - Assert.Equal(expectedFactDays, actualFactDays); + 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); } }