DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/Services/DailyReportServiceTest.cs

194 lines
5.8 KiB
C#
Raw Normal View History

using System;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Data.DailyReport;
using AsbCloudApp.Data.DailyReport.Blocks.Sign;
using AsbCloudApp.Data.DailyReport.Blocks.Subsystems;
using AsbCloudApp.Data.DailyReport.Blocks.TimeBalance;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudApp.Services.ProcessMaps.WellDrilling;
using AsbCloudApp.Services.Subsystems;
using AsbCloudInfrastructure.Services.DailyReport;
using NSubstitute;
using Xunit;
namespace AsbCloudWebApi.Tests.ServicesTests;
public class DailyReportServiceTest
{
private const int idDailyReport = 1;
private const int idWell = 2;
private const int idUser = 3;
private readonly DateTime dateStart = new DateOnly(2023, 10, 26).ToDateTime(TimeOnly.MinValue);
private readonly IWellService wellServiceMock = Substitute.For<IWellService>();
private readonly ITrajectoryFactRepository trajectoryFactRepositoryMock = Substitute.For<ITrajectoryFactRepository>();
private readonly IDailyReportRepository dailyReportRepositoryMock = Substitute.For<IDailyReportRepository>();
private readonly IScheduleRepository scheduleRepositoryMock = Substitute.For<IScheduleRepository>();
private readonly IWellOperationRepository wellOperationRepositoryMock = Substitute.For<IWellOperationRepository>();
private readonly ISubsystemOperationTimeService subsystemOperationTimeServiceMock = Substitute.For<ISubsystemOperationTimeService>();
private readonly IProcessMapReportWellDrillingService processMapReportWellDrillingServiceMock = Substitute.For<IProcessMapReportWellDrillingService>();
private readonly IDetectedOperationService detectedOperationServiceMock = Substitute.For<IDetectedOperationService>();
private readonly DailyReportService dailyReportService;
private readonly DailyReportDto fakeDailyReport;
public DailyReportServiceTest()
{
fakeDailyReport = new()
{
Id = idDailyReport,
IdWell = idWell,
DateStart = dateStart
};
dailyReportService = new DailyReportService(wellServiceMock,
trajectoryFactRepositoryMock,
dailyReportRepositoryMock,
scheduleRepositoryMock,
wellOperationRepositoryMock,
subsystemOperationTimeServiceMock,
processMapReportWellDrillingServiceMock,
detectedOperationServiceMock);
dailyReportRepositoryMock.InsertAsync(Arg.Any<DailyReportDto>(), Arg.Any<CancellationToken>())
.ReturnsForAnyArgs(idDailyReport);
dailyReportRepositoryMock.GetOrDefaultAsync(idDailyReport, Arg.Any<CancellationToken>())
.Returns(fakeDailyReport);
dailyReportRepositoryMock.UpdateAsync(Arg.Any<DailyReportDto>(), Arg.Any<CancellationToken>())
.ReturnsForAnyArgs(idDailyReport);
}
[Fact]
public async Task InsertAsync_ShouldReturn_ExceptionAboutDuplicate()
{
//arrange
dailyReportRepositoryMock.AnyAsync(Arg.Any<int>(), Arg.Any<DateTime>(), Arg.Any<CancellationToken>())
.ReturnsForAnyArgs(true);
//act
Task Result() => dailyReportService.InsertAsync(idWell, dateStart, CancellationToken.None);
//assert
var exception = await Assert.ThrowsAsync<ArgumentInvalidException>(Result);
Assert.Equal("Суточный отчёт уже существует", exception.Message);
}
[Fact]
public async Task InsertAsync_ShouldReturn_PositiveId()
{
//arrange
dailyReportRepositoryMock.AnyAsync(Arg.Any<int>(), Arg.Any<DateTime>(), Arg.Any<CancellationToken>())
.ReturnsForAnyArgs(false);
//act
var result = await dailyReportService.InsertAsync(idWell, dateStart, CancellationToken.None);
//assert
Assert.Equal(idDailyReport, result);
}
[Fact]
public async Task UpdateSubsystemBlock_ShouldReturn_Success()
{
//arrange
var fakeSubsystemBlock = new SubsystemBlockDto
{
IdUser = idUser,
WellBoreDepth = 999,
MeasurementsPerDaily = 999,
TotalRopPlan = 999,
Comment = "Увеличить обороты",
Modules = new[]
{
new SubsystemRecordDto
{
SubsystemName = "АвтоСПО",
IdTimeInterval = 1,
UsedTimeHours = 24,
SumDepthInterval = 1500,
KUsage = 100
}
}
};
//act
var result = await dailyReportService.UpdateBlockAsync(idDailyReport, idUser, fakeSubsystemBlock, CancellationToken.None);
//assert
Assert.NotNull(fakeDailyReport.DateLastUpdate);
Assert.Equal(fakeDailyReport.SubsystemBlock, fakeSubsystemBlock);
Assert.Equal(idDailyReport, result);
}
[Fact]
public async Task UpdateSignBlock_ShouldReturn_Success()
{
//arrange
var fakeSignBlock = new SignBlockDto
{
IdUser = idUser,
DrillingMaster = new SignRecordDto()
{
Name = "Иван",
Patronymic = "Иванович",
Surname = "Иванов"
},
Supervisor = new SignRecordDto()
{
Name = "Илья",
Patronymic = "Ильич",
Surname = "Бурилов"
}
};
//act
var result = await dailyReportService.UpdateBlockAsync(idDailyReport, idUser, fakeSignBlock, CancellationToken.None);
//assert
Assert.NotNull(fakeDailyReport.DateLastUpdate);
Assert.Equal(fakeDailyReport.SignBlock, fakeSignBlock);
Assert.Equal(idDailyReport, result);
}
[Fact]
public async Task UpdateTimeBalance_ShouldReturn_Success()
{
//arrange
var fakeTimeBalanceBlock = new TimeBalanceBlockDto
{
IdUser = idUser,
IdSection = 1,
WellDepthPlan = 2000,
WellOperations = new[]
{
new TimeBalanceRecordDto()
{
DurationHours = new PlanFactDto<double>()
{
Fact = 100,
Plan = 150,
},
DrillingDeviationPerSection = 90,
DrillingDeviationPerDaily = 100,
ReasonDeviation = "Отклонение"
}
}
};
//act
var result = await dailyReportService.UpdateBlockAsync(idDailyReport, idUser, fakeTimeBalanceBlock, CancellationToken.None);
//assert
Assert.NotNull(fakeDailyReport.DateLastUpdate);
Assert.Equal(fakeDailyReport.TimeBalanceBlock, fakeTimeBalanceBlock);
Assert.Equal(idDailyReport, result);
}
}