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.Services; 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(); private readonly ITrajectoryFactRepository trajectoryFactRepositoryMock = Substitute.For(); private readonly IDailyReportRepository dailyReportRepositoryMock = Substitute.For(); private readonly IScheduleRepository scheduleRepositoryMock = Substitute.For(); private readonly IWellOperationRepository wellOperationRepositoryMock = Substitute.For(); private readonly ISubsystemOperationTimeService subsystemOperationTimeServiceMock = Substitute.For(); private readonly IProcessMapReportWellDrillingService processMapReportWellDrillingServiceMock = Substitute.For(); private readonly IDetectedOperationService detectedOperationServiceMock = Substitute.For(); 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(), Arg.Any()) .ReturnsForAnyArgs(idDailyReport); dailyReportRepositoryMock.GetOrDefaultAsync(idDailyReport, Arg.Any()) .Returns(fakeDailyReport); dailyReportRepositoryMock.UpdateAsync(Arg.Any(), Arg.Any()) .ReturnsForAnyArgs(idDailyReport); } [Fact] public async Task InsertAsync_ShouldReturn_ExceptionAboutDuplicate() { //arrange dailyReportRepositoryMock.AnyAsync(Arg.Any(), Arg.Any(), Arg.Any()) .ReturnsForAnyArgs(true); //act Task Result() => dailyReportService.InsertAsync(idWell, dateStart, CancellationToken.None); //assert var exception = await Assert.ThrowsAsync(Result); Assert.Equal("Суточный отчёт уже существует", exception.Message); } [Fact] public async Task InsertAsync_ShouldReturn_PositiveId() { //arrange dailyReportRepositoryMock.AnyAsync(Arg.Any(), Arg.Any(), Arg.Any()) .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() { 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); } }