using AsbCloudApp.Data; using AsbCloudApp.Data.ProcessMaps.Operations; using AsbCloudApp.Data.Trajectory; using AsbCloudApp.Data.User; using AsbCloudApp.Data.WellOperation; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudApp.Services.ProcessMaps.WellDrilling; using AsbCloudInfrastructure.Services.WellReport; using NSubstitute; using System.Collections.Generic; using System; using System.Threading; using Xunit; using AsbCloudDb.Model; using System.Linq; using System.Threading.Tasks; namespace AsbCloudInfrastructure.Tests.Services; public class WellReportServiceTest { private static readonly WellDto Well = new() { Caption = "Скважина №1", Cluster = "Кластер A", Deposit = "Месторождение Б", Latitude = 55.7558, Longitude = 37.6176, Timezone = new SimpleTimezoneDto { Hours = 3 }, WellType = "Разведочная", IdWellType = 1, IdCluster = 1001, IdState = 1, StartDate = DateTimeOffset.Now.AddMonths(-2), LastTelemetryDate = DateTimeOffset.Now, IdTelemetry = 12345, }; private static readonly IEnumerable WellOperations = new[] { new WellOperationDto { Id = 1, IdWell = 101, IdWellSectionType = 1001, IdType = 1, IdCategory = 2001, DepthStart = 1500, DepthEnd = 1550, DateStart = new DateTimeOffset(new DateTime(2024, 1, 13, 2, 0, 0)), DurationHours = 48, IdPlan = null, IdParentCategory = 2001, Day = 5 }, new WellOperationDto { Id = 4, IdWell = 101, IdWellSectionType = 1002, IdType = 1, IdCategory = 2001, DepthStart = 1500, DepthEnd = 1550, DateStart = new DateTimeOffset(new DateTime(2024, 1, 10, 0, 0, 0)), DurationHours = 48, IdPlan = null, IdParentCategory = 2001, Day = 3 }, new WellOperationDto { Id = 2, IdWell = 102, IdWellSectionType = 1002, IdType = 0, IdCategory = 2002, DepthStart = 2500, DepthEnd = 2600, DateStart = new DateTimeOffset(new DateTime(2024, 1, 10, 0, 0, 0)), DurationHours = 72, IdPlan = 1, IdParentCategory = 3002, Day = 3 }, new WellOperationDto { Id = 3, IdWell = 103, IdWellSectionType = 1003, IdType = 0, IdCategory = 2003, DepthStart = 3500, DepthEnd = 3600, DateStart = new DateTimeOffset(new DateTime(2024, 1, 10, 1, 0, 0)), DurationHours = 24, IdPlan = 2, IdParentCategory = 3003, Day = 4 } }; private readonly IWellService wellService; private readonly IWellOperationService wellOperationService; private readonly IWellContactService wellContactService; private readonly IProcessMapReportDrillingService processMapReportDrillingService; private readonly ISubsystemService subsystemService; private readonly ITrajectoryRepository trajectoryPlanRepository; private readonly ITrajectoryRepository trajectoryFactRepository; private readonly IChangeLogRepository processMapPlanRotorRepository; private readonly IScheduleRepository scheduleRepository; private readonly WellReportService wellReportService; public WellReportServiceTest() { wellService = Substitute.For(); wellOperationService = Substitute.For(); wellContactService = Substitute.For(); processMapReportDrillingService = Substitute.For(); subsystemService = Substitute.For(); trajectoryPlanRepository = Substitute.For>(); trajectoryFactRepository = Substitute.For>(); processMapPlanRotorRepository = Substitute.For>(); scheduleRepository = Substitute.For(); wellService.GetOrDefaultAsync(Arg.Any(), Arg.Any()) .ReturnsForAnyArgs(Well); wellOperationService .GetAsync(Arg.Is(x => x.OperationType == WellOperation.IdOperationTypeFact), Arg.Any()) .Returns(WellOperations.Where(x => x.IdType == WellOperation.IdOperationTypeFact)); wellOperationService .GetAsync(Arg.Is(x => x.OperationType == WellOperation.IdOperationTypePlan), Arg.Any()) .Returns(WellOperations.Where(x => x.IdType == WellOperation.IdOperationTypePlan)); wellReportService = new WellReportService(wellService, wellOperationService, wellContactService, processMapReportDrillingService, subsystemService, trajectoryPlanRepository, trajectoryFactRepository, processMapPlanRotorRepository, scheduleRepository); } [Fact] public async Task Returns_well_info_not_null() { //act var result = await wellReportService.GetAsync(1, CancellationToken.None); //assert Assert.NotNull(result); Assert.NotNull(result.Well); } [Fact] public async Task Returns_contacts_not_empty() { //arrange var contacts = new[] { new ContactDto() { Id = 1, IdCompanyType = 2, IdWell = 101, FullName = "Ivan Petrov", Email = "ivan.petrov@example.com", Phone = "+7 (123) 456-78-90", Position = "Chief Engineer", Company = "test" } }; wellContactService.GetAllAsync(Arg.Any(), Arg.Any()) .ReturnsForAnyArgs(contacts); //act var result = await wellReportService.GetAsync(1, CancellationToken.None); //assert Assert.NotNull(result); Assert.Single(result.Contacts); } [Fact] public async Task Returns_valid_from_and_to_dates() { //act var result = await wellReportService.GetAsync(1, CancellationToken.None); //assert Assert.NotNull(result); var expectedDateFrom = new DateTimeOffset(new DateTime(2024, 1, 10, 0, 0, 0)); var expectedDateTo = new DateTimeOffset(new DateTime(2024, 1, 11, 1, 0, 0)); Assert.Equal(expectedDateFrom, result.DateFrom); Assert.Equal(expectedDateTo, result.DateTo); } [Fact] public async Task Returns_valid_days() { //act var result = await wellReportService.GetAsync(1, CancellationToken.None); //assert Assert.NotNull(result); Assert.Equal(4, result.Days.Plan); Assert.Equal(4, result.Days.Fact); } [Fact] public async Task Returns_valid_vertical_depth() { //arrange var planTrajectory = new TrajectoryGeoPlanDto { Id = 1, IdWell = 123, WellboreDepth = 1500.75, ZenithAngle = 45.5, AzimuthGeo = 120.0, AzimuthMagnetic = 115.5, VerticalDepth = 1480.3, UpdateDate = DateTimeOffset.UtcNow, IdUser = 42 }; var factTrajectory = new TrajectoryGeoFactDto { Id = 1, IdWell = 123, WellboreDepth = 1500.75, ZenithAngle = 45.5, AzimuthGeo = 120.0, AzimuthMagnetic = 115.5, VerticalDepth = 1600, UpdateDate = DateTimeOffset.UtcNow, IdUser = 42 }; trajectoryPlanRepository.GetAsync(Arg.Any(), Arg.Any()) .ReturnsForAnyArgs(new[] { planTrajectory }); trajectoryFactRepository.GetAsync(Arg.Any(), Arg.Any()) .ReturnsForAnyArgs(new[] { factTrajectory }); //act var result = await wellReportService.GetAsync(1, CancellationToken.None); //assert Assert.NotNull(result); Assert.Equal(result.VerticalDepth.Plan, 1480.3); Assert.Equal(result.VerticalDepth.Fact, 1600); } [Fact] public async Task Returns_valid_without_ntp_days() { //act var result = await wellReportService.GetAsync(1, CancellationToken.None); //assert Assert.NotNull(result); Assert.Equal(4, result.WithoutNtpDays); } [Fact] public async Task Returns_section_reports_not_empty() { //act var result = await wellReportService.GetAsync(1, CancellationToken.None); //assert Assert.NotNull(result); Assert.Equal(2, result.SectionReports.Count()); } [Fact] public async Task Returns_driller_reports_not_empty() { //arrange var schedules = new[] { new ScheduleDto { Id = 1, IdDriller = 2001, ShiftStart = new TimeDto(7), ShiftEnd = new TimeDto(20), DrillStart = new DateTimeOffset(2024, 9, 1, 7, 0, 0, TimeSpan.Zero), DrillEnd = new DateTimeOffset(2024, 9, 1, 19, 0, 0, TimeSpan.Zero), }, new ScheduleDto { Id = 2, IdDriller = 2002, ShiftStart = new TimeDto(20), ShiftEnd = new TimeDto(16), DrillStart = new DateTimeOffset(2024, 9, 1, 7, 0, 0, TimeSpan.Zero), DrillEnd = new DateTimeOffset(2024, 9, 1, 19, 0, 0, TimeSpan.Zero), } }; scheduleRepository.GetByIdWellAsync(Arg.Any(), Arg.Any()) .ReturnsForAnyArgs(schedules); //act var result = await wellReportService.GetAsync(1, CancellationToken.None); //assert Assert.NotNull(result); Assert.Equal(2, result.DrillerReports.Count()); } }