refactor WellOperationServiceTest test

This commit is contained in:
ngfrolov 2024-08-19 10:51:09 +05:00
parent cb20a81cb0
commit bc1c5fa00f
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
2 changed files with 86 additions and 73 deletions

View File

@ -91,6 +91,33 @@ public class WellOperationBaseDto : ItemInfoDto,
[StringLength(4096, ErrorMessage = "Комментарий не может быть длиннее 4096 символов")] [StringLength(4096, ErrorMessage = "Комментарий не может быть длиннее 4096 символов")]
public string? Comment { get; set; } public string? Comment { get; set; }
/// <summary>
/// ctor
/// </summary>
public WellOperationBaseDto() { }
/// <summary>
/// ctor
/// </summary>
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;
}
/// <summary> /// <summary>
/// Валидация даты /// Валидация даты
/// </summary> /// </summary>
@ -104,4 +131,5 @@ public class WellOperationBaseDto : ItemInfoDto,
$"{nameof(DateStart)}: DateStart не может быть меньше {gtDate}", $"{nameof(DateStart)}: DateStart не может быть меньше {gtDate}",
new[] { nameof(DateStart) }); new[] { nameof(DateStart) });
} }
} }

View File

@ -5,10 +5,8 @@ using AsbCloudApp.Requests;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.WellOperationService.WellOperationService; using AsbCloudInfrastructure.Services.WellOperationService.WellOperationService;
using Mapster;
using NSubstitute; using NSubstitute;
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -23,11 +21,12 @@ public class WellOperationServiceTest
private readonly IWellService wellServiceMock = Substitute.For<IWellService>(); private readonly IWellService wellServiceMock = Substitute.For<IWellService>();
private readonly IWellOperationCategoryRepository wellOperationCategoryRepositoryMock = Substitute.For<IWellOperationCategoryRepository>(); private readonly IWellOperationCategoryRepository wellOperationCategoryRepositoryMock = Substitute.For<IWellOperationCategoryRepository>();
private readonly SimpleTimezoneDto timeZone = new SimpleTimezoneDto() private readonly SimpleTimezoneDto timeZone = new()
{ {
Hours = 5, Hours = 5,
}; };
private readonly WellOperationBaseDto wellOperation = new WellOperationBaseDto()
private readonly WellOperationBaseDto operation = new WellOperationBaseDto()
{ {
IdWell = idWell, IdWell = idWell,
Id = 1, Id = 1,
@ -36,97 +35,83 @@ public class WellOperationServiceTest
DepthEnd = 2, DepthEnd = 2,
DateStart = DateTimeOffset.UtcNow, DateStart = DateTimeOffset.UtcNow,
DurationHours = 3, DurationHours = 3,
IdCategory = WellOperationCategory.NonProductiveTimeSubIds.FirstOrDefault(), IdCategory = WellOperationCategory.NonProductiveTimeSubIds.First(),
IdWellSectionType = 1 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() public WellOperationServiceTest()
{ {
//wellOperationServiceMock.GetTvdAsync(Arg.Any<int>(), Arg.Any<CancellationToken>())
// .Returns(tvds);
wellServiceMock wellServiceMock
.GetTimezone(Arg.Any<int>()) .GetTimezone(Arg.Any<int>())
.Returns(timeZone); .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); wellOperationServiceMock = new WellOperationService(wellServiceMock, wellOperationRepositoryMock, wellOperationCategoryRepositoryMock);
} }
[Fact] [Fact]
public async Task GetTvdAsync_ShouldReturn_Success() public async Task GetTvdAsync_ShouldReturn_Success()
{ {
var factOperations = wellOperations //arrange
.Where(o => o.IdType == WellOperation.IdOperationTypeFact); 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 //act
var data = await wellOperationServiceMock.GetTvdAsync(idWell, CancellationToken.None); 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
Assert.Equal(expectedFactNptHours, actualFactNptHours); var plans = data
Assert.Equal(expectedFactDays, actualFactDays); .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);
} }
} }